/
test_longsequence.py
366 lines (307 loc) · 14.8 KB
/
test_longsequence.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
from io import BytesIO
import pytest
import pygsti.data as pdata
from pygsti import io
from pygsti.drivers import longsequence as ls
from pygsti.forwardsims import mapforwardsim
from pygsti.modelmembers import operations as operation
from pygsti.models.gaugegroup import UnitaryGaugeGroup
from pygsti.models.modelconstruction import create_explicit_model
from . import fixtures as pkg
from ..util import BaseCase, with_temp_path
# TODO optimize everything
class LongSequenceBasePlain:
@classmethod
def setUpClass(cls):
cls.pspec = pkg.pspec
cls.model = pkg.model
cls.maxLens = pkg.maxLengthList
cls.opLabels = pkg.opLabels
cls.prep_fids = pkg.prep_fids
cls.meas_fids = pkg.meas_fids
cls.germs = pkg.germs
cls.lsgstStrings = pkg.lsgstStrings
cls.ds = pkg.dataset
def setUp(self):
self.model = self.model.copy()
self.ds = self.ds.copy()
class LongSequenceBase(LongSequenceBasePlain, BaseCase):
# just wrap the version that doesn't inherit from BaseCase
pass
class MapForwardSimulatorWrapper(mapforwardsim.MapForwardSimulator):
Message = """
Hit the forward simulator wrapper!
"""
def _bulk_fill_probs(self, array_to_fill, layout):
print(self.Message)
super(MapForwardSimulatorWrapper, self)._bulk_fill_probs(array_to_fill, layout)
def _bulk_fill_probs_atom(self, array_to_fill, layout_atom, resource_alloc):
print(self.Message)
super(MapForwardSimulatorWrapper, self)._bulk_fill_probs_atom(array_to_fill, layout_atom, resource_alloc)
class ModelTestTester(LongSequenceBasePlain):
def setUp(self):
super(ModelTestTester, self).setUpClass()
super(ModelTestTester, self).setUp()
self.mdl_guess = self.model.depolarize(op_noise=0.01, spam_noise=0.01)
def test_model_test(self):
self.setUp()
result = ls.run_model_test(
self.mdl_guess, self.ds, self.pspec, self.prep_fids,
self.meas_fids, self.germs, self.maxLens
)
# TODO assert correctness
def test_model_test_advanced_options(self, capfd: pytest.LogCaptureFixture):
self.setUp()
result = ls.run_model_test(
self.mdl_guess, self.ds, self.pspec, self.prep_fids,
self.meas_fids, self.germs, self.maxLens,
advanced_options=dict(objective='chi2', profile=2),
simulator=MapForwardSimulatorWrapper
)
stdout, _ = capfd.readouterr()
assert MapForwardSimulatorWrapper.Message in stdout
# TODO assert correctness
def test_model_test_pickle_output(self):
self.setUp()
with BytesIO() as pickle_stream:
result = ls.run_model_test(
self.mdl_guess, self.ds, self.pspec, self.prep_fids,
self.meas_fids, self.germs, self.maxLens, output_pkl=pickle_stream
)
assert len(pickle_stream.getvalue()) > 0
# TODO assert correctness
def test_model_test_raises_on_bad_options(self):
self.setUp()
with pytest.raises(ValueError):
ls.run_model_test(
self.mdl_guess, self.ds, self.pspec, self.prep_fids,
self.meas_fids, self.germs, self.maxLens,
advanced_options=dict(objective='foobar')
)
with pytest.raises(ValueError):
ls.run_model_test(
self.mdl_guess, self.ds, self.pspec, self.prep_fids,
self.meas_fids, self.germs, self.maxLens,
advanced_options=dict(profile='foobar')
)
class StdPracticeGSTTester(LongSequenceBase):
def setUp(self):
super(StdPracticeGSTTester, self).setUp()
self.mdl_guess = self.model.depolarize(op_noise=0.01, spam_noise=0.01)
def test_stdpractice_gst_TP(self):
result = ls.run_stdpractice_gst(self.ds, self.model, self.prep_fids, self.meas_fids, self.germs, self.maxLens,
modes="full TP", models_to_test={"Test": self.mdl_guess}, comm=None,
mem_limit=None, verbosity=5)
# TODO assert correctness
def test_stdpractice_gst_CPTP(self):
result = ls.run_stdpractice_gst(self.ds, self.model, self.prep_fids, self.meas_fids, self.germs, self.maxLens,
modes="CPTPLND", models_to_test={"Test": self.mdl_guess}, comm=None,
mem_limit=None, verbosity=5)
# TODO assert correctness
def test_stdpractice_gst_Test(self):
result = ls.run_stdpractice_gst(self.ds, self.model, self.prep_fids, self.meas_fids, self.germs, self.maxLens,
modes="Test", models_to_test={"Test": self.mdl_guess}, comm=None,
mem_limit=None, verbosity=5)
# TODO assert correctness
def test_stdpractice_gst_Target(self):
result = ls.run_stdpractice_gst(self.ds, self.model, self.prep_fids, self.meas_fids, self.germs, self.maxLens,
modes="Target", models_to_test={"Test": self.mdl_guess}, comm=None,
mem_limit=None, verbosity=5)
# TODO assert correctness
@with_temp_path
@with_temp_path
@with_temp_path
@with_temp_path
@with_temp_path
def test_stdpractice_gst_file_args(self, ds_path, model_path, prep_fiducial_path, meas_fiducial_path, germ_path):
import pickle
#io.write_model(self.model, model_path)
io.write_dataset(ds_path, self.ds, self.lsgstStrings[-1])
io.write_circuit_list(prep_fiducial_path, self.prep_fids)
io.write_circuit_list(meas_fiducial_path, self.meas_fids)
io.write_circuit_list(germ_path, self.germs)
target_model = create_explicit_model(self.pspec, ideal_gate_type='static')
target_model.write(model_path + '.json')
result = ls.run_stdpractice_gst(ds_path, model_path+'.json', prep_fiducial_path, meas_fiducial_path, germ_path, self.maxLens,
modes="full TP", comm=None, mem_limit=None, verbosity=5)
# TODO assert correctness
def test_stdpractice_gst_gaugeOptTarget(self):
myGaugeOptSuiteDict = {
'MyGaugeOpt': {
'item_weights': {'gates': 1, 'spam': 0.0001}
}
}
target_model = create_explicit_model(self.pspec, ideal_gate_type='static')
result = ls.run_stdpractice_gst(self.ds, target_model, self.prep_fids, self.meas_fids, self.germs, self.maxLens,
modes="full TP", gaugeopt_suite=myGaugeOptSuiteDict,
gaugeopt_target=self.mdl_guess, comm=None, mem_limit=None, verbosity=5)
# TODO assert correctness
def test_stdpractice_gst_gaugeOptTarget_warns_on_target_override(self):
myGaugeOptSuiteDict = {
'MyGaugeOpt': {
'item_weights': {'gates': 1, 'spam': 0.0001},
'target_model': self.pspec # to test overriding internal target model (prints a warning)
}
}
with self.assertWarns(Warning):
target_model = create_explicit_model(self.pspec, ideal_gate_type='static')
result = ls.run_stdpractice_gst(self.ds, target_model, self.prep_fids, self.meas_fids, self.germs,
self.maxLens, modes="full TP", gaugeopt_suite=myGaugeOptSuiteDict,
gaugeopt_target=self.mdl_guess, comm=None, mem_limit=None, verbosity=5)
# TODO assert correctness
def test_stdpractice_gst_advanced_options(self):
target_model = create_explicit_model(self.pspec, ideal_gate_type='static')
result = ls.run_stdpractice_gst(self.ds, target_model, self.prep_fids, self.meas_fids, self.germs, self.maxLens,
modes="full TP", comm=None, mem_limit=None, advanced_options={'all': {
'objective': 'chi2',
'bad_fit_threshold': -100, # so we create a robust estimate and convey guage opt to it.
'on_bad_fit': ["robust"]
}}, verbosity=5)
# TODO assert correctness
def test_stdpractice_gst_pickle_output(self):
with BytesIO() as pickle_stream:
target_model = create_explicit_model(self.pspec, ideal_gate_type='static')
result = ls.run_stdpractice_gst(self.ds, target_model, self.prep_fids, self.meas_fids, self.germs,
self.maxLens, modes="Target", output_pkl=pickle_stream)
self.assertTrue(len(pickle_stream.getvalue()) > 0)
# TODO assert correctness
def test_stdpractice_gst_raises_on_bad_mode(self):
target_model = create_explicit_model(self.pspec, ideal_gate_type='static')
with self.assertRaises(ValueError):
result = ls.run_stdpractice_gst(self.ds, target_model, self.prep_fids, self.meas_fids, self.germs,
self.maxLens, modes="Foobar")
class LongSequenceGSTBase(LongSequenceBase):
def setUp(self):
super(LongSequenceGSTBase, self).setUp()
self.options = {}
def test_long_sequence_gst(self):
result = ls.run_long_sequence_gst(
self.ds, self.model, self.prep_fids, self.meas_fids,
self.germs, self.maxLens, advanced_options=self.options)
# TODO assert correctness
class LongSequenceGSTWithChi2(LongSequenceGSTBase):
def test_long_sequence_gst_chi2(self):
self.options.update(
objective='chi2'
)
result = ls.run_long_sequence_gst(
self.ds, self.model, self.prep_fids, self.meas_fids,
self.germs, self.maxLens,
advanced_options=self.options)
# TODO assert correctness
class LongSequenceGSTTester(LongSequenceGSTWithChi2):
def test_long_sequence_gst_advanced_options(self):
# TODO what exactly is being tested?
self.options.update({
#'starting point': self.model, #this is deprecated now - need to use protocol objects
'depolarize_start': 0.05,
'cptp_penalty_factor': 1.0
})
result = ls.run_long_sequence_gst(
self.ds, self.model, self.prep_fids, None,
self.germs, self.maxLens,
advanced_options=self.options
)
# TODO assert correctness
def test_long_sequence_gst_raises_on_bad_profile_options(self):
#check invalid profile options
with self.assertRaises(ValueError):
ls.run_long_sequence_gst(
self.ds, self.model, self.prep_fids, self.meas_fids,
self.germs, self.maxLens,
advanced_options={'profile': 3}
)
def test_long_sequence_gst_raises_on_bad_advanced_options(self):
with self.assertRaises(ValueError):
ls.run_long_sequence_gst(
self.ds, self.model, self.prep_fids, None,
self.germs, self.maxLens,
advanced_options={'objective': "FooBar"}
) # bad objective
with self.assertRaises(ValueError):
ls.run_long_sequence_gst(
self.ds, self.model, self.prep_fids, None,
self.germs, self.maxLens,
advanced_options={'starting_point': "FooBar"}
) # bad starting point
class WholeGermPowersTester(LongSequenceGSTWithChi2):
def setUp(self):
super(WholeGermPowersTester, self).setUp()
self.options = {} # 'truncScheme': "whole germ powers"}
# Trunce scheme has been removed as an option - we only ever use whole germ powers now
@with_temp_path
@with_temp_path
@with_temp_path
@with_temp_path
@with_temp_path
def test_long_sequence_gst_with_file_args(self, ds_path, model_path, prep_fiducial_path, meas_fiducial_path, germ_path):
io.write_dataset(ds_path, self.ds, self.lsgstStrings[-1])
self.model.write(model_path + '.json')
io.write_circuit_list(prep_fiducial_path, self.prep_fids)
io.write_circuit_list(meas_fiducial_path, self.meas_fids)
io.write_circuit_list(germ_path, self.germs)
self.options.update(
randomize_start=1e-6,
profile=2,
)
result = ls.run_long_sequence_gst(
ds_path, model_path+'.json', prep_fiducial_path, meas_fiducial_path, germ_path, self.maxLens,
advanced_options=self.options, verbosity=10
)
# TODO assert correctness
class CPTPGatesTester(LongSequenceGSTBase):
# TODO optimize!!
def setUp(self):
super(CPTPGatesTester, self).setUp()
self.model.set_all_parameterizations("CPTPLND")
class SGatesTester(LongSequenceGSTBase):
def setUp(self):
super(SGatesTester, self).setUp()
self.model.set_all_parameterizations("S")
class HPlusSGatesTester(LongSequenceGSTBase):
# TODO optimize!!!!
def setUp(self):
super(HPlusSGatesTester, self).setUp()
self.model.set_all_parameterizations("H+S")
class GLNDModelTester(LongSequenceGSTBase):
def setUp(self):
super(GLNDModelTester, self).setUp()
for lbl, gate in self.model.operations.items():
self.model.operations[lbl] = operation.convert(gate, "GLND", "gm")
self.model.default_gauge_group = UnitaryGaugeGroup(self.model.state_space, "gm")
class MapCalcTester(LongSequenceGSTBase):
def setUp(self):
super(MapCalcTester, self).setUp()
self.model._calcClass = mapforwardsim.MapForwardSimulator
self.options = {}
class BadFitTester(LongSequenceGSTWithChi2):
def setUp(self):
super(BadFitTester, self).setUp()
self.options = {
'bad_fit_threshold': -100
}
class RobustDataScalingTester(LongSequenceGSTBase):
@classmethod
def setUpClass(cls):
super(RobustDataScalingTester, cls).setUpClass()
datagen_gateset = cls.model.depolarize(op_noise=0.1, spam_noise=0.03).rotate((0.05, 0.13, 0.02))
ds2 = pdata.simulate_data(
datagen_gateset, cls.lsgstStrings[-1], num_samples=1000, sample_error='binomial', seed=100
).copy_nonstatic()
ds2.add_counts_from_dataset(cls.ds)
ds2.done_adding_data()
cls.ds = ds2
def setUp(self):
super(RobustDataScalingTester, self).setUp()
self.options = {
'bad_fit_threshold': -100,
'on_bad_fit': ["do nothing", "robust", "Robust", "robust+", "Robust+"]
}
def test_long_sequence_gst_raises_on_bad_badfit_options(self):
with self.assertRaises(ValueError):
ls.run_long_sequence_gst(
self.ds, self.model, self.prep_fids, self.meas_fids,
self.germs, self.maxLens,
advanced_options={'bad_fit_threshold': -100,
'on_bad_fit': ["foobar"]}
)