Skip to content

Commit 25b0adc

Browse files
committed
review
1 parent f110c95 commit 25b0adc

File tree

4 files changed

+190
-137
lines changed

4 files changed

+190
-137
lines changed

dwave_networkx/drawing/chimera_layout.py

+45-39
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
__all__ = ['chimera_layout', 'draw_chimera', 'draw_chimera_embedding', 'draw_chimera_yield']
2828

2929

30-
def chimera_layout(G, scale=1., center=None, dim=2, normalize_kwargs = None):
30+
def chimera_layout(G, scale=1., center=None, dim=2, plot_kwargs=None):
3131
"""Positions the nodes of graph G in a Chimera cross topology.
3232
3333
NumPy (https://scipy.org) is required for this function.
@@ -51,10 +51,12 @@ def chimera_layout(G, scale=1., center=None, dim=2, normalize_kwargs = None):
5151
Number of dimensions. When dim > 2, all extra dimensions are
5252
set to 0.
5353
54-
normalize_kwargs : None or dict (default None)
55-
A dict of keyword arguments to be used in a plotting function. If not
56-
None, we will populate the "ax" keyword with matplotlib axes, and the
57-
"node_size" and "width" keywords with defaults if they are not set.
54+
plot_kwargs : None or dict (default None)
55+
A dict of keyword arguments to be used in a plotting function (see
56+
:func:`networkx.draw` and :func:`draw_lineplot`), to scale node and edge
57+
sizes appropriately to the graph `G`. Optional keys in ``plot_kwargs``
58+
are set to default values by the :func:`normalize_size_and_aspect`
59+
function. Do nothing if ``plot_kwargs`` is None.
5860
5961
Returns
6062
-------
@@ -80,7 +82,7 @@ def chimera_layout(G, scale=1., center=None, dim=2, normalize_kwargs = None):
8082
n = G.graph['columns']
8183
t = G.graph['tile']
8284
# get a node placement function
83-
xy_coords = chimera_node_placer_2d(m, n, t, scale, center, dim, normalize_kwargs = normalize_kwargs)
85+
xy_coords = chimera_node_placer_2d(m, n, t, scale, center, dim, plot_kwargs=plot_kwargs)
8486

8587
if G.graph['labels'] == 'coordinate':
8688
pos = {v: xy_coords(*v) for v in G.nodes()}
@@ -102,15 +104,15 @@ def chimera_layout(G, scale=1., center=None, dim=2, normalize_kwargs = None):
102104
m = max(idx[0] for idx in chimera_indices.values()) + 1
103105
n = max(idx[1] for idx in chimera_indices.values()) + 1
104106
t = max(idx[3] for idx in chimera_indices.values()) + 1
105-
xy_coords = chimera_node_placer_2d(m, n, t, scale, center, dim, normalize_kwargs = normalize_kwargs)
107+
xy_coords = chimera_node_placer_2d(m, n, t, scale, center, dim, plot_kwargs=plot_kwargs)
106108

107109
# compute our coordinates
108110
pos = {v: xy_coords(i, j, u, k) for v, (i, j, u, k) in chimera_indices.items()}
109111

110112
return pos
111113

112114

113-
def chimera_node_placer_2d(m, n, t, scale=1., center=None, dim=2, normalize_kwargs = None):
115+
def chimera_node_placer_2d(m, n, t, scale=1., center=None, dim=2, plot_kwargs=None):
114116
"""Generates a function that converts Chimera indices to x, y
115117
coordinates for a plot.
116118
@@ -135,11 +137,13 @@ def chimera_node_placer_2d(m, n, t, scale=1., center=None, dim=2, normalize_kwar
135137
dim : int (default 2)
136138
Number of dimensions. When dim > 2, all extra dimensions are
137139
set to 0.
138-
139-
normalize_kwargs : None or dict (default None)
140-
A dict of keyword arguments to be used in a plotting function. If not
141-
None, we will populate the "ax" keyword with matplotlib axes, and the
142-
"node_size" and "width" keywords with defaults if they are not set.
140+
141+
plot_kwargs : None or dict (default None)
142+
A dict of keyword arguments to be used in a plotting function (see
143+
:func:`networkx.draw` and :func:`draw_lineplot`), to scale node and edge
144+
sizes appropriately to the graph `G`. Optional keys in ``plot_kwargs``
145+
are set to default values by the :func:`normalize_size_and_aspect`
146+
function. Do nothing if ``plot_kwargs`` is None.
143147
144148
Returns
145149
-------
@@ -151,19 +155,20 @@ def chimera_node_placer_2d(m, n, t, scale=1., center=None, dim=2, normalize_kwar
151155
"""
152156
import numpy as np
153157

154-
line_plot = False if normalize_kwargs is None else normalize_kwargs.get('line_plot')
158+
line_plot = False if plot_kwargs is None else plot_kwargs.get('line_plot')
155159

156160
center_pad = 0 if line_plot else 1
161+
border_pad = 2
157162

158163
tile_center = t // 2
159-
tile_length = t + 2 + center_pad # 2 for spacing between tiles
164+
tile_length = t + border_pad + center_pad
160165

161166
# want the enter plot to fill in [0, 1] when scale=1
162-
fabric_scale = max(m, n) * tile_length - 2 - center_pad
167+
fabric_scale = max(m, n) * tile_length - border_pad - center_pad
163168
scale /= fabric_scale
164169

165-
if normalize_kwargs is not None:
166-
normalize_size_and_aspect(fabric_scale, 200, normalize_kwargs)
170+
if plot_kwargs is not None:
171+
normalize_size_and_aspect(fabric_scale, 200, plot_kwargs)
167172

168173
grid_offsets = {}
169174

@@ -175,6 +180,7 @@ def chimera_node_placer_2d(m, n, t, scale=1., center=None, dim=2, normalize_kwar
175180
if dim < 2:
176181
raise ValueError("layout must have at least two dimensions")
177182

183+
# pad the dimensions beyond the second with zeros
178184
paddims = np.zeros(dim - 2, dtype='float')
179185

180186
if len(center) != dim:
@@ -241,12 +247,12 @@ def draw_chimera(G, **kwargs):
241247
edges (i.e., :math:`i=j`) are treated as linear biases.
242248
243249
line_plot : boolean (optional, default False)
244-
If line_plot is True, then qubits are drawn as line segments, and edges
245-
are drawn either as line segments between qubits, or as circles where
246-
two qubits overlap. In this drawing style, the interpretation the width
247-
and node_size parameters (provided in kwargs) determines the area of the
248-
circles, and line widths, respectively. For more information, see
249-
:func:`dwave_networkx.qubit_layout.draw_lineplot`.
250+
If ``line_plot`` is True, then qubits are drawn as line segments, and
251+
edges are drawn either as line segments between qubits, or as circles
252+
where two qubits overlap. In this drawing style, the interpretation of
253+
the ``width`` and ``node_size`` parameters (provided in ``kwargs``)
254+
determine the area of the circles and line widths respectively. See
255+
:func:`dwave_networkx.qubit_layout.draw_lineplot` for more information.
250256
251257
kwargs : optional keywords
252258
See networkx.draw_networkx() for a description of optional keywords,
@@ -265,7 +271,7 @@ def draw_chimera(G, **kwargs):
265271
>>> plt.show() # doctest: +SKIP
266272
267273
"""
268-
layout = chimera_layout(G, normalize_kwargs = kwargs)
274+
layout = chimera_layout(G, plot_kwargs=kwargs)
269275
draw_qubit_graph(G, layout, **kwargs)
270276

271277
def draw_chimera_embedding(G, *args, **kwargs):
@@ -314,20 +320,20 @@ def draw_chimera_embedding(G, *args, **kwargs):
314320
concentric circles.
315321
316322
line_plot : boolean (optional, default False)
317-
If line_plot is True, then qubits are drawn as line segments, and edges
318-
are drawn either as line segments between qubits, or as circles where
319-
two qubits overlap. In this drawing style, the interpretation the width
320-
and node_size parameters (provided in kwargs) determines the area of the
321-
circles, and line widths, respectively. For more information, see
322-
:func:`dwave_networkx.qubit_layout.draw_lineplot`.
323+
If ``line_plot`` is True, then qubits are drawn as line segments, and
324+
edges are drawn either as line segments between qubits, or as circles
325+
where two qubits overlap. In this drawing style, the interpretation of
326+
the ``width`` and ``node_size`` parameters (provided in ``kwargs``)
327+
determine the area of the circles and line widths respectively. See
328+
:func:`dwave_networkx.qubit_layout.draw_lineplot` for more information.
323329
324330
kwargs : optional keywords
325331
See networkx.draw_networkx() for a description of optional keywords,
326332
with the exception of the `pos` parameter which is not used by this
327333
function. If `linear_biases` or `quadratic_biases` are provided,
328334
any provided `node_color` or `edge_color` arguments are ignored.
329335
"""
330-
layout = chimera_layout(G, normalize_kwargs = kwargs)
336+
layout = chimera_layout(G, plot_kwargs=kwargs)
331337
draw_embedding(G, layout, *args, **kwargs)
332338

333339

@@ -358,12 +364,12 @@ def draw_chimera_yield(G, **kwargs):
358364
Edge fault line style (solid|dashed|dotted|dashdot)
359365
360366
line_plot : boolean (optional, default False)
361-
If line_plot is True, then qubits are drawn as line segments, and edges
362-
are drawn either as line segments between qubits, or as circles where
363-
two qubits overlap. In this drawing style, the interpretation the width
364-
and node_size parameters (provided in kwargs) determines the area of the
365-
circles, and line widths, respectively. For more information, see
366-
:func:`dwave_networkx.qubit_layout.draw_lineplot`.
367+
If ``line_plot`` is True, then qubits are drawn as line segments, and
368+
edges are drawn either as line segments between qubits, or as circles
369+
where two qubits overlap. In this drawing style, the interpretation of
370+
the ``width`` and ``node_size`` parameters (provided in ``kwargs``)
371+
determine the area of the circles and line widths respectively. See
372+
:func:`dwave_networkx.qubit_layout.draw_lineplot` for more information.
367373
368374
kwargs : optional keywords
369375
See networkx.draw_networkx() for a description of optional keywords,
@@ -382,5 +388,5 @@ def draw_chimera_yield(G, **kwargs):
382388
tile, and label attributes to be able to identify faulty qubits.")
383389

384390
perfect_graph = chimera_graph(m,n,t, coordinates=coordinates)
385-
layout = chimera_layout(perfect_graph, normalize_kwargs = kwargs)
391+
layout = chimera_layout(perfect_graph, plot_kwargs=kwargs)
386392
draw_yield(G, layout, perfect_graph, **kwargs)

dwave_networkx/drawing/pegasus_layout.py

+44-34
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
]
3232

3333

34-
def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False, normalize_kwargs=None):
34+
def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False, plot_kwargs=None):
3535
"""Positions the nodes of graph G in a Pegasus topology.
3636
3737
`NumPy <https://scipy.org>`_ is required for this function.
@@ -58,10 +58,12 @@ def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False, normalize_kwa
5858
rather than L configuration. Ignored if G is defined with
5959
``nice_coordinates=True``.
6060
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.
61+
plot_kwargs : None or dict (default None)
62+
A dict of keyword arguments to be used in a plotting function (see
63+
:func:`networkx.draw` and :func:`draw_lineplot`), to scale node and edge
64+
sizes appropriately to the graph `G`. Optional keys in ``plot_kwargs``
65+
are set to default values by the :func:`normalize_size_and_aspect`
66+
function. Do nothing if ``plot_kwargs`` is None.
6567
6668
Returns
6769
-------
@@ -81,7 +83,10 @@ def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False, normalize_kwa
8183
raise ValueError("G must be generated by dwave_networkx.pegasus_graph")
8284

8385
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)
86+
if labels == 'coordinate':
87+
crosses = True
88+
89+
xy_coords = pegasus_node_placer_2d(G, scale, center, dim, crosses, plot_kwargs)
8590

8691
if labels == 'coordinate':
8792
pos = {v: xy_coords(*v) for v in G.nodes()}
@@ -100,7 +105,7 @@ def pegasus_layout(G, scale=1., center=None, dim=2, crosses=False, normalize_kwa
100105
return pos
101106

102107

103-
def pegasus_node_placer_2d(G, scale=1., center=None, dim=2, crosses=False, normalize_kwargs=None):
108+
def pegasus_node_placer_2d(G, scale=1., center=None, dim=2, crosses=False, plot_kwargs=None):
104109
"""Generates a function to convert Pegasus indices to plottable coordinates.
105110
106111
Parameters
@@ -124,6 +129,13 @@ def pegasus_node_placer_2d(G, scale=1., center=None, dim=2, crosses=False, norma
124129
If True, :math:`K_{4,4}` subgraphs are shown in a cross
125130
rather than L configuration.
126131
132+
plot_kwargs : None or dict (default None)
133+
A dict of keyword arguments to be used in a plotting function (see
134+
:func:`networkx.draw` and :func:`draw_lineplot`), to scale node and edge
135+
sizes appropriately to the graph `G`. Optional keys in ``plot_kwargs``
136+
are set to default values by the :func:`normalize_size_and_aspect`
137+
function. Do nothing if ``plot_kwargs`` is None.
138+
127139
Returns
128140
-------
129141
xy_coords : function
@@ -133,7 +145,7 @@ def pegasus_node_placer_2d(G, scale=1., center=None, dim=2, crosses=False, norma
133145
"""
134146
import numpy as np
135147

136-
line_plot = False if normalize_kwargs is None else normalize_kwargs.get('line_plot')
148+
line_plot = False if plot_kwargs is None else plot_kwargs.get('line_plot')
137149

138150
if line_plot:
139151
crosses = False
@@ -150,8 +162,8 @@ def pegasus_node_placer_2d(G, scale=1., center=None, dim=2, crosses=False, norma
150162
fabric_scale = m*tile_width - 2*odd_k_wobble - 1
151163
scale /= fabric_scale
152164

153-
if normalize_kwargs is not None:
154-
normalize_size_and_aspect(fabric_scale, 150, normalize_kwargs)
165+
if plot_kwargs is not None:
166+
normalize_size_and_aspect(fabric_scale, 150, plot_kwargs)
155167

156168
if center is None:
157169
center = np.zeros(dim)
@@ -164,6 +176,7 @@ def pegasus_node_placer_2d(G, scale=1., center=None, dim=2, crosses=False, norma
164176
if dim < 0:
165177
raise ValueError("layout must have at least two dimensions")
166178

179+
# pad the dimensions beyond the second with zeros
167180
paddims = np.zeros(dim - 2)
168181

169182
if len(center) != dim:
@@ -227,13 +240,12 @@ def draw_pegasus(G, crosses=False, **kwargs):
227240
``nice_coordinates=True`` or ``line_plot=True``.
228241
229242
line_plot : boolean (optional, default False)
230-
If line_plot is True, then qubits are drawn as line segments, and edges
231-
are drawn either as line segments between qubits, or as circles where
232-
two qubits overlap. In this drawing style, the interpretation the width
233-
and node_size parameters (provided in kwargs) determines the area of the
234-
circles, and line widths, respectively. For more information, see
235-
:func:`dwave_networkx.qubit_layout.draw_lineplot`.
236-
243+
If ``line_plot`` is True, then qubits are drawn as line segments, and
244+
edges are drawn either as line segments between qubits, or as circles
245+
where two qubits overlap. In this drawing style, the interpretation of
246+
the ``width`` and ``node_size`` parameters (provided in ``kwargs``)
247+
determine the area of the circles and line widths respectively. See
248+
:func:`dwave_networkx.qubit_layout.draw_lineplot` for more information.
237249
238250
kwargs : optional keywords
239251
See networkx.draw_networkx() for a description of optional keywords,
@@ -253,7 +265,7 @@ def draw_pegasus(G, crosses=False, **kwargs):
253265
>>> plt.show() # doctest: +SKIP
254266
255267
"""
256-
layout = pegasus_layout(G, normalize_kwargs = kwargs)
268+
layout = pegasus_layout(G, plot_kwargs=kwargs)
257269
draw_qubit_graph(G, layout, **kwargs)
258270

259271
def draw_pegasus_embedding(G, *args, crosses=False, **kwargs):
@@ -304,21 +316,20 @@ def draw_pegasus_embedding(G, *args, crosses=False, **kwargs):
304316
in G), and these overlaps are displayed as concentric circles.
305317
306318
line_plot : boolean (optional, default False)
307-
If line_plot is True, then qubits are drawn as line segments, and edges
308-
are drawn either as line segments between qubits, or as circles where
309-
two qubits overlap. In this drawing style, the interpretation the width
310-
and node_size parameters (provided in kwargs) determines the area of the
311-
circles, and line widths, respectively. For more information, see
312-
:func:`dwave_networkx.qubit_layout.draw_lineplot`.
313-
319+
If ``line_plot`` is True, then qubits are drawn as line segments, and
320+
edges are drawn either as line segments between qubits, or as circles
321+
where two qubits overlap. In this drawing style, the interpretation of
322+
the ``width`` and ``node_size`` parameters (provided in ``kwargs``)
323+
determine the area of the circles and line widths respectively. See
324+
:func:`dwave_networkx.qubit_layout.draw_lineplot` for more information.
314325
315326
kwargs : optional keywords
316327
See networkx.draw_networkx() for a description of optional keywords,
317328
with the exception of the ``pos`` parameter, which is not used by this
318329
function. If ``linear_biases`` or ``quadratic_biases`` are provided,
319330
any provided ``node_color`` or ``edge_color`` arguments are ignored.
320331
"""
321-
layout = pegasus_layout(G, crosses=crosses, normalize_kwargs=kwargs)
332+
layout = pegasus_layout(G, crosses=crosses, plot_kwargs=kwargs)
322333
draw_embedding(G, layout, *args, **kwargs)
323334

324335
def draw_pegasus_yield(G, crosses=False, **kwargs):
@@ -353,13 +364,12 @@ def draw_pegasus_yield(G, crosses=False, **kwargs):
353364
``nice_coordinates=True`` or ``line_plot=True``.
354365
355366
line_plot : boolean (optional, default False)
356-
If line_plot is True, then qubits are drawn as line segments, and edges
357-
are drawn either as line segments between qubits, or as circles where
358-
two qubits overlap. In this drawing style, the interpretation the width
359-
and node_size parameters (provided in kwargs) determines the area of the
360-
circles, and line widths, respectively. For more information, see
361-
:func:`dwave_networkx.qubit_layout.draw_lineplot`.
362-
367+
If ``line_plot`` is True, then qubits are drawn as line segments, and
368+
edges are drawn either as line segments between qubits, or as circles
369+
where two qubits overlap. In this drawing style, the interpretation of
370+
the ``width`` and ``node_size`` parameters (provided in ``kwargs``)
371+
determine the area of the circles and line widths respectively. See
372+
:func:`dwave_networkx.qubit_layout.draw_lineplot` for more information.
363373
364374
kwargs : optional keywords
365375
See networkx.draw_networkx() for a description of optional keywords,
@@ -380,5 +390,5 @@ def draw_pegasus_yield(G, crosses=False, **kwargs):
380390

381391

382392
perfect_graph = pegasus_graph(m, offset_lists=offset_lists, coordinates=coordinates, nice_coordinates=nice)
383-
layout = pegasus_layout(perfect_graph, crosses=crosses, normalize_kwargs=kwargs)
393+
layout = pegasus_layout(perfect_graph, crosses=crosses, plot_kwargs=kwargs)
384394
draw_yield(G, layout, perfect_graph, **kwargs)

0 commit comments

Comments
 (0)