|
21 | 21 | import cirq_ionq as ionq
|
22 | 22 |
|
23 | 23 |
|
| 24 | +PARAMS_FOR_ONE_ANGLE_GATE = [0, 0.1, 0.4, math.pi / 2, math.pi, 2 * math.pi] |
| 25 | +PARAMS_FOR_TWO_ANGLE_GATE = [ |
| 26 | + (0, 1), |
| 27 | + (0.1, 1), |
| 28 | + (0.4, 1), |
| 29 | + (math.pi / 2, 0), |
| 30 | + (0, math.pi), |
| 31 | + (0.1, 2 * math.pi), |
| 32 | +] |
| 33 | +INVALID_GATE_POWER = [-2, -0.5, 0, 0.5, 2] |
| 34 | + |
| 35 | + |
24 | 36 | @pytest.mark.parametrize(
|
25 | 37 | "gate,nqubits,diagram",
|
26 | 38 | [
|
@@ -74,3 +86,50 @@ def test_ms_unitary(phases):
|
74 | 86 |
|
75 | 87 | mat = cirq.protocols.unitary(gate)
|
76 | 88 | numpy.testing.assert_array_almost_equal(mat.dot(mat.conj().T), numpy.identity(4))
|
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize( |
| 92 | + "gate", |
| 93 | + [ |
| 94 | + *[ionq.GPIGate(phi=angle) for angle in PARAMS_FOR_ONE_ANGLE_GATE], |
| 95 | + *[ionq.GPI2Gate(phi=angle) for angle in PARAMS_FOR_ONE_ANGLE_GATE], |
| 96 | + *[ionq.MSGate(phi0=angles[0], phi1=angles[1]) for angles in PARAMS_FOR_TWO_ANGLE_GATE], |
| 97 | + ], |
| 98 | +) |
| 99 | +def test_gate_inverse(gate): |
| 100 | + """Tests that the inverse of natives gate are correct.""" |
| 101 | + mat = cirq.protocols.unitary(gate) |
| 102 | + mat_inverse = cirq.protocols.unitary(gate**-1) |
| 103 | + dim = mat.shape[0] |
| 104 | + |
| 105 | + numpy.testing.assert_array_almost_equal(mat.dot(mat_inverse), numpy.identity(dim)) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize( |
| 109 | + "gate", |
| 110 | + [ |
| 111 | + *[ionq.GPIGate(phi=angle) for angle in PARAMS_FOR_ONE_ANGLE_GATE], |
| 112 | + *[ionq.GPI2Gate(phi=angle) for angle in PARAMS_FOR_ONE_ANGLE_GATE], |
| 113 | + *[ionq.MSGate(phi0=angles[0], phi1=angles[1]) for angles in PARAMS_FOR_TWO_ANGLE_GATE], |
| 114 | + ], |
| 115 | +) |
| 116 | +def test_gate_power1(gate): |
| 117 | + """Tests that power=1 for native gates are correct.""" |
| 118 | + mat = cirq.protocols.unitary(gate) |
| 119 | + mat_power1 = cirq.protocols.unitary(gate**1) |
| 120 | + |
| 121 | + numpy.testing.assert_array_almost_equal(mat, mat_power1) |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.parametrize( |
| 125 | + "gate,power", |
| 126 | + [ |
| 127 | + *[(ionq.GPIGate(phi=0.1), power) for power in INVALID_GATE_POWER], |
| 128 | + *[(ionq.GPI2Gate(phi=0.1), power) for power in INVALID_GATE_POWER], |
| 129 | + *[(ionq.MSGate(phi0=0.1, phi1=0.2), power) for power in INVALID_GATE_POWER], |
| 130 | + ], |
| 131 | +) |
| 132 | +def test_gate_power_not_implemented(gate, power): |
| 133 | + """Tests that any power other than 1 and -1 is not implemented.""" |
| 134 | + with pytest.raises(TypeError): |
| 135 | + _ = gate**power |
0 commit comments