19
19
import networkx as nx
20
20
from networkx import draw
21
21
22
- from dwave_networkx .drawing .qubit_layout import draw_qubit_graph , draw_embedding , draw_yield
22
+ from dwave_networkx .drawing .qubit_layout import draw_qubit_graph , draw_embedding , draw_yield , normalize_size_and_aspect
23
23
from dwave_networkx .generators .pegasus import pegasus_graph , pegasus_coordinates
24
24
from dwave_networkx .drawing .chimera_layout import chimera_node_placer_2d
25
25
31
31
]
32
32
33
33
34
- def pegasus_layout (G , scale = 1. , center = None , dim = 2 , crosses = False ):
34
+ def pegasus_layout (G , scale = 1. , center = None , dim = 2 , crosses = False , normalize_kwargs = None ):
35
35
"""Positions the nodes of graph G in a Pegasus topology.
36
36
37
37
`NumPy <https://scipy.org>`_ is required for this function.
@@ -58,6 +58,11 @@ def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False):
58
58
rather than L configuration. Ignored if G is defined with
59
59
``nice_coordinates=True``.
60
60
61
+ normalize_kwargs : None or dict (default None)
62
+ A dict of keyword arguments to be used in a plotting function. If not
63
+ None, we will populate the "ax" keyword with matplotlib axes, and the
64
+ "node_size" and "width" keywords with defaults if they are not set.
65
+
61
66
Returns
62
67
-------
63
68
pos : dict
@@ -75,28 +80,27 @@ def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False):
75
80
if not isinstance (G , nx .Graph ) or G .graph .get ("family" ) != "pegasus" :
76
81
raise ValueError ("G must be generated by dwave_networkx.pegasus_graph" )
77
82
78
- if G .graph .get ('labels' ) == 'nice' :
79
- m = 3 * (G .graph ['rows' ]- 1 )
80
- c_coords = chimera_node_placer_2d (m , m , 4 , scale = scale , center = center , dim = dim )
81
- def xy_coords (t , y , x , u , k ):
82
- return c_coords (3 * y + 2 - t , 3 * x + t , u , k )
83
+ labels = G .graph .get ('labels' )
84
+ xy_coords = pegasus_node_placer_2d (G , scale , center , dim , crosses = (crosses or labels == 'nice_coordinates' ), normalize_kwargs = normalize_kwargs )
85
+
86
+ if labels == 'coordinate' :
83
87
pos = {v : xy_coords (* v ) for v in G .nodes ()}
88
+ elif G .graph ['data' ]:
89
+ pos = {v : xy_coords (* dat ['pegasus_index' ]) for v , dat in G .nodes (data = True )}
84
90
else :
85
- xy_coords = pegasus_node_placer_2d (G , scale , center , dim , crosses = crosses )
86
-
87
- if G .graph .get ('labels' ) == 'coordinate' :
88
- pos = {v : xy_coords (* v ) for v in G .nodes ()}
89
- elif G .graph .get ('data' ):
90
- pos = {v : xy_coords (* dat ['pegasus_index' ]) for v , dat in G .nodes (data = True )}
91
- else :
92
- m = G .graph .get ('rows' )
93
- coord = pegasus_coordinates (m )
91
+ m = G .graph ['rows' ]
92
+ coord = pegasus_coordinates (m )
93
+ if labels == 'int' :
94
94
pos = {v : xy_coords (* coord .linear_to_pegasus (v )) for v in G .nodes ()}
95
+ elif labels == 'nice' :
96
+ pos = {v : xy_coords (* coord .nice_to_pegasus (v )) for v in G .nodes ()}
97
+ else :
98
+ raise ValueError (f"Pegasus labeling { labels } not recognized" )
95
99
96
100
return pos
97
101
98
102
99
- def pegasus_node_placer_2d (G , scale = 1. , center = None , dim = 2 , crosses = False ):
103
+ def pegasus_node_placer_2d (G , scale = 1. , center = None , dim = 2 , crosses = False , normalize_kwargs = None ):
100
104
"""Generates a function to convert Pegasus indices to plottable coordinates.
101
105
102
106
Parameters
@@ -138,7 +142,11 @@ def pegasus_node_placer_2d(G, scale=1., center=None, dim=2, crosses=False):
138
142
cross_shift = 2 if crosses else 0
139
143
140
144
# want the enter plot to fill in [0, 1] when scale=1
141
- scale /= m * tile_width - 2 * odd_k_wobble - 1
145
+ fabric_scale = m * tile_width - 2 * odd_k_wobble - 1
146
+ scale /= fabric_scale
147
+
148
+ if normalize_kwargs is not None :
149
+ normalize_size_and_aspect (fabric_scale , 150 , normalize_kwargs )
142
150
143
151
if center is None :
144
152
center = np .zeros (dim )
@@ -219,11 +227,10 @@ def draw_pegasus(G, crosses=False, **kwargs):
219
227
>>> plt.show() # doctest: +SKIP
220
228
221
229
"""
230
+ layout = pegasus_layout (G , normalize_kwargs = kwargs )
231
+ draw_qubit_graph (G , layout , ** kwargs )
222
232
223
- draw_qubit_graph (G , pegasus_layout (G , crosses = crosses ), ** kwargs )
224
-
225
-
226
- def draw_pegasus_embedding (G , * args , ** kwargs ):
233
+ def draw_pegasus_embedding (G , * args , crosses = False , ** kwargs ):
227
234
"""Draws an embedding onto Pegasus graph G.
228
235
229
236
Parameters
@@ -276,10 +283,10 @@ def draw_pegasus_embedding(G, *args, **kwargs):
276
283
function. If ``linear_biases`` or ``quadratic_biases`` are provided,
277
284
any provided ``node_color`` or ``edge_color`` arguments are ignored.
278
285
"""
279
- crosses = kwargs . pop ( " crosses" , False )
280
- draw_embedding (G , pegasus_layout ( G , crosses = crosses ) , * args , ** kwargs )
286
+ layout = pegasus_layout ( G , crosses = crosses , normalize_kwargs = kwargs )
287
+ draw_embedding (G , layout , * args , ** kwargs )
281
288
282
- def draw_pegasus_yield (G , ** kwargs ):
289
+ def draw_pegasus_yield (G , crosses = False , ** kwargs ):
283
290
"""Draws the given graph G with highlighted faults, according to layout.
284
291
285
292
Parameters
@@ -302,6 +309,11 @@ def draw_pegasus_yield(G, **kwargs):
302
309
fault_style : string, optional (default='dashed')
303
310
Edge fault line style (solid|dashed|dotted|dashdot)
304
311
312
+ crosses: boolean (optional, default False)
313
+ If True, :math:`K_{4,4}` subgraphs are shown in a cross
314
+ rather than L configuration. Ignored if G is defined with
315
+ ``nice_coordinates=True``.
316
+
305
317
kwargs : optional keywords
306
318
See networkx.draw_networkx() for a description of optional keywords,
307
319
with the exception of the `pos` parameter which is not used by this
@@ -321,5 +333,5 @@ def draw_pegasus_yield(G, **kwargs):
321
333
322
334
323
335
perfect_graph = pegasus_graph (m , offset_lists = offset_lists , coordinates = coordinates , nice_coordinates = nice )
324
-
325
- draw_yield (G , pegasus_layout ( perfect_graph ) , perfect_graph , ** kwargs )
336
+ layout = pegasus_layout ( perfect_graph , crosses = crosses , normalize_kwargs = kwargs )
337
+ draw_yield (G , layout , perfect_graph , ** kwargs )
0 commit comments