-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_unitary.py
498 lines (397 loc) · 18.7 KB
/
test_unitary.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
'''
Tests for stoqcompiler.unitary modules.
'''
import pytest
import numpy as np
from stoqcompiler.unitary import (
Unitary,
UnitarySequence,
UnitarySequenceEntry,
UnitaryDefinitions,
ParameterizedUnitary,
ParameterizedUnitaryParameter,
ParameterizedUnitaryDefinitions)
class TestUnitary:
def test_default(self) -> None:
dimension = 4
unitary = Unitary(dimension)
assert unitary.get_dimension() == dimension
assert unitary.close_to(np.identity(dimension))
def test_close_to(self) -> None:
dimension = 2
random_unitary = Unitary.random(dimension)
t = UnitaryDefinitions.t()
assert random_unitary.close_to(random_unitary)
assert random_unitary.close_to(random_unitary, None)
assert random_unitary.close_to(random_unitary, 0.999)
assert not random_unitary.close_to(t)
assert not random_unitary.close_to(t, None)
assert not random_unitary.close_to(t, 0.999)
assert random_unitary.close_to(t, 0.0)
def test_non_square_matrix(self) -> None:
dimension = 2
with pytest.raises(Exception):
Unitary(dimension, np.array([[1, 0, 0], [0, 1, 0]]))
def test_non_unitary_matrix(self) -> None:
dimension = 2
with pytest.raises(Exception):
Unitary(dimension, np.array([[1, 0], [1, 1]]))
def test_mismatched_dimension(self) -> None:
dimension = 4
with pytest.raises(Exception):
Unitary(dimension, np.identity(dimension - 1))
def test_inverse_fixed(self) -> None:
dimension = 2
operation_name = 'U'
unitary = Unitary(dimension, np.array([
[np.exp(1j * np.pi / 4), 0],
[0, 1j]]), operation_name)
inverse = unitary.inverse()
assert unitary.left_multiply(inverse).close_to(np.identity(dimension))
assert unitary.right_multiply(inverse).close_to(np.identity(dimension))
assert inverse.get_display_name() == 'U†'
double_inverse = inverse.inverse()
assert double_inverse.get_display_name() == 'U'
assert unitary.close_to(double_inverse)
def test_inverse_random(self) -> None:
dimension = 2
unitary = Unitary.random(dimension)
inverse = unitary.inverse()
assert unitary.left_multiply(inverse).close_to(np.identity(dimension))
assert unitary.right_multiply(inverse).close_to(np.identity(dimension))
def test_tensor(self) -> None:
dimension = 2
unitary = Unitary(dimension)
tensor_product = unitary.tensor(unitary)
assert tensor_product.get_dimension() == dimension ** 2
assert tensor_product.close_to(np.identity(dimension ** 2))
tensor_product = UnitaryDefinitions.sigmax().tensor(
UnitaryDefinitions.sigmax())
assert tensor_product.get_dimension() == dimension ** 2
assert not tensor_product.close_to(np.identity(dimension ** 2))
tensor_product = tensor_product.left_multiply(tensor_product)
assert tensor_product.close_to(np.identity(dimension ** 2))
def test_multiply(self) -> None:
dimension = 2
identity = Unitary.identity(dimension)
product = Unitary(dimension)
product = product.left_multiply(UnitaryDefinitions.sigmax())
assert product.close_to(UnitaryDefinitions.sigmax())
product = product.right_multiply(UnitaryDefinitions.sigmay())
assert product.close_to(UnitaryDefinitions.sigmaz())
product = product.left_multiply(UnitaryDefinitions.sigmaz())
assert product.close_to(identity)
def test_definitions(self) -> None:
dimension = 2
identity_1q = Unitary.identity(dimension)
h = UnitaryDefinitions.h()
assert h.left_multiply(h).close_to(identity_1q)
t = UnitaryDefinitions.t()
assert t.left_multiply(t).left_multiply(t).left_multiply(t).close_to(
UnitaryDefinitions.sigmaz())
dimension = 8
identity_3q = Unitary.identity(dimension)
ccnot = UnitaryDefinitions.ccnot()
assert ccnot.left_multiply(ccnot).close_to(identity_3q)
qecc = UnitaryDefinitions.qecc_phase_flip()
assert qecc.left_multiply(qecc.inverse()).close_to(identity_3q)
def test_rphi(self) -> None:
theta_values = [
0, np.pi / 8, np.pi / 4, np.pi, 3 * np.pi / 2, -np.pi / 4]
for theta in theta_values:
assert UnitaryDefinitions.rphi(theta, 0).close_to(
UnitaryDefinitions.rx(theta))
assert UnitaryDefinitions.rphi(theta, np.pi / 2).close_to(
UnitaryDefinitions.ry(theta))
def test_display_name(self) -> None:
dimension = 2
operation_name = "Rx"
unitary = Unitary(dimension, np.array([
[np.exp(1j * np.pi / 4), 0],
[0, 1j]]), operation_name)
display_name_with_zero_parameters = unitary.get_display_name()
assert isinstance(display_name_with_zero_parameters, str)
assert operation_name == display_name_with_zero_parameters
parameter_name_1 = "abc"
unitary = Unitary(
dimension, unitary.get_matrix(), operation_name,
{parameter_name_1: (1.0, True)})
display_name_with_one_parameter = unitary.get_display_name()
assert isinstance(display_name_with_one_parameter, str)
assert operation_name in display_name_with_one_parameter
parameter_name_2 = "def"
unitary = Unitary(
dimension, unitary.get_matrix(), operation_name,
{parameter_name_1: (1.0, True), parameter_name_2: (2.0, False)})
display_name_with_two_parameters = unitary.get_display_name()
print(display_name_with_two_parameters)
assert isinstance(display_name_with_two_parameters, str)
assert operation_name in display_name_with_two_parameters
assert parameter_name_1 in display_name_with_two_parameters
assert parameter_name_2 in display_name_with_two_parameters
def test_qasm(self) -> None:
dimension = 2
operation_name = "Rcustom"
unitary = Unitary(dimension, np.array([
[np.exp(1j * np.pi / 4), 0],
[0, 1j]]), operation_name)
assert unitary.get_qasm() == operation_name + "\t" + "q[0];"
def test_jaqal(self) -> None:
dimension = 2
operation_name = "Rcustom"
unitary = Unitary(dimension, np.array([
[np.exp(1j * np.pi / 4), 0],
[0, 1j]]), operation_name)
assert unitary.get_jaqal() == operation_name + " q[0] "
def test_gms(self) -> None:
for num_qubits in [3, 4, 5]:
u = UnitaryDefinitions.gms(num_qubits)
assert (u.left_multiply(u).left_multiply(u).left_multiply(u)
.close_to(Unitary.identity(u.get_dimension())))
class TestParameterizedUnitary:
def test_parameterized_rotation(self) -> None:
dimension = 2
def rotation_matrix(
alpha: float,
beta: float,
gamma: float
) -> np.ndarray:
return np.array(
[[np.cos(beta / 2) * np.exp(-1j * (alpha + gamma) / 2),
-np.sin(beta / 2) * np.exp(-1j * (alpha - gamma) / 2)],
[np.sin(beta / 2) * np.exp(1j * (alpha - gamma) / 2),
np.cos(beta / 2) * np.exp(1j * (alpha + gamma) / 2)]])
min_value = 0
max_value = 2 * np.pi
parameters = [ParameterizedUnitaryParameter(
"alpha", min_value, max_value, is_angle=True),
ParameterizedUnitaryParameter(
"beta", min_value, max_value, is_angle=True),
ParameterizedUnitaryParameter(
"gamma", min_value, max_value, is_angle=True)]
operation_name = "R"
rotation = ParameterizedUnitary(
dimension, rotation_matrix, parameters, operation_name)
zero_rotation_unitary = rotation.as_unitary([0, 0, 0])
assert zero_rotation_unitary.close_to(Unitary.identity(dimension))
assert operation_name in zero_rotation_unitary.get_display_name()
assert [
p.get_parameter_name()
in zero_rotation_unitary.get_display_name()
for p in parameters]
random_values = [p.random_value() for p in parameters]
assert np.all([
parameters[i].is_valid(r)
for i, r in enumerate(random_values)])
random_rotation_unitary = rotation.as_unitary(random_values)
assert operation_name in random_rotation_unitary.get_display_name()
assert [
p.get_parameter_name()
in random_rotation_unitary.get_display_name()
for p in parameters]
assert random_rotation_unitary.left_multiply(
random_rotation_unitary.inverse()).close_to(
Unitary.identity(dimension))
def test_parameterized_unitary_classmethods(self) -> None:
rotation_xy = ParameterizedUnitaryDefinitions.rotation_xy()
zero_rotation_unitary = rotation_xy.as_unitary([0, 0])
assert zero_rotation_unitary.close_to(
Unitary.identity(rotation_xy.get_dimension()))
rotation_xyz = ParameterizedUnitaryDefinitions.rotation_xyz()
zero_rotation_unitary = rotation_xyz.as_unitary([0, 0, 0])
assert zero_rotation_unitary.close_to(
Unitary.identity(rotation_xyz.get_dimension()))
xx = ParameterizedUnitaryDefinitions.xx()
xx_angle = 2 * np.pi
full_rotation_unitary = xx.as_unitary([xx_angle])
assert full_rotation_unitary.close_to(
Unitary.identity(xx.get_dimension()))
angle_parameter_name = xx.get_parameters()[0].get_parameter_name()
assert (xx_angle, True) == full_rotation_unitary.get_parameter_value(
angle_parameter_name)
gms = ParameterizedUnitaryDefinitions.gms(num_qubits=2)
gms_angle = np.pi / 3
gms_unitary = gms.as_unitary([gms_angle])
assert gms_unitary.close_to(
ParameterizedUnitaryDefinitions.xx().as_unitary([gms_angle]))
angle_parameter_name = gms.get_parameters()[0].get_parameter_name()
assert (gms_angle, True) == gms_unitary.get_parameter_value(
angle_parameter_name)
def test_parameterized_unitary_time_evolution(self) -> None:
sigmax = np.array([[0, 1], [1, 0]])
t_min = -1.234
t_max = 1.234
time_evolution = ParameterizedUnitaryDefinitions.time_evolution(
sigmax, t_min, t_max)
zero_time_unitary = time_evolution.as_unitary([0])
assert zero_time_unitary.close_to(
Unitary.identity(time_evolution.get_dimension()))
evolution_time = t_max / 2.0
forward_time_unitary = time_evolution.as_unitary([evolution_time])
backward_time_unitary = time_evolution.as_unitary([-evolution_time])
assert forward_time_unitary.close_to(backward_time_unitary.inverse())
time_parameter_name = (time_evolution.get_parameters()[0]
.get_parameter_name())
assert (
(evolution_time, False)
== forward_time_unitary.get_parameter_value(time_parameter_name))
assert (
(-evolution_time, False)
== backward_time_unitary.get_parameter_value(time_parameter_name))
with pytest.raises(Exception):
# switch ordering of t_min and t_max
time_evolution = ParameterizedUnitaryDefinitions.time_evolution(
sigmax, t_max, t_min)
with pytest.raises(Exception):
# time outside valid range
time_evolution.as_unitary([2 * t_max])
class TestUnitarySequenceEntry:
def test_identity(self) -> None:
dimension = 2
entry = UnitarySequenceEntry(Unitary.identity(dimension), [0])
assert entry.get_dimension() == dimension
assert np.array_equal(entry.get_apply_to(), [0])
for system_dimension in [2, 4, 8, 16]:
full_unitary = entry.get_full_unitary(system_dimension)
assert full_unitary.close_to(Unitary.identity(system_dimension))
def test_cnot(self) -> None:
entry = UnitarySequenceEntry(UnitaryDefinitions.cnot(), [0, 1])
with pytest.raises(Exception):
system_dimension = 2
full_unitary = entry.get_full_unitary(system_dimension)
system_dimension = 4
full_unitary = entry.get_full_unitary(system_dimension)
assert full_unitary.close_to(UnitaryDefinitions.cnot())
system_dimension = 8
full_unitary = entry.get_full_unitary(system_dimension)
assert full_unitary.close_to(
UnitaryDefinitions.cnot().tensor(Unitary.identity(2)))
system_dimension = 16
full_unitary = entry.get_full_unitary(system_dimension)
assert full_unitary.close_to(
UnitaryDefinitions.cnot().tensor(Unitary.identity(4)))
assert full_unitary.left_multiply(full_unitary).close_to(
Unitary.identity(system_dimension))
def test_cnot_swapped(self) -> None:
dimension = 4
entry = UnitarySequenceEntry(UnitaryDefinitions.cnot(), [1, 0])
system_dimension = 4
full_unitary = entry.get_full_unitary(system_dimension)
assert full_unitary.close_to(Unitary(dimension, np.array(
[[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 1, 0, 0]])))
class TestUnitarySequence:
def test_default(self) -> None:
dimension = 2
sequence = UnitarySequence(dimension)
assert sequence.get_dimension() == dimension
assert sequence.product().close_to(np.identity(dimension))
def test_identity_roots_correct(self) -> None:
dimension = 2
t = Unitary(dimension, np.array([[1, 0], [0, np.exp(1j * np.pi / 4)]]))
t_entry = UnitarySequenceEntry(t, [0])
sequence = UnitarySequence(dimension, np.repeat(t_entry, 8))
assert sequence.get_dimension() == dimension
assert sequence.get_length() == 8
assert sequence.product().close_to(np.identity(dimension))
def test_identity_roots_incorrect(self) -> None:
dimension = 2
t = Unitary(dimension, np.array([[1, 0], [0, np.exp(1j * np.pi / 4)]]))
t_entry = UnitarySequenceEntry(t, [0])
sequence = UnitarySequence(dimension, np.repeat(t_entry, 7))
assert sequence.get_dimension() == dimension
assert sequence.get_length() == 7
assert not sequence.product().close_to(np.identity(dimension))
def test_append_and_remove(self) -> None:
dimension = 2
identity = Unitary.identity(dimension)
sequence = UnitarySequence(dimension)
assert sequence.get_length() == 0
assert sequence.product().close_to(identity)
sequence.append_first(
UnitarySequenceEntry(UnitaryDefinitions.sigmax(), [0]))
assert sequence.get_length() == 1
assert sequence.product().close_to(UnitaryDefinitions.sigmax())
sequence.append_last(
UnitarySequenceEntry(UnitaryDefinitions.sigmay(), [0]))
assert sequence.get_length() == 2
assert sequence.product().close_to(UnitaryDefinitions.sigmaz())
sequence.append_first(
UnitarySequenceEntry(UnitaryDefinitions.sigmaz(), [0]))
assert sequence.get_length() == 3
assert sequence.product().close_to(identity)
sequence.remove_last()
assert sequence.get_length() == 2
assert sequence.product().close_to(UnitaryDefinitions.sigmay())
sequence.remove_first()
assert sequence.get_length() == 1
assert sequence.product().close_to(UnitaryDefinitions.sigmax())
sequence.remove_first()
assert sequence.get_length() == 0
assert sequence.product().close_to(identity)
def test_undo(self) -> None:
dimension = 2
identity = Unitary.identity(dimension)
sequence = UnitarySequence(dimension)
assert sequence.get_length() == 0
with pytest.raises(Exception):
sequence.undo()
sequence.append_first(
UnitarySequenceEntry(UnitaryDefinitions.sigmax(), [0]))
assert sequence.get_length() == 1
assert sequence.product().close_to(UnitaryDefinitions.sigmax())
sequence.undo()
assert sequence.get_length() == 0
assert sequence.product().close_to(identity)
with pytest.raises(Exception):
sequence.undo()
sequence.append_first(
UnitarySequenceEntry(UnitaryDefinitions.sigmay(), [0]))
sequence.append_first(
UnitarySequenceEntry(UnitaryDefinitions.sigmay(), [0]))
assert sequence.get_length() == 2
assert sequence.product().close_to(identity)
sequence.remove_last()
assert sequence.get_length() == 1
assert sequence.product().close_to(UnitaryDefinitions.sigmay())
sequence.undo()
assert sequence.get_length() == 2
assert sequence.product().close_to(identity)
with pytest.raises(Exception):
sequence.undo()
def test_combine(self) -> None:
dimension = 2
t = Unitary(dimension, np.array([[1, 0], [0, np.exp(1j * np.pi / 4)]]))
t_entry = UnitarySequenceEntry(t, [0])
sequence_1 = UnitarySequence(
dimension, np.repeat(t_entry, 3))
sequence_2 = UnitarySequence(
dimension, [
UnitarySequenceEntry(UnitaryDefinitions.sigmay(), [0])])
combined_sequence = UnitarySequence.combine(sequence_1, sequence_2)
assert (combined_sequence.get_length()
== sequence_1.get_length() + sequence_2.get_length())
assert combined_sequence.product().close_to(
sequence_1.product().left_multiply(sequence_2.product()))
def test_inverse(self) -> None:
dimension = 2
rx_entry = UnitarySequenceEntry(UnitaryDefinitions.rx(np.pi / 3), [0])
ry_entry = UnitarySequenceEntry(UnitaryDefinitions.ry(np.pi / 3), [0])
sequence = UnitarySequence(dimension, [rx_entry, ry_entry])
product = sequence.product()
inverse_sequence = sequence.inverse()
inverse_product = inverse_sequence.product()
assert inverse_product.close_to(product.inverse())
inverse_sequence.sequence_product = None
inverse_product = inverse_sequence.product()
assert inverse_product.close_to(product.inverse())
def test_sequence_output_formats(self) -> None:
dimension = 2
rx_entry = UnitarySequenceEntry(UnitaryDefinitions.rx(np.pi / 3), [0])
ry_entry = UnitarySequenceEntry(UnitaryDefinitions.ry(np.pi / 3), [0])
sequence = UnitarySequence(dimension, [rx_entry, ry_entry])
assert sequence.get_qasm()
assert sequence.get_jaqal()
assert sequence.get_display_output()