Skip to content

Commit 27b5d4b

Browse files
authored
Fix classically controlled op Moment diagram (#5777)
Fixes #5776 by accounting for the fact that `args.label_map` is null when coming from a Moment renderer, and handling appropriately.
1 parent adff66e commit 27b5d4b

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

cirq-core/cirq/ops/classically_controlled_operation.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ def _circuit_diagram_info_(
149149
sub_info = protocols.circuit_diagram_info(self._sub_operation, sub_args, None)
150150
if sub_info is None:
151151
return NotImplemented # coverage: ignore
152-
control_count = len({k for c in self._conditions for k in c.keys})
153-
wire_symbols = sub_info.wire_symbols + ('^',) * control_count
154-
if any(not isinstance(c, value.KeyCondition) for c in self._conditions):
152+
control_label_count = 0
153+
if args.label_map is not None:
154+
control_label_count = len({k for c in self._conditions for k in c.keys})
155+
wire_symbols = sub_info.wire_symbols + ('^',) * control_label_count
156+
if control_label_count == 0 or any(
157+
not isinstance(c, value.KeyCondition) for c in self._conditions
158+
):
155159
wire_symbols = (
156160
wire_symbols[0]
157161
+ '(conditions=['
@@ -160,9 +164,9 @@ def _circuit_diagram_info_(
160164
) + wire_symbols[1:]
161165
exponent_qubit_index = None
162166
if sub_info.exponent_qubit_index is not None:
163-
exponent_qubit_index = sub_info.exponent_qubit_index + control_count
167+
exponent_qubit_index = sub_info.exponent_qubit_index + control_label_count
164168
elif sub_info.exponent is not None:
165-
exponent_qubit_index = control_count
169+
exponent_qubit_index = control_label_count
166170
return protocols.CircuitDiagramInfo(
167171
wire_symbols=wire_symbols,
168172
exponent=sub_info.exponent,

cirq-core/cirq/ops/classically_controlled_operation_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -1017,3 +1017,19 @@ def test_commutes():
10171017
assert not cirq.commutes(
10181018
cirq.X(q0).with_classical_controls('a'), cirq.H(q0).with_classical_controls('a')
10191019
)
1020+
1021+
1022+
def test_moment_diagram():
1023+
a, _, c, d = cirq.GridQubit.rect(2, 2)
1024+
m = cirq.Moment(cirq.CZ(a, d), cirq.X(c).with_classical_controls('m'))
1025+
assert (
1026+
str(m).strip()
1027+
== """
1028+
╷ 0 1
1029+
╶─┼─────────────────────
1030+
0 │ @─────────────────┐
1031+
│ │
1032+
1 │ X(conditions=[m]) @
1033+
1034+
""".strip()
1035+
)

0 commit comments

Comments
 (0)