-
Notifications
You must be signed in to change notification settings - Fork 6
/
testVQSD.py
521 lines (411 loc) · 15.2 KB
/
testVQSD.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
"""testVQSD.py
Test cases for VQSD class.
"""
# =============================================================================
# imports
# =============================================================================
import time
import cirq
import numpy as np
from VQSD import VQSD, min_to_vqsd, vqsd_to_min, symbol_list
# =============================================================================
# constants
# =============================================================================
SEP_STRING = "="
CHAR_LEN = 80
DOT_LEN = 50
DOT = "."
# =============================================================================
# helper functions
# =============================================================================
def print_sep(title, char_len=CHAR_LEN, sep_string=SEP_STRING):
"""Prints separation strings."""
print("".center(char_len, sep_string))
print(title)
print("".center(char_len, sep_string))
# =============================================================================
# unit tests
# =============================================================================
def test_num_qubits(num_tests=100, maxn=30):
"""Tests the getter method VQSD.get_num_qubits(). If this is failing
something's gone horribly wrong.
"""
for _ in range(num_tests):
n = np.random.randint(1, maxn)
circ = VQSD(n)
assert circ.get_num_qubits() == n
print("\ntest_num_qubits()".ljust(DOT_LEN, DOT), "passed!", sep="")
def test_angle_format_conversions(maxn=5):
"""Tests min_to_vqsd and vqsd_to_min functions in runVQSD."""
for n in range(2, maxn + 1):
params = np.array((n // 2)
* [[[.1, .2, .3], [1, .2, .3],
[.3, .2, 1], [.4, .2, .1]]]
)
min_params = vqsd_to_min(params)
assert (vqsd_to_min(min_to_vqsd(min_params, n)) == min_params).all()
print("test_angle_format_conversions()".ljust(DOT_LEN, DOT),
"passed!",
sep="")
def test_number_of_params(nqubits=2, nlayers=1):
"""Tests that the number of parameters is correct for a diagonalizing
unitary with a given number of qubits and layers.
Args:
nqubits [type: int]
number of qubits in state
nlayers [type: int]
number of layers in diagonalizing unitary
"""
pass
def test_two_qubit_state_identity(repetitions=1000, verbose=False):
"""Tests the minimum distance is acheived for a circuit that
does nothing."""
# number of qubits and layers in the unitary
n = 2
num_layers = 1
# state preperation angles
prep_angles = [[0, 0], [0, 0], [0, 0]]
post_angles = []
# symbols for a single layer in the diagonalizing unitary
params = min_to_vqsd(symbol_list(n, num_layers))
shifted_params = params
# =========================================================================
# get a VQSD circ
# =========================================================================
# make a VQSD circuit on n qubits
circ = VQSD(n)
# add the state preperation
circ.state_prep(prep_angles, post_angles, copy=0)
circ.state_prep(prep_angles, post_angles, copy=1)
# add the unitary
for _ in range(num_layers):
circ.layer(params, shifted_params, copy=0)
circ.layer(params, shifted_params, copy=1)
# add the dip test
circ.dip_test()
# print the algorithm with symbols
if verbose:
print("Structure of Circuit:\n", circ.algorithm())
print("\n")
# print the algoirthm with resolved angles
test_angles = [0.0] * circ.num_angles_required_for_unitary()
if verbose:
print("Circuit with instantiated parameters:\n",
circ.resolved_algorithm(test_angles))
print("\n")
# get the objective and time how long it takes
start = time.time()
distance = circ.obj_dip_resolved(test_angles, repetitions=repetitions)
runtime = time.time() - start
# print the objective and the timing info
if verbose:
print("Now running the circuit with instantiated parameters...")
print("Total runtime = {} seconds".format(runtime))
print("HS distance between the two states = ", distance)
# make sure the overlap is close to one
tolerance = 1.0 / repetitions
assert distance < tolerance
# print success message
print("test_two_qubit_state_identity()".ljust(DOT_LEN, DOT),
"passed!",
sep="")
def test_two_qubit_product_state_identity(
half_turn=0.1, repetitions=1000,
verbose=False):
"""Tests the VQSD algorithm for two qubit pure product states.
State preperation is single qubit rotations.
Diagonalizing unitary is the inverse of state prep.
Tests to see if overlap is maximal.
"""
# get a VQSD circuit
circ = VQSD(2)
# define the rotations
rot = cirq.RotXGate(half_turns=half_turn)
rotdag = cirq.RotXGate(half_turns=-half_turn)
# add the state preperation
circ.state_prep_circ.append(
[rot(circ.qubits[x]) for x in [0, 1, 4, 5]]
)
# add the unitary
circ.unitary_circ.append(
[rotdag(circ.qubits[x]) for x in [0, 1, 4, 5]]
)
# add the dip test circuit
circ.dip_test()
# verbose output
if verbose:
print("The total circuit is", circ.algorithm(), sep="\n")
# get the HS distance
distance = circ.obj_dip(repetitions=repetitions)
# make sure we're close
tolerance = 1 / repetitions
assert distance < tolerance
# print success message
print("test_two_qubit_product_state_identity()".ljust(DOT_LEN, DOT),
"passed!",
sep="")
def test_n_qubit_product_state_identity(
n=3, rot_type='x',
half_turn=0.1, verbose=False):
"""Tests the VQSD algorithm for n qubit pure product states."""
# TODO: implement!
pass
def test_dip_test_n_qubits_identity(n=2, repetitions=1000, verbose=False):
"""Runs the DIP Test only and tests overlap is maximal."""
circ = VQSD(n)
circ.dip_test()
if verbose:
print(circ.algorithm())
obj = circ.obj_dip(repetitions=repetitions)
if verbose:
print("objective = ", obj)
tolerance = 1 / repetitions
assert obj < tolerance
def test_dip_test_identity_loop(maxn=10, repetitions=1000, verbose=False):
"""Loops over numbers of qubits and tests that the DIP Test acheives
maximal overlap for a circuit with only the DIP Test circuit.
"""
# loop through circuit sizes and test the dip test
for n in range(2, maxn):
test_dip_test_n_qubits_identity(
n=n, repetitions=repetitions, verbose=verbose
)
# print success message
print("test_dip_test_identity_loop()".ljust(DOT_LEN, DOT),
"passed!",
sep="")
def test_purity_pure_state(repetitions=10000, verbose=False):
"""Forms a VQSD circuit diagonalizing a pure state and asserts the purity
is one.
"""
# number of qubits
n = 2
# state preperation angles
prep_angles = [[0, 0], [0, 0], [0, 0]]
post_angles = []
# make a VQSD circuit on n qubits
circ = VQSD(n)
# add the state preperation
circ.state_prep(prep_angles, post_angles, copy=0)
circ.state_prep(prep_angles, post_angles, copy=1)
# add the dip test
circ.dip_test()
# print the algorithm with symbols
if verbose:
print("Structure of Circuit:\n", circ.algorithm())
print("\n")
# compute the purity of the state
circ.compute_purity(repetitions=10000)
# print the purity
if verbose:
print("the purity of the state is:", circ.purity)
tolerance = 1 / repetitions
assert abs(circ.purity - 1) < tolerance
# print success message
print("test_purity_pure_state()".ljust(DOT_LEN, DOT), "passed!", sep="")
def test_state_overlap_visual(num_qubits=4):
"""Computes the state overlap circuit and prints it to the console for
visual verification of correctness.
"""
print(VQSD(num_qubits).state_overlap())
def test_state_overlap_basic(n=2, nreps=1000, random=False, verbose=False):
"""Basic test for state overlap.
Checks that the purity of a pure state is one and the purity of a
mixed state is less than one.
"""
# state preperation angles
if random:
prep_angles = [np.random.rand(n)] * 3
else:
prep_angles = [np.zeros(n)] * 3
post_angles = []
# make a VQSD circuit on n qubits
circ = VQSD(n)
# add the state preperation
circ.state_prep(prep_angles, post_angles, copy=0)
circ.state_prep(prep_angles, post_angles, copy=1)
# get rid of the CNOTS in the state preperation to test pure states
circ.state_prep_circ = circ.state_prep_circ[: -2]
# add the state overlap circuit
circ.dip_test_circ = circ.state_overlap()
# print the circuit if verbose output is desired
if verbose:
print("The algorithm is:", circ.algorithm(), sep="\n\n")
# get the circuit output bit strings
out = circ.run(repetitions=nreps)
vals = out.measurements[circ._measure_key]
# do the postprocessing
purity = circ.state_overlap_postprocessing(vals)
if verbose:
print("purity = ", purity)
# check that the purity is unity
assert abs(purity - 1) < 1 / nreps
def test_state_overlap_basic_loop(maxn=8, nreps=1000,
random=False, verbose=False):
"""Runs test_state_overlap_basic for many circuit sizes."""
for n in range(2, maxn):
test_state_overlap_basic(n=n, nreps=nreps,
random=random, verbose=verbose)
# print success message
print("test_state_overlap_basic_loop()".ljust(DOT_LEN, DOT),
"passed!", sep="")
def test_purity_analytic(verbose=False):
"""Tests that the computed purity for a state whose analytic purity
we know is correct.
"""
# number of qubits
n = 2
# prep angles in circuit
pangles = np.array([
[0, 0],
[0.5, 0.5],
[0, 0]
])
# make a VQSD circuit on n qubits
circ = VQSD(n)
# add the state preperation
circ.state_prep(pangles, None, copy=0)
circ.state_prep(pangles, None, copy=1)
# verbose output
if verbose:
# print the algorithm with symbols
print("Structure of Circuit:\n", circ.algorithm())
print("\n")
# compute the purity of the state
circ.compute_purity(repetitions=100000)
print("the purity of the state is:", circ.purity)
# make sure it's close to the actual value
def test_pdip_visual(indices, verbose=True):
"""Visual test for PDIP Test circuit."""
circ = VQSD(4)
circ.pdip_test(indices)
if verbose:
print("Circuit for PDIP Test with indices", indices)
print(circ.dip_test_circ)
# print success message
print("test_pdip_visual()".ljust(DOT_LEN, DOT),
"passed!", sep="")
def test_get_mask_for_all_zero_outcome(n=4, verbose=False):
"""Tests that VQSD._get_mask_for_all_zero_outcome(...) returns the correct
mask.
"""
circ = VQSD(n)
# do the pdip test with the zeroth qubit
circ.pdip_test([0])
# verbose option
if verbose:
print(circ.algorithm())
# simulate and measure
sim = cirq.google.XmonSimulator()
res = sim.run(circ.algorithm(), repetitions=100)
# split the measurements
dipm = res.measurements["z"]
pdipm = res.measurements["p"]
assert not np.any(dipm)
mask = circ._get_mask_for_all_zero_outcome(dipm)
assert np.all(mask)
assert np.all(pdipm[mask] == pdipm)
# print success message
print("test_get_mask_for_all_zero_outcome()".ljust(DOT_LEN, DOT),
"passed!", sep="")
def test_get_mask_for_all_zero_outcome2(n=4, verbose=False):
"""Tests that VQSD._get_mask_for_all_zero_outcome(...) returns the correct
mask.
"""
circ = VQSD(n)
# flip the zeroth qubit
circ.state_prep_circ.append(cirq.H(circ.qubits[0]))
# do the pdip test
circ.pdip_test([0])
# verbose option
if verbose:
print(circ.algorithm())
# simulate and measure
sim = cirq.google.XmonSimulator()
res = sim.run(circ.algorithm(), repetitions=100)
# split the measurements
dipm = res.measurements["z"]
pdipm = res.measurements["p"]
mask = circ._get_mask_for_all_zero_outcome(dipm)
assert len(mask) == len(dipm)
numel = len(np.where(mask == True))
assert len(pdipm[mask] == numel)
assert len(pdipm[mask]) < len(pdipm)
# print success message
print("test_get_mask_for_all_zero_outcome2()".ljust(DOT_LEN, DOT),
"passed!", sep="")
def test_overlap_pdip(n=2):
"""Tests that the overlap for a pure state
as computed by the PDIP Test is 1."""
circ = VQSD(n)
assert np.isclose(circ.overlap_pdip(), 1.0)
# print success message
print("test_overlap_pdip()".ljust(DOT_LEN, DOT),
"passed!", sep="")
def test_obj_pdip(n=2):
"""Tests that the cost for |0><0| as computed by the PDIP Test is 0."""
circ = VQSD(n)
circ.compute_purity()
assert np.isclose(circ.obj_pdip(), 0.0)
# print success message
print("test_obj_pdip()".ljust(DOT_LEN, DOT),
"passed!", sep="")
def test_overlap_pdip2(n=2):
"""Tests that the overlap for the |++> state."""
circ = VQSD(n)
circ.state_prep_circ.append(
cirq.H.on_each(circ.qubits[:n] + circ.qubits[2 * n : 3 * n])
)
circ.compute_purity()
print("overlap =", circ.overlap_pdip())
assert abs(circ.overlap_pdip() - 0.5) < 5e-2
# print success message
print("test_overlap_pdip()".ljust(DOT_LEN, DOT),
"passed!", sep="")
def test_obj_pdip_diagonal():
"""Tests the objective as computed by the PDIP Test is zero for a
diagonal state on four qubits."""
circ = VQSD(4)
circ.state_prep_circ.append(
cirq.X.on_each(circ.qubits[:4] + circ.qubits[8:12])
)
print(circ.algorithm())
circ.compute_purity()
assert np.isclose(circ.obj_pdip(), 0.0)
# print success message
print("test_obj_pdip_diagonal()".ljust(DOT_LEN, DOT),
"passed!", sep="")
def test_obj_pdip_diagonal2():
"""Tests the objective as computed by the PDIP Test is zero for a
diagonal state on four qubits."""
circ = VQSD(4)
print(circ.algorithm())
circ.compute_purity()
assert np.isclose(circ.obj_pdip(), 0.0)
# print success message
print("test_obj_pdip_diagonal2()".ljust(DOT_LEN, DOT),
"passed!", sep="")
# =============================================================================
# run the tests
# =============================================================================
if __name__ == "__main__":
# TODO: grab input arguments for options (e.g., verbose, what tests to
# run, etc.)
print_sep("now testing VQSD")
test_num_qubits()
test_angle_format_conversions()
test_two_qubit_state_identity(repetitions=100)
test_two_qubit_product_state_identity()
test_dip_test_identity_loop(maxn=8)
test_purity_pure_state()
test_state_overlap_basic_loop()
test_pdip_visual([0], verbose=True)
test_get_mask_for_all_zero_outcome()
test_get_mask_for_all_zero_outcome2()
test_overlap_pdip()
test_obj_pdip()
test_overlap_pdip2()
test_obj_pdip_diagonal()
test_obj_pdip_diagonal2()
print("\n")
print_sep("all tests for VQSD passed!")