/
testDrivers.py
242 lines (183 loc) · 10.7 KB
/
testDrivers.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
import unittest
import pygsti
from pygsti.construction import std1Q_XYI as std
import numpy as np
import sys
class DriversTestCase(unittest.TestCase):
def setUp(self):
self.gateset = std.gs_target
self.germs = std.germs
self.fiducials = std.fiducials
self.maxLens = [0,1,2,4]
self.gateLabels = self.gateset.keys()
self.elgstStrings = pygsti.construction.make_elgst_lists(
self.gateLabels, self.germs, self.maxLens )
self.lsgstStrings = pygsti.construction.make_lsgst_lists(
self.gateLabels, self.fiducials, self.germs, self.maxLens )
self.lsgstStrings_tgp = pygsti.construction.make_lsgst_lists(
self.gateLabels, self.fiducials, self.germs, self.maxLens,
truncScheme="truncated germ powers" )
self.lsgstStrings_lae = pygsti.construction.make_lsgst_lists(
self.gateLabels, self.fiducials, self.germs, self.maxLens,
truncScheme='length as exponent' )
# RUN BELOW LINES TO GENERATE SAVED DATASETS
#datagen_gateset = self.gateset.depolarize(gate_noise=0.05, spam_noise=0.1)
#ds = pygsti.construction.generate_fake_data(
# datagen_gateset, self.lsgstStrings[-1],
# nSamples=1000,sampleError='binomial', seed=100)
#
#ds_tgp = pygsti.construction.generate_fake_data(
# datagen_gateset, self.lsgstStrings_tgp[-1],
# nSamples=1000,sampleError='binomial', seed=100)
#
#ds_lae = pygsti.construction.generate_fake_data(
# datagen_gateset, self.lsgstStrings_lae[-1],
# nSamples=1000,sampleError='binomial', seed=100)
#
#ds.save("cmp_chk_files/drivers.dataset")
#ds_tgp.save("cmp_chk_files/drivers_tgp.dataset")
#ds_lae.save("cmp_chk_files/drivers_lae.dataset")
def runSilent(self, callable, *args, **kwds):
orig_stdout = sys.stdout
sys.stdout = open("temp_test_files/silent.txt","w")
result = callable(*args, **kwds)
sys.stdout.close()
sys.stdout = orig_stdout
return result
class TestDriversMethods(DriversTestCase):
def test_longSequenceGST_WholeGermPowers(self):
ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/drivers.dataset")
ts = "whole germ powers"
maxLens = self.maxLens
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts)
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts, objective="chi2")
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts, constrainToTP=False)
#Try using files instead of objects
pygsti.io.write_gateset(std.gs_target, "temp_test_files/driver.gateset")
pygsti.io.write_dataset("temp_test_files/driver_test_dataset.txt",
self.lsgstStrings[-1], ds)
pygsti.io.write_gatestring_list("temp_test_files/driver_fiducials.txt", std.fiducials)
pygsti.io.write_gatestring_list("temp_test_files/driver_germs.txt", std.germs)
result = self.runSilent(pygsti.do_long_sequence_gst,
"temp_test_files/driver_test_dataset.txt",
"temp_test_files/driver.gateset",
"temp_test_files/driver_fiducials.txt",
"temp_test_files/driver_fiducials.txt",
"temp_test_files/driver_germs.txt",
maxLens, truncScheme=ts)
#Try using EStrs == None and gaugeOptToCPTP
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, None,
std.germs, maxLens, truncScheme=ts,
gaugeOptToCPTP=True)
#Check errors
with self.assertRaises(ValueError):
self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, None,
std.germs, maxLens, truncScheme=ts, objective="FooBar") #bad objective
def test_longSequenceGST_TruncGermPowers(self):
ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/drivers_tgp.dataset")
ts = "truncated germ powers"
maxLens = self.maxLens
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts)
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts, objective="chi2")
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts, constrainToTP=False)
def test_longSequenceGST_LengthAsExponent(self):
ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/drivers_lae.dataset")
ts = "length as exponent"
maxLens = self.maxLens
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts)
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts, objective="chi2")
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts, constrainToTP=False)
def test_longSequenceGST_fiducialPairReduction(self):
ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/drivers.dataset")
ts = "whole germ powers"
rhoEPairs = pygsti.alg.find_sufficient_fiducial_pairs(
std.gs_target, std.fiducials, std.fiducials, std.germs, verbosity=0)
maxLens = self.maxLens
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, std.gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts, rhoEPairs=rhoEPairs)
#create a report...
result.create_full_report_pdf(filename="temp_test_files/full_report_FPR.pdf",
debugAidsAppendix=False, gaugeOptAppendix=False,
pixelPlotAppendix=False, whackamoleAppendix=False,
verbosity=2)
def test_longSequenceGST_parameterizedGates(self):
ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/drivers.dataset")
ts = "whole germ powers"
gs_target = pygsti.construction.build_gateset([2],[('Q0',)], ['Gi','Gx','Gy'],
[ "D(Q0)","X(pi/2,Q0)", "Y(pi/2,Q0)"],
rhoExpressions=["0"], EExpressions=["1"],
spamLabelDict={'plus': (0,0), 'minus': (0,-1) },
parameterization="linear")
maxLens = self.maxLens
result = self.runSilent(pygsti.do_long_sequence_gst,
ds, gs_target, std.fiducials, std.fiducials,
std.germs, maxLens, truncScheme=ts, constrainToTP=False)
#create a report...
result.create_full_report_pdf(filename="temp_test_files/full_report_LPGates.pdf",
debugAidsAppendix=False, gaugeOptAppendix=False,
pixelPlotAppendix=False, whackamoleAppendix=False,
verbosity=2)
#rhoStrsListOrFilename, EStrsListOrFilename,
#germsListOrFilename, maxLengths, gateLabels,
#weightsDict, rhoEPairs, constrainToTP,
#gaugeOptToCPTP, gaugeOptRatio, objective="logl",
#advancedOptions={}, lsgstLists=None,
#truncScheme="whole germ powers"):
def test_bootstrap(self):
ds = pygsti.objects.DataSet(fileToLoadFrom="cmp_chk_files/drivers.dataset")
specs = pygsti.construction.build_spam_specs(std.fiducials)
gs = pygsti.do_lgst(ds, specs, targetGateset=std.gs_target, svdTruncateTo=4, verbosity=0)
bootds_p = pygsti.drivers.make_bootstrap_dataset(ds,'parametric',gs, seed=1234 )
bootds_np = pygsti.drivers.make_bootstrap_dataset(ds,'nonparametric', seed=1234 )
maxLengths = [0] #just do LGST strings to make this fast...
bootgs_p = self.runSilent(pygsti.drivers.make_bootstrap_gatesets,
2, ds, 'parametric', std.fiducials, std.fiducials,
std.germs, maxLengths, inputGateSet=gs, constrainToTP=True,
returnData=False)
bootgs_np, bootds_np2 = self.runSilent(pygsti.drivers.make_bootstrap_gatesets,
2, ds, 'nonparametric', std.fiducials, std.fiducials,
std.germs, maxLengths, targetGateSet=gs,
constrainToTP=True, returnData=True)
self.runSilent(pygsti.drivers.gauge_optimize_gs_list, bootgs_p, std.gs_target, constrainToTP=True,
gateMetric = 'frobenius', spamMetric = 'frobenius', plot=False)
#Test utility functions -- just make sure they run for now...
def gsFn(gs):
return gs.get_dimension()
pygsti.drivers.gs_stdev(gsFn, bootgs_p)
pygsti.drivers.gs_mean(gsFn, bootgs_p)
pygsti.drivers.gs_stdev1(gsFn, bootgs_p)
pygsti.drivers.gs_mean1(gsFn, bootgs_p)
pygsti.drivers.to_vector(bootgs_p[0])
pygsti.drivers.to_mean_gateset(bootgs_p, std.gs_target)
pygsti.drivers.to_std_gateset(bootgs_p, std.gs_target)
pygsti.drivers.to_rms_gateset(bootgs_p, std.gs_target)
pygsti.drivers.gateset_jtracedist(bootgs_p[0], std.gs_target)
pygsti.drivers.gateset_process_fidelity(bootgs_p[0], std.gs_target)
pygsti.drivers.gateset_diamonddist(bootgs_p[0], std.gs_target)
pygsti.drivers.gateset_decomp_angle(bootgs_p[0])
pygsti.drivers.gateset_decomp_decay_diag(bootgs_p[0])
pygsti.drivers.gateset_decomp_decay_offdiag(bootgs_p[0])
pygsti.drivers.spamrameter(bootgs_p[0])
if __name__ == "__main__":
unittest.main(verbosity=2)