-
Notifications
You must be signed in to change notification settings - Fork 986
/
parity_gates.py
404 lines (321 loc) · 12.4 KB
/
parity_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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# Copyright 2018 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.
"""Quantum gates that phase with respect to product-of-pauli observables."""
from typing import Any, Dict, List, Optional, Tuple, Union, TYPE_CHECKING
from typing_extensions import Self
import numpy as np
from cirq import protocols, value
from cirq._compat import proper_repr
from cirq._doc import document
from cirq.ops import gate_features, eigen_gate, common_gates, pauli_gates
if TYPE_CHECKING:
import cirq
class XXPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate):
r"""The X-parity gate, possibly raised to a power.
The XX**t gate implements the following unitary:
$$
(X \otimes X)^t = \begin{bmatrix}
c & 0 & 0 & s \\
0 & c & s & 0 \\
0 & s & c & 0 \\
s & 0 & 0 & c
\end{bmatrix}
$$
where
$$
c = f \cos\left(\frac{\pi t}{2}\right)
$$
$$
s = -i f \sin\left(\frac{\pi t}{2}\right)
$$
$$
f = e^{\frac{i \pi t}{2}}.
$$
See also: `cirq.ops.MSGate` (the Mølmer–Sørensen gate), which is
implemented via this class.
"""
def _num_qubits_(self) -> int:
return 2
def _eigen_components(self) -> List[Tuple[float, np.ndarray]]:
return [
(
0.0,
np.array([[0.5, 0, 0, 0.5], [0, 0.5, 0.5, 0], [0, 0.5, 0.5, 0], [0.5, 0, 0, 0.5]]),
),
(
1.0,
np.array(
[[0.5, 0, 0, -0.5], [0, 0.5, -0.5, 0], [0, -0.5, 0.5, 0], [-0.5, 0, 0, 0.5]]
),
),
]
def _eigen_shifts(self):
return [0, 1]
def _trace_distance_bound_(self) -> Optional[float]:
if self._is_parameterized_():
return None
return abs(np.sin(self._exponent * 0.5 * np.pi))
def _decompose_into_clifford_with_qubits_(self, qubits):
from cirq.ops.clifford_gate import SingleQubitCliffordGate
from cirq.ops.pauli_interaction_gate import PauliInteractionGate
if self.exponent % 2 == 0:
return []
if self.exponent % 2 == 0.5:
return [
PauliInteractionGate(pauli_gates.X, False, pauli_gates.X, False).on(*qubits),
SingleQubitCliffordGate.X_sqrt.on_each(*qubits),
]
if self.exponent % 2 == 1:
return [SingleQubitCliffordGate.X.on_each(*qubits)]
if self.exponent % 2 == 1.5:
return [
PauliInteractionGate(pauli_gates.X, False, pauli_gates.X, False).on(*qubits),
SingleQubitCliffordGate.X_nsqrt.on_each(*qubits),
]
return NotImplemented
def _decompose_(self, qubits: Tuple['cirq.Qid', ...]) -> 'cirq.OP_TREE':
yield common_gates.YPowGate(exponent=-0.5).on_each(*qubits)
yield ZZPowGate(exponent=self.exponent, global_shift=self.global_shift)(*qubits)
yield common_gates.YPowGate(exponent=0.5).on_each(*qubits)
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> Union[str, 'protocols.CircuitDiagramInfo']:
return protocols.CircuitDiagramInfo(
wire_symbols=('XX', 'XX'), exponent=self._diagram_exponent(args)
)
def __str__(self) -> str:
if self.exponent == 1:
return 'XX'
return f'XX**{self._exponent!r}'
def __repr__(self) -> str:
if self._global_shift == 0:
if self._exponent == 1:
return 'cirq.XX'
return f'(cirq.XX**{proper_repr(self._exponent)})'
return (
f'cirq.XXPowGate(exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)
class YYPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate):
r"""The Y-parity gate, possibly raised to a power.
The YY**t gate implements the following unitary:
$$
(Y \otimes Y)^t = \begin{bmatrix}
c & 0 & 0 & -s \\
0 & c & s & 0 \\
0 & s & c & 0 \\
-s & 0 & 0 & c \\
\end{bmatrix}
$$
where
$$
c = f \cos\left(\frac{\pi t}{2}\right)
$$
$$
s = -i f \sin\left(\frac{\pi t}{2}\right)
$$
$$
f = e^{\frac{i \pi t}{2}}.
$$
"""
def _num_qubits_(self) -> int:
return 2
def _eigen_components(self) -> List[Tuple[float, np.ndarray]]:
return [
(
0.0,
np.array(
[[0.5, 0, 0, -0.5], [0, 0.5, 0.5, 0], [0, 0.5, 0.5, 0], [-0.5, 0, 0, 0.5]]
),
),
(
1.0,
np.array(
[[0.5, 0, 0, 0.5], [0, 0.5, -0.5, 0], [0, -0.5, 0.5, 0], [0.5, 0, 0, 0.5]]
),
),
]
def _eigen_shifts(self):
return [0, 1]
def _trace_distance_bound_(self) -> Optional[float]:
if self._is_parameterized_():
return None
return abs(np.sin(self._exponent * 0.5 * np.pi))
def _decompose_into_clifford_with_qubits_(self, qubits):
from cirq.ops.clifford_gate import SingleQubitCliffordGate
from cirq.ops.pauli_interaction_gate import PauliInteractionGate
if self.exponent % 2 == 0:
return []
if self.exponent % 2 == 0.5:
return [
PauliInteractionGate(pauli_gates.Y, False, pauli_gates.Y, False).on(*qubits),
SingleQubitCliffordGate.Y_sqrt.on_each(*qubits),
]
if self.exponent % 2 == 1:
return [SingleQubitCliffordGate.Y.on_each(*qubits)]
if self.exponent % 2 == 1.5:
return [
PauliInteractionGate(pauli_gates.Y, False, pauli_gates.Y, False).on(*qubits),
SingleQubitCliffordGate.Y_nsqrt.on_each(*qubits),
]
return NotImplemented
def _decompose_(self, qubits: Tuple['cirq.Qid', ...]) -> 'cirq.OP_TREE':
yield common_gates.XPowGate(exponent=0.5).on_each(*qubits)
yield ZZPowGate(exponent=self.exponent, global_shift=self.global_shift)(*qubits)
yield common_gates.XPowGate(exponent=-0.5).on_each(*qubits)
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> 'cirq.CircuitDiagramInfo':
return protocols.CircuitDiagramInfo(
wire_symbols=('YY', 'YY'), exponent=self._diagram_exponent(args)
)
def __str__(self) -> str:
if self._exponent == 1:
return 'YY'
return f'YY**{self._exponent!r}'
def __repr__(self) -> str:
if self._global_shift == 0:
if self._exponent == 1:
return 'cirq.YY'
return f'(cirq.YY**{proper_repr(self._exponent)})'
return (
f'cirq.YYPowGate(exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)
class ZZPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate):
r"""The Z-parity gate, possibly raised to a power.
The ZZ**t gate implements the following unitary:
$$
(Z \otimes Z)^t = \begin{bmatrix}
1 & & & \\
& e^{i \pi t} & & \\
& & e^{i \pi t} & \\
& & & 1
\end{bmatrix}
$$
"""
def _num_qubits_(self) -> int:
return 2
def _decompose_(self, qubits):
yield common_gates.ZPowGate(exponent=self.exponent)(qubits[0])
yield common_gates.ZPowGate(exponent=self.exponent)(qubits[1])
yield common_gates.CZPowGate(
exponent=-2 * self.exponent, global_shift=-self.global_shift / 2
)(qubits[0], qubits[1])
def _eigen_components(self) -> List[Tuple[float, np.ndarray]]:
return [(0, np.diag([1, 0, 0, 1])), (1, np.diag([0, 1, 1, 0]))]
def _eigen_shifts(self):
return [0, 1]
def _trace_distance_bound_(self) -> Optional[float]:
if self._is_parameterized_():
return None
return abs(np.sin(self._exponent * 0.5 * np.pi))
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> 'cirq.CircuitDiagramInfo':
return protocols.CircuitDiagramInfo(
wire_symbols=('ZZ', 'ZZ'), exponent=self._diagram_exponent(args)
)
def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs') -> Optional[np.ndarray]:
if protocols.is_parameterized(self):
return None
global_phase = 1j ** (2 * self._exponent * self._global_shift)
if global_phase != 1:
args.target_tensor *= global_phase
relative_phase = 1j ** (2 * self.exponent)
zo = args.subspace_index(0b01)
oz = args.subspace_index(0b10)
args.target_tensor[oz] *= relative_phase
args.target_tensor[zo] *= relative_phase
return args.target_tensor
def _phase_by_(self, phase_turns: float, qubit_index: int) -> "ZZPowGate":
return self
def __str__(self) -> str:
if self._exponent == 1:
return 'ZZ'
return f'ZZ**{self._exponent}'
def __repr__(self) -> str:
if self._global_shift == 0:
if self._exponent == 1:
return 'cirq.ZZ'
return f'(cirq.ZZ**{proper_repr(self._exponent)})'
return (
f'cirq.ZZPowGate(exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)
class MSGate(XXPowGate):
"""The Mølmer–Sørensen gate, a native two-qubit operation in ion traps.
A rotation around the XX axis in the two-qubit bloch sphere.
The gate implements the following unitary:
exp(-i t XX) = [ cos(t) 0 0 -isin(t)]
[ 0 cos(t) -isin(t) 0 ]
[ 0 -isin(t) cos(t) 0 ]
[-isin(t) 0 0 cos(t) ]
"""
def __init__(self, *, rads: float): # Forces keyword args.
XXPowGate.__init__(self, exponent=rads * 2 / np.pi, global_shift=-0.5)
self.rads = rads
def _with_exponent(self, exponent: value.TParamVal) -> Self:
return type(self)(rads=exponent * np.pi / 2)
def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
) -> Union[str, 'protocols.CircuitDiagramInfo']:
angle_str = self._format_exponent_as_angle(args, order=4)
symbol = f'MS({angle_str})'
return protocols.CircuitDiagramInfo(wire_symbols=(symbol, symbol))
def __str__(self) -> str:
if self._exponent == 1:
return 'MS(π/2)'
return f'MS({self._exponent!r}π/2)'
def __repr__(self) -> str:
if self._exponent == 1:
return 'cirq.ms(np.pi/2)'
return f'cirq.ms({self._exponent!r}*np.pi/2)'
def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, ["rads"])
@classmethod
def _from_json_dict_(cls, rads: float, **kwargs: Any) -> 'MSGate':
return cls(rads=rads)
def ms(rads: float) -> MSGate:
"""A helper to construct the `cirq.MSGate` for the given angle specified in radians.
Args:
rads: The rotation angle in radians.
Returns:
Mølmer–Sørensen gate rotating by the desired amount.
"""
return MSGate(rads=rads)
XX = XXPowGate()
document(
XX,
r"""The tensor product of two X gates.
Useful for creating `cirq.XXPowGate`s via `cirq.XX**t`.
This is the `exponent=1` instance of `cirq.XXPowGate`.
""",
)
YY = YYPowGate()
document(
YY,
r"""The tensor product of two Y gates.
Useful for creating `cirq.YYPowGate`s via `cirq.YY**t`.
This is the `exponent=1` instance of `cirq.YYPowGate`.
""",
)
ZZ = ZZPowGate()
document(
ZZ,
r"""The tensor product of two Z gates.
Useful for creating `cirq.ZZPowGate`s via `cirq.ZZ**t`.
This is the `exponent=1` instance of `cirq.ZZPowGate`.
""",
)