5
5
import pytest
6
6
7
7
8
-
9
-
10
8
def test_three_qbit_clean ():
11
9
c = circuit .Circuit ()
12
10
13
11
qubit_names = ["D1" , "A1" , "D2" , "A2" , "D3" ]
14
12
15
13
# clean ancillas have infinite life-time
16
14
for qb in qubit_names :
17
- #set lifetime to only almost inf so that waiting gates are added but ineffective
18
- c .add_qubit (qb , np .inf , 1e10 )
19
-
15
+ # set lifetime to only almost inf so that waiting gates are added but
16
+ # ineffective
17
+ c .add_qubit (qb , np .inf , 1e10 )
18
+
20
19
c .add_hadamard ("A1" , time = 0 )
21
20
c .add_hadamard ("A2" , time = 0 )
22
21
@@ -52,26 +51,25 @@ def test_three_qbit_clean():
52
51
for i in range (100 ):
53
52
c .apply_to (sdm )
54
53
55
-
56
54
assert len (m1 .measurements ) == 100
57
55
assert len (m2 .measurements ) == 100
58
56
59
57
assert sdm .classical == {}
60
58
61
- #in a clean run, we expect just one possible path
59
+ # in a clean run, we expect just one possible path
62
60
assert np .allclose (sdm .trace (), 1 )
63
61
64
- assert m1 .measurements == [1 ]* 100
65
- assert m2 .measurements == [0 , 1 ]* 50
62
+ assert m1 .measurements == [1 ] * 100
63
+ assert m2 .measurements == [0 , 1 ] * 50
64
+
66
65
67
66
def test_noisy_measurement_sampler ():
68
67
c = circuit .Circuit ()
69
68
c .add_qubit ("A" , 0 , 0 )
70
69
71
-
72
70
c .add_hadamard ("A" , 1 )
73
71
74
- sampler = circuit .uniform_noisy_sampler (seed = 42 , readout_error = 0.1 )
72
+ sampler = circuit .uniform_noisy_sampler (seed = 42 , readout_error = 0.1 )
75
73
m1 = c .add_measurement ("A" , time = 2 , sampler = sampler )
76
74
77
75
sdm = sparsedm .SparseDM ("A" )
@@ -82,54 +80,61 @@ def test_noisy_measurement_sampler():
82
80
true_state .append (sdm .classical ['A' ])
83
81
84
82
# these samples assume a certain seed (=42)
85
- assert m1 .measurements == [0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 1 ]
83
+ assert m1 .measurements == [0 , 1 , 0 , 0 , 1 , 0 ,
84
+ 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 1 ]
86
85
assert true_state != m1 .measurements
87
- assert true_state == [0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 ]
86
+ assert true_state == [0 , 1 , 0 , 0 , 1 , 0 , 1 ,
87
+ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 ]
88
88
89
89
# we have two measurement errors
90
90
mprob = 0.9 ** 18 * 0.1 ** 2
91
91
assert np .allclose (sdm .classical_probability , mprob )
92
-
93
92
# and each measurement has outcome 1/2
94
93
totprob = mprob * 0.5 ** 20
95
94
assert np .allclose (sdm .trace (), totprob )
96
95
96
+
97
97
def test_measurement_with_output_bit ():
98
98
c = circuit .Circuit ()
99
99
c .add_qubit ("A" )
100
100
101
101
c .add_qubit ("O" )
102
102
c .add_qubit ("O2" )
103
103
104
- c .add_rotate_y ("A" , time = 0 , angle = np .pi / 2 )
104
+ c .add_rotate_y ("A" , time = 0 , angle = np .pi / 2 )
105
105
106
106
sampler = circuit .selection_sampler (1 )
107
107
c .add_measurement ("A" , time = 1 , sampler = sampler , output_bit = "O" )
108
108
109
- c .add_rotate_y ("A" , time = 3.5 , angle = np .pi / 2 )
109
+ c .add_rotate_y ("A" , time = 3.5 , angle = np .pi / 2 )
110
110
111
111
sampler = circuit .selection_sampler (1 )
112
112
c .add_measurement ("A" , time = 4 , sampler = sampler , output_bit = "O2" )
113
113
114
- c .add_rotate_y ("A" , time = 5 , angle = np .pi / 2 )
114
+ c .add_rotate_y ("A" , time = 5 , angle = np .pi / 2 )
115
115
c .order ()
116
116
117
117
sdm = sparsedm .SparseDM (c .get_qubit_names ())
118
118
119
119
assert sdm .classical ['O' ] == 0
120
120
assert sdm .classical ['O2' ] == 0
121
121
122
-
123
122
c .apply_to (sdm )
124
123
125
-
126
124
assert np .allclose (sdm .trace (), 0.25 )
127
125
128
- assert sdm .classical == {'O' : 1 , 'O2' :1 }
126
+ assert sdm .classical == {'O' : 1 , 'O2' : 1 }
127
+
129
128
130
129
@pytest .mark .skip ()
131
130
def test_integration_surface17 ():
132
- def make_circuit (t1 = np .inf , t2 = np .inf , seed = 42 , readout_error = 0.015 , t_gate = 40 , t_rest = 1000 ):
131
+ def make_circuit (
132
+ t1 = np .inf ,
133
+ t2 = np .inf ,
134
+ seed = 42 ,
135
+ readout_error = 0.015 ,
136
+ t_gate = 40 ,
137
+ t_rest = 1000 ):
133
138
surf17 = circuit .Circuit ("Surface 17" )
134
139
135
140
t_rest += t_gate # nominal rest time is between two gates
@@ -173,7 +178,8 @@ def add_x(c, x_anc, d_bits, t=0, t_gate=t_gate):
173
178
surf17 .add_hadamard (b , time = 4 * t_gate + t_rest + 5 * t_gate )
174
179
175
180
for b in z_bits :
176
- surf17 .add_measurement (b , time = 10 * t_gate + t_rest , sampler = sampler )
181
+ surf17 .add_measurement (
182
+ b , time = 10 * t_gate + t_rest , sampler = sampler )
177
183
178
184
for b in x_bits :
179
185
surf17 .add_measurement (b , time = 6 * t_gate , sampler = sampler )
@@ -190,13 +196,12 @@ def syndrome_to_byte(syndrome):
190
196
byte = 0
191
197
192
198
for i in range (4 ):
193
- byte += syndrome ["X%d" % i ] << (i + 4 )
199
+ byte += syndrome ["X%d" % i ] << (i + 4 )
194
200
for i in range (4 ):
195
- byte += syndrome ["Z%d" % i ] << i
201
+ byte += syndrome ["Z%d" % i ] << i
196
202
197
203
return byte
198
204
199
-
200
205
seed = 890793515
201
206
202
207
t1 = 25000.0
@@ -210,12 +215,10 @@ def syndrome_to_byte(syndrome):
210
215
c = make_circuit (t1 = t1 , t2 = t2 , seed = seed ,
211
216
readout_error = ro_error , t_gate = t_gate , t_rest = t_rest )
212
217
213
-
214
218
sdm = sparsedm .SparseDM (c .get_qubit_names ())
215
- for b in ["D%d" % i for i in range (9 )]:
219
+ for b in ["D%d" % i for i in range (9 )]:
216
220
sdm .ensure_dense (b )
217
221
218
-
219
222
syndromes = []
220
223
for _ in range (rounds ):
221
224
c .apply_to (sdm )
@@ -228,9 +231,11 @@ def syndrome_to_byte(syndrome):
228
231
229
232
assert syndrome == b'jHhJhL\x08 L\t K)K\x08 K\x08 K\x08 K\x08 I'
230
233
234
+
231
235
def test_free_decay ():
232
236
233
- for t1 , t2 in [(np .inf , np .inf ), (1000 , 2000 ), (np .inf , 1000 ), (1000 , 1000 )]:
237
+ for t1 , t2 in [(np .inf , np .inf ), (1000 , 2000 ),
238
+ (np .inf , 1000 ), (1000 , 1000 )]:
234
239
c = circuit .Circuit ("Free decay" )
235
240
c .add_qubit ("Q" , t1 = t1 , t2 = t2 )
236
241
c .add_rotate_y ("Q" , time = 0 , angle = np .pi )
@@ -242,24 +247,25 @@ def test_free_decay():
242
247
c .apply_to (sdm )
243
248
sdm .project_measurement ("Q" , 0 )
244
249
245
- assert np .allclose (sdm .trace (), np .exp (- 1000 / t1 ))
250
+ assert np .allclose (sdm .trace (), np .exp (- 1000 / t1 ))
251
+
246
252
247
253
def test_ramsey ():
248
254
249
- for t1 , t2 in [(np .inf , np .inf ), (1000 , 2000 ), (np .inf , 1000 ), (1000 , 1000 )]:
255
+ for t1 , t2 in [(np .inf , np .inf ), (1000 , 2000 ),
256
+ (np .inf , 1000 ), (1000 , 1000 )]:
250
257
c = circuit .Circuit ("Ramsey" )
251
258
c .add_qubit ("Q" , t1 = t1 , t2 = t2 )
252
- c .add_rotate_y ("Q" , time = 0 , angle = np .pi / 2 )
253
- c .add_rotate_y ("Q" , time = 1000 , angle = - np .pi / 2 )
259
+ c .add_rotate_y ("Q" , time = 0 , angle = np .pi / 2 )
260
+ c .add_rotate_y ("Q" , time = 1000 , angle = - np .pi / 2 )
254
261
c .add_waiting_gates ()
255
262
c .order ()
256
263
257
264
sdm = sparsedm .SparseDM (c .get_qubit_names ())
258
265
c .apply_to (sdm )
259
266
sdm .project_measurement ("Q" , 0 )
260
267
261
- assert np .allclose (sdm .trace (), 0.5 * (1 + np .exp (- 1000 / t2 )))
262
-
268
+ assert np .allclose (sdm .trace (), 0.5 * (1 + np .exp (- 1000 / t2 )))
263
269
264
270
265
271
def test_two_qubit_tpcp ():
@@ -271,17 +277,19 @@ def test_two_qubit_tpcp():
271
277
c .add_gate ("rotate_y" , "B" , angle = 0.2 , time = 0 )
272
278
c .add_gate ("rotate_z" , "A" , angle = 0.1 , time = 1 )
273
279
c .add_gate ("rotate_x" , "B" , angle = 0.3 , time = 1 )
274
- c .add_gate ("cphase" ,"A" , "B" , time = 2 )
280
+ c .add_gate ("cphase" , "A" , "B" , time = 2 )
275
281
276
282
sdm = sparsedm .SparseDM (c .get_qubit_names ())
277
283
for i in range (100 ):
278
284
c .apply_to (sdm )
279
285
x = sdm .full_dm .get_diag ()
280
- assert np .allclose (x .sum (), 1 ) # trace preserved
281
- assert np .all (x > 0 ) # probabilities greater than zero
282
-
283
- assert np .allclose (np .linalg .eigvalsh (sdm .full_dm .to_array ()), [0 , 0 , 0 , 1 ])
286
+ assert np .allclose (x .sum (), 1 ) # trace preserved
287
+ assert np .all (x > 0 ) # probabilities greater than zero
284
288
289
+ assert np .allclose (
290
+ np .linalg .eigvalsh (
291
+ sdm .full_dm .to_array ()), [
292
+ 0 , 0 , 0 , 1 ])
285
293
286
294
287
295
def test_cphase_rotation ():
@@ -293,9 +301,8 @@ def test_cphase_rotation():
293
301
c .add_gate ("rotate_y" , "A" , angle = 1.2 , time = 0 )
294
302
c .add_gate ("rotate_y" , "B" , angle = 1.2 , time = 0 )
295
303
296
-
297
- for t in [1 ,2 ,3 ,4 ,5 ]:
298
- g = circuit .CPhaseRotation ("A" , "B" , 2 * np .pi / 5 , t )
304
+ for t in [1 , 2 , 3 , 4 , 5 ]:
305
+ g = circuit .CPhaseRotation ("A" , "B" , 2 * np .pi / 5 , t )
299
306
c .add_gate (g )
300
307
301
308
c .add_gate ("rotate_y" , "A" , angle = - 1.2 , time = 6 )
@@ -305,15 +312,38 @@ def test_cphase_rotation():
305
312
306
313
sdm = sparsedm .SparseDM (c .get_qubit_names ())
307
314
315
+ c .apply_to (sdm )
308
316
317
+ d = sdm .full_dm .get_diag ()
309
318
310
- c . apply_to ( sdm )
319
+ assert np . allclose ( d , [ 1 , 0 , 0 , 0 ] )
311
320
312
321
322
+ def test_euler_rotation ():
323
+ c = circuit .Circuit ("test" )
324
+ c .add_qubit ("A" )
313
325
314
- d = sdm .full_dm .get_diag ()
326
+ theta = 0.3
327
+ lamda = 0.7
328
+ phi = 4.2
315
329
316
- assert np .allclose (d , [1 , 0 , 0 , 0 ])
330
+ g = circuit .RotateEuler (bit = "A" , time = 0 , theta = theta , lamda = lamda , phi = phi )
331
+ gconj = circuit .RotateEuler (
332
+ bit = "A" ,
333
+ time = 10 ,
334
+ theta = - theta ,
335
+ lamda = - phi ,
336
+ phi = - lamda )
337
+
338
+ c .add_gate (g )
339
+ c .add_gate (gconj )
340
+
341
+ c .order ()
317
342
343
+ sdm = sparsedm .SparseDM (c .get_qubit_names ())
344
+
345
+ c .apply_to (sdm )
318
346
347
+ d = sdm .full_dm .get_diag ()
319
348
349
+ assert np .allclose (d , [1 , 0 ])
0 commit comments