Skip to content

Commit

Permalink
Adjust graphviz options for better layout with plot_gate_map() (#12770)…
Browse files Browse the repository at this point in the history
… (#13089)

* Adjust graphviz options for better layout with plot_gate_map()

This commit fixes an issue with the visualizations of some
backends/coupling maps. The default neato settings worked well in most
cases but for some graphs it was not generating a good layout. This
tweaks the settings a bit so that layouts work with more classes of
graphs.

* Don't set graph attributes if qubit_coordinates is set

* adjust sizes

* planar branching

* support for backendv1

* remove the formula

* new ref images

---------

Co-authored-by: Luciano Bello <[email protected]>
Co-authored-by: Elena Peña Tapia <[email protected]>
(cherry picked from commit 52733ae)

Co-authored-by: Matthew Treinish <[email protected]>
  • Loading branch information
mergify[bot] and mtreinish authored Sep 5, 2024
1 parent 722eba2 commit 584ee9a
Show file tree
Hide file tree
Showing 30 changed files with 40 additions and 12 deletions.
34 changes: 28 additions & 6 deletions qiskit/visualization/gate_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ def plot_gate_map(
font_color,
ax,
filename,
planar=rx.is_planar(coupling_map.graph.to_undirected(multigraph=False)),
)


Expand All @@ -966,6 +967,8 @@ def plot_coupling_map(
font_color="white",
ax=None,
filename=None,
*,
planar=True,
):
"""Plots an arbitrary coupling map of qubits (embedded in a plane).
Expand All @@ -987,6 +990,7 @@ def plot_coupling_map(
font_color (str): The font color for the qubit labels.
ax (Axes): A Matplotlib axes instance.
filename (str): file path to save image to.
planar (bool): If the coupling map is planar or not. Default: ``True`` (i.e. it is planar)
Returns:
Figure: A Matplotlib figure instance.
Expand Down Expand Up @@ -1057,16 +1061,21 @@ def plot_coupling_map(

if font_size is None:
max_characters = max(1, max(len(str(x)) for x in qubit_labels))
font_size = max(int(20 / max_characters), 1)
if max_characters == 1:
font_size = 20
elif max_characters == 2:
font_size = 14
elif max_characters == 3:
font_size = 12
else:
font_size = 1

def color_node(node):
if qubit_coordinates:
out_dict = {
"label": str(qubit_labels[node]),
"color": f'"{qubit_color[node]}"',
"fillcolor": f'"{qubit_color[node]}"',
"style": "filled",
"shape": "circle",
"pos": f'"{qubit_coordinates[node][0]},{qubit_coordinates[node][1]}"',
"pin": "True",
}
Expand All @@ -1075,11 +1084,11 @@ def color_node(node):
"label": str(qubit_labels[node]),
"color": f'"{qubit_color[node]}"',
"fillcolor": f'"{qubit_color[node]}"',
"style": "filled",
"shape": "circle",
}
out_dict["style"] = "filled"
out_dict["shape"] = "circle"
out_dict["fontcolor"] = f'"{font_color}"'
out_dict["fontsize"] = str(font_size)
out_dict["fontsize"] = f'"{str(font_size)}!"'
out_dict["height"] = str(qubit_size * px)
out_dict["fixedsize"] = "True"
out_dict["fontname"] = '"DejaVu Sans"'
Expand All @@ -1093,9 +1102,22 @@ def color_edge(edge):
}
return out_dict

graph_attributes = None
if not qubit_coordinates:
if planar:
graph_attributes = {
"overlap_scaling": "-7",
"overlap": "prism",
"model": "subset",
}
else:
graph_attributes = {
"overlap": "true",
}
plot = graphviz_draw(
graph,
method="neato",
graph_attr=graph_attributes,
node_attr_fn=color_node,
edge_attr_fn=color_edge,
filename=filename,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixes an issue with the visualizations of some backends/coupling maps that showed as folded on their own. The
`default ``neato`` setting <https://graphviz.org/docs/layouts/neato/>`_ works well in most cases. However,
``prism`` overlap returns a more regular layout for other scenarios.
Binary file modified test/visual/mpl/graph/references/16_qubit_gate_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/27_qubit_gate_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/5_qubit_gate_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/65_qubit_gate_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/7_qubit_gate_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/bloch_multivector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/figsize.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/font_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/hinton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_2_sets_with_rest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_desc_value_sort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_hamming.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_legend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_multiple_colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_value_sort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/histogram_with_rest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/line_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/paulivec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/qsphere.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/qubit_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/qubit_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/visual/mpl/graph/references/qubit_size.png
Binary file modified test/visual/mpl/graph/references/state_city.png
12 changes: 6 additions & 6 deletions test/visual/mpl/graph/test_graph_matplotlib_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_plot_bloch_multivector(self):
FAILURE_DIFF_DIR,
FAILURE_PREFIX,
)
self.assertGreaterEqual(ratio, 0.99)
self.assertGreaterEqual(ratio, 0.99, msg=fname)

def test_plot_state_hinton(self):
"""test plot_state_hinton"""
Expand Down Expand Up @@ -437,7 +437,7 @@ def test_plot_7_qubit_gate_map(self):
FAILURE_DIFF_DIR,
FAILURE_PREFIX,
)
self.assertGreaterEqual(ratio, 0.99)
self.assertGreaterEqual(ratio, 0.99, msg=fname)

def test_plot_16_qubit_gate_map(self):
"""Test plot_gate_map using 16 qubit backend"""
Expand All @@ -455,7 +455,7 @@ def test_plot_16_qubit_gate_map(self):
FAILURE_DIFF_DIR,
FAILURE_PREFIX,
)
self.assertGreaterEqual(ratio, 0.99)
self.assertGreaterEqual(ratio, 0.99, msg=fname)

def test_plot_27_qubit_gate_map(self):
"""Test plot_gate_map using 27 qubit backend"""
Expand Down Expand Up @@ -527,7 +527,7 @@ def test_qubit_size(self):
FAILURE_DIFF_DIR,
FAILURE_PREFIX,
)
self.assertGreaterEqual(ratio, 0.99)
self.assertGreaterEqual(ratio, 0.99, msg=fname)

def test_qubit_color(self):
"""Test qubit_color parameter of plot_gate_map"""
Expand All @@ -545,7 +545,7 @@ def test_qubit_color(self):
FAILURE_DIFF_DIR,
FAILURE_PREFIX,
)
self.assertGreaterEqual(ratio, 0.99)
self.assertGreaterEqual(ratio, 0.99, msg=fname)

def test_qubit_labels(self):
"""Test qubit_labels parameter of plot_gate_map"""
Expand Down Expand Up @@ -658,7 +658,7 @@ def test_plot_bloch_multivector_figsize_improvements(self):
FAILURE_DIFF_DIR,
FAILURE_PREFIX,
)
self.assertGreaterEqual(ratio, 0.99)
self.assertGreaterEqual(ratio, 0.99, msg=fname)


if __name__ == "__main__":
Expand Down

0 comments on commit 584ee9a

Please sign in to comment.