-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ionq_native_gates.py
362 lines (273 loc) · 9.44 KB
/
ionq_native_gates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright 2019 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Native gates for IonQ hardware"""
from typing import Any, Dict, Sequence, Union
import cmath
import math
import numpy as np
import cirq
from cirq import protocols
from cirq._doc import document
@cirq.value.value_equality
class GPIGate(cirq.Gate):
r"""The GPI gate is a single qubit gate representing a pi pulse.
The unitary matrix of this gate is:
$$
\begin{bmatrix}
0 & e^{-i 2\pi\phi} \\
e^{i 2\pi\phi} & 0
\end{bmatrix}
$$
See [IonQ best practices](https://ionq.com/docs/getting-started-with-native-gates){:external}.
"""
def __init__(self, *, phi):
self.phi = phi
def _unitary_(self) -> np.ndarray:
top = cmath.exp(-self.phi * 2 * math.pi * 1j)
bot = cmath.exp(self.phi * 2 * math.pi * 1j)
return np.array([[0, top], [bot, 0]])
def __str__(self) -> str:
return 'GPI'
def _num_qubits_(self) -> int:
return 1
@property
def phase(self) -> float:
return self.phi
def __repr__(self) -> str:
return f'cirq_ionq.GPIGate(phi={self.phi!r})'
def _json_dict_(self) -> Dict[str, Any]:
return cirq.obj_to_dict_helper(self, ['phi'])
def _value_equality_values_(self) -> Any:
return self.phi
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> Union[str, 'protocols.CircuitDiagramInfo']:
return protocols.CircuitDiagramInfo(wire_symbols=(f'GPI({self.phase!r})',))
def __pow__(self, power):
if power == 1:
return self
if power == -1:
return self
return NotImplemented
GPI = GPIGate(phi=0)
document(
GPI,
r"""An instance of the single qubit GPI gate with no phase.
The unitary matrix of this gate is:
$$
\begin{bmatrix}
0 & 1 \\
1 & 0
\end{bmatrix}
$$
See [IonQ best practices](https://ionq.com/docs/getting-started-with-native-gates){:external}.
""",
)
@cirq.value.value_equality
class GPI2Gate(cirq.Gate):
r"""The GPI2 gate is a single qubit gate representing a pi/2 pulse.
The unitary matrix of this gate is
$$
\frac{1}{\sqrt{2}}
\begin{bmatrix}
1 & -i e^{-i 2\pi\phi} \\
-i e^{i 2\pi\phi} & 1
\end{bmatrix}
$$
See [IonQ best practices](https://ionq.com/docs/getting-started-with-native-gates){:external}.
"""
def __init__(self, *, phi):
self.phi = phi
def _unitary_(self) -> np.ndarray:
top = -1j * cmath.exp(self.phase * 2 * math.pi * -1j)
bot = -1j * cmath.exp(self.phase * 2 * math.pi * 1j)
return np.array([[1, top], [bot, 1]]) / math.sqrt(2)
@property
def phase(self) -> float:
return self.phi
def __str__(self) -> str:
return 'GPI2'
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> Union[str, 'protocols.CircuitDiagramInfo']:
return protocols.CircuitDiagramInfo(wire_symbols=(f'GPI2({self.phase!r})',))
def _num_qubits_(self) -> int:
return 1
def __repr__(self) -> str:
return f'cirq_ionq.GPI2Gate(phi={self.phi!r})'
def _json_dict_(self) -> Dict[str, Any]:
return cirq.obj_to_dict_helper(self, ['phi'])
def _value_equality_values_(self) -> Any:
return self.phi
def __pow__(self, power):
if power == 1:
return self
if power == -1:
return GPI2Gate(phi=self.phi + 0.5)
return NotImplemented
GPI2 = GPI2Gate(phi=0)
document(
GPI2,
r"""An instance of the single qubit GPI2 gate with no phase.
The unitary matrix of this gate is
$$
\frac{1}{\sqrt{2}}
\begin{bmatrix}
1 & -i \\
-i & 1
\end{bmatrix}
$$
See [IonQ best practices](https://ionq.com/docs/getting-started-with-native-gates){:external}.
""",
)
@cirq.value.value_equality
class MSGate(cirq.Gate):
r"""The Mølmer-Sørensen (MS) gate is a two qubit gate native to trapped ions.
The unitary matrix of this gate for parameters $\phi_0$, $\phi_1$ and $\theta$ is
$$
\begin{bmatrix}
\cos(\pi \theta) & 0 & 0 & -ie^{-i2\pi(\phi_0+\phi_1)}\sin(\pi\theta) \\
0 & \cos(\pi\theta) & -ie^{-i2\pi(\phi_0-\phi_1)}\sin(\pi\theta) & 0 \\
0 & -ie^{i2\pi(\phi_0-\phi_1)}\sin(\pi\theta) & \cos(\pi\theta) & 0 \\
-ie^{i2\pi(\phi_0+\phi_1)}\sin(\pi\theta) & 0 & 0 & \cos(\pi\theta)
\end{bmatrix}
$$
See [IonQ best practices](https://ionq.com/docs/getting-started-with-native-gates){:external}.
"""
def __init__(self, *, phi0, phi1, theta=0.25):
self.phi0 = phi0
self.phi1 = phi1
self.theta = theta
def _unitary_(self) -> np.ndarray:
theta = self.theta
phi0 = self.phi0
phi1 = self.phi1
diag = np.cos(math.pi * theta)
sin = np.sin(math.pi * theta)
return np.array(
[
[diag, 0, 0, sin * -1j * cmath.exp(-1j * 2 * math.pi * (phi0 + phi1))],
[0, diag, sin * -1j * cmath.exp(-1j * 2 * math.pi * (phi0 - phi1)), 0],
[0, sin * -1j * cmath.exp(1j * 2 * math.pi * (phi0 - phi1)), diag, 0],
[sin * -1j * cmath.exp(1j * 2 * math.pi * (phi0 + phi1)), 0, 0, diag],
]
)
@property
def phases(self) -> Sequence[float]:
return [self.phi0, self.phi1]
def __str__(self) -> str:
return 'MS'
def _num_qubits_(self) -> int:
return 2
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> Union[str, 'protocols.CircuitDiagramInfo']:
return protocols.CircuitDiagramInfo(
wire_symbols=(f'MS({self.phi0!r})', f'MS({self.phi1!r})')
)
def __repr__(self) -> str:
return f'cirq_ionq.MSGate(phi0={self.phi0!r}, phi1={self.phi1!r})'
def _json_dict_(self) -> Dict[str, Any]:
return cirq.obj_to_dict_helper(self, ['phi0', 'phi1', 'theta'])
def _value_equality_values_(self) -> Any:
return (self.phi0, self.phi1)
def __pow__(self, power):
if power == 1:
return self
if power == -1:
return MSGate(phi0=self.phi0 + 0.5, phi1=self.phi1)
return NotImplemented
MS = MSGate(phi0=0, phi1=0)
document(
MS,
r"""An instance of the two qubit Mølmer–Sørensen (MS) gate with no phases.
The unitary matrix of this gate for parameters $\phi_0$ and $\phi_1$ is
$$
\frac{1}{\sqrt{2}}
\begin{bmatrix}
1 & 0 & 0 & -i \\
0 & 1 & -i & 0 \\
0 & -i & 1 & 0 \\
-i & 0 & 0 & 1 \\
\end{bmatrix}
$$
See [IonQ best practices](https://ionq.com/docs/getting-started-with-native-gates){:external}.
""",
)
@cirq.value.value_equality
class ZZGate(cirq.Gate):
r"""The ZZ gate is another two qubit gate native to trapped ions. The ZZ gate only
requires a single parameter, θ, to set the phase of the entanglement.
The unitary matrix of this gate using the parameter $\theta$ is:
$$
\begin{bmatrix}
e{-i\pi\theta} & 0 & 0 & 0 \\
0 & e{i\pi\theta} & 0 & 0 \\
0 & 0 & e{i\pi\theta} & 0 \\
0 & 0 & 0 & e{-i\pi\theta}
\end{bmatrix}
$$
See [IonQ best practices](https://ionq.com/docs/getting-started-with-native-gates){:external}.
"""
def __init__(self, *, theta):
self.theta = theta
def _unitary_(self) -> np.ndarray:
theta = self.theta
return np.array(
[
[cmath.exp(-1j * theta * math.pi), 0, 0, 0],
[0, cmath.exp(1j * theta * math.pi), 0, 0],
[0, 0, cmath.exp(1j * theta * math.pi), 0],
[0, 0, 0, cmath.exp(-1j * theta * math.pi)],
]
)
@property
def phase(self) -> float:
return self.theta
def __str__(self) -> str:
return 'ZZ'
def _num_qubits_(self) -> int:
return 2
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> Union[str, 'protocols.CircuitDiagramInfo']:
return protocols.CircuitDiagramInfo(wire_symbols=(f'ZZ({self.theta!r})', 'ZZ'))
def __repr__(self) -> str:
return f'cirq_ionq.ZZGate(theta={self.theta!r})'
def _json_dict_(self) -> Dict[str, Any]:
return cirq.obj_to_dict_helper(self, ['theta'])
def _value_equality_values_(self) -> Any:
return self.theta
def __pow__(self, power):
if power == 1:
return self
if power == -1:
return ZZGate(theta=-self.theta)
return NotImplemented
ZZ = ZZGate(theta=0)
document(
ZZ,
r"""An instance of the two qubit ZZ gate with no phase.
The unitary matrix of this gate for parameters $\theta$ is
$$
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 \\
\end{bmatrix}
$$
See [IonQ best practices](https://ionq.com/docs/getting-started-with-native-gates){:external}.
""",
)