-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathgate_operation_test.py
398 lines (290 loc) · 12.3 KB
/
gate_operation_test.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
# 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.
import numpy as np
import pytest
import sympy
import cirq
def test_gate_operation_init():
q = cirq.NamedQubit('q')
g = cirq.SingleQubitGate()
v = cirq.GateOperation(g, (q,))
assert v.gate == g
assert v.qubits == (q,)
def test_invalid_gate_operation():
three_qubit_gate = cirq.ThreeQubitGate()
single_qubit = [cirq.GridQubit(0, 0)]
with pytest.raises(ValueError, match="number of qubits"):
cirq.GateOperation(three_qubit_gate, single_qubit)
def test_gate_operation_eq():
g1 = cirq.SingleQubitGate()
g2 = cirq.SingleQubitGate()
g3 = cirq.TwoQubitGate()
r1 = [cirq.NamedQubit('r1')]
r2 = [cirq.NamedQubit('r2')]
r12 = r1 + r2
r21 = r2 + r1
eq = cirq.testing.EqualsTester()
eq.make_equality_group(lambda: cirq.GateOperation(g1, r1))
eq.make_equality_group(lambda: cirq.GateOperation(g2, r1))
eq.make_equality_group(lambda: cirq.GateOperation(g1, r2))
eq.make_equality_group(lambda: cirq.GateOperation(g3, r12))
eq.make_equality_group(lambda: cirq.GateOperation(g3, r21))
eq.add_equality_group(cirq.GateOperation(cirq.CZ, r21),
cirq.GateOperation(cirq.CZ, r12))
@cirq.value_equality
class PairGate(cirq.Gate, cirq.InterchangeableQubitsGate):
"""Interchangeable substes."""
def __init__(self, num_qubits):
self._num_qubits = num_qubits
def num_qubits(self) -> int:
return self._num_qubits
def qubit_index_to_equivalence_group_key(self, index: int):
return index // 2
def _value_equality_values_(self):
return self.num_qubits(),
def p(*q):
return PairGate(len(q)).on(*q)
a0, a1, b0, b1, c0 = cirq.LineQubit.range(5)
eq.add_equality_group(p(a0, a1, b0, b1), p(a1, a0, b1, b0))
eq.add_equality_group(p(b0, b1, a0, a1))
eq.add_equality_group(p(a0, a1, b0, b1, c0), p(a1, a0, b1, b0, c0))
eq.add_equality_group(p(a0, b0, a1, b1, c0))
eq.add_equality_group(p(a0, c0, b0, b1, a1))
eq.add_equality_group(p(b0, a1, a0, b1, c0))
def test_gate_operation_approx_eq():
a = [cirq.NamedQubit('r1')]
b = [cirq.NamedQubit('r2')]
assert cirq.approx_eq(cirq.GateOperation(cirq.XPowGate(), a),
cirq.GateOperation(cirq.XPowGate(), a))
assert not cirq.approx_eq(cirq.GateOperation(cirq.XPowGate(), a),
cirq.GateOperation(cirq.XPowGate(), b))
assert cirq.approx_eq(cirq.GateOperation(cirq.XPowGate(exponent=0), a),
cirq.GateOperation(cirq.XPowGate(exponent=1e-9), a))
assert not cirq.approx_eq(cirq.GateOperation(cirq.XPowGate(exponent=0), a),
cirq.GateOperation(cirq.XPowGate(exponent=1e-7),
a))
assert cirq.approx_eq(cirq.GateOperation(cirq.XPowGate(exponent=0), a),
cirq.GateOperation(cirq.XPowGate(exponent=1e-7), a),
atol=1e-6)
def test_gate_operation_qid_shape():
class ShapeGate(cirq.Gate):
def _qid_shape_(self):
return (1, 2, 3, 4)
op = ShapeGate().on(*cirq.LineQid.for_qid_shape((1, 2, 3, 4)))
assert cirq.qid_shape(op) == (1, 2, 3, 4)
assert cirq.num_qubits(op) == 4
def test_gate_operation_num_qubits():
class NumQubitsGate(cirq.Gate):
def _num_qubits_(self):
return 4
op = NumQubitsGate().on(*cirq.LineQubit.range(4))
assert cirq.qid_shape(op) == (2, 2, 2, 2)
assert cirq.num_qubits(op) == 4
def test_gate_operation_pow():
Y = cirq.Y
q = cirq.NamedQubit('q')
assert (Y ** 0.5)(q) == Y(q) ** 0.5
def test_with_qubits_and_transform_qubits():
g = cirq.ThreeQubitGate()
op = cirq.GateOperation(g, cirq.LineQubit.range(3))
assert op.with_qubits(*cirq.LineQubit.range(3, 0, -1)) \
== cirq.GateOperation(g, cirq.LineQubit.range(3, 0, -1))
assert op.transform_qubits(lambda e: cirq.LineQubit(-e.x)
) == cirq.GateOperation(g, [cirq.LineQubit(0),
cirq.LineQubit(-1),
cirq.LineQubit(-2)])
def test_extrapolate():
q = cirq.NamedQubit('q')
# If the gate isn't extrapolatable, you get a type error.
op0 = cirq.GateOperation(cirq.SingleQubitGate(), [q])
with pytest.raises(TypeError):
_ = op0**0.5
op1 = cirq.GateOperation(cirq.Y, [q])
assert op1**0.5 == cirq.GateOperation(cirq.Y**0.5, [q])
assert (cirq.Y**0.5).on(q) == cirq.Y(q)**0.5
def test_inverse():
q = cirq.NamedQubit('q')
# If the gate isn't reversible, you get a type error.
op0 = cirq.GateOperation(cirq.SingleQubitGate(), [q])
assert cirq.inverse(op0, None) is None
op1 = cirq.GateOperation(cirq.S, [q])
assert cirq.inverse(op1) == op1**-1 == cirq.GateOperation(cirq.S**-1, [q])
assert cirq.inverse(cirq.S).on(q) == cirq.inverse(cirq.S.on(q))
def test_text_diagrammable():
q = cirq.NamedQubit('q')
# If the gate isn't diagrammable, you get a type error.
op0 = cirq.GateOperation(cirq.SingleQubitGate(), [q])
with pytest.raises(TypeError):
_ = cirq.circuit_diagram_info(op0)
op1 = cirq.GateOperation(cirq.S, [q])
actual = cirq.circuit_diagram_info(op1)
expected = cirq.circuit_diagram_info(cirq.S)
assert actual == expected
def test_bounded_effect():
q = cirq.NamedQubit('q')
# If the gate isn't bounded, you get a type error.
op0 = cirq.GateOperation(cirq.SingleQubitGate(), [q])
assert cirq.trace_distance_bound(op0) >= 1
op1 = cirq.GateOperation(cirq.Z**0.000001, [q])
op1_bound = cirq.trace_distance_bound(op1)
assert op1_bound == cirq.trace_distance_bound(cirq.Z**0.000001)
def test_parameterizable_effect():
q = cirq.NamedQubit('q')
r = cirq.ParamResolver({'a': 0.5})
op1 = cirq.GateOperation(cirq.Z**sympy.Symbol('a'), [q])
assert cirq.is_parameterized(op1)
op2 = cirq.resolve_parameters(op1, r)
assert not cirq.is_parameterized(op2)
assert op2 == cirq.S.on(q)
def test_pauli_expansion():
a = cirq.NamedQubit('a')
b = cirq.NamedQubit('b')
assert cirq.pauli_expansion(cirq.X(a)) == cirq.LinearDict({'X': 1})
assert (cirq.pauli_expansion(cirq.CNOT(a, b)) == cirq.pauli_expansion(
cirq.CNOT))
class No(cirq.Gate):
def num_qubits(self) -> int:
return 1
class Yes(cirq.Gate):
def num_qubits(self) -> int:
return 1
def _pauli_expansion_(self):
return cirq.LinearDict({'X': 0.5})
assert cirq.pauli_expansion(No().on(a), default=None) is None
assert cirq.pauli_expansion(Yes().on(a)) == cirq.LinearDict({'X': 0.5})
def test_unitary():
a = cirq.NamedQubit('a')
b = cirq.NamedQubit('b')
assert not cirq.has_unitary(cirq.measure(a))
assert cirq.unitary(cirq.measure(a), None) is None
np.testing.assert_allclose(cirq.unitary(cirq.X(a)),
np.array([[0, 1], [1, 0]]),
atol=1e-8)
np.testing.assert_allclose(cirq.unitary(cirq.CNOT(a, b)),
cirq.unitary(cirq.CNOT),
atol=1e-8)
def test_channel():
a = cirq.NamedQubit('a')
op = cirq.bit_flip(0.5).on(a)
np.testing.assert_allclose(cirq.channel(op), cirq.channel(op.gate))
assert cirq.has_channel(op)
assert cirq.channel(cirq.SingleQubitGate()(a), None) is None
assert not cirq.has_channel(cirq.SingleQubitGate()(a))
def test_measurement_key():
a = cirq.NamedQubit('a')
assert cirq.measurement_key(cirq.measure(a, key='lock')) == 'lock'
def assert_mixtures_equal(actual, expected):
"""Assert equal for tuple of mixed scalar and array types."""
for a, e in zip(actual, expected):
np.testing.assert_almost_equal(a[0], e[0])
np.testing.assert_almost_equal(a[1], e[1])
def test_mixture():
a = cirq.NamedQubit('a')
op = cirq.bit_flip(0.5).on(a)
assert_mixtures_equal(cirq.mixture(op), cirq.mixture(op.gate))
assert cirq.has_mixture(op)
assert cirq.mixture(cirq.X(a), None) is None
assert not cirq.has_mixture(cirq.X(a))
def test_repr():
a, b = cirq.LineQubit.range(2)
assert repr(cirq.GateOperation(
cirq.CZ, (a, b))) == 'cirq.CZ(cirq.LineQubit(0), cirq.LineQubit(1))'
class Inconsistent(cirq.SingleQubitGate):
def __repr__(self):
return 'Inconsistent'
def on(self, *qubits):
return cirq.GateOperation(Inconsistent(), qubits)
assert (repr(cirq.GateOperation(Inconsistent(), [a])) ==
'cirq.GateOperation(gate=Inconsistent, qubits=[cirq.LineQubit(0)])')
@pytest.mark.parametrize('gate1,gate2,eq_up_to_global_phase', [
(cirq.rz(0.3 * np.pi), cirq.Z**0.3, True),
(cirq.rz(0.3), cirq.Z**0.3, False),
(cirq.ZZPowGate(global_shift=0.5), cirq.ZZ, True),
(cirq.ZPowGate(global_shift=0.5)**sympy.Symbol('e'), cirq.Z, False),
(cirq.Z**sympy.Symbol('e'), cirq.Z**sympy.Symbol('f'), False),
])
def test_equal_up_to_global_phase_on_gates(gate1, gate2, eq_up_to_global_phase):
num_qubits1, num_qubits2 = (cirq.num_qubits(g) for g in (gate1, gate2))
qubits = cirq.LineQubit.range(max(num_qubits1, num_qubits2) + 1)
op1, op2 = gate1(*qubits[:num_qubits1]), gate2(*qubits[:num_qubits2])
assert cirq.equal_up_to_global_phase(op1, op2) == eq_up_to_global_phase
op2_on_diff_qubits = gate2(*qubits[1:num_qubits2 + 1])
assert not cirq.equal_up_to_global_phase(op1, op2_on_diff_qubits)
def test_equal_up_to_global_phase_on_diff_types():
op = cirq.X(cirq.LineQubit(0))
assert not cirq.equal_up_to_global_phase(op, 3)
def test_gate_on_operation_besides_gate_operation():
a, b = cirq.LineQubit.range(2)
op = -1j * cirq.X(a) * cirq.Y(b)
assert isinstance(op.gate, cirq.DensePauliString)
assert op.gate == -1j * cirq.DensePauliString('XY')
assert not isinstance(op.gate, cirq.XPowGate)
def test_mul():
class GateRMul(cirq.Gate):
def num_qubits(self) -> int:
return 1
def _rmul_with_qubits(self, qubits, other):
if other == 2:
return 3
if (isinstance(other, cirq.Operation) and
isinstance(other.gate, GateRMul)):
return 4
raise NotImplementedError()
class GateMul(cirq.Gate):
def num_qubits(self) -> int:
return 1
def _mul_with_qubits(self, qubits, other):
if other == 2:
return 5
if (isinstance(other, cirq.Operation) and
isinstance(other.gate, GateMul)):
return 6
raise NotImplementedError()
# Delegates right multiplication.
q = cirq.LineQubit(0)
r = GateRMul().on(q)
assert 2 * r == 3
with pytest.raises(TypeError):
_ = r * 2
# Delegates left multiplication.
m = GateMul().on(q)
assert m * 2 == 5
with pytest.raises(TypeError):
_ = 2 * m
# Handles the symmetric type case correctly.
assert m * m == 6
assert r * r == 4
def test_with_gate():
g1 = cirq.GateOperation(cirq.X, cirq.LineQubit.range(1))
g2 = cirq.GateOperation(cirq.Y, cirq.LineQubit.range(1))
assert g1.with_gate(cirq.X) is g1
assert g1.with_gate(cirq.Y) == g2
def test_is_parameterized():
class No1(cirq.Gate):
def num_qubits(self) -> int:
return 1
class No2(cirq.Gate):
def num_qubits(self) -> int:
return 1
def _is_parameterized_(self):
return False
class Yes(cirq.Gate):
def num_qubits(self) -> int:
return 1
def _is_parameterized_(self):
return True
q = cirq.LineQubit(0)
assert not cirq.is_parameterized(No1().on(q))
assert not cirq.is_parameterized(No2().on(q))
assert cirq.is_parameterized(Yes().on(q))