Skip to content

Commit

Permalink
Update plot_gate_map() family to leverage graphviz for visualization (#…
Browse files Browse the repository at this point in the history
…10208)

* Update gate_map.py

Update the gate_map.py to migrate the visualization modules from matplotlib to rustworkx.graphviz

* Removed has_rustworkx instances

* Added release notes

* Update test_gate_map.py

Updated tests for the modified gate_map.py file

* Formatted gate_map.py

* Format test_gate_map.py

* Added release notes for the fix of #9031

* Update gate_map.py

* Update test_gate_map.py

Test file updated so that all tests can be passed.

* Update test_gate_map.py

* Update gate_map.py for rerunning tests.

* Update test_clifford.py

* Update gate_map.py to reuse rx.draw_graphiz

* Update test_gate_map.py to omit qubit_visualization

* Update test_gate_map.py to fix formatting changes

* Update gate_map.py

* Update gate_map.py

* Update gate_map.py

* Update test_gate_map.py

* Update test_gate_map.py

* Update gate_map.py

* Update gate_map.py

* Update gate_map.py

* Update gate_map.py

* Update gate_map.py

* Update gate_map.py

* Update gate_map.py

* Update test_gate_map.py

* Update test_gate_map.py to add rx import

* Update test_gate_map.py

* Update test_gate_map.py

* Update test_gate_map.py to fix imports

* Update test_gate_map.py to add HAS_GRAPHVIZ to all tests

* Update test_graph_matplotlib_drawer.py to have HAS_GRAPHVIZ

* Update test_graph_matplotlib_drawer.py to add HAS_GRAPHVIZ import to test_font_color function

* Update test_graph_matplotlib_drawer.py to add GRAPHVIZ check to the class

* Update test_graph_matplotlib_drawer.py to add HAS_GRAPHVIZ to both gate_plot_map and test_gate_plot_map import to test_font_color function

* Update test_graph_matplotlib_drawer.py

* Update test_graph_matplotlib_drawer.py

* Update test_graph_matplotlib_drawer.py

* Fixed the `font_color` parameter in `gate_map.py/plot_gate_map()`

The font_color parameter can now accept hex values as well.

* Modify color_edge function to increase speed

* Update gate_map.py to fix formatting

* Update gate_map.py to shift seaborn import to `plot_error_map`

* Add HAS_SEABORN to test_plot_error_map

* Reformat gate_map.py

* Fixed node autoscaling in `gate_map.py` graphs

* Add return type to `test_from_gate_with_cyclic_definition ` in `test_clifford.py`

* Update update-gate_map-visualizations-6ea907a0502fdc1a.yaml

* Rename update-gate_map-visualizations-6ea907a0502fdc1a.yaml to update-gate-map-visualizations-6ea907a0502fdc1a.yaml

* Update update-gate-map-visualizations-6ea907a0502fdc1a.yaml

* Update update-gate-map-visualizations-6ea907a0502fdc1a.yaml

* Update pauli_op.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update operator.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update pauli_sum_op.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update random.py to use np.product

`np.product` is deprecated and is causing test fails.

* Update chi.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Replace np.product with np.prod

`np.product` is deprecated and is causing test fails.

* Update ptm.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update stinespring.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update superop.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update transformations.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update densitymatrix.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update random.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update statevector.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update local_readout_mitigator.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update unitary_synthesis.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update test_random.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update test_scalar_op.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update test_random.py to use np.prod

`np.product` is deprecated and is causing test fails.

* Update test_clifford.py to remove debugging code

* Move matplotlib imports to occur at run time

* Apply suggestions from code review

* Move matplotlib_close_if_inline to runtime import too

* Flatten parallel edges

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Fix 1 qubit backend handling

* Fix qubit label and font sizes

* new reference images

* Adjust font scaling

* Update reference images

* Remove unrelated reference file update

* Improve formatting

* Adjust pixel scaling factor

* Update reference images with formatting changes

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
  • Loading branch information
3 people authored Sep 22, 2023
1 parent af24227 commit 778acaf
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 163 deletions.
267 changes: 105 additions & 162 deletions qiskit/visualization/gate_map.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
features:
- |
The visualizations from the :func:`~.plot_gate_map`, :func:`~.plot_coupling_map`.
:func:`~.plot_error_map`, and :func:`~.plot_circuit_layout` functions have been significantly
improved for rendering layouts of backends with large numbers of qubits. This was accomplished
by leveraging `graphviz <https://graphviz.org/>`__ through rustworkx's ``graphviz_draw()`` function
to perform a more sophisticated algorithmic graph layout that scales for large numbers of
qubits.
upgrade:
- |
The visualization functions: :func:`~.plot_gate_map`, :func:`~.plot_coupling_map`.
:func:`~.plot_error_map`, and :func:`~.plot_circuit_layout` now depend on
`graphviz <https://graphviz.org/>`__ being installed to function. This change was
necessary to enable visualizing backends with larger numbers of qubits. This
additional external requirement is in addition to the existing optional dependencies
these functions previously required. You find details on how to install
graphviz here: https://graphviz.org/download/
20 changes: 19 additions & 1 deletion test/python/visualization/test_gate_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class TestGateMap(QiskitVisualizationTestCase):
)

@data(*backends)
@unittest.skipIf(not optionals.HAS_MATPLOTLIB, "matplotlib not available.")
@unittest.skipUnless(optionals.HAS_GRAPHVIZ, "Graphviz not installed")
def test_plot_gate_map(self, backend):
"""tests plotting of gate map of a device (20 qubit, 16 qubit, 14 qubit and 5 qubit)"""
n = backend.configuration().n_qubits
Expand All @@ -67,6 +69,8 @@ def test_plot_gate_map(self, backend):
plt.close(fig)

@data(*backends)
@unittest.skipIf(not optionals.HAS_MATPLOTLIB, "matplotlib not available.")
@unittest.skipUnless(optionals.HAS_GRAPHVIZ, "Graphviz not installed")
def test_plot_circuit_layout(self, backend):
"""tests plot_circuit_layout for each device"""
layout_length = int(backend._configuration.n_qubits / 2)
Expand All @@ -83,9 +87,11 @@ def test_plot_circuit_layout(self, backend):
with BytesIO() as img_buffer:
fig.savefig(img_buffer, format="png")
img_buffer.seek(0)
self.assertImagesAreEqual(Image.open(img_buffer), img_ref, 0.1)
self.assertImagesAreEqual(Image.open(img_buffer), img_ref, 0.2)
plt.close(fig)

@unittest.skipIf(not optionals.HAS_MATPLOTLIB, "matplotlib not available.")
@unittest.skipUnless(optionals.HAS_GRAPHVIZ, "Graphviz not installed")
def test_plot_gate_map_no_backend(self):
"""tests plotting of gate map without a device"""
n_qubits = 8
Expand All @@ -101,6 +107,9 @@ def test_plot_gate_map_no_backend(self):
self.assertImagesAreEqual(Image.open(img_buffer), img_ref, 0.2)
plt.close(fig)

@unittest.skipIf(not optionals.HAS_MATPLOTLIB, "matplotlib not available.")
@unittest.skipUnless(optionals.HAS_GRAPHVIZ, "Graphviz not installed")
@unittest.skipUnless(optionals.HAS_SEABORN, "Seaborn not installed")
def test_plot_error_map_backend_v1(self):
"""Test plotting error map with fake backend v1."""
backend = FakeKolkata()
Expand All @@ -112,6 +121,9 @@ def test_plot_error_map_backend_v1(self):
self.assertImagesAreEqual(Image.open(img_buffer), img_ref, 0.2)
plt.close(fig)

@unittest.skipIf(not optionals.HAS_MATPLOTLIB, "matplotlib not available.")
@unittest.skipUnless(optionals.HAS_GRAPHVIZ, "Graphviz not installed")
@unittest.skipUnless(optionals.HAS_SEABORN, "Seaborn not installed")
def test_plot_error_map_backend_v2(self):
"""Test plotting error map with fake backend v2."""
backend = FakeKolkataV2()
Expand All @@ -123,6 +135,9 @@ def test_plot_error_map_backend_v2(self):
self.assertImagesAreEqual(Image.open(img_buffer), img_ref, 0.2)
plt.close(fig)

@unittest.skipIf(not optionals.HAS_MATPLOTLIB, "matplotlib not available.")
@unittest.skipUnless(optionals.HAS_GRAPHVIZ, "Graphviz not installed")
@unittest.skipUnless(optionals.HAS_SEABORN, "Seaborn not installed")
def test_plot_error_map_over_100_qubit(self):
"""Test plotting error map with large fake backend."""
backend = FakeWashington()
Expand All @@ -134,6 +149,9 @@ def test_plot_error_map_over_100_qubit(self):
self.assertImagesAreEqual(Image.open(img_buffer), img_ref, 0.2)
plt.close(fig)

@unittest.skipIf(not optionals.HAS_MATPLOTLIB, "matplotlib not available.")
@unittest.skipUnless(optionals.HAS_GRAPHVIZ, "Graphviz not installed")
@unittest.skipUnless(optionals.HAS_SEABORN, "Seaborn not installed")
def test_plot_error_map_over_100_qubit_backend_v2(self):
"""Test plotting error map with large fake backendv2."""
backend = FakeWashingtonV2()
Expand Down
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/1_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/coupling_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/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/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/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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 778acaf

Please sign in to comment.