/
test_core.py
428 lines (365 loc) · 16.3 KB
/
test_core.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
import numpy as np
from ..util import BaseCase
from . import fixtures
import pygsti.construction as pc
from pygsti.objects import Circuit, Label
from pygsti.algorithms import core
class CoreStdData(object):
def setUp(self):
super(CoreStdData, self).setUp()
self.ds = fixtures.ds.copy()
self.model = fixtures.model.copy()
self.fiducials = fixtures.fiducials
class CoreFuncTester(CoreStdData, BaseCase):
def test_gram_rank_and_evals(self):
rank, evals, target_evals = core.gram_rank_and_evals(self.ds, self.fiducials, self.fiducials, self.model)
# TODO assert correctness
def test_gram_rank_and_evals_raises_on_no_target(self):
# XXX is this neccessary? EGN: probably not
with self.assertRaises(ValueError):
core.gram_rank_and_evals(self.ds, self.fiducials, self.fiducials, None)
def test_find_closest_unitary_opmx_raises_on_multi_qubit(self):
with self.assertRaises(ValueError):
core.find_closest_unitary_opmx(np.identity(16, 'd'))
class CoreLGSTTester(CoreStdData, BaseCase):
def setUp(self):
super(CoreLGSTTester, self).setUp()
self.datagen_gateset = fixtures.datagen_gateset
self.lgstStrings = fixtures.lgstStrings
def test_do_lgst(self):
mdl_lgst = core.do_lgst(
self.ds, self.fiducials, self.fiducials, self.model,
svdTruncateTo=4
)
# TODO assert correctness
# XXX is this neccessary? EGN: tests higher verbosity printing.
mdl_lgst_2 = core.do_lgst(
self.ds, self.fiducials, self.fiducials, self.model,
svdTruncateTo=4, verbosity=10
)
# TODO assert correctness
self.assertAlmostEqual(mdl_lgst.frobeniusdist(mdl_lgst_2), 0)
def test_do_lgst_raises_on_no_target(self):
# XXX is this neccessary?
with self.assertRaises(ValueError):
core.do_lgst(
self.ds, self.fiducials, self.fiducials, None, svdTruncateTo=4
)
def test_do_lgst_raises_on_no_spam_dict(self):
with self.assertRaises(ValueError):
core.do_lgst(
self.ds, self.fiducials, self.fiducials, None,
opLabels=list(self.model.operations.keys()), svdTruncateTo=4
)
def test_do_lgst_raises_on_bad_fiducials(self):
bad_fids = pc.circuit_list([('Gx',), ('Gx',), ('Gx',), ('Gx',)])
with self.assertRaises(ValueError):
core.do_lgst(
self.ds, bad_fids, bad_fids, self.model, svdTruncateTo=4
) # bad fiducials (rank deficient)
def test_do_lgst_raises_on_incomplete_ab_matrix(self):
incomplete_strings = self.lgstStrings[5:] # drop first 5 strings...
bad_ds = pc.generate_fake_data(
self.datagen_gateset, incomplete_strings,
nSamples=10, sampleError='none')
with self.assertRaises(KeyError):
core.do_lgst(
bad_ds, self.fiducials, self.fiducials, self.model,
svdTruncateTo=4
)
def test_do_lgst_raises_on_incomplete_x_matrix(self):
incomplete_strings = self.lgstStrings[:-5] # drop last 5 strings...
bad_ds = pc.generate_fake_data(
self.datagen_gateset, incomplete_strings,
nSamples=10, sampleError='none')
with self.assertRaises(KeyError):
core.do_lgst(
bad_ds, self.fiducials, self.fiducials, self.model,
svdTruncateTo=4
)
class CoreELGSTTester(CoreStdData, BaseCase):
def setUp(self):
super(CoreELGSTTester, self).setUp()
self.mdl_clgst = fixtures.mdl_clgst.copy()
self.elgstStrings = fixtures.elgstStrings
def test_do_exlgst(self):
err_vec, model = core.do_exlgst(
self.ds, self.mdl_clgst, self.elgstStrings[0], self.fiducials,
self.fiducials, self.model, regularizeFactor=1e-3, svdTruncateTo=4
)
model._check_paramvec()
# TODO assert correctness
# XXX is this neccesary? (verbosity increase)
err_vec_2, model_2 = core.do_exlgst(
self.ds, self.mdl_clgst, self.elgstStrings[0], self.fiducials,
self.fiducials, self.model, regularizeFactor=1e-3, svdTruncateTo=4,
verbosity=10
)
model_2._check_paramvec()
# TODO assert correctness
self.assertAlmostEqual(model.frobeniusdist(model_2), 0)
def test_do_iterative_exlgst(self):
mdl_exlgst = core.do_iterative_exlgst(
self.ds, self.mdl_clgst, self.fiducials, self.fiducials,
self.elgstStrings, targetModel=self.model, svdTruncateTo=4
)
# TODO assert correctness
# XXX this doesn't really look useful...
mdl_exlgst_2 = core.do_iterative_exlgst(
self.ds, self.mdl_clgst, self.fiducials, self.fiducials,
self.elgstStrings, targetModel=self.model, svdTruncateTo=4,
verbosity=10
)
# TODO assert correctness
self.assertAlmostEqual(mdl_exlgst.frobeniusdist(mdl_exlgst_2), 0)
# XXX this doesn't look useful either
all_min_errs, all_gs_exlgst_tups = core.do_iterative_exlgst(
self.ds, self.mdl_clgst, self.fiducials, self.fiducials,
[[cir.tup for cir in gsList] for gsList in self.elgstStrings],
targetModel=self.model, svdTruncateTo=4,
returnAll=True, returnErrorVec=True
)
# TODO assert correctness
self.assertAlmostEqual(mdl_exlgst.frobeniusdist(all_gs_exlgst_tups[-1]), 0)
def test_do_iterative_exlgst_with_regularize_factor(self):
mdl_exlgst = core.do_iterative_exlgst(
self.ds, self.mdl_clgst, self.fiducials, self.fiducials,
self.elgstStrings, targetModel=self.model, svdTruncateTo=4,
regularizeFactor=10
)
# TODO assert correctness
def test_do_iterative_exlgst_check_jacobian(self):
mdl_exlgst = core.do_iterative_exlgst(
self.ds, self.mdl_clgst, self.fiducials, self.fiducials,
self.elgstStrings, targetModel=self.model, svdTruncateTo=4,
check_jacobian=True
)
# TODO assert correctness
class CoreMC2GSTTester(CoreStdData, BaseCase):
def setUp(self):
super(CoreMC2GSTTester, self).setUp()
self.mdl_clgst = fixtures.mdl_clgst.copy()
self.lsgstStrings = fixtures.lsgstStrings
def test_do_mc2gst(self):
mdl_lsgst = core.do_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings[0],
minProbClipForWeighting=1e-4, probClipInterval=(-1e6, 1e6)
)
# TODO assert correctness
def test_do_mc2gst_regularize_factor(self):
mdl_lsgst = core.do_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings[0],
minProbClipForWeighting=1e-4, probClipInterval=(-1e6, 1e6),
regularizeFactor=1e-3
)
# TODO assert correctness
def test_do_mc2gst_CPTP_penalty_factor(self):
mdl_lsgst = core.do_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings[0],
minProbClipForWeighting=1e-4, probClipInterval=(-1e6, 1e6),
cptp_penalty_factor=1.0
)
# TODO assert correctness
def test_do_mc2gst_SPAM_penalty_factor(self):
mdl_lsgst = core.do_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings[0],
minProbClipForWeighting=1e-4, probClipInterval=(-1e6, 1e6),
spam_penalty_factor=1.0
)
# TODO assert correctness
def test_do_mc2gst_CPTP_SPAM_penalty_factor(self):
mdl_lsgst = core.do_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings[0],
minProbClipForWeighting=1e-4, probClipInterval=(-1e6, 1e6),
cptp_penalty_factor=1.0, spam_penalty_factor=1.0
)
# TODO assert correctness
def test_do_mc2gst_alias_model(self):
aliased_list = [
Circuit([
(x if x != Label("Gx") else Label("GA1")) for x in mdl
]) for mdl in self.lsgstStrings[0]
]
aliased_model = self.mdl_clgst.copy()
aliased_model.operations['GA1'] = self.mdl_clgst.operations['Gx']
aliased_model.operations.pop('Gx')
mdl_lsgst = core.do_mc2gst(
self.ds, aliased_model, aliased_list, minProbClipForWeighting=1e-4,
probClipInterval=(-1e6, 1e6),
opLabelAliases={Label('GA1'): Circuit(['Gx'])}
)
# TODO assert correctness
def test_do_iterative_mc2gst(self):
mdl_lsgst = core.do_iterative_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings,
minProbClipForWeighting=1e-6, probClipInterval=(-1e6, 1e6)
)
# TODO assert correctness
# XXX are these useful? (verbosity test)
mdl_lsgst_2 = core.do_iterative_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings, verbosity=10,
minProbClipForWeighting=1e-6, probClipInterval=(-1e6, 1e6)
)
# TODO assert correctness
self.assertAlmostEqual(mdl_lsgst.frobeniusdist(mdl_lsgst_2), 0)
all_min_errs, all_gs_lsgst_tups = core.do_iterative_mc2gst(
self.ds, self.mdl_clgst,
[[mdl.tup for mdl in gsList] for gsList in self.lsgstStrings],
minProbClipForWeighting=1e-6, probClipInterval=(-1e6, 1e6),
returnAll=True, returnErrorVec=True
)
# TODO assert correctness
self.assertAlmostEqual(mdl_lsgst.frobeniusdist(all_gs_lsgst_tups[-1]), 0)
def test_do_iterative_mc2gst_regularize_factor(self):
mdl_lsgst = core.do_iterative_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings,
minProbClipForWeighting=1e-6, probClipInterval=(-1e6, 1e6),
regularizeFactor=10
)
# TODO assert correctness
def test_do_iterative_mc2gst_check_jacobian(self):
mdl_lsgst = core.do_iterative_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings,
minProbClipForWeighting=1e-6, probClipInterval=(-1e6, 1e6),
check_jacobian=True
)
# TODO assert correctness
def test_do_iterative_mc2gst_use_freq_weighted_chi2(self):
mdl_lsgst = core.do_iterative_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings,
minProbClipForWeighting=1e-6, probClipInterval=(-1e6, 1e6),
useFreqWeightedChiSq=True
)
# TODO assert correctness
def test_do_iterative_mc2gst_circuit_set_labels(self):
mdl_lsgst = core.do_iterative_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings,
minProbClipForWeighting=1e-6, probClipInterval=(-1e6, 1e6),
circuitSetLabels=["Set1", "Set2", "Set3"]
)
# TODO assert correctness
def test_do_iterative_mc2gst_circuit_weights_dict(self):
mdl_lsgst = core.do_iterative_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings,
minProbClipForWeighting=1e-6, probClipInterval=(-1e6, 1e6),
circuitWeightsDict={('Gx',): 2.0}
)
# TODO assert correctness
def test_do_mc2gst_raises_on_out_of_memory(self):
with self.assertRaises(MemoryError):
core.do_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings[0],
minProbClipForWeighting=1e-4, probClipInterval=(-1e6, 1e6),
memLimit=0
)
def test_do_mc2gst_raises_on_conflicting_spec(self):
with self.assertRaises(AssertionError):
core.do_mc2gst(
self.ds, self.mdl_clgst, self.lsgstStrings[0],
minProbClipForWeighting=1e-4, probClipInterval=(-1e6, 1e6),
regularizeFactor=1e-3, cptp_penalty_factor=1.0
)
# XXX shouldn't this code be reused?
class CoreMLGSTTester(CoreStdData, BaseCase):
def setUp(self):
super(CoreMLGSTTester, self).setUp()
self.mdl_clgst = fixtures.mdl_clgst.copy()
self.lsgstStrings = fixtures.lsgstStrings
def test_do_mlgst(self):
model = core.do_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
probClipInterval=(-1e2, 1e2)
)
# TODO assert correctness
def test_do_mlgst_CPTP_penalty_factor(self):
model = core.do_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), cptp_penalty_factor=1.0
)
# TODO assert correctness
def test_do_mlgst_SPAM_penalty_factor(self):
model = core.do_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), spam_penalty_factor=1.0
)
# TODO assert correctness
def test_do_mlgst_CPTP_SPAM_penalty_factor(self):
# this test often gives an assetion error "finite Jacobian has
# inf norm!" on Travis CI Python 3 case. Just ignore for now.
# FUTURE: see what we can do in custom LM about scaling large
# jacobians...
self.skipTest("Ignore for now.")
model = core.do_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), cptp_penalty_factor=1.0,
spam_penalty_factor=1.0
)
# TODO assert correctness
def test_do_mlgst_alias_model(self):
aliased_list = [
Circuit([
(x if x != Label("Gx") else Label("GA1")) for x in mdl
]) for mdl in self.lsgstStrings[0]
]
aliased_model = self.mdl_clgst.copy()
aliased_model.operations['GA1'] = self.mdl_clgst.operations['Gx']
aliased_model.operations.pop('Gx')
model = core.do_mlgst(
self.ds, aliased_model, aliased_list, minProbClip=1e-4,
probClipInterval=(-1e6, 1e6),
opLabelAliases={Label('GA1'): Circuit(['Gx'])}
)
# TODO assert correctness
def test_do_iterative_mlgst(self):
model = core.do_iterative_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings, minProbClip=1e-4,
probClipInterval=(-1e2, 1e2)
)
# # XXX This probably shouldn't exist?
# # From the core.do_iterative_mlgst docstring:
# # check : boolean, optional
# # If True, perform extra checks within code to verify correctness. Used
# # for testing, and runs much slower when True.
# def test_do_iterative_mlgst_with_check(self):
# model = core.do_iterative_mlgst(
# self.ds, self.mdl_clgst, self.lsgstStrings, minProbClip=1e-4,
# probClipInterval=(-1e2, 1e2), check=True
# )
def test_do_iterative_mlgst_circuit_set_labels(self):
model = core.do_iterative_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings, minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), circuitSetLabels=["Set1", "Set2", "Set3"]
)
# TODO assert correctness
def test_do_iterative_mlgst_use_freq_weighted_chi2(self):
model = core.do_iterative_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings, minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), useFreqWeightedChiSq=True
)
# TODO assert correctness
def test_do_iterative_mlgst_circuit_weights_dict(self):
model = core.do_iterative_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings, minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), circuitWeightsDict={(Label('Gx'),): 2.0}
)
# TODO assert correctness
def test_do_iterative_mlgst_always_perform_MLE(self):
model = core.do_iterative_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings, minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), alwaysPerformMLE=True
)
# TODO assert correctness
def test_do_mlgst_raises_on_out_of_memory(self):
with self.assertRaises(MemoryError):
core.do_mlgst(
self.ds, self.mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), memLimit=0
)
# XXX if this function needs explicit coverage, it should be public!
def test_do_mlgst_base_forcefn_grad(self):
forcefn_grad = np.ones((1, self.mdl_clgst.num_params()), 'd')
model = core._do_mlgst_base(
self.ds, self.mdl_clgst, self.lsgstStrings[0], minProbClip=1e-4,
probClipInterval=(-1e2, 1e2), forcefn_grad=forcefn_grad
)
# TODO assert correctness