|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -import numpy as np |
16 | 15 | import pytest
|
17 | 16 |
|
18 | 17 | import cirq
|
19 | 18 |
|
20 | 19 |
|
21 |
| -Q = cirq.LineQubit.range(3) |
22 |
| - |
23 |
| - |
24 |
| -def test_coverage(): |
25 |
| - with cirq.testing.assert_deprecated( |
26 |
| - "Use cirq.optimize_for_target_gateset", deadline='v0.16', count=3 |
27 |
| - ): |
28 |
| - q = cirq.LineQubit.range(3) |
29 |
| - g = cirq.testing.ThreeQubitGate() |
30 |
| - |
31 |
| - class FakeOperation(cirq.Operation): |
32 |
| - def __init__(self, gate, qubits): |
33 |
| - self._gate = gate |
34 |
| - self._qubits = qubits |
35 |
| - |
36 |
| - @property |
37 |
| - def qubits(self): |
38 |
| - return self._qubits |
39 |
| - |
40 |
| - def with_qubits(self, *new_qubits): |
41 |
| - return FakeOperation(self._gate, new_qubits) |
42 |
| - |
43 |
| - op = FakeOperation(g, q).with_qubits(*q) |
44 |
| - circuit_ops = [cirq.Y(q[0]), cirq.ParallelGate(cirq.X, 3).on(*q)] |
45 |
| - c = cirq.Circuit(circuit_ops) |
46 |
| - cirq.neutral_atoms.ConvertToNeutralAtomGates().optimize_circuit(c) |
47 |
| - assert c == cirq.Circuit(circuit_ops) |
48 |
| - assert cirq.neutral_atoms.ConvertToNeutralAtomGates().convert(cirq.X.on(q[0])) == [ |
49 |
| - cirq.X.on(q[0]) |
50 |
| - ] |
51 |
| - with pytest.raises(TypeError, match="Don't know how to work with"): |
52 |
| - cirq.neutral_atoms.ConvertToNeutralAtomGates().convert(op) |
53 |
| - assert not cirq.neutral_atoms.is_native_neutral_atom_op(op) |
54 |
| - assert not cirq.neutral_atoms.is_native_neutral_atom_gate(g) |
55 |
| - |
56 |
| - |
57 |
| -def test_avoids_decompose_fallback_when_matrix_available_single_qubit(): |
58 |
| - class OtherX(cirq.testing.SingleQubitGate): |
59 |
| - def _unitary_(self) -> np.ndarray: |
60 |
| - return np.array([[0, 1], [1, 0]]) |
61 |
| - |
62 |
| - class OtherOtherX(cirq.testing.SingleQubitGate): |
63 |
| - def _decompose_(self, qubits): |
64 |
| - return OtherX().on(*qubits) |
65 |
| - |
66 |
| - q = cirq.GridQubit(0, 0) |
67 |
| - c = cirq.Circuit(OtherX().on(q), OtherOtherX().on(q)) |
68 |
| - with cirq.testing.assert_deprecated("Use cirq.optimize_for_target_gateset", deadline='v0.16'): |
69 |
| - cirq.neutral_atoms.ConvertToNeutralAtomGates().optimize_circuit(c) |
70 |
| - cirq.testing.assert_has_diagram(c, '(0, 0): ───PhX(1)───PhX(1)───') |
71 |
| - |
72 |
| - |
73 |
| -def test_avoids_decompose_fallback_when_matrix_available_two_qubit(): |
74 |
| - class OtherCZ(cirq.testing.TwoQubitGate): |
75 |
| - def _unitary_(self) -> np.ndarray: |
76 |
| - return np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]]) |
77 |
| - |
78 |
| - class OtherOtherCZ(cirq.testing.TwoQubitGate): |
79 |
| - def _decompose_(self, qubits): |
80 |
| - return OtherCZ().on(*qubits) |
81 |
| - |
82 |
| - q00 = cirq.GridQubit(0, 0) |
83 |
| - q01 = cirq.GridQubit(0, 1) |
84 |
| - c = cirq.Circuit(OtherCZ().on(q00, q01), OtherOtherCZ().on(q00, q01)) |
85 |
| - expected_diagram = """ |
86 |
| -(0, 0): ───@───@─── |
87 |
| - │ │ |
88 |
| -(0, 1): ───@───@─── |
89 |
| -""" |
90 |
| - with cirq.testing.assert_deprecated("Use cirq.optimize_for_target_gateset", deadline='v0.16'): |
91 |
| - cirq.neutral_atoms.ConvertToNeutralAtomGates().optimize_circuit(c) |
92 |
| - cirq.testing.assert_has_diagram(c, expected_diagram) |
| 20 | +Q, Q2, Q3 = cirq.LineQubit.range(3) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "op,expected", |
| 25 | + [ |
| 26 | + (cirq.H(Q), False), |
| 27 | + (cirq.HPowGate(exponent=0.5)(Q), False), |
| 28 | + (cirq.PhasedXPowGate(exponent=0.25, phase_exponent=0.125)(Q), True), |
| 29 | + (cirq.XPowGate(exponent=0.5)(Q), True), |
| 30 | + (cirq.YPowGate(exponent=0.25)(Q), True), |
| 31 | + (cirq.ZPowGate(exponent=0.125)(Q), True), |
| 32 | + (cirq.CZPowGate(exponent=0.5)(Q, Q2), False), |
| 33 | + (cirq.CZ(Q, Q2), True), |
| 34 | + (cirq.CNOT(Q, Q2), True), |
| 35 | + (cirq.SWAP(Q, Q2), False), |
| 36 | + (cirq.ISWAP(Q, Q2), False), |
| 37 | + (cirq.CCNOT(Q, Q2, Q3), True), |
| 38 | + (cirq.CCZ(Q, Q2, Q3), True), |
| 39 | + (cirq.ParallelGate(cirq.X, num_copies=3)(Q, Q2, Q3), True), |
| 40 | + (cirq.ParallelGate(cirq.Y, num_copies=3)(Q, Q2, Q3), True), |
| 41 | + (cirq.ParallelGate(cirq.Z, num_copies=3)(Q, Q2, Q3), True), |
| 42 | + (cirq.X(Q).controlled_by(Q2, Q3), True), |
| 43 | + (cirq.Z(Q).controlled_by(Q2, Q3), True), |
| 44 | + (cirq.ZPowGate(exponent=0.5)(Q).controlled_by(Q2, Q3), False), |
| 45 | + ], |
| 46 | +) |
| 47 | +def test_gateset(op: cirq.Operation, expected: bool): |
| 48 | + assert cirq.is_native_neutral_atom_op(op) == expected |
| 49 | + if op.gate is not None: |
| 50 | + assert cirq.is_native_neutral_atom_gate(op.gate) == expected |
0 commit comments