Skip to content

Commit

Permalink
Exponent should be drawn on target qubit in CX and CCX gates (#4462)
Browse files Browse the repository at this point in the history
`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 #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.
  • Loading branch information
tanujkhattar committed Sep 21, 2021
1 parent 9f371b0 commit 9f6c359
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/common_gates.py
Expand Up @@ -1275,7 +1275,7 @@ def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> 'cirq.CircuitDiagramInfo':
return protocols.CircuitDiagramInfo(
wire_symbols=('@', 'X'), exponent=self._diagram_exponent(args)
wire_symbols=('@', 'X'), exponent=self._diagram_exponent(args), exponent_qubit_index=1
)

def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs') -> Optional[np.ndarray]:
Expand Down
14 changes: 8 additions & 6 deletions cirq-core/cirq/ops/common_gates_test.py
Expand Up @@ -720,6 +720,8 @@ def test_text_diagrams():
cirq.CZ(a, b),
cirq.CNOT(a, b),
cirq.CNOT(b, a),
cirq.CNOT(a, b) ** 0.5,
cirq.CNOT(b, a) ** 0.5,
cirq.H(a) ** 0.5,
cirq.I(a),
cirq.IdentityGate(2)(a, b),
Expand All @@ -729,18 +731,18 @@ def test_text_diagrams():
cirq.testing.assert_has_diagram(
circuit,
"""
a: ───X───Y───Z───Z^x───Rx(x)───@───@───X───H^0.5───I───I───@─────
│ │ │ │ │
b: ─────────────────────────────@───X───@───────────────I───@^t───
a: ───X───Y───Z───Z^x───Rx(x)───@───@───X───@───────X^0.5───H^0.5───I───I───@─────
│ │ │ │ │ │ │
b: ─────────────────────────────@───X───@───X^0.5───@───────────────────I───@^t───
""",
)

cirq.testing.assert_has_diagram(
circuit,
"""
a: ---X---Y---Z---Z^x---Rx(x)---@---@---X---H^0.5---I---I---@-----
| | | | |
b: -----------------------------@---X---@---------------I---@^t---
a: ---X---Y---Z---Z^x---Rx(x)---@---@---X---@-------X^0.5---H^0.5---I---I---@-----
| | | | | | |
b: -----------------------------@---X---@---X^0.5---@-------------------I---@^t---
""",
use_unicode_characters=False,
)
Expand Down
4 changes: 3 additions & 1 deletion cirq-core/cirq/ops/three_qubit_gates.py
Expand Up @@ -397,7 +397,9 @@ def _decompose_(self, qubits):
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> 'cirq.CircuitDiagramInfo':
return protocols.CircuitDiagramInfo(('@', '@', 'X'), exponent=self._diagram_exponent(args))
return protocols.CircuitDiagramInfo(
('@', '@', 'X'), exponent=self._diagram_exponent(args), exponent_qubit_index=2
)

def _qasm_(self, args: 'cirq.QasmArgs', qubits: Tuple['cirq.Qid', ...]) -> Optional[str]:
if self._exponent != 1:
Expand Down
29 changes: 15 additions & 14 deletions cirq-core/cirq/ops/three_qubit_gates_test.py
Expand Up @@ -234,6 +234,7 @@ def test_diagram():
circuit = cirq.Circuit(
cirq.TOFFOLI(a, b, c),
cirq.TOFFOLI(a, b, c) ** 0.5,
cirq.TOFFOLI(c, b, a) ** 0.5,
cirq.CCX(a, c, b),
cirq.CCZ(a, d, b),
cirq.CCZ(a, d, b) ** 0.5,
Expand All @@ -243,25 +244,25 @@ def test_diagram():
cirq.testing.assert_has_diagram(
circuit,
"""
0: ───@───@───────@───@───@───────@───@───
│ │ │ │ │ │ │
1: ───@───@───────X───@───@───────┼───×───
│ │ │ │ │ │ │
2: ───X───X^0.5───@───┼───┼───────×───×───
│ │ │
3: ───────────────────@───@^0.5───×───────
0: ───@───@───────X^0.5───@───@───@───────@───@───
│ │ │ │ │ │ │
1: ───@───@───────@───────X───@───@───────┼───×───
│ │ │ │ │ │ │
2: ───X───X^0.5───@───────@───┼───┼───────×───×───
│ │ │
3: ───────────────────────────@───@^0.5───×───────
""",
)
cirq.testing.assert_has_diagram(
circuit,
"""
0: ---@---@-------@---@---@-------@------@------
| | | | | | |
1: ---@---@-------X---@---@-------|------swap---
| | | | | | |
2: ---X---X^0.5---@---|---|-------swap---swap---
| | |
3: -------------------@---@^0.5---swap----------
0: ---@---@-------X^0.5---@---@---@-------@------@------
| | | | | | | |
1: ---@---@-------@-------X---@---@-------|------swap---
| | | | | | | |
2: ---X---X^0.5---@-------@---|---|-------swap---swap---
| | |
3: ---------------------------@---@^0.5---swap----------
""",
use_unicode_characters=False,
)
Expand Down

0 comments on commit 9f6c359

Please sign in to comment.