Skip to content

Commit

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

* Fix typing-extensions

* Fix control flow with fold minus one

(cherry picked from commit 43381ae)

Co-authored-by: Edwin Navarro <enavarro@comcast.net>
  • Loading branch information
mergify[bot] and enavarro51 committed Mar 19, 2024
1 parent 89d97a8 commit c9b4687
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 @@ -1544,7 +1544,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>`__.
Loading
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 @@ -2228,6 +2228,27 @@ def test_control_flow_nested_layout(self):
)
self.assertGreaterEqual(ratio, self.threshold)

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", 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, self.threshold)

def test_annotated_operation(self):
"""Test AnnotatedOperations and other non-Instructions."""
circuit = QuantumCircuit(3)
Expand Down

0 comments on commit c9b4687

Please sign in to comment.