/
simplifierhelper.py
390 lines (322 loc) · 12.6 KB
/
simplifierhelper.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
""" Defines the SimplifierHelper class and supporting functionality."""
from __future__ import division, print_function, absolute_import, unicode_literals
#***************************************************************************************************
# Copyright 2015, 2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
# Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain rights
# in this software.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 or in the LICENSE file in the root pyGSTi directory.
#***************************************************************************************************
import numpy as _np
import scipy as _scipy
import itertools as _itertools
import collections as _collections
import warnings as _warnings
import time as _time
import uuid as _uuid
import bisect as _bisect
import copy as _copy
from ..tools import matrixtools as _mt
from ..tools import optools as _gt
from ..tools import slicetools as _slct
from ..tools import likelihoodfns as _lf
from ..tools import jamiolkowski as _jt
from ..tools import compattools as _compat
from ..tools import basistools as _bt
from ..tools import listtools as _lt
from ..tools import symplectic as _symp
from . import modelmember as _gm
from . import circuit as _cir
from . import operation as _op
from . import spamvec as _sv
from . import povm as _povm
from . import instrument as _instrument
from . import labeldicts as _ld
from . import gaugegroup as _gg
from . import matrixforwardsim as _matrixfwdsim
from . import mapforwardsim as _mapfwdsim
from . import termforwardsim as _termfwdsim
from . import explicitcalc as _explicitcalc
from ..baseobjs import VerbosityPrinter as _VerbosityPrinter
from ..baseobjs import Basis as _Basis
from ..baseobjs import Label as _Label
class SimplifierHelper(object):
"""
Defines the minimal interface for performing :class:`Circuit` "compiling"
(pre-processing for forward simulators, which only deal with preps, ops,
and effects) needed by :class:`Model`.
To simplify a circuit a `Model` doesn't, for instance, need to know *all*
possible state preparation labels, as a dict of preparation operations
would provide - it only needs a function to check if a given value is a
viable state-preparation label.
"""
pass
class BasicSimplifierHelper(SimplifierHelper):
"""
Performs the work of a :class:`SimplifierHelper` using user-supplied lists
"""
def __init__(self, preplbls, povmlbls, instrumentlbls,
povm_effect_lbls, instrument_member_lbls):
"""
Create a new BasicSimplifierHelper.
preplbls, povmlbls, instrumentlbls, povm_effect_lbls,
instrument_member_lbls : list
Lists of all the state-preparation, POVM, instrument,
POVM-effect, and instrument-member labels of a model.
"""
self.preplbls = preplbls
self.povmlbls = povmlbls
self.instrumentlbls = instrumentlbls
self.povm_effect_lbls = povm_effect_lbls
self.instrument_member_lbls = instrument_member_lbls
def is_prep_lbl(self, lbl):
"""Whether `lbl` is a valid state prep label (returns boolean)"""
return lbl in self.preplbls
def is_povm_lbl(self, lbl):
"""Whether `lbl` is a valid POVM label (returns boolean)"""
return lbl in self.povmlbls
def is_instrument_lbl(self, lbl):
"""Whether `lbl` is a valid instrument label (returns boolean)"""
return lbl in self.instrumentlbls
def get_default_prep_lbl(self):
"""
Gets the default state prep label (used when a circuit
is specified without one). Returns `None` if there is
no default and one *must* be specified.
Returns
-------
Label or None
"""
return self.preplbls[0] \
if len(self.preplbls) == 1 else None
def get_default_povm_lbl(self):
"""
Gets the default POVM label (used when a circuit
is specified without one). Returns `None` if there is
no default and one *must* be specified.
Returns
-------
Label or None
"""
return self.povmlbls[0] \
if len(self.povmlbls) == 1 else None
def has_preps(self):
"""Whether this model contains any state preps (returns boolean)"""
return len(self.preplbls) > 0
def has_povms(self):
"""Whether this model contains any POVM (returns boolean)"""
return len(self.povmlbls) > 0
def get_effect_labels_for_povm(self, povm_lbl):
"""
Gets the effect labels corresponding to the possible outcomes of POVM
label `povm_lbl`.
Parameters
----------
pomv_lbl : Label
Returns
-------
list
A list of strings which label the POVM outcomes.
"""
return self.povm_effect_lbls[povm_lbl]
def get_member_labels_for_instrument(self, inst_lbl):
"""
Gets the member labels corresponding to the possible outcomes of
the instrument labeled by `inst_lbl`.
Parameters
----------
inst_lbl : Label
Returns
-------
list
A list of strings which label the instrument members.
"""
return self.instrument_member_lbls[inst_lbl]
class MemberDictSimplifierHelper(SimplifierHelper):
"""
Performs the work of a :class:`SimplifierHelper` using a set of
`OrderedMemberDict` objects, such as those contained in an
:class:`ExplicitOpModel`.
"""
def __init__(self, preps, povms, instruments):
"""
Create a new MemberDictSimplifierHelper.
Parameters
----------
preps, povms, instruments : OrderedMemberDict
"""
self.preps = preps
self.povms = povms
self.instruments = instruments
def is_prep_lbl(self, lbl):
"""Whether `lbl` is a valid state prep label (returns boolean)"""
return lbl in self.preps
def is_povm_lbl(self, lbl):
"""Whether `lbl` is a valid POVM label (returns boolean)"""
return lbl in self.povms
def is_instrument_lbl(self, lbl):
"""Whether `lbl` is a valid instrument label (returns boolean)"""
return lbl in self.instruments
def get_default_prep_lbl(self):
"""
Gets the default state prep label (used when a circuit
is specified without one). Returns `None` if there is
no default and one *must* be specified.
Returns
-------
Label or None
"""
return tuple(self.preps.keys())[0] \
if len(self.preps) == 1 else None
def get_default_povm_lbl(self):
"""
Gets the default POVM label (used when a circuit
is specified without one). Returns `None` if there is
no default and one *must* be specified.
Returns
-------
Label or None
"""
return tuple(self.povms.keys())[0] \
if len(self.povms) == 1 else None
def has_preps(self):
"""Whether this model contains any state preps (returns boolean)"""
return len(self.preps) > 0
def has_povms(self):
"""Whether this model contains any POVM (returns boolean)"""
return len(self.povms) > 0
def get_effect_labels_for_povm(self, povm_lbl):
"""
Gets the effect labels corresponding to the possible outcomes of POVM
label `povm_lbl`.
Parameters
----------
pomv_lbl : Label
Returns
-------
list
A list of strings which label the POVM outcomes.
"""
return tuple(self.povms[povm_lbl].keys())
def get_member_labels_for_instrument(self, inst_lbl):
"""
Gets the member labels corresponding to the possible outcomes of
the instrument labeled by `inst_lbl`.
Parameters
----------
inst_lbl : Label
Returns
-------
list
A list of strings which label the instrument members.
"""
return tuple(self.instruments[inst_lbl].keys())
class MemberDictDictSimplifierHelper(SimplifierHelper):
"""
Performs the work of a :class:`SimplifierHelper` using a set of
dictionaries of `OrderedMemberDict` objects, such as those
contained in an :class:`ImplicitOpModel`.
"""
def __init__(self, prep_blks, povm_blks, instrument_blks):
"""
Create a new MemberDictDictSimplifierHelper.
Parameters
----------
prep_blks, povm_blks, instrument_blks : dict of OrderedMemberDict
"""
self.prep_blks = prep_blks
self.povm_blks = povm_blks
self.instrument_blks = instrument_blks
def is_prep_lbl(self, lbl):
"""Whether `lbl` is a valid state prep label (returns boolean)"""
return any([(lbl in prepdict) for prepdict in self.prep_blks.values()])
def is_povm_lbl(self, lbl):
"""Whether `lbl` is a valid POVM label (returns boolean)"""
return any([(lbl in povmdict) for povmdict in self.povm_blks.values()])
def is_instrument_lbl(self, lbl):
"""Whether `lbl` is a valid instrument label (returns boolean)"""
return any([(lbl in idict) for idict in self.instrument_blks.values()])
def get_default_prep_lbl(self):
"""
Gets the default state prep label (used when a circuit
is specified without one). Returns `None` if there is
no default and one *must* be specified.
Returns
-------
Label or None
"""
npreps = sum([len(prepdict) for prepdict in self.prep_blks.values()])
if npreps == 1:
for prepdict in self.prep_blks.values():
if len(prepdict) > 0:
return tuple(prepdict.keys())[0]
assert(False), "Logic error: one prepdict should have had lenght > 0!"
else:
return None
def get_default_povm_lbl(self):
"""
Gets the default POVM label (used when a circuit
is specified without one). Returns `None` if there is
no default and one *must* be specified.
Returns
-------
Label or None
"""
npovms = sum([len(povmdict) for povmdict in self.povm_blks.values()])
if npovms == 1:
for povmdict in self.povm_blks.values():
if len(povmdict) > 0:
return tuple(povmdict.keys())[0]
assert(False), "Logic error: one povmdict should have had lenght > 0!"
else:
return None
def has_preps(self):
"""Whether this model contains any state preps (returns boolean)"""
return any([(len(prepdict) > 0) for prepdict in self.prep_blks.values()])
def has_povms(self):
"""Whether this model contains any POVM (returns boolean)"""
return any([(len(povmdict) > 0) for povmdict in self.povm_blks.values()])
def get_effect_labels_for_povm(self, povm_lbl):
"""
Gets the effect labels corresponding to the possible outcomes of POVM
label `povm_lbl`.
Parameters
----------
pomv_lbl : Label
Returns
-------
list
A list of strings which label the POVM outcomes.
"""
for povmdict in self.povm_blks.values():
if povm_lbl in povmdict:
return tuple(povmdict[povm_lbl].keys())
raise KeyError("No POVM labeled %s!" % povm_lbl)
def get_member_labels_for_instrument(self, inst_lbl):
"""
Gets the member labels corresponding to the possible outcomes of
the instrument labeled by `inst_lbl`.
Parameters
----------
inst_lbl : Label
Returns
-------
list
A list of strings which label the instrument members.
"""
for idict in self.instrument_blks.values():
if inst_lbl in idict:
return tuple(idict[inst_lbl].keys())
raise KeyError("No instrument labeled %s!" % inst_lbl)
class ImplicitModelSimplifierHelper(MemberDictDictSimplifierHelper):
""" Performs the work of a "Simplifier Helper" using user-supplied dicts """
def __init__(self, implicitModel):
"""
Create a new ImplicitModelSimplifierHelper.
Parameters
----------
implicitModel : ImplicitOpModel
"""
super(ImplicitModelSimplifierHelper, self).__init__(
implicitModel.prep_blks, implicitModel.povm_blks, implicitModel.instrument_blks)