Skip to content

Commit 351fc0a

Browse files
enavarro51mergify[bot]
authored andcommitted
Fix display of Box End after transpile in text drawer (#14278)
* Fix typing-extensions * Fix text drawer flow end display * Lint * Add reno --------- Co-authored-by: Jake Lishman <[email protected]> (cherry picked from commit 3e96ade) # Conflicts: # test/python/visualization/test_circuit_text_drawer.py
1 parent 3afa61f commit 351fc0a

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

qiskit/visualization/circuit/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ def lookup_var(var):
13961396
layers.append(flow_layer2.full_layer)
13971397

13981398
# Draw the right box for End
1399-
flow_layer = self.draw_flow_box(node, flow_wire_map, CF_RIGHT, conditional=False)
1399+
flow_layer = self.draw_flow_box(node, wire_map, CF_RIGHT, conditional=False)
14001400
layers.append(flow_layer.full_layer)
14011401

14021402
def draw_flow_box(self, node, flow_wire_map, section, circ_num=0, conditional=False):
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a visualization bug in the text circuit drawer where post-transpilation control-flow
5+
operations could have their closing "bracket" rendered vertically out of order. See
6+
`#14271 <https://github.com/Qiskit/qiskit/issues/14271>`__ for more detail.

test/python/visualization/test_circuit_text_drawer.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,67 @@ def test_wire_order(self):
403403
expected,
404404
)
405405

406+
<<<<<<< HEAD
407+
=======
408+
def test_box_end_after_transpile(self):
409+
"""Test that drawing a `box` doesn't explode."""
410+
# The exact output is not important - feel free to change it. We only care that it doesn't
411+
# explode when drawing.
412+
qc = QuantumCircuit(5)
413+
qc = QuantumCircuit(4)
414+
with qc.box():
415+
qc.cx(0, 1)
416+
qc.cx(0, 3)
417+
418+
qc_ = transpile(qc, initial_layout=[2, 3, 1, 0])
419+
# We don't care about trailing whitespace on a line.
420+
actual = "\n".join(
421+
line.rstrip() for line in str(qc_.draw("text", fold=80, idle_wires=True)).splitlines()
422+
)
423+
424+
expected = """\
425+
┌─────── ┌───┐ ───────┐
426+
q_3 -> 0 ┤ ─────┤ X ├ ├─
427+
│ └─┬─┘ │
428+
q_2 -> 1 ┤ ───────┼── ├─
429+
│ Box-0 │ End-0 │
430+
q_0 -> 2 ┤ ──■────■── ├─
431+
│ ┌─┴─┐ │
432+
q_1 -> 3 ┤ ┤ X ├───── ├─
433+
└─────── └───┘ ───────┘
434+
""".rstrip()
435+
self.assertEqual(actual, expected)
436+
437+
def test_basic_box(self):
438+
"""Test that drawing a `box` doesn't explode."""
439+
# The exact output is not important - feel free to change it. We only care that it doesn't
440+
# explode when drawing.
441+
qc = QuantumCircuit(5)
442+
with qc.box():
443+
qc.x(0)
444+
with qc.box():
445+
qc.cx(2, 3)
446+
with qc.box():
447+
qc.noop(4)
448+
# We don't care about trailing whitespace on a line.
449+
actual = "\n".join(line.rstrip() for line in str(qc.draw("text", fold=80)).splitlines())
450+
451+
expected = """\
452+
┌─────── ┌───┐ ───────┐
453+
q_0: ┤ Box-0 ┤ X ├ End-0 ├────────────────────────────────────────────
454+
└─────── └───┘ ───────┘
455+
q_1: ───────────────────────────────────────────────────────────────────
456+
┌─────── ───────┐
457+
q_2: ────────────────────────┤ ──■───────────────────── ├─
458+
│ ┌─┴─┐ │
459+
q_3: ────────────────────────┤ Box-0 ┤ X ├─────────────────── End-0 ├─
460+
│ └───┘┌─────── ───────┐ │
461+
q_4: ────────────────────────┤ ─────┤ Box-1 End-1 ├─ ├─
462+
└─────── └─────── ───────┘ ───────┘
463+
""".rstrip()
464+
self.assertEqual(actual, expected)
465+
466+
>>>>>>> 3e96ade54 (Fix display of Box End after transpile in text drawer (#14278))
406467
def test_text_swap(self):
407468
"""Swap drawing."""
408469
expected = "\n".join(

0 commit comments

Comments
 (0)