/
vqls_test.py
643 lines (519 loc) · 21.1 KB
/
vqls_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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
"""Unit tests for VQLS using effective Hamiltonian."""
from itertools import product
from math import pi
import numpy as np
from pyquil import get_qc
import vqls
def test_pauli_matrices():
"""Simple checks for Pauli matrices."""
assert np.array_equal(vqls.matrix([1], ["Y"]), vqls.ymat)
assert np.array_equal(vqls.vector([1], ["Y"]), vqls.ymat[:, 0])
def test_single_pauli_mult():
"""Tests single qubit Pauli multiplication."""
paulis = ("I", "X", "Y", "Z")
# Check multiplication with identity and squared terms
for p in paulis:
assert vqls.mult("I", p) == (1., p)
assert vqls.mult(p, "I") == (1., p)
assert vqls.mult(p, p) == (1., "I")
# Check "cross terms"
assert vqls.mult("X", "Y") == (1j, "Z")
assert vqls.mult("Y", "X") == (-1j, "Z")
assert vqls.mult("X", "Z") == (-1j, "Y")
assert vqls.mult("Z", "X") == (1j, "Y")
assert vqls.mult("Y", "Z") == (1j, "X")
assert vqls.mult("Z", "Y") == (-1j, "X")
def test_pauli_mult():
"""Tests n-qubit Pauli multiplication."""
assert vqls.multn("IXY", "ZYY") == (1j, "ZZI")
assert vqls.multn("IIIIII", "XYZXYZ") == (1., "XYZXYZ")
assert vqls.multn("YY", "ZI") == (1j, "XY")
assert vqls.multn("XZ", "ZX") == (1 + 0j, "YY")
def Pbexpansion_test_helper(Bcoeffs, Bterms, verbose=False):
"""Tests matrix equality for Pauli expansion of Pb = B|0><0|B^dagger."""
Pbcoeffs, Pbpaulis = vqls.Pb_expansion(Bcoeffs, Bterms)
if verbose:
print("Found Pauli expansion of Pb:")
print(Pbcoeffs)
print(Pbpaulis)
# Determine the bvector
bvec = vqls.vector(Bcoeffs, Bterms)
if verbose:
print("\nbvec computed from Bcoeffs and Bterms:")
print(bvec)
Pbmat = vqls.matrix(Pbcoeffs, Pbpaulis)
if verbose:
print("\nComputed Pb:")
print(Pbmat)
print("\nActual Pb = |b><b|:")
print(np.outer(bvec, bvec.conj()))
return np.array_equal(Pbmat, np.outer(bvec, bvec.conj()))
def test_Pbexpansion_single_qubit_single_term():
"""Test Pb expansion with one-term, single qubit B matrices."""
assert Pbexpansion_test_helper([1], ["I"])
assert Pbexpansion_test_helper([1], ["X"])
assert Pbexpansion_test_helper([1], ["Y"])
assert Pbexpansion_test_helper([1], ["Z"])
def test_Pbexpansion_single_qubit_multiple_terms():
"""Test Pb expansion with multiple terms, single qubit B matrices."""
assert Pbexpansion_test_helper([1, 2], ["I", "X"])
assert Pbexpansion_test_helper([1, 1, 3], ["I", "X", "Y"])
def test_Pbexpansion_multi_qubit_multi_term():
"""Tests Pb expansion with multiple qubit, multiple term B matrices."""
assert Pbexpansion_test_helper([1, 1], ["XZ", "XZ"])
assert Pbexpansion_test_helper([2, 3, 1], ["IIZ", "ZXY", "YYX"])
def test_simplify_hamiltonain():
"""Tests combining Paulis and dropping zero terms to simplify Hamiltonians."""
# Test 1
ham = [(1j, "XY"), (-1j, "XY")]
assert vqls.drop_zero(vqls.combine_paulis(ham)) == []
# Test 2
ham = [(1.0, 'II'), (1j, 'XY'), (-1j, 'XY'), (1.0, 'II')]
assert vqls.drop_zero(vqls.combine_paulis(ham)) == [[2.0, "II"]]
# Test 3
ham = [[2.0, 'II'], [-0.25, 'II'], [-0.25, 'IZ'], [-0.25, 'ZI'],
[-0.25, 'ZZ'], [-0.25j, 'XY'], [-0.25, 'XX'], [(0.25+0j), 'YY'],
[-0.25j, 'YX'], [0.25j, 'XY'], [(-0.25+0j), 'XX'], [(0.25+0j), 'YY'],
[0.25j, 'YX'], [-0.25, 'II'], [(0.25+0j), 'IZ'], [(0.25+0j), 'ZI'],
[(-0.25+0j), 'ZZ']]
assert vqls.drop_zero(vqls.combine_paulis(ham)) == [[1.5, 'II'], [(-0.5+0j), 'ZZ'], [(-0.5+0j), 'XX'], [(0.5+0j), 'YY']]
def effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct,
verbose = False):
"""Checks matrix equality for the effective Hamiltonian expansion.
Ensures minimum evalue of effective Hamiltonian is 0 and
corresponding evector is the solution to the linear system."""
# Get the effective Hamiltonian
ham = vqls.effective_hamiltonian(Acoeffs, Aterms, Bcoeffs, Bterms)
# Get the matrix and vector of the linear system, and solve it
Amat = vqls.matrix(Acoeffs, Aterms)
assert np.array_equal(Amat, Amat_correct)
bvec = vqls.vector(Bcoeffs, Bterms)
if verbose:
print("Computed bvec:")
print(bvec)
assert np.array_equal(bvec, bvec_correct)
xvec = np.linalg.solve(Amat, bvec)
xvec /= np.linalg.norm(xvec)
if verbose:
print("\nEffective Hamiltonian expansion:")
print(ham)
# Get the matrix of the Hamiltonian
Heff = vqls.matrix_of_hamiltonian(ham)
# Check correctness of the effective Hamiltonian
Hexact = (Amat_correct.conj().T @
(np.identity(len(Amat)) - np.outer(bvec_correct, bvec_correct.conj())) @
Amat_correct)
if verbose:
print("\nExact Heff:")
print(Hexact)
print("\nComputed Heff:")
print(Heff)
assert np.allclose(Heff, Hexact)
assert np.allclose(Heff, Heff.conj().T)
if verbose:
print("Heff @ xvec:")
print(Heff @ xvec)
print("||Heff @ xvec|| = ", np.linalg.norm(Heff @ xvec))
assert np.isclose(np.linalg.norm(Heff @ xvec), 0.0, atol=1e-5)
# Do the diagonalization
evals, evecs = np.linalg.eigh(Heff)
if verbose:
print("Min eval:")
print(evals[0])
print("Evec of minimum eval:")
print(evecs[:, 0])
print("Solution of linear system:")
print(xvec)
assert np.isclose(evals[0], 0.0, atol=1e-5)
assert np.isclose(abs(np.dot(evecs[:, 0], xvec.conj().T))**2, 1.0)
# =======================
# Tests with A = Identity
# =======================
def test_effective_hamiltonian_iden1():
Bcoeffs = [1 / 2] * 4
Bterms = ["II", "IX", "XI", "XX"]
bvec_correct = np.ones(4) / 2
Acoeffs = [1]
Aterms = ["II"]
Amat_correct = np.identity(4)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
def test_effective_hamiltonian_iden2():
Bcoeffs = [1 / 4] * 4
Bterms = ["II", "IZ", "ZI", "ZZ"]
bvec_correct = np.array([1, 0, 0, 0])
Acoeffs = [1]
Aterms = ["II"]
Amat_correct = np.identity(4)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
def test_effective_hamiltonian_iden3():
Bcoeffs = [1 / np.sqrt(2), 1 / np.sqrt(2)]
Bterms = ["II", "XY"]
bvec_correct = vqls.vector(Bcoeffs, Bterms)
Acoeffs = [1]
Aterms = ["II"]
Amat_correct = np.identity(4)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
def test_effective_hamiltonian_iden4():
Bcoeffs = [1 / np.sqrt(2), 1 / np.sqrt(2)]
Bterms = ["III", "XYZ"]
bvec_correct = vqls.vector(Bcoeffs, Bterms)
Acoeffs = [1]
Aterms = ["III"]
Amat_correct = np.identity(8)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
# ========================
# Tests with A != Identity
# ========================
def test_effective_hamiltonian_noniden1():
Bcoeffs = [1 / 2] * 4
Bterms = ["II", "IX", "XI", "XX"]
bvec_correct = np.ones(4) / 2
Acoeffs = [1]
Aterms = ["IZ"]
Amat_correct = vqls.matrix(Acoeffs, Aterms)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
def test_effective_hamiltonian_noniden2():
Bcoeffs = [1]
Bterms = ["II"]
bvec_correct = vqls.vector(Bcoeffs, Bterms)
Acoeffs = [1, 1]
Aterms = ["IZ", "XX"]
Amat_correct = vqls.matrix(Acoeffs, Aterms)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
def test_effective_hamiltonian_noniden3():
Bcoeffs = [1]
Bterms = ["ZY"]
bvec_correct = vqls.vector(Bcoeffs, Bterms)
Acoeffs = [1, 1]
Aterms = ["IZ", "XX"]
Amat_correct = vqls.matrix(Acoeffs, Aterms)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
# ===========================
# Test example linear systems
# ===========================
def test_effective_hamiltonian_exampleLS1():
"""Three qubit linear system example from VQLS paper."""
Bcoeffs = [1 / 2**(3/2)] * 8
Bterms = ["XXX", "XXZ", "XZX", "XZZ", "ZXX", "ZXZ", "ZZX", "ZZZ"]
bvec_correct = vqls.vector(Bcoeffs, Bterms)
Acoeffs = [1, 0.2, 0.2]
Aterms = ["III", "XII", "XZI"]
Amat_correct = vqls.matrix(Acoeffs, Aterms)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
def test_effective_hamiltonian_exampleLS2():
"""Five qubit linear system example from VQLS paper."""
Bcoeffs = [1 / 2**(5/2)] * 32
paulis = [["X", "Z"]] * 5
prods = list(product(*paulis))
Bterms = ["".join(p) for p in prods]
bvec_correct = vqls.vector(Bcoeffs, Bterms)
Acoeffs = [1, 0.2, 0.2]
Aterms = ["IIIII", "XIIII", "XZIII"]
Amat_correct = vqls.matrix(Acoeffs, Aterms)
effective_hamiltonian_test_helper(Acoeffs, Aterms, Amat_correct,
Bcoeffs, Bterms, bvec_correct)
# =================================
# Test computing expectation values
# =================================
def test_expectation_three_qubits():
"""Tests several expectation values for a three qubit circuit."""
n = 3
qcomputer = f"Aspen-7-{n}Q-B"
lattice = get_qc(qcomputer, as_qvm=True)
circ, creg = vqls.yansatz(lattice)
SHOTS = 10000
tol = 1e-1 # Set smaller than necessary to be safe
assert np.isclose(vqls.expectation([0] * n, 1, "ZZZ", circ, creg,
lattice, shots=SHOTS), 1.0, atol=tol)
assert np.isclose(vqls.expectation([0] * n, 0.8675309, "ZZZ", circ, creg,
lattice, shots=SHOTS), 0.8675309, atol=tol)
assert np.isclose(vqls.expectation([0] * n, 1, "XII", circ, creg,
lattice, shots=SHOTS), 0.0, atol=tol)
assert np.isclose(vqls.expectation([pi / 2, 0, 0], 1, "XII", circ, creg,
lattice, shots=SHOTS), 1.0, atol=tol)
assert np.isclose(vqls.expectation([pi / 2, pi / 2, 0], 1, "XXI", circ, creg,
lattice, shots=SHOTS), 1.0, atol=tol)
assert np.isclose(vqls.expectation([pi / 2, pi / 2, pi / 2], 1, "XXX", circ, creg,
lattice, shots=SHOTS), 1.0, atol=tol)
assert np.isclose(vqls.expectation([pi / 2, pi / 2, 0], 1, "XXZ", circ, creg,
lattice, shots=SHOTS), 1.0, atol=tol)
def test_energy1():
"""Tests energy computation for identity Hamiltonian."""
test_hamiltonian = [(1.0, "II")]
test_computer = get_qc("Aspen-7-2Q-B", as_qvm=True)
test_circuit, test_creg = vqls.yansatz(test_computer)
assert np.isclose(
vqls.energy([0, 0], test_hamiltonian, test_circuit, test_creg, test_computer, shots=10000, verbose=False),
1.0,
atol=1e-5
)
def test_energy2():
"""Tests energy for simple Hamiltonian."""
test_hamiltonian = [(1.0, "II"), (-0.1, "IX")]
test_computer = get_qc("Aspen-7-2Q-B", as_qvm=True)
test_circuit, test_creg = vqls.yansatz(test_computer)
assert np.isclose(
vqls.energy([0, 0], test_hamiltonian, test_circuit, test_creg, test_computer, shots=10000, verbose=False),
1.0,
1e-2
)
assert np.isclose(
vqls.energy([0, pi / 2], test_hamiltonian, test_circuit, test_creg, test_computer, shots=10000, verbose=False),
0.9,
1e-2
)
def test_energy_with_min_weight():
"""Tests computing energy of Hamiltonian with small weight terms."""
test_hamiltonian = [(1.0, "II"), (-0.1, "II"), (-0.01, "II")]
test_computer = get_qc("Aspen-7-2Q-B", as_qvm=True)
test_circuit, test_creg = vqls.yansatz(test_computer)
assert np.isclose(
vqls.energy([0, 0], test_hamiltonian, test_circuit, test_creg, test_computer, shots=10000, min_weight=0.1),
0.9,
1e-2
)
assert np.isclose(
vqls.energy([0, 0], test_hamiltonian, test_circuit, test_creg, test_computer, shots=10000, min_weight=0.01),
0.89,
1e-2
)
assert np.isclose(
vqls.energy([0, 0], test_hamiltonian, test_circuit, test_creg, test_computer, shots=10000, min_weight=2.0),
0.0,
1e-2
)
# ========================================
# Unit tests for simultaneous measurements
# ========================================
def test_merge():
assert vqls.merge("IX", "ZI") == "ZX"
assert vqls.merge("ZX", "ZI") == "ZX"
assert vqls.merge("IIZ", "ZZI") == "ZZZ"
def test_squash():
assert vqls.squash(["III", "IXY", "ZII"]) == "ZXY"
assert vqls.squash(["I", "Z"]) == "Z"
assert vqls.squash(
["IIII", "IIXI", "ZXII", "IIIY"]
) == "ZXXY"
assert vqls.squash(["IIIII", "IIIXZ"]) == "IIIXZ"
assert vqls.squash(["IXXI", "ZIII"]) == "ZXXI"
def test_support():
assert vqls.support("III") == []
assert vqls.support("IXY") == [1, 2]
assert vqls.support("ZZI") == [0, 1]
def test_supports():
paulis = ["III", "IXZ", "ZZI", "XII", "IYI", "IIY", "XYZ"]
supports = [vqls.support(p) for p in paulis]
assert supports == [
[],
[1, 2],
[0, 1],
[0],
[1],
[2],
[0, 1, 2]
]
def test_islice():
vals = [0, 1, 2, 3, 4]
assert vqls.islice(vals, [0, 1, 4]) == [0, 1, 4]
assert vqls.islice(vals, [0]) == [0]
assert vqls.islice(vals, []) == []
def test_is_sim_meas():
"""Unit test for determining if two paulis can be measured simultaneously."""
# Cases which can be measured simultaneously
assert vqls.is_sim_meas("III", "IXZ")
assert vqls.is_sim_meas("IZZ", "ZZI")
assert vqls.is_sim_meas("X" * 20, "X" * 20)
assert vqls.is_sim_meas("YYIZZ", "IIXZI")
# Cases which cannot be measured simultaneously
assert not vqls.is_sim_meas("X", "Z")
assert not vqls.is_sim_meas("Z", "X")
assert not vqls.is_sim_meas("YY", "YZ")
assert not vqls.is_sim_meas("XIZ", "IIY")
def test_can_be_grouped_with():
"""Unit test for measurments with a group."""
# Cases which the grouping works
assert vqls.can_be_grouped_with("IIZ", ["ZZI", "IZI", "ZZZ"])
assert vqls.can_be_grouped_with("IIZ", [])
assert vqls.can_be_grouped_with("XII", ["XXX", "IXI", "IXX"])
assert vqls.can_be_grouped_with("Z", ["I", "Z"])
assert vqls.can_be_grouped_with("IXYZ", ["IIII", "XXYZ"])
# Cases which the grouping doesn't work
assert not vqls.can_be_grouped_with("ZZI", ["ZZZ", "XZI"])
assert not vqls.can_be_grouped_with("Z", ["I", "X"])
assert not vqls.can_be_grouped_with("IXYZ", ["IIII", "XXYZ", "IIIX"])
assert not vqls.can_be_grouped_with("XXX", ["YII", "IIZ"])
def test_is_sim_meas_group():
"""Unit test for seeing if a group is simultaneously measurable."""
# Groups which are simultaneously measurable
assert vqls.is_sim_meas_group(["IXZ", "IIZ", "IXI"])
assert vqls.is_sim_meas_group(["IIZI", "ZZIZ", "ZZZZ", "ZIZI"])
assert vqls.is_sim_meas_group(["I", "Z"])
assert vqls.is_sim_meas_group(["IX", "XI"])
# Groups which are not simultaneously measurable
assert not vqls.is_sim_meas_group(["ZZZ", "XXX"])
assert not vqls.is_sim_meas_group(["I", "X", "Z"])
assert not vqls.is_sim_meas_group(["IZY", "IZI", "ZZX"])
def test_split_ham():
ham = [
[1, "IIZ"],
[2, "XIZ"],
[3, "YYY"]
]
coeffs, paulis = vqls.split_ham_to_coeffs_and_paulis(ham)
assert coeffs == [1.0, 2.0, 3.0]
assert paulis == ['IIZ', 'XIZ', 'YYY']
def test_greedy_group():
ham = [
[1, "IIZ"],
[2, "XIZ"],
[3, "YYY"]
]
grouped = vqls.group_greedy(ham, randomized=False)
assert len(grouped) == 2
assert grouped == [[(1.0, 'IIZ'), (2.0, 'XIZ')], [(3.0, 'YYY')]]
def test_greedy_group2():
ham = [
[1, "IIZ"],
[2, "IIX"],
[3, "ZIZ"]
]
grouped = vqls.group_greedy(ham, randomized=False)
assert len(grouped) == 2
assert grouped == [[(1.0, 'IIZ'), (3.0, 'ZIZ')], [(2.0, 'IIX')]]
def test_greedy_group3():
ham = [
[1, "IIZ"],
[2, "IIX"],
[3, "ZIZ"],
[4, "XII"]
]
grouped = vqls.group_greedy(ham, randomized=False)
assert len(grouped) == 2
assert grouped == [[(1.0, 'IIZ'), (3.0, 'ZIZ')], [(2.0, 'IIX'), (4.0, 'XII')]]
def test_greedy_group4():
ham = [
[1, "IIZ"],
[2, "IIX"],
[3, "ZIZ"],
[4, "XII"],
[5, "IZI"]
]
grouped = vqls.group_greedy(ham, randomized=False)
assert len(grouped) == 2
assert grouped == [[(1.0, 'IIZ'), (3.0, 'ZIZ'), (5.0, 'IZI')], [(2.0, 'IIX'), (4.0, 'XII')]]
def test_greedy_group5():
ham = [
[1, "IIZ"],
[2, "IIX"],
[3, "ZIZ"],
[4, "XII"],
[5, "IZI"],
[6, "ZZZ"],
[7, "XXX"],
[8, "YYY"]
]
grouped = vqls.group_greedy(ham, randomized=False)
assert len(grouped) == 3
def test_measure_group_identity_2q_zgroup():
# Define number of qubits
n = 2
# Get a group to measure
group = [(1, "IZ"), (-1, "ZI"), (1, "ZZ")]
# Get a quantum computer to run on
qcomputer = f"Aspen-7-{n}Q-B"
lattice = get_qc(qcomputer, as_qvm=True) # Change to as_qvm=False to run on QC. Must have reservation.
# Get an ansatz and set the angles
circ, creg = vqls.yansatz(lattice)
angles = [0, 0]
# Compute expectation via individual terms
itot = 0.
for coeff, pauli in group:
itot += vqls.expectation(angles, coeff, pauli, circ, creg, lattice, shots=10_000)
# Compute expectation by grouping
gtot = vqls.measure_group(angles, group, circ, creg, lattice, shots=10_000)
# Compare to each other
assert np.isclose(gtot, itot)
# Compare to known answer
assert np.isclose(gtot, 1.0)
def test_measure_group_identity_2q_xgroup():
# Define number of qubits
n = 2
# Get a group to measure
group = [(1, "IX"), (-1, "XI")]
# Get a quantum computer to run on
qcomputer = f"Aspen-7-{n}Q-B"
lattice = get_qc(qcomputer, as_qvm=True) # Change to as_qvm=False to run on QC. Must have reservation.
# Get an ansatz and set the angles
circ, creg = vqls.yansatz(lattice)
angles = [0, 0]
# Compute expectation via individual terms
itot = 0.
for coeff, pauli in group:
itot += vqls.expectation(angles, coeff, pauli, circ, creg, lattice, shots=10_000)
# Compute expectation by grouping
gtot = vqls.measure_group(angles, group, circ, creg, lattice, shots=10_000)
# Compare to each other
assert np.isclose(gtot, itot, atol=0.05)
# Compare to known answer
assert np.isclose(gtot, 0.0, atol=0.05)
def test_measure_group_2q_xgroup():
# Define number of qubits
n = 2
# Get a group to measure
group = [(1, "IX"), (-1, "XI")]
# Get a quantum computer to run on
qcomputer = f"Aspen-7-{n}Q-B"
lattice = get_qc(qcomputer, as_qvm=True) # Change to as_qvm=False to run on QC. Must have reservation.
# Get an ansatz and set the angles
circ, creg = vqls.yansatz(lattice)
angles = [pi / 2, 0]
# Compute expectation via individual terms
itot = 0.
for coeff, pauli in group:
itot += vqls.expectation(angles, coeff, pauli, circ, creg, lattice, shots=10_000)
# Compute expectation by grouping
gtot = vqls.measure_group(angles, group, circ, creg, lattice, shots=10_000)
# Compare to each other
assert np.isclose(gtot, itot, atol=0.05)
# Compare to known anser
assert np.isclose(gtot, -1.0, atol=0.05)
def test_measure_group_2q_xgroup_loop_angles():
# Define number of qubits
n = 2
# Get a group to measure
group = [(1, "IX"), (-1, "XI")]
# Get a quantum computer to run on
qcomputer = f"Aspen-7-{n}Q-B"
lattice = get_qc(qcomputer, as_qvm=True) # Change to as_qvm=False to run on QC. Must have reservation.
# Get an ansatz and set the angles
circ, creg = vqls.yansatz(lattice)
for angles in ([0, 0], [pi / 4, pi / 4], [pi / 2, pi / 4],
[pi / 4, pi / 2], [pi / 2, pi / 2]):
# Compute expectation via individual terms
itot = 0.
for coeff, pauli in group:
itot += vqls.expectation(angles, coeff, pauli, circ, creg, lattice, shots=10_000)
# Compute expectation by grouping
gtot = vqls.measure_group(angles, group, circ, creg, lattice, shots=10_000)
# Compare to each other
assert np.isclose(gtot, itot, atol=0.05)
def test_measure_energy_sim():
"""Unit test for measuring <H> using simultaneous measurements."""
ham = [[(1, "I"), (1, "X")], [(-1, "Z")]]
n = 1
qcomputer = f"{n}q-qvm"
lattice = get_qc(qcomputer, as_qvm=True)
circ, creg = vqls.yansatz(lattice)
angles = [0]
cost = vqls.energy_sim(angles, ham, circ, creg, lattice, shots=10_000)
assert np.isclose(cost, 0.0, atol=0.05)