diff --git a/qiskit/visualization/circuit/text.py b/qiskit/visualization/circuit/text.py index f019198246f7..63a4fed3d9f5 100644 --- a/qiskit/visualization/circuit/text.py +++ b/qiskit/visualization/circuit/text.py @@ -1396,7 +1396,7 @@ def lookup_var(var): layers.append(flow_layer2.full_layer) # Draw the right box for End - flow_layer = self.draw_flow_box(node, flow_wire_map, CF_RIGHT, conditional=False) + flow_layer = self.draw_flow_box(node, wire_map, CF_RIGHT, conditional=False) layers.append(flow_layer.full_layer) def draw_flow_box(self, node, flow_wire_map, section, circ_num=0, conditional=False): diff --git a/releasenotes/notes/draw-control-flow-end-ceb58ed2c46ff758.yaml b/releasenotes/notes/draw-control-flow-end-ceb58ed2c46ff758.yaml new file mode 100644 index 000000000000..3d68e970f4b5 --- /dev/null +++ b/releasenotes/notes/draw-control-flow-end-ceb58ed2c46ff758.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed a visualization bug in the text circuit drawer where post-transpilation control-flow + operations could have their closing "bracket" rendered vertically out of order. See + `#14271 `__ for more detail. diff --git a/test/python/visualization/test_circuit_text_drawer.py b/test/python/visualization/test_circuit_text_drawer.py index e7ec6c03a4f3..452c4832a685 100644 --- a/test/python/visualization/test_circuit_text_drawer.py +++ b/test/python/visualization/test_circuit_text_drawer.py @@ -397,6 +397,35 @@ def test_wire_order(self): expected, ) + def test_box_end_after_transpile(self): + """Test that drawing a `box` doesn't explode.""" + # The exact output is not important - feel free to change it. We only care that it doesn't + # explode when drawing. + qc = QuantumCircuit(5) + qc = QuantumCircuit(4) + with qc.box(): + qc.cx(0, 1) + qc.cx(0, 3) + + qc_ = transpile(qc, initial_layout=[2, 3, 1, 0]) + # We don't care about trailing whitespace on a line. + actual = "\n".join( + line.rstrip() for line in str(qc_.draw("text", fold=80, idle_wires=True)).splitlines() + ) + + expected = """\ + ┌─────── ┌───┐ ───────┐ +q_3 -> 0 ┤ ─────┤ X ├ ├─ + │ └─┬─┘ │ +q_2 -> 1 ┤ ───────┼── ├─ + │ Box-0 │ End-0 │ +q_0 -> 2 ┤ ──■────■── ├─ + │ ┌─┴─┐ │ +q_1 -> 3 ┤ ┤ X ├───── ├─ + └─────── └───┘ ───────┘ +""".rstrip() + self.assertEqual(actual, expected) + def test_basic_box(self): """Test that drawing a `box` doesn't explode.""" # The exact output is not important - feel free to change it. We only care that it doesn't