13
13
# limitations under the License.
14
14
"""Tests for IonQ native gates"""
15
15
16
- import math
17
16
import cirq
18
17
import numpy
19
18
import pytest
20
19
21
20
import cirq_ionq as ionq
22
21
23
22
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
- ]
23
+ PARAMS_FOR_ONE_ANGLE_GATE = [0 , 0.1 , 0.4 , 0.5 , 1 , 2 ]
24
+ PARAMS_FOR_TWO_ANGLE_GATE = [(0 , 1 ), (0.1 , 1 ), (0.4 , 1 ), (0.5 , 0 ), (0 , 1 ), (0.1 , 2 )]
33
25
INVALID_GATE_POWER = [- 2 , - 0.5 , 0 , 0.5 , 2 ]
34
26
35
27
39
31
(ionq .GPIGate (phi = 0.1 ), 1 , "0: ───GPI(0.1)───" ),
40
32
(ionq .GPI2Gate (phi = 0.2 ), 1 , "0: ───GPI2(0.2)───" ),
41
33
(ionq .MSGate (phi0 = 0.1 , phi1 = 0.2 ), 2 , "0: ───MS(0.1)───\n │\n 1: ───MS(0.2)───" ),
34
+ (ionq .ZZGate (theta = 0.3 ), 2 , "0: ───ZZ(0.3)───\n │\n 1: ───ZZ────────" ),
42
35
],
43
36
)
44
37
def test_gate_methods (gate , nqubits , diagram ):
@@ -52,14 +45,20 @@ def test_gate_methods(gate, nqubits, diagram):
52
45
53
46
54
47
@pytest .mark .parametrize (
55
- "gate" , [ionq .GPIGate (phi = 0.1 ), ionq .GPI2Gate (phi = 0.2 ), ionq .MSGate (phi0 = 0.1 , phi1 = 0.2 )]
48
+ "gate" ,
49
+ [
50
+ ionq .GPIGate (phi = 0.1 ),
51
+ ionq .GPI2Gate (phi = 0.2 ),
52
+ ionq .MSGate (phi0 = 0.1 , phi1 = 0.2 ),
53
+ ionq .ZZGate (theta = 0.4 ),
54
+ ],
56
55
)
57
56
def test_gate_json (gate ):
58
57
g_json = cirq .to_json (gate )
59
58
assert cirq .read_json (json_text = g_json ) == gate
60
59
61
60
62
- @pytest .mark .parametrize ("phase" , [0 , 0.1 , 0.4 , math . pi / 2 , math . pi , 2 * math . pi ])
61
+ @pytest .mark .parametrize ("phase" , [0 , 0.1 , 0.4 , 0.5 , 1 , 2 ])
63
62
def test_gpi_unitary (phase ):
64
63
"""Tests that the GPI gate is unitary."""
65
64
gate = ionq .GPIGate (phi = phase )
@@ -68,7 +67,7 @@ def test_gpi_unitary(phase):
68
67
numpy .testing .assert_array_almost_equal (mat .dot (mat .conj ().T ), numpy .identity (2 ))
69
68
70
69
71
- @pytest .mark .parametrize ("phase" , [0 , 0.1 , 0.4 , math . pi / 2 , math . pi , 2 * math . pi ])
70
+ @pytest .mark .parametrize ("phase" , [0 , 0.1 , 0.4 , 0.5 , 1 , 2 ])
72
71
def test_gpi2_unitary (phase ):
73
72
"""Tests that the GPI2 gate is unitary."""
74
73
gate = ionq .GPI2Gate (phi = phase )
@@ -77,9 +76,7 @@ def test_gpi2_unitary(phase):
77
76
numpy .testing .assert_array_almost_equal (mat .dot (mat .conj ().T ), numpy .identity (2 ))
78
77
79
78
80
- @pytest .mark .parametrize (
81
- "phases" , [(0 , 1 ), (0.1 , 1 ), (0.4 , 1 ), (math .pi / 2 , 0 ), (0 , math .pi ), (0.1 , 2 * math .pi )]
82
- )
79
+ @pytest .mark .parametrize ("phases" , [(0 , 1 ), (0.1 , 1 ), (0.4 , 1 ), (0.5 , 0 ), (0 , 1 ), (0.1 , 2 )])
83
80
def test_ms_unitary (phases ):
84
81
"""Tests that the MS gate is unitary."""
85
82
gate = ionq .MSGate (phi0 = phases [0 ], phi1 = phases [1 ])
@@ -88,12 +85,22 @@ def test_ms_unitary(phases):
88
85
numpy .testing .assert_array_almost_equal (mat .dot (mat .conj ().T ), numpy .identity (4 ))
89
86
90
87
88
+ @pytest .mark .parametrize ("phase" , [0 , 0.1 , 0.4 , 0.5 , 1 , 2 ])
89
+ def test_zz_unitary (phase ):
90
+ """Tests that the ZZ gate is unitary."""
91
+ gate = ionq .ZZGate (theta = phase )
92
+
93
+ mat = cirq .protocols .unitary (gate )
94
+ numpy .testing .assert_array_almost_equal (mat .dot (mat .conj ().T ), numpy .identity (4 ))
95
+
96
+
91
97
@pytest .mark .parametrize (
92
98
"gate" ,
93
99
[
94
100
* [ionq .GPIGate (phi = angle ) for angle in PARAMS_FOR_ONE_ANGLE_GATE ],
95
101
* [ionq .GPI2Gate (phi = angle ) for angle in PARAMS_FOR_ONE_ANGLE_GATE ],
96
102
* [ionq .MSGate (phi0 = angles [0 ], phi1 = angles [1 ]) for angles in PARAMS_FOR_TWO_ANGLE_GATE ],
103
+ * [ionq .ZZGate (theta = angle ) for angle in PARAMS_FOR_ONE_ANGLE_GATE ],
97
104
],
98
105
)
99
106
def test_gate_inverse (gate ):
@@ -110,6 +117,7 @@ def test_gate_inverse(gate):
110
117
[
111
118
* [ionq .GPIGate (phi = angle ) for angle in PARAMS_FOR_ONE_ANGLE_GATE ],
112
119
* [ionq .GPI2Gate (phi = angle ) for angle in PARAMS_FOR_ONE_ANGLE_GATE ],
120
+ * [ionq .ZZGate (theta = angle ) for angle in PARAMS_FOR_ONE_ANGLE_GATE ],
113
121
* [ionq .MSGate (phi0 = angles [0 ], phi1 = angles [1 ]) for angles in PARAMS_FOR_TWO_ANGLE_GATE ],
114
122
],
115
123
)
@@ -127,6 +135,7 @@ def test_gate_power1(gate):
127
135
* [(ionq .GPIGate (phi = 0.1 ), power ) for power in INVALID_GATE_POWER ],
128
136
* [(ionq .GPI2Gate (phi = 0.1 ), power ) for power in INVALID_GATE_POWER ],
129
137
* [(ionq .MSGate (phi0 = 0.1 , phi1 = 0.2 ), power ) for power in INVALID_GATE_POWER ],
138
+ * [(ionq .ZZGate (theta = 0.1 ), power ) for power in INVALID_GATE_POWER ],
130
139
],
131
140
)
132
141
def test_gate_power_not_implemented (gate , power ):
0 commit comments