Skip to content

Commit d4e1692

Browse files
authored
Exponent should be drawn on target qubit in CX and CCX gates (quantumlib#4462)
`CX` and `CCX` gates currently print the exponent on the bottom most qubit in the circuit diagram. This PR modifies this behavior to always draw the exponent on the target qubit, which is also the expected behavior for other `ControlledOperations`. This came up in discussions on quantumlib#4167 (comment) Note that behavior of `CZ` and `CCZ` is left unchanged, i.e. draws exponent on the bottom most qubit, since these gates are symmetrical across all applied qubits. BREAKING CHANGE: Tests depending upon diagrams of `CX` and `CCX` can now fail.
1 parent d7092dd commit d4e1692

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

cirq/ops/common_gates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ def _circuit_diagram_info_(
12751275
self, args: 'cirq.CircuitDiagramInfoArgs'
12761276
) -> 'cirq.CircuitDiagramInfo':
12771277
return protocols.CircuitDiagramInfo(
1278-
wire_symbols=('@', 'X'), exponent=self._diagram_exponent(args)
1278+
wire_symbols=('@', 'X'), exponent=self._diagram_exponent(args), exponent_qubit_index=1
12791279
)
12801280

12811281
def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs') -> Optional[np.ndarray]:

cirq/ops/common_gates_test.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ def test_text_diagrams():
720720
cirq.CZ(a, b),
721721
cirq.CNOT(a, b),
722722
cirq.CNOT(b, a),
723+
cirq.CNOT(a, b) ** 0.5,
724+
cirq.CNOT(b, a) ** 0.5,
723725
cirq.H(a) ** 0.5,
724726
cirq.I(a),
725727
cirq.IdentityGate(2)(a, b),
@@ -729,18 +731,18 @@ def test_text_diagrams():
729731
cirq.testing.assert_has_diagram(
730732
circuit,
731733
"""
732-
a: ───X───Y───Z───Z^x───Rx(x)───@───@───X───H^0.5───I───I───@─────
733-
│ │ │ │ │
734-
b: ─────────────────────────────@───X───@───────────────I───@^t───
734+
a: ───X───Y───Z───Z^x───Rx(x)───@───@───X───@───────X^0.5───H^0.5───I───I───@─────
735+
│ │ │ │ │ │ │
736+
b: ─────────────────────────────@───X───@───X^0.5───@───────────────────I───@^t───
735737
""",
736738
)
737739

738740
cirq.testing.assert_has_diagram(
739741
circuit,
740742
"""
741-
a: ---X---Y---Z---Z^x---Rx(x)---@---@---X---H^0.5---I---I---@-----
742-
| | | | |
743-
b: -----------------------------@---X---@---------------I---@^t---
743+
a: ---X---Y---Z---Z^x---Rx(x)---@---@---X---@-------X^0.5---H^0.5---I---I---@-----
744+
| | | | | | |
745+
b: -----------------------------@---X---@---X^0.5---@-------------------I---@^t---
744746
""",
745747
use_unicode_characters=False,
746748
)

cirq/ops/three_qubit_gates.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ def _decompose_(self, qubits):
397397
def _circuit_diagram_info_(
398398
self, args: 'cirq.CircuitDiagramInfoArgs'
399399
) -> 'cirq.CircuitDiagramInfo':
400-
return protocols.CircuitDiagramInfo(('@', '@', 'X'), exponent=self._diagram_exponent(args))
400+
return protocols.CircuitDiagramInfo(
401+
('@', '@', 'X'), exponent=self._diagram_exponent(args), exponent_qubit_index=2
402+
)
401403

402404
def _qasm_(self, args: 'cirq.QasmArgs', qubits: Tuple['cirq.Qid', ...]) -> Optional[str]:
403405
if self._exponent != 1:

cirq/ops/three_qubit_gates_test.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def test_diagram():
234234
circuit = cirq.Circuit(
235235
cirq.TOFFOLI(a, b, c),
236236
cirq.TOFFOLI(a, b, c) ** 0.5,
237+
cirq.TOFFOLI(c, b, a) ** 0.5,
237238
cirq.CCX(a, c, b),
238239
cirq.CCZ(a, d, b),
239240
cirq.CCZ(a, d, b) ** 0.5,
@@ -243,25 +244,25 @@ def test_diagram():
243244
cirq.testing.assert_has_diagram(
244245
circuit,
245246
"""
246-
0: ───@───@───────@───@───@───────@───@───
247-
│ │ │ │ │ │ │
248-
1: ───@───@───────X───@───@───────┼───×───
249-
│ │ │ │ │ │ │
250-
2: ───X───X^0.5───@───┼───┼───────×───×───
251-
│ │ │
252-
3: ───────────────────@───@^0.5───×───────
247+
0: ───@───@───────X^0.5───@───@───@───────@───@───
248+
│ │ │ │ │ │ │
249+
1: ───@───@───────@───────X───@───@───────┼───×───
250+
│ │ │ │ │ │ │
251+
2: ───X───X^0.5───@───────@───┼───┼───────×───×───
252+
│ │ │
253+
3: ───────────────────────────@───@^0.5───×───────
253254
""",
254255
)
255256
cirq.testing.assert_has_diagram(
256257
circuit,
257258
"""
258-
0: ---@---@-------@---@---@-------@------@------
259-
| | | | | | |
260-
1: ---@---@-------X---@---@-------|------swap---
261-
| | | | | | |
262-
2: ---X---X^0.5---@---|---|-------swap---swap---
263-
| | |
264-
3: -------------------@---@^0.5---swap----------
259+
0: ---@---@-------X^0.5---@---@---@-------@------@------
260+
| | | | | | | |
261+
1: ---@---@-------@-------X---@---@-------|------swap---
262+
| | | | | | | |
263+
2: ---X---X^0.5---@-------@---|---|-------swap---swap---
264+
| | |
265+
3: ---------------------------@---@^0.5---swap----------
265266
""",
266267
use_unicode_characters=False,
267268
)

0 commit comments

Comments
 (0)