Skip to content

Commit e0bf221

Browse files
committed
Include CY and CCY in existing tests
1 parent 8d85bea commit e0bf221

2 files changed

Lines changed: 89 additions & 23 deletions

File tree

cirq-core/cirq/ops/common_gates_test.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,10 @@ def test_cy_unitary() -> None:
233233
[
234234
(cirq.Z, cirq.CCZ),
235235
(cirq.X, cirq.CCX),
236+
(cirq.Y, cirq.CCY),
236237
(cirq.ZPowGate(exponent=0.5), cirq.CCZPowGate(exponent=0.5)),
237238
(cirq.XPowGate(exponent=0.5), cirq.CCXPowGate(exponent=0.5)),
239+
(cirq.YPowGate(exponent=0.5), cirq.CCYPowGate(exponent=0.5)),
238240
],
239241
)
240242
def test_specialized_control_two_step(input_gate, specialized_output) -> None:
@@ -257,6 +259,8 @@ def test_specialized_control_two_step(input_gate, specialized_output) -> None:
257259
(cirq.CZPowGate(global_shift=-0.5, exponent=0.5), cirq.CCZPowGate),
258260
(cirq.XPowGate(global_shift=-0.5, exponent=0.5), cirq.CXPowGate),
259261
(cirq.CXPowGate(global_shift=-0.5, exponent=0.5), cirq.CCXPowGate),
262+
(cirq.YPowGate(global_shift=-0.5, exponent=0.5), cirq.CYPowGate),
263+
(cirq.CYPowGate(global_shift=-0.5, exponent=0.5), cirq.CCYPowGate),
260264
],
261265
)
262266
def test_no_specialized_control_for_global_shift_non_zero(gate, specialized_type) -> None:
@@ -283,6 +287,21 @@ def test_no_specialized_control_for_global_shift_non_zero(gate, specialized_type
283287
]
284288
),
285289
),
290+
(
291+
cirq.YPowGate(global_shift=-0.5, exponent=1),
292+
np.block(
293+
[[np.eye(2), np.zeros((2, 2))], [np.zeros((2, 2)), np.array([[0, -1], [1, 0]])]]
294+
),
295+
),
296+
(
297+
cirq.CYPowGate(global_shift=-0.5, exponent=1),
298+
np.block(
299+
[
300+
[np.diag([1, 1, 1, 1, -1j, -1j]), np.zeros((6, 2))],
301+
[np.zeros((2, 6)), np.array([[0, -1], [1, 0]])],
302+
]
303+
),
304+
),
286305
],
287306
)
288307
def test_global_phase_controlled_gate(gate, matrix) -> None:
@@ -297,6 +316,7 @@ def test_rot_gates_eq() -> None:
297316
lambda p: cirq.Y**p,
298317
lambda p: cirq.Z**p,
299318
lambda p: cirq.CNOT**p,
319+
lambda p: cirq.CY**p,
300320
]
301321
for gate in gates:
302322
eq.add_equality_group(gate(3.5), gate(-0.5))
@@ -317,6 +337,7 @@ def test_rot_gates_eq() -> None:
317337
cirq.CNotPowGate(), cirq.CXPowGate(), cirq.CNotPowGate(exponent=1), cirq.CNOT
318338
)
319339
eq.add_equality_group(cirq.CZPowGate(), cirq.CZPowGate(exponent=1), cirq.CZ)
340+
eq.add_equality_group(cirq.CYPowGate(), cirq.CYPowGate(exponent=1), cirq.CY)
320341

321342

322343
def test_z_unitary() -> None:
@@ -762,13 +783,20 @@ def test_interchangeable_qubit_eq() -> None:
762783
eq.add_equality_group(cirq.CNOT(b, a))
763784
eq.add_equality_group(cirq.CNOT(a, c))
764785

786+
eq.add_equality_group(cirq.CY(a, b))
787+
eq.add_equality_group(cirq.CY(b, a))
788+
eq.add_equality_group(cirq.CY(a, c))
789+
765790

766791
def test_identity_multiplication() -> None:
767792
a, b, c = cirq.LineQubit.range(3)
768793
assert cirq.I(a) * cirq.CX(a, b) == cirq.CX(a, b)
769794
assert cirq.CX(a, b) * cirq.I(a) == cirq.CX(a, b)
770795
assert cirq.CZ(a, b) * cirq.I(c) == cirq.CZ(a, b)
771796
assert cirq.CX(a, b) ** 0.5 * cirq.I(c) == cirq.CX(a, b) ** 0.5
797+
assert cirq.I(a) * cirq.CY(a, b) == cirq.CY(a, b)
798+
assert cirq.CY(a, b) * cirq.I(a) == cirq.CY(a, b)
799+
assert cirq.CY(a, b) ** 0.5 * cirq.I(c) == cirq.CY(a, b) ** 0.5
772800
assert cirq.I(c) * cirq.CZ(b, c) ** 0.5 == cirq.CZ(b, c) ** 0.5
773801
assert cirq.T(a) * cirq.I(a) == cirq.T(a)
774802
assert cirq.T(b) * cirq.I(c) == cirq.T(b)
@@ -790,6 +818,10 @@ def test_text_diagrams() -> None:
790818
cirq.CNOT(b, a),
791819
cirq.CNOT(a, b) ** 0.5,
792820
cirq.CNOT(b, a) ** 0.5,
821+
cirq.CY(a, b),
822+
cirq.CY(b, a),
823+
cirq.CY(a, b) ** 0.5,
824+
cirq.CY(b, a) ** 0.5,
793825
cirq.H(a) ** 0.5,
794826
cirq.I(a),
795827
cirq.IdentityGate(2)(a, b),
@@ -799,18 +831,18 @@ def test_text_diagrams() -> None:
799831
cirq.testing.assert_has_diagram(
800832
circuit,
801833
"""
802-
a: ───X───Y───Z───Z^x───Rx(x)───@───@───X───@───────X^0.5───H^0.5───I───I───@─────
803-
│ │ │ │ │ │ │
804-
b: ─────────────────────────────@───X───@───X^0.5───@───────────────────I───@^t───
834+
a: ───X───Y───Z───Z^x───Rx(x)───@───@───X───@───────X^0.5───@───Y───@───────Y^0.5───H^0.5───I───I───@─────
835+
│ │ │ │ │ │ │ │ │ │ │
836+
b: ─────────────────────────────@───X───@───X^0.5───@───────Y───@───Y^0.5───@───────────────────I───@^t───
805837
""",
806838
)
807839

808840
cirq.testing.assert_has_diagram(
809841
circuit,
810842
"""
811-
a: ---X---Y---Z---Z^x---Rx(x)---@---@---X---@-------X^0.5---H^0.5---I---I---@-----
812-
| | | | | | |
813-
b: -----------------------------@---X---@---X^0.5---@-------------------I---@^t---
843+
a: ---X---Y---Z---Z^x---Rx(x)---@---@---X---@-------X^0.5---@---Y---@-------Y^0.5---H^0.5---I---I---@-----
844+
| | | | | | | | | | |
845+
b: -----------------------------@---X---@---X^0.5---@-------Y---@---Y^0.5---@-------------------I---@^t---
814846
""",
815847
use_unicode_characters=False,
816848
)
@@ -836,6 +868,12 @@ def test_cnot_decompose() -> None:
836868
assert cirq.decompose_once(cirq.CNOT(a, b) ** sympy.Symbol('x')) is not None
837869

838870

871+
def test_cy_decompose() -> None:
872+
a = cirq.NamedQubit('a')
873+
b = cirq.NamedQubit('b')
874+
assert cirq.decompose_once(cirq.CY(a, b) ** sympy.Symbol('x')) is not None
875+
876+
839877
def test_repr() -> None:
840878
assert repr(cirq.X) == 'cirq.X'
841879
assert repr(cirq.X**0.5) == '(cirq.X**0.5)'
@@ -996,7 +1034,7 @@ def test_h_stabilizer() -> None:
9961034
assert not cirq.has_stabilizer_effect(gate**foo)
9971035

9981036

999-
@pytest.mark.parametrize('gate', [cirq.CX, cirq.CZ])
1037+
@pytest.mark.parametrize('gate', [cirq.CX, cirq.CZ, cirq.CY])
10001038
def test_cx_cz_stabilizer(gate) -> None:
10011039
assert cirq.has_stabilizer_effect(gate)
10021040
assert not cirq.has_stabilizer_effect(gate**0.5)
@@ -1133,12 +1171,14 @@ def test_trace_distance() -> None:
11331171
sh = cirq.H**foo
11341172
scx = cirq.CX**foo
11351173
scz = cirq.CZ**foo
1174+
scy = cirq.CY**foo
11361175
# These values should have 1.0 or 0.0 directly returned
11371176
assert cirq.trace_distance_bound(sx) == 1.0
11381177
assert cirq.trace_distance_bound(sy) == 1.0
11391178
assert cirq.trace_distance_bound(sz) == 1.0
11401179
assert cirq.trace_distance_bound(scx) == 1.0
11411180
assert cirq.trace_distance_bound(scz) == 1.0
1181+
assert cirq.trace_distance_bound(scy) == 1.0
11421182
assert cirq.trace_distance_bound(sh) == 1.0
11431183
assert cirq.trace_distance_bound(cirq.I) == 0.0
11441184
# These values are calculated, so we use approx_eq
@@ -1147,7 +1187,9 @@ def test_trace_distance() -> None:
11471187
assert cirq.approx_eq(cirq.trace_distance_bound(cirq.Z**0.5), np.sin(np.pi / 4))
11481188
assert cirq.approx_eq(cirq.trace_distance_bound(cirq.H**0.25), np.sin(np.pi / 8))
11491189
assert cirq.approx_eq(cirq.trace_distance_bound(cirq.CX**2), 0.0)
1190+
assert cirq.approx_eq(cirq.trace_distance_bound(cirq.CY**2), 0.0)
11501191
assert cirq.approx_eq(cirq.trace_distance_bound(cirq.CZ ** (1 / 9)), np.sin(np.pi / 18))
1192+
assert cirq.approx_eq(cirq.trace_distance_bound(cirq.CY ** (1 / 9)), np.sin(np.pi / 18))
11511193

11521194

11531195
def test_commutes() -> None:

cirq-core/cirq/ops/three_qubit_gates_test.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ def test_str() -> None:
170170
assert str(cirq.CSWAP) == 'FREDKIN'
171171
assert str(cirq.FREDKIN) == 'FREDKIN'
172172
assert str(cirq.CCZ) == 'CCZ'
173+
assert str(cirq.CCY) == 'CCY'
173174

174175
assert str(cirq.CCX**0.5) == 'TOFFOLI**0.5'
175176
assert str(cirq.CCZ**0.5) == 'CCZ**0.5'
177+
assert str(cirq.CCY**0.5) == 'CCY**0.5'
176178

177179

178180
def test_repr() -> None:
@@ -181,9 +183,11 @@ def test_repr() -> None:
181183
assert repr(cirq.CSWAP) == 'cirq.FREDKIN'
182184
assert repr(cirq.FREDKIN) == 'cirq.FREDKIN'
183185
assert repr(cirq.CCZ) == 'cirq.CCZ'
186+
assert repr(cirq.CCY) == 'cirq.CCY'
184187

185188
assert repr(cirq.CCX**0.5) == '(cirq.TOFFOLI**0.5)'
186189
assert repr(cirq.CCZ**0.5) == '(cirq.CCZ**0.5)'
190+
assert repr(cirq.CCY**0.5) == '(cirq.CCY**0.5)'
187191

188192

189193
def test_eq() -> None:
@@ -200,6 +204,12 @@ def test_eq() -> None:
200204
eq.add_equality_group(cirq.TOFFOLI(a, b, c), cirq.CCX(a, b, c))
201205
eq.add_equality_group(cirq.TOFFOLI(a, c, b), cirq.TOFFOLI(c, a, b))
202206
eq.add_equality_group(cirq.TOFFOLI(a, b, d))
207+
eq.add_equality_group(cirq.CCY(a, b, c), cirq.CCY(b, a, c))
208+
eq.add_equality_group(
209+
cirq.CCY(a, b, c) ** 0.5, cirq.CCY(b, a, c) ** 2.5, cirq.CCY(a, b, c) ** -1.5
210+
)
211+
eq.add_equality_group(cirq.CCY(a, c, b), cirq.CCY(c, a, b))
212+
eq.add_equality_group(cirq.CCY(a, b, d))
203213
eq.add_equality_group(cirq.CSWAP(a, b, c), cirq.FREDKIN(a, b, c), cirq.FREDKIN(a, b, c) ** -1)
204214
eq.add_equality_group(cirq.CSWAP(b, a, c), cirq.CSWAP(b, c, a))
205215

@@ -210,6 +220,7 @@ def test_gate_equality() -> None:
210220
eq.add_equality_group(cirq.CZPowGate(), cirq.CZPowGate())
211221
eq.add_equality_group(cirq.CCXPowGate(), cirq.CCXPowGate(), cirq.CCNotPowGate())
212222
eq.add_equality_group(cirq.CCZPowGate(), cirq.CCZPowGate())
223+
eq.add_equality_group(cirq.CCYPowGate(), cirq.CCYPowGate())
213224

214225

215226
def test_identity_multiplication() -> None:
@@ -218,13 +229,18 @@ def test_identity_multiplication() -> None:
218229
assert cirq.CCX(a, b, c) * cirq.I(b) == cirq.CCX(a, b, c)
219230
assert cirq.CCX(a, b, c) ** 0.5 * cirq.I(c) == cirq.CCX(a, b, c) ** 0.5
220231
assert cirq.I(c) * cirq.CCZ(a, b, c) ** 0.5 == cirq.CCZ(a, b, c) ** 0.5
232+
assert cirq.CCY(a, b, c) * cirq.I(a) == cirq.CCY(a, b, c)
233+
assert cirq.CCY(a, b, c) * cirq.I(b) == cirq.CCY(a, b, c)
234+
assert cirq.CCY(a, b, c) ** 0.5 * cirq.I(c) == cirq.CCY(a, b, c) ** 0.5
235+
assert cirq.I(c) * cirq.CCY(a, b, c) ** 0.5 == cirq.CCY(a, b, c) ** 0.5
221236

222237

223238
@pytest.mark.parametrize(
224239
'op,max_two_cost',
225240
[
226241
(cirq.CCZ(*cirq.LineQubit.range(3)), 8),
227242
(cirq.CCX(*cirq.LineQubit.range(3)), 8),
243+
(cirq.CCY(*cirq.LineQubit.range(3)), 8),
228244
(cirq.CCZ(cirq.LineQubit(0), cirq.LineQubit(2), cirq.LineQubit(1)), 8),
229245
(cirq.CCZ(cirq.LineQubit(0), cirq.LineQubit(2), cirq.LineQubit(1)) ** sympy.Symbol("s"), 8),
230246
(cirq.CSWAP(*cirq.LineQubit.range(3)), 9),
@@ -259,7 +275,13 @@ def test_diagonal_gate_property() -> None:
259275

260276
@pytest.mark.parametrize(
261277
'gate',
262-
[cirq.CCX, cirq.CSWAP, cirq.CCZ, cirq.ThreeQubitDiagonalGate([2, 3, 5, 7, 11, 13, 17, 19])],
278+
[
279+
cirq.CCX,
280+
cirq.CCY,
281+
cirq.CSWAP,
282+
cirq.CCZ,
283+
cirq.ThreeQubitDiagonalGate([2, 3, 5, 7, 11, 13, 17, 19]),
284+
],
263285
)
264286
def test_decomposition_respects_locality(gate) -> None:
265287
a = cirq.GridQubit(0, 0)
@@ -279,6 +301,8 @@ def test_diagram() -> None:
279301
cirq.TOFFOLI(a, b, c) ** 0.5,
280302
cirq.TOFFOLI(c, b, a) ** 0.5,
281303
cirq.CCX(a, c, b),
304+
cirq.CCY(a, b, c),
305+
cirq.CCY(a, b, c) ** 0.5,
282306
cirq.CCZ(a, d, b),
283307
cirq.CCZ(a, d, b) ** 0.5,
284308
cirq.CSWAP(a, c, d),
@@ -287,25 +311,25 @@ def test_diagram() -> None:
287311
cirq.testing.assert_has_diagram(
288312
circuit,
289313
"""
290-
0: ───@───@───────X^0.5───@───@───@───────@───@───
291-
│ │ │ │ │ │ │ │
292-
1: ───@───@───────@───────X───@───@───────┼───×───
293-
│ │ │ │ │ │ │ │
294-
2: ───X───X^0.5───@───────@───┼───┼───────×───×───
295-
│ │ │
296-
3: ───────────────────────────@───@^0.5───×───────
314+
0: ───@───@───────X^0.5───@───@───@───────@───@───────@───@───
315+
│ │ │ │ │ │ │ │ │ │
316+
1: ───@───@───────@───────X───@───@───────@───@───────┼───×───
317+
│ │ │ │ │ │ │ │ │ │
318+
2: ───X───X^0.5───@───────@───Y───Y^0.5───┼───┼───────×───×───
319+
│ │ │
320+
3: ───────────────────────────────────────@───@^0.5───×───────
297321
""",
298322
)
299323
cirq.testing.assert_has_diagram(
300324
circuit,
301325
"""
302-
0: ---@---@-------X^0.5---@---@---@-------@------@------
303-
| | | | | | | |
304-
1: ---@---@-------@-------X---@---@-------|------swap---
305-
| | | | | | | |
306-
2: ---X---X^0.5---@-------@---|---|-------swap---swap---
307-
| | |
308-
3: ---------------------------@---@^0.5---swap----------
326+
0: ---@---@-------X^0.5---@---@---@-------@---@-------@------@------
327+
| | | | | | | | | |
328+
1: ---@---@-------@-------X---@---@-------@---@-------|------swap---
329+
| | | | | | | | | |
330+
2: ---X---X^0.5---@-------@---Y---Y^0.5---|---|-------swap---swap---
331+
| | |
332+
3: ---------------------------------------@---@^0.5---swap----------
309333
""",
310334
use_unicode_characters=False,
311335
)
@@ -365,7 +389,7 @@ def test_resolve(resolve_fn) -> None:
365389
assert not cirq.is_parameterized(diagonal_gate)
366390

367391

368-
@pytest.mark.parametrize('gate', [cirq.CCX, cirq.CCZ, cirq.CSWAP])
392+
@pytest.mark.parametrize('gate', [cirq.CCX, cirq.CCY, cirq.CCZ, cirq.CSWAP])
369393
def test_controlled_ops_consistency(gate) -> None:
370394
a, b, c, d = cirq.LineQubit.range(4)
371395
assert gate.controlled(0) is gate

0 commit comments

Comments
 (0)