Skip to content

Commit c18feed

Browse files
authored
display tags by str instead of repr in circuit diagrams (#6530)
Use more readable rendering of tagged operations in diagrams. Fixes: #6411
1 parent 1bec6b5 commit c18feed

13 files changed

+221
-223
lines changed

cirq-core/cirq/circuits/circuit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,7 @@ def _draw_moment_in_diagram(
26302630
if desc:
26312631
y = max(label_map.values(), default=0) + 1
26322632
if tags and include_tags:
2633-
desc = desc + str(tags)
2633+
desc = desc + f"[{', '.join(map(str, tags))}]"
26342634
out_diagram.write(x0, y, desc)
26352635

26362636
if not non_global_ops:

cirq-core/cirq/circuits/circuit_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2305,9 +2305,9 @@ def test_diagram_global_phase(circuit_cls):
23052305
cirq.testing.assert_has_diagram(
23062306
c,
23072307
"""\
2308-
2: ───X──────────
2308+
2: ───X────────
23092309
2310-
π['tag']""",
2310+
π[tag]""",
23112311
)
23122312

23132313

cirq-core/cirq/ops/raw_types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def _circuit_diagram_info_(
921921
# Add tag to wire symbol if it exists.
922922
if sub_op_info is not NotImplemented and args.include_tags and sub_op_info.wire_symbols:
923923
sub_op_info.wire_symbols = (
924-
sub_op_info.wire_symbols[0] + str(list(self._tags)),
924+
sub_op_info.wire_symbols[0] + f"[{', '.join(map(str, self._tags))}]",
925925
) + sub_op_info.wire_symbols[1:]
926926
return sub_op_info
927927

cirq-core/cirq/ops/raw_types_test.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -490,17 +490,17 @@ def test_cannot_remap_non_measurement_gate():
490490

491491
def test_circuit_diagram():
492492
class TaggyTag:
493-
"""Tag with a custom repr function to test circuit diagrams."""
493+
"""Tag with a custom str function to test circuit diagrams."""
494494

495-
def __repr__(self):
496-
return 'TaggyTag()'
495+
def __str__(self):
496+
return '<taggy>'
497497

498498
h = cirq.H(cirq.GridQubit(1, 1))
499499
tagged_h = h.with_tags('tag1')
500500
non_string_tag_h = h.with_tags(TaggyTag())
501501

502502
expected = cirq.CircuitDiagramInfo(
503-
wire_symbols=("H['tag1']",),
503+
wire_symbols=("H[tag1]",),
504504
exponent=1.0,
505505
connected=True,
506506
exponent_qubit_index=None,
@@ -511,14 +511,14 @@ def __repr__(self):
511511
assert cirq.circuit_diagram_info(tagged_h, args) == cirq.circuit_diagram_info(h)
512512

513513
c = cirq.Circuit(tagged_h)
514-
diagram_with_tags = "(1, 1): ───H['tag1']───"
514+
diagram_with_tags = "(1, 1): ───H[tag1]───"
515515
diagram_without_tags = "(1, 1): ───H───"
516516
assert str(cirq.Circuit(tagged_h)) == diagram_with_tags
517517
assert c.to_text_diagram() == diagram_with_tags
518518
assert c.to_text_diagram(include_tags=False) == diagram_without_tags
519519

520520
c = cirq.Circuit(non_string_tag_h)
521-
diagram_with_non_string_tag = "(1, 1): ───H[TaggyTag()]───"
521+
diagram_with_non_string_tag = "(1, 1): ───H[<taggy>]───"
522522
assert c.to_text_diagram() == diagram_with_non_string_tag
523523
assert c.to_text_diagram(include_tags=False) == diagram_without_tags
524524

@@ -531,7 +531,7 @@ def test_circuit_diagram_tagged_global_phase():
531531
# Just global phase in a circuit
532532
assert cirq.circuit_diagram_info(global_phase, default='default') == 'default'
533533
cirq.testing.assert_has_diagram(
534-
cirq.Circuit(global_phase), "\n\nglobal phase: π['tag0']", use_unicode_characters=True
534+
cirq.Circuit(global_phase), "\n\nglobal phase: π[tag0]", use_unicode_characters=True
535535
)
536536
cirq.testing.assert_has_diagram(
537537
cirq.Circuit(global_phase),
@@ -558,9 +558,7 @@ def _circuit_diagram_info_(
558558
no_wire_symbol_op = NoWireSymbols(coefficient=-1.0)().with_tags('tag0')
559559
assert cirq.circuit_diagram_info(no_wire_symbol_op, default='default') == expected
560560
cirq.testing.assert_has_diagram(
561-
cirq.Circuit(no_wire_symbol_op),
562-
"\n\nglobal phase: π['tag0']",
563-
use_unicode_characters=True,
561+
cirq.Circuit(no_wire_symbol_op), "\n\nglobal phase: π[tag0]", use_unicode_characters=True
564562
)
565563

566564
# Two global phases in one moment
@@ -570,9 +568,9 @@ def _circuit_diagram_info_(
570568
cirq.testing.assert_has_diagram(
571569
c,
572570
"""\
573-
a: ─────────────X───────────────────
571+
a: ─────────────X───────────────
574572
575-
global phase: π['tag1', 'tag2']""",
573+
global phase: π[tag1, tag2]""",
576574
use_unicode_characters=True,
577575
precision=2,
578576
)
@@ -583,9 +581,9 @@ def _circuit_diagram_info_(
583581
cirq.testing.assert_has_diagram(
584582
c,
585583
"""\
586-
a: ─────────────X['x_tag']─────X──────────────
584+
a: ─────────────X[x_tag]─────X────────────
587585
588-
global phase: 0.5π['tag1'] 0.5π['tag2']
586+
global phase: 0.5π[tag1] 0.5π[tag2]
589587
""",
590588
use_unicode_characters=True,
591589
include_tags=True,
@@ -603,7 +601,7 @@ def __repr__(self):
603601
q = cirq.GridQubit(1, 1)
604602
expected = "(1, 1): ───guess-i-will-repr───"
605603
assert cirq.Circuit(NoCircuitDiagram()(q)).to_text_diagram() == expected
606-
expected = "(1, 1): ───guess-i-will-repr['taggy']───"
604+
expected = "(1, 1): ───guess-i-will-repr[taggy]───"
607605
assert cirq.Circuit(NoCircuitDiagram()(q).with_tags('taggy')).to_text_diagram() == expected
608606

609607

cirq-core/cirq/protocols/circuit_diagram_info_protocol.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def _op_info_with_fallback(
356356

357357
# Add tags onto the representation, if they exist
358358
if op.tags:
359-
name += f'{list(op.tags)}'
359+
name += f"[{', '.join(map(str, op.tags))}]"
360360

361361
# Include ordering in the qubit labels.
362362
symbols = (name,) + tuple(f'#{i + 1}' for i in range(1, len(op.qubits)))

cirq-core/cirq/transformers/merge_k_qubit_gates_test.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ def test_merge_complex_circuit_preserving_moment_structure():
122122
cirq.testing.assert_has_diagram(
123123
c_orig,
124124
'''
125-
0: ───H───@───@───H───@───X───────@─────────────────X───X['ignore']───@───X───
126-
│ │ │ │ │ ║
127-
1: ───H───┼───X───────@───────Y───X───@['ignore']───────Y─────────────X───╫───
128-
│ │
129-
2: ───H───X───────────────────────────X─────────────────Z─────────────M───╫───
130-
║ ║
131-
a: ═══════════════════════════════════════════════════════════════════@═══^═══
125+
0: ───H───@───@───H───@───X───────@───────────────X───X[ignore]───@───X───
126+
│ │ │ │ │ ║
127+
1: ───H───┼───X───────@───────Y───X───@[ignore]───────Y───────────X───╫───
128+
│ │ ║
129+
2: ───H───X───────────────────────────X───────────────Z───────────M───╫───
130+
║ ║
131+
a: ═══════════════════════════════════════════════════════════════@═══^═══
132132
''',
133133
)
134134
component_id = 0
@@ -147,15 +147,15 @@ def rewriter_merge_to_circuit_op(op: 'cirq.CircuitOperation') -> 'cirq.OP_TREE':
147147
cirq.testing.assert_has_diagram(
148148
cirq.drop_empty_moments(c_new),
149149
'''
150-
[ 0: ───H───@─── ] [ 0: ───────@───H───@───X───@───X─── ] [ 0: ───────@─── ]
151-
0: ───[ │ ]────────[ │ │ │ ]──────────────────────X['ignore']───────────[ │ ]────────X───
152-
[ 2: ───H───X─── ]['1'] [ 1: ───H───X───────@───Y───X─────── ]['2'] [ 1: ───Y───X─── ]['4'] ║
153-
154-
1: ───┼─────────────────────────#2────────────────────────────────────────────@['ignore']─────────────────────────#2────────────────────────╫───
155-
156-
2: ───#2──────────────────────────────────────────────────────────────────────X─────────────[ 2: ───Z─── ]['3']───M─────────────────────────╫───
157-
158-
a: ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════@═════════════════════════^═══''',
150+
[ 0: ───H───@─── ] [ 0: ───────@───H───@───X───@───X─── ] [ 0: ───────@─── ]
151+
0: ───[ │ ]──────[ │ │ │ ]──────────────────X[ignore]───────────[ │ ]──────X───
152+
[ 2: ───H───X─── ][1] [ 1: ───H───X───────@───Y───X─────── ][2] [ 1: ───Y───X─── ][4] ║
153+
│ │
154+
1: ───┼───────────────────────#2──────────────────────────────────────────@[ignore]───────────────────────#2──────────────────────╫───
155+
156+
2: ───#2──────────────────────────────────────────────────────────────────X───────────[ 2: ───Z─── ][3]───M───────────────────────╫───
157+
158+
a: ═══════════════════════════════════════════════════════════════════════════════════════════════════════@═══════════════════════^═══''',
159159
)
160160

161161
component_id = 0
@@ -179,13 +179,13 @@ def rewriter_replace_with_decomp(op: 'cirq.CircuitOperation') -> 'cirq.OP_TREE':
179179
cirq.testing.assert_has_diagram(
180180
cirq.drop_empty_moments(c_new),
181181
'''
182-
0: ───T['1']───iSwap['1']───T['1']───T['2']───iSwap['2']───T['2']─────────────────X['ignore']───T['4']───iSwap['4']───T['4']───X───
183-
184-
1: ────────────┼─────────────────────T['2']───iSwap^0.5────T['2']───@['ignore']─────────────────T['4']───iSwap^0.5────T['4']───╫───
185-
186-
2: ───T['1']───iSwap^0.5────T['1']──────────────────────────────────X─────────────T['3']────────M──────────────────────────────╫───
187-
188-
a: ═════════════════════════════════════════════════════════════════════════════════════════════@══════════════════════════════^═══''',
182+
0: ───T[1]───iSwap[1]────T[1]───T[2]───iSwap[2]────T[2]───────────────X[ignore]───T[4]───iSwap[4]────T[4]───X───
183+
│ │
184+
1: ──────────┼──────────────────T[2]───iSwap^0.5───T[2]───@[ignore]───────────────T[4]───iSwap^0.5───T[4]───╫───
185+
186+
2: ───T[1]───iSwap^0.5───T[1]─────────────────────────────X───────────T[3]────────M─────────────────────────╫───
187+
188+
a: ═══════════════════════════════════════════════════════════════════════════════@═════════════════════════^═══''',
189189
)
190190

191191

cirq-core/cirq/transformers/merge_single_qubit_gates_test.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,27 @@ def test_merge_single_qubit_moments_to_phxz():
153153
cirq.testing.assert_has_diagram(
154154
c_orig,
155155
'''
156-
0: ───X───────Y───@───X───────Y───Y['nocompile']───X───M───────────
157-
158-
1: ───X───T───Y───@───X───T───Y───Z────────────────────╫───X───X───
159-
║ ║
160-
2: ───────T───────Y───────T───────Z────────────────────╫───╫───────
161-
║ ║
162-
a: ════════════════════════════════════════════════════@═══^═══════
156+
0: ───X───────Y───@───X───────Y───Y[nocompile]───X───M───────────
157+
│ ║
158+
1: ───X───T───Y───@───X───T───Y───Z──────────────────╫───X───X───
159+
║ ║
160+
2: ───────T───────Y───────T───────Z──────────────────╫───╫───────
161+
║ ║
162+
a: ══════════════════════════════════════════════════@═══^═══════
163163
''',
164164
)
165165
context = cirq.TransformerContext(tags_to_ignore=("nocompile",))
166166
c_new = cirq.merge_single_qubit_moments_to_phxz(c_orig, context=context)
167167
cirq.testing.assert_has_diagram(
168168
c_new,
169169
'''
170-
0: ───PhXZ(a=-0.5,x=0,z=-1)──────@───PhXZ(a=-0.5,x=0,z=-1)──────Y['nocompile']───X───M───────────
171-
172-
1: ───PhXZ(a=-0.25,x=0,z=0.75)───@───PhXZ(a=-0.25,x=0,z=0.75)───Z────────────────────╫───X───X───
173-
║ ║
174-
2: ───PhXZ(a=0.25,x=0,z=0.25)────Y───PhXZ(a=0.25,x=0,z=0.25)────Z────────────────────╫───╫───────
175-
║ ║
176-
a: ══════════════════════════════════════════════════════════════════════════════════@═══^═══════
170+
0: ───PhXZ(a=-0.5,x=0,z=-1)──────@───PhXZ(a=-0.5,x=0,z=-1)──────Y[nocompile]───X───M───────────
171+
│ ║
172+
1: ───PhXZ(a=-0.25,x=0,z=0.75)───@───PhXZ(a=-0.25,x=0,z=0.75)───Z──────────────────╫───X───X───
173+
║ ║
174+
2: ───PhXZ(a=0.25,x=0,z=0.25)────Y───PhXZ(a=0.25,x=0,z=0.25)────Z──────────────────╫───╫───────
175+
║ ║
176+
a: ════════════════════════════════════════════════════════════════════════════════@═══^═══════
177177
''',
178178
)
179179

0 commit comments

Comments
 (0)