Skip to content

Commit 2fb5be6

Browse files
authored
Fix barrier label trunctation in matplotlib circuit renders (#15973)
* Fix mpl barrier_label_len with non-default value * update test * fix the whitespace issue too * update test image
1 parent e8180ed commit 2fb5be6

4 files changed

Lines changed: 7 additions & 1 deletion

File tree

qiskit/visualization/circuit/circuit_visualization.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ def check_clbit_in_inst(circuit, cregbundle):
382382
wire_order=complete_wire_order,
383383
expr_len=expr_len,
384384
measure_arrows=measure_arrows,
385+
barrier_label_len=barrier_label_len,
385386
)
386387
else:
387388
raise VisualizationError(

qiskit/visualization/circuit/matplotlib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ def _get_layer_widths(self, node_data, wire_map, outer_circuit, glob_data):
431431
gate_text, ctrl_text, raw_gate_text = get_gate_ctrl_text(
432432
op, "mpl", style=self._style
433433
)
434+
# Truncate directive labels to match the rendered length so the layer
435+
# width doesn't account for text that will never be displayed.
436+
if getattr(op, "_directive", False) and len(gate_text) > self._barrier_label_len:
437+
gate_text = gate_text[: self._barrier_label_len] + "..."
434438
node_data[node].gate_text = gate_text
435439
node_data[node].ctrl_text = ctrl_text
436440
# Measure doesn't use raw_gate_text since it displays a dial
1.72 KB
Loading

test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,8 @@ def test_barrier_label_truncation(self):
12461246
"""Test that long barrier labels are truncated"""
12471247
circuit = QuantumCircuit(2)
12481248
circuit.barrier()
1249-
circuit.barrier(label="a" * 20)
1249+
circuit.barrier(label="a" * 10)
1250+
circuit.barrier(label="b" * 1000)
12501251

12511252
fname = "barrier_label_truncation.png"
12521253
self.circuit_drawer(circuit, output="mpl", filename=fname, barrier_label_len=9)

0 commit comments

Comments
 (0)