/
demoCalcMethods2Q.py
280 lines (225 loc) · 16 KB
/
demoCalcMethods2Q.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
from __future__ import print_function
import unittest
import numpy as np
import pygsti
import pygsti.construction as pc
from pygsti.construction import std2Q_XYCNOT as std
from pygsti.construction import std1Q_XY
from pygsti.objects import Label as L
from pygsti.io import json
import sys, os
from ..testutils import BaseTestCase, compare_files, temp_files
class CalcMethods2QTestCase(BaseTestCase):
@classmethod
def setUpClass(cls):
"""
Handle all once-per-class (slow) computation and loading,
to avoid calling it for each test (like setUp). Store
results in class variable for use within setUp.
"""
super(CalcMethods2QTestCase, cls).setUpClass()
#Change to test_packages directory (since setUp hasn't been called yet...)
origDir = os.getcwd()
os.chdir(os.path.abspath(os.path.dirname(__file__)))
os.chdir('..') # The test_packages directory
#Note: std is a 2Q model
cls.maxLengths = [1]
#cls.germs = std.germs_lite
cls.germs = pygsti.construction.circuit_list([ (gl,) for gl in std.target_model().operations ])
cls.mdl_datagen = std.target_model().depolarize(op_noise=0.1, spam_noise=0.001)
cls.listOfExperiments = pygsti.construction.make_lsgst_experiment_list(
std.target_model(), std.prepStrs, std.effectStrs, cls.germs, cls.maxLengths)
#RUN BELOW FOR DATAGEN (UNCOMMENT to regenerate)
#ds = pygsti.construction.generate_fake_data(cls.mdl_datagen, cls.listOfExperiments,
# nSamples=1000, sampleError="multinomial", seed=1234)
#ds.save(compare_files + "/calcMethods2Q.dataset%s" % cls.versionsuffix)
cls.ds = pygsti.objects.DataSet(fileToLoadFrom=compare_files + "/calcMethods2Q.dataset%s" % cls.versionsuffix)
cls.advOpts = {'tolerance': 1e-2 }
#Reduced model GST dataset
cls.nQubits=2
cls.mdl_redmod_datagen = pc.build_nqnoise_model(cls.nQubits, geometry="line", maxIdleWeight=1, maxhops=1,
extraWeight1Hops=0, extraGateWeight=1, sparse=False, sim_type="matrix", verbosity=1,
gateNoise=(1234,0.01), prepNoise=(456,0.01), povmNoise=(789,0.01))
#Create a reduced set of fiducials and germs
opLabels = list(cls.mdl_redmod_datagen.operations.keys())
fids1Q = std1Q_XY.fiducials[0:2] # for speed
cls.redmod_fiducials = []
for i in range(cls.nQubits):
cls.redmod_fiducials.extend( pygsti.construction.manipulate_circuit_list(
fids1Q, [ ( (L('Gx'),) , (L('Gx',i),) ), ( (L('Gy'),) , (L('Gy',i),) ) ]) )
#print(redmod_fiducials, "Fiducials")
cls.redmod_germs = pygsti.construction.circuit_list([ (gl,) for gl in opLabels ])
cls.redmod_maxLs = [1]
expList = pygsti.construction.make_lsgst_experiment_list(
cls.mdl_redmod_datagen, cls.redmod_fiducials, cls.redmod_fiducials,
cls.redmod_germs, cls.redmod_maxLs)
#RUN BELOW FOR DATAGEN (UNCOMMENT to regenerate)
#redmod_ds = pygsti.construction.generate_fake_data(cls.mdl_redmod_datagen, expList, 1000, "round", seed=1234)
#redmod_ds.save(compare_files + "/calcMethods2Q_redmod.dataset%s" % cls.versionsuffix)
cls.redmod_ds = pygsti.objects.DataSet(fileToLoadFrom=compare_files + "/calcMethods2Q_redmod.dataset%s" % cls.versionsuffix)
#print(len(expList)," reduced model sequences")
#Random starting points - little kick so we don't get hung up at start
np.random.seed(1234)
cls.rand_start18 = np.random.random(18)*1e-6
cls.rand_start206 = np.random.random(206)*1e-6
cls.rand_start228 = np.random.random(228)*1e-6
os.chdir(origDir) # return to original directory
## GST using "full" (non-embedded/composed) gates
# All of these calcs use dense matrices; While sparse operation matrices (as Maps) could be used,
# they'd need to enter as a sparse basis to a LindbladDenseOp (maybe add this later?)
def test_stdgst_matrix(self):
# Using matrix-based calculations
target_model = std.target_model().copy()
target_model.set_all_parameterizations("CPTP")
target_model.set_simtype('matrix') # the default for 1Q, so we could remove this line
results = pygsti.do_long_sequence_gst(self.ds, target_model, std.prepStrs, std.effectStrs,
self.germs, self.maxLengths, advancedOptions=self.advOpts,
verbosity=4)
#RUN BELOW LINES TO SAVE GATESET (UNCOMMENT to regenerate)
#pygsti.io.write_model(results.estimates['default'].models['go0'],
# compare_files + "/test2Qcalc_std_exact.model","Saved Standard-Calc 2Q test model")
#Note: expected nSigma of 143 is so high b/c we use very high tol of 1e-2 => result isn't very good
print("MISFIT nSigma = ",results.estimates['default'].misfit_sigma())
self.assertAlmostEqual( results.estimates['default'].misfit_sigma(), 143, delta=2.0)
mdl_compare = pygsti.io.load_model(compare_files + "/test2Qcalc_std_exact.model")
self.assertAlmostEqual( results.estimates['default'].models['go0'].frobeniusdist(mdl_compare), 0, places=3)
def test_stdgst_map(self):
# Using map-based calculation
target_model = std.target_model().copy()
target_model.set_all_parameterizations("CPTP")
target_model.set_simtype('map')
results = pygsti.do_long_sequence_gst(self.ds, target_model, std.prepStrs, std.effectStrs,
self.germs, self.maxLengths, advancedOptions=self.advOpts,
verbosity=4)
#Note: expected nSigma of 143 is so high b/c we use very high tol of 1e-2 => result isn't very good
print("MISFIT nSigma = ",results.estimates['default'].misfit_sigma())
self.assertAlmostEqual( results.estimates['default'].misfit_sigma(), 143, delta=2.0)
mdl_compare = pygsti.io.load_model(compare_files + "/test2Qcalc_std_exact.model")
self.assertAlmostEqual( results.estimates['default'].models['go0'].frobeniusdist(mdl_compare), 0, places=3)
def test_stdgst_terms(self):
# Using term-based (path integral) calculation
# This performs a map-based unitary evolution along each path.
target_model = std.target_model().copy()
target_model.set_all_parameterizations("H+S terms")
target_model.set_simtype('termorder:1') # this is the default set by set_all_parameterizations above
results = pygsti.do_long_sequence_gst(self.ds, target_model, std.prepStrs, std.effectStrs,
self.germs, self.maxLengths, verbosity=4)
#RUN BELOW LINES TO SAVE GATESET (UNCOMMENT to regenerate)
#pygsti.io.json.dump(results.estimates['default'].models['go0'],
# open(compare_files + "/test2Qcalc_std_terms.model",'w'))
print("MISFIT nSigma = ",results.estimates['default'].misfit_sigma())
self.assertAlmostEqual( results.estimates['default'].misfit_sigma(), 5, delta=1.0)
mdl_compare = pygsti.io.json.load(open(compare_files + "/test2Qcalc_std_terms.model"))
self.assertAlmostEqual( np.linalg.norm(results.estimates['default'].models['go0'].to_vector()
- mdl_compare.to_vector()), 0, places=3)
# ## GST using "reduced" models
# Reduced, meaning that we use composed and embedded gates to form a more complex error model with
# shared parameters and qubit connectivity graphs. Calculations *can* use dense matrices and matrix calcs,
# but usually will use sparse mxs and map-based calcs.
def test_reducedmod_matrix(self):
# Using dense matrices and matrix-based calcs
target_model = pc.build_nqnoise_model(self.nQubits, geometry="line", maxIdleWeight=1, maxhops=1,
extraWeight1Hops=0, extraGateWeight=1, sparse=False,
sim_type="matrix", verbosity=1)
target_model.from_vector(self.rand_start206)
results = pygsti.do_long_sequence_gst(self.redmod_ds, target_model, self.redmod_fiducials,
self.redmod_fiducials, self.redmod_germs, self.redmod_maxLs,
verbosity=4, advancedOptions={'tolerance': 1e-3})
#RUN BELOW LINES TO SAVE GATESET (UNCOMMENT to regenerate)
#pygsti.io.json.dump(results.estimates['default'].models['go0'],
# open(compare_files + "/test2Qcalc_redmod_exact.model",'w'))
print("MISFIT nSigma = ",results.estimates['default'].misfit_sigma())
self.assertAlmostEqual( results.estimates['default'].misfit_sigma(), 1.0, delta=1.0)
mdl_compare = pygsti.io.json.load( open(compare_files + "/test2Qcalc_redmod_exact.model"))
self.assertAlmostEqual( results.estimates['default'].models['go0'].frobeniusdist(mdl_compare), 0, places=3)
def test_reducedmod_map1(self):
# Using dense embedded matrices and map-based calcs (maybe not really necessary to include?)
target_model = pc.build_nqnoise_model(self.nQubits, geometry="line", maxIdleWeight=1, maxhops=1,
extraWeight1Hops=0, extraGateWeight=1, sparse=False,
sim_type="map", verbosity=1)
target_model.from_vector(self.rand_start206)
results = pygsti.do_long_sequence_gst(self.redmod_ds, target_model, self.redmod_fiducials,
self.redmod_fiducials, self.redmod_germs, self.redmod_maxLs,
verbosity=4, advancedOptions={'tolerance': 1e-3})
print("MISFIT nSigma = ",results.estimates['default'].misfit_sigma())
self.assertAlmostEqual( results.estimates['default'].misfit_sigma(), 1.0, delta=1.0)
mdl_compare = pygsti.io.json.load( open(compare_files + "/test2Qcalc_redmod_exact.model"))
self.assertAlmostEqual( results.estimates['default'].models['go0'].frobeniusdist(mdl_compare), 0, places=1)
#Note: models aren't necessarily exactly equal given gauge freedoms that we don't know
# how to optimizize over exactly - so this is a very loose test...
def test_reducedmod_map2(self):
# Using sparse embedded matrices and map-based calcs
target_model = pc.build_nqnoise_model(self.nQubits, geometry="line", maxIdleWeight=1, maxhops=1,
extraWeight1Hops=0, extraGateWeight=1, sparse=True,
sim_type="map", verbosity=1)
target_model.from_vector(self.rand_start206)
results = pygsti.do_long_sequence_gst(self.redmod_ds, target_model, self.redmod_fiducials,
self.redmod_fiducials, self.redmod_germs, self.redmod_maxLs,
verbosity=4, advancedOptions={'tolerance': 1e-3})
print("MISFIT nSigma = ",results.estimates['default'].misfit_sigma())
self.assertAlmostEqual( results.estimates['default'].misfit_sigma(), 1.0, delta=1.0)
mdl_compare = pygsti.io.json.load( open(compare_files + "/test2Qcalc_redmod_exact.model"))
self.assertAlmostEqual( np.linalg.norm(results.estimates['default'].models['go0'].to_vector()
- mdl_compare.to_vector()), 0, places=1)
#Note: models aren't necessarily exactly equal given gauge freedoms that we don't know
# how to optimizize over exactly - so this is a very loose test...
def test_reducedmod_svterm(self):
# Using term-based calcs using map-based state-vector propagation
target_model = pc.build_nqnoise_model(self.nQubits, geometry="line", maxIdleWeight=1, maxhops=1,
extraWeight1Hops=0, extraGateWeight=1, sparse=False, verbosity=1,
sim_type="termorder:1", parameterization="H+S terms")
target_model.from_vector(self.rand_start228)
results = pygsti.do_long_sequence_gst(self.redmod_ds, target_model, self.redmod_fiducials,
self.redmod_fiducials, self.redmod_germs, self.redmod_maxLs,
verbosity=4, advancedOptions={'tolerance': 1e-3})
#RUN BELOW LINES TO SAVE GATESET (UNCOMMENT to regenerate)
#pygsti.io.json.dump(results.estimates['default'].models['go0'],
# open(compare_files + "/test2Qcalc_redmod_terms.model",'w'))
print("MISFIT nSigma = ",results.estimates['default'].misfit_sigma())
self.assertAlmostEqual( results.estimates['default'].misfit_sigma(), 3.0, delta=1.0)
mdl_compare = pygsti.io.json.load( open(compare_files + "/test2Qcalc_redmod_terms.model"))
self.assertAlmostEqual( np.linalg.norm(results.estimates['default'].models['go0'].to_vector()
- mdl_compare.to_vector()), 0, places=3)
def test_reducedmod_cterm(self):
# Using term-based calcs using map-based stabilizer-state propagation
target_model = pc.build_nqnoise_model(self.nQubits, geometry="line", maxIdleWeight=1, maxhops=1,
extraWeight1Hops=0, extraGateWeight=1, sparse=False, verbosity=1,
sim_type="termorder:1", parameterization="H+S clifford terms")
target_model.from_vector(self.rand_start228)
results = pygsti.do_long_sequence_gst(self.redmod_ds, target_model, self.redmod_fiducials,
self.redmod_fiducials, self.redmod_germs, self.redmod_maxLs,
verbosity=4, advancedOptions={'tolerance': 1e-3})
print("MISFIT nSigma = ",results.estimates['default'].misfit_sigma())
self.assertAlmostEqual( results.estimates['default'].misfit_sigma(), 3.0, delta=1.0)
mdl_compare = pygsti.io.json.load( open(compare_files + "/test2Qcalc_redmod_terms.model"))
self.assertAlmostEqual( np.linalg.norm(results.estimates['default'].models['go0'].to_vector()
- mdl_compare.to_vector()), 0, places=3)
def test_circuitsim_stabilizer_2Qcheck(self):
#Test 2Q circuits
#from pygsti.construction import std2Q_XYICNOT as stdChk
from pygsti.construction import std2Q_XYICPHASE as stdChk
maxLengths = [1,2,4]
listOfExperiments = pygsti.construction.make_lsgst_experiment_list(
stdChk.target_model(), stdChk.prepStrs, stdChk.effectStrs, stdChk.germs, maxLengths)
#listOfExperiments = pygsti.construction.circuit_list([ ('Gcnot','Gxi') ])
#listOfExperiments = pygsti.construction.circuit_list([ ('Gxi','Gcphase','Gxi','Gix') ])
mdl_normal = stdChk.target_model().copy()
mdl_clifford = stdChk.target_model().copy()
#print(mdl_clifford['Gcnot'])
self.assertTrue(stdChk.target_model()._evotype == "densitymx")
mdl_clifford.set_all_parameterizations('static unitary') # reduces dim...
self.assertTrue(mdl_clifford._evotype == "statevec")
mdl_clifford.set_all_parameterizations('clifford')
self.assertTrue(mdl_clifford._evotype == "stabilizer")
for opstr in listOfExperiments:
#print(str(opstr))
p_normal = mdl_normal.probs(opstr)
p_clifford = mdl_clifford.probs(opstr)
#p_clifford = bprobs[opstr]
for outcm in p_normal.keys():
if abs(p_normal[outcm]-p_clifford[outcm]) > 1e-8:
print(str(opstr)," ERR: \n",p_normal,"\n",p_clifford);
self.assertTrue(False)
print("Done checking %d sequences!" % len(listOfExperiments))
if __name__ == "__main__":
unittest.main(verbosity=2)