Skip to content

Commit

Permalink
Fix mpl circuit drawer with fold=-1 hanging with ControlFlow ops (bac…
Browse files Browse the repository at this point in the history
…kport #12016) (#12036)

* Fix mpl circuit drawer with fold=-1 hanging with ControlFlow ops (#12016)

* Fix typing-extensions

* Fix control flow with fold minus one

(cherry picked from commit 43381ae)

# Conflicts:
#	test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py

* Update test_circuit_matplotlib_drawer.py

* Update test_circuit_matplotlib_drawer.py

* Update test_circuit_matplotlib_drawer.py

* Update test_circuit_matplotlib_drawer.py

* Update test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py

---------

Co-authored-by: Edwin Navarro <enavarro@comcast.net>
Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
  • Loading branch information
3 people committed Mar 22, 2024
1 parent e43d5f1 commit 08f5812
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/visualization/circuit/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ def _flow_op_gate(self, node, node_data, glob_data):
while end_x > 0.0:
x_shift = fold_level * self._fold
y_shift = fold_level * (glob_data["n_lines"] + 1)
end_x = xpos + box_width - x_shift
end_x = xpos + box_width - x_shift if self._fold > 0 else 0.0

if isinstance(node.op, IfElseOp):
flow_text = " If"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Fixed an issue with the :func:`circuit_drawer` or :meth:`QuantumCircuit.draw`
when using the ``mpl`` output option where the program would hang if the
circuit being drawn had a ControlFlow operation in it and the ``fold`` option
was set to -1 (meaning no fold).
Fixed `#12012 <https://github.com/Qiskit/qiskit/issues/12012>`__.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,27 @@ def test_control_flow_nested_layout(self):
)
self.assertGreaterEqual(ratio, 0.9999)

def test_control_flow_with_fold_minus_one(self):
"""Test control flow works with fold=-1. Qiskit issue #12012"""
qreg = QuantumRegister(2, "qr")
creg = ClassicalRegister(2, "cr")
circuit = QuantumCircuit(qreg, creg)
with circuit.if_test((creg[1], 1)):
circuit.h(0)
circuit.cx(0, 1)

fname = "control_flow_fold_minus_one.png"
self.circuit_drawer(circuit, output="mpl", style="iqp", filename=fname, fold=-1)

ratio = VisualTestUtilities._save_diff(
self._image_path(fname),
self._reference_path(fname),
fname,
FAILURE_DIFF_DIR,
FAILURE_PREFIX,
)
self.assertGreaterEqual(ratio, 0.9999)

def test_default_futurewarning(self):
"""Test using the default scheme emits a future warning."""
qc = QuantumCircuit(1)
Expand Down

0 comments on commit 08f5812

Please sign in to comment.