@@ -286,21 +286,22 @@ def draw_matplotlib(
286
286
# library_code += f.read() + '\n'
287
287
# library_code += '</script>'
288
288
# display(HTML(library_code))
289
+
289
290
def auto_draw_vertex_locs (g :BaseGraph [VT , ET ]): #Force-based graph drawing algorithm given by Eades(1984):
290
- c1 = 2
291
+ c1 = 2 #Sample parameters that work decently well
291
292
c2 = 1
292
293
c3 = 1
293
294
c4 = .1
294
295
v_loc :Dict [VT , Tuple [int , int ]] = dict ()
295
296
for v in g .vertices ():
296
- v_loc [v ]= (random .random ()* 10 , random .random ()* 10 )
297
+ v_locs [v ]= (random .random ()* math . sqrt ( g . num_vertices ()) , random .random ()* math . sqrt ( g . num_vertices ()) )
297
298
for i in range (100 ): #100 iterations of force-based drawing
298
299
forces :Dict [VT , Tuple [int , int ]] = dict ()
299
300
for v in g .vertices ():
300
301
forces [v ] = (0 , 0 )
301
302
for v1 in g .vertices ():
302
303
if (v != v1 ):
303
- diff = (v_loc [v ][0 ]- v_loc [v1 ][0 ], v_loc [v ][1 ]- v_loc [v1 ][1 ])
304
+ diff = (v_locs [v ][0 ]- v_locs [v1 ][0 ], v_locs [v ][1 ]- v_locs [v1 ][1 ])
304
305
d = math .sqrt (diff [0 ]* diff [0 ]+ diff [1 ]* diff [1 ])
305
306
if g .connected (v1 , v ): #edge between vertices: apply rule c1*log(d/c2)
306
307
force_mag = - c1 * math .log (d / c2 ) #negative force attracts
@@ -311,15 +312,13 @@ def auto_draw_vertex_locs(g:BaseGraph[VT, ET]): #Force-based graph drawing algor
311
312
v_force = (diff [0 ]* force_mag * c4 / d , diff [1 ]* force_mag * c4 / d )
312
313
forces [v ] = (forces [v ][0 ]+ v_force [0 ], forces [v ][1 ]+ v_force [1 ])
313
314
for v in g .vertices (): #leave y value constant if input or output
314
- v_loc [v ]= (v_loc [v ][0 ]+ forces [v ][0 ], v_loc [v ][1 ]+ forces [v ][1 ])
315
+ v_locs [v ]= (v_locs [v ][0 ]+ forces [v ][0 ], v_locs [v ][1 ]+ forces [v ][1 ])
315
316
max_x = max (v [0 ] for v in v_loc .values ())
316
317
min_x = min (v [0 ] for v in v_loc .values ())
317
318
max_y = max (v [1 ] for v in v_loc .values ())
318
319
min_y = min (v [1 ] for v in v_loc .values ())
319
- v_loc = {k :(v [0 ]- min_x , v [1 ]- min_y ) for k , v in v_loc .items ()} #dont rescale
320
-
321
- #v_loc = {k:((v[0]-min_x)*(x_scale/(max_x-min_x)), (v[1]-min_y)*y_scale/(max_y-min_y)) for k, v in v_loc.items()} #rescale
322
- return v_loc , max_x - min_x , max_y - min_y
320
+ v_locs = {k :(v [0 ]- min_x , v [1 ]- min_y ) for k , v in v_locs .items ()} #translate to origin
321
+ return v_locs , max_x - min_x , max_y - min_y
323
322
324
323
325
324
def draw_d3 (
@@ -352,8 +351,8 @@ def draw_d3(
352
351
if scale > 50 : scale = 50
353
352
if scale < 20 : scale = 20
354
353
355
- w = w * scale
356
- h = h * scale
354
+ w = ( w + 2 ) * scale
355
+ h = ( h + 3 ) * scale
357
356
else :
358
357
minrow = min ([g .row (v ) for v in g .vertices ()], default = 0 )
359
358
maxrow = max ([g .row (v ) for v in g .vertices ()], default = 0 )
@@ -372,8 +371,8 @@ def draw_d3(
372
371
if node_size < 2 : node_size = 2
373
372
374
373
nodes = [{'name' : str (v ),
375
- 'x' : v_dict [v ][0 ]* scale if auto_draw else (g .row (v )- minrow + 1 ) * scale ,
376
- 'y' : v_dict [v ][1 ]* scale if auto_draw else (g .qubit (v )- minqub + 2 ) * scale ,
374
+ 'x' : ( v_dict [v ][0 ]+ 1 ) * scale if auto_draw else (g .row (v )- minrow + 1 ) * scale ,
375
+ 'y' : ( v_dict [v ][1 ]+ 2 ) * scale if auto_draw else (g .qubit (v )- minqub + 2 ) * scale ,
377
376
't' : g .type (v ),
378
377
'phase' : phase_to_s (g .phase (v ), g .type (v )) if g .type (v ) != VertexType .Z_BOX else str (get_z_box_label (g , v )),
379
378
'ground' : g .is_ground (v ),
0 commit comments