Skip to content

Commit 4f2a270

Browse files
authored
Merge pull request #538 from sandialabs/feature-errorgen-propagation-refactors
Error Generator Propagation Module
2 parents 52da6cd + 5c68539 commit 4f2a270

38 files changed

+14964
-862
lines changed

Diff for: jupyter_notebooks/Tutorials/algorithms/ErrorGeneratorPropagation.ipynb

+788
Large diffs are not rendered by default.

Diff for: pygsti/algorithms/randomcircuit.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,9 @@ def create_random_circuit(pspec, length, qubit_labels=None, sampler='Qeliminatio
711711
1-element list consisting of a list of the relevant gate names (e.g., `lsargs` = ['Gi,
712712
'Gxpi, 'Gypi', 'Gzpi']).
713713
714-
rand_state: RandomState, optional
715-
A np.random.RandomState object for seeding RNG
714+
rand_state: RandomState or int, optional (default None)
715+
A np.random.RandomState object for seeding RNG. If an integer is passed in
716+
this is used to set the seed for a newly constructed RNG.
716717
717718
Returns
718719
-------
@@ -726,6 +727,8 @@ def create_random_circuit(pspec, length, qubit_labels=None, sampler='Qeliminatio
726727
lsargs = []
727728
if rand_state is None:
728729
rand_state = _np.random.RandomState()
730+
if isinstance(rand_state, int):
731+
rand_state = _np.random.RandomState(rand_state)
729732

730733
if isinstance(sampler, str):
731734

Diff for: pygsti/baseobjs/basis.py

+89-75
Large diffs are not rendered by default.

Diff for: pygsti/baseobjs/basisconstructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def sizes(self, dim, sparse):
442442
def std_matrices(matrix_dim):
443443
"""
444444
Get the elements of the matrix unit, or "standard", basis of matrix-dimension `matrix_dim`.
445-
The matrices are ordered so that the row index changes the fastest.
445+
The matrices are ordered so that the column index changes the fastest.
446446
447447
Constructs the standard basis spanning the density-matrix space given by
448448
`matrix_dim` x `matrix_dim` matrices.

Diff for: pygsti/baseobjs/errorgenbasis.py

+468-153
Large diffs are not rendered by default.

Diff for: pygsti/baseobjs/errorgenlabel.py

+144-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ class LocalElementaryErrorgenLabel(ElementaryErrorgenLabel):
2929
"""
3030
@classmethod
3131
def cast(cls, obj, sslbls=None, identity_label='I'):
32+
"""
33+
Method for casting an object to an instance of LocalElementaryErrorgenLabel
34+
35+
Parameters
36+
----------
37+
obj : `LocalElementaryErrorgenLabel`, `GlobalElementaryErrorgenLabel`, str, tuple or list
38+
Object to cast. If a `GlobalElementaryErrorgenLabel` then a value for the `sslbls`
39+
argument should be passed with the full list of state space labels for the system.
40+
Other castable options include:
41+
42+
-str: A string formatted as '<type>(<bel1>[,<bel2>])'. E.g. 'H(XX)' or
43+
'C(X,Y)'
44+
-tuple/list: These can be specified either in 'global-style' or 'local-style'.
45+
- local-style: format is (<type>, <bel1>[,<bel2>])
46+
- global-style:format is (<type>, (<bel1>,[<bel2>]), (<sslbls>))
47+
Where sslbls above is specifically the subset of state space labels this error
48+
generator acts on nontrivially. When specifying global-style tuple labels the sslbls kwarg of this method
49+
which contains the complete set of state-space labels must also be specified.
50+
51+
sslbls : tuple or list, optional (default None)
52+
A complete set of state space labels. Used when casting from a GlobalElementaryErrorgenLabel
53+
or from a tuple of length 3 (wherein the final element is interpreted as the set of ssblbs the error
54+
generator acts upon).
55+
56+
identity_label : str, optional (default 'I')
57+
An optional string specifying the label used to denote the identity in basis element labels.
58+
59+
Returns
60+
-------
61+
LocalElementaryErrorgenLabel
62+
"""
3263
if isinstance(obj, LocalElementaryErrorgenLabel):
3364
return obj
3465
elif isinstance(obj, GlobalElementaryErrorgenLabel):
@@ -65,11 +96,37 @@ def cast(cls, obj, sslbls=None, identity_label='I'):
6596
raise ValueError("Cannot convert %s to a local elementary errorgen label!" % str(obj))
6697

6798
def __init__(self, errorgen_type, basis_element_labels):
99+
"""
100+
Parameters
101+
----------
102+
errorgen_type : str
103+
A string corresponding to the error generator sector this error generator label is
104+
an element of. Allowed values are 'H', 'S', 'C' and 'A'.
105+
106+
basis_element_labels : tuple or list
107+
A list or tuple of strings labeling basis elements used to label this error generator.
108+
This is either length-1 for 'H' and 'S' type error generators, or length-2 for 'C' and 'A'
109+
type.
110+
"""
111+
#TODO: Store non-standard identity labels with object so we don't need to specify this in
112+
#support_indices.
68113
self.errorgen_type = str(errorgen_type)
69114
self.basis_element_labels = tuple(basis_element_labels)
115+
self._hash = hash((self.errorgen_type, self.basis_element_labels))
70116

71117
def __hash__(self):
72-
return hash((self.errorgen_type, self.basis_element_labels))
118+
return self._hash
119+
120+
#pickle management functions
121+
def __getstate__(self):
122+
state_dict = self.__dict__
123+
return state_dict
124+
125+
def __setstate__(self, state_dict):
126+
for k, v in state_dict.items():
127+
self.__dict__[k] = v
128+
#reinitialize the hash
129+
self._hash = hash((self.errorgen_type, self.basis_element_labels))
73130

74131
def __eq__(self, other):
75132
return (self.errorgen_type == other.errorgen_type
@@ -80,6 +137,16 @@ def __str__(self):
80137

81138
def __repr__(self):
82139
return str((self.errorgen_type, self.basis_element_labels))
140+
141+
def support_indices(self, identity_label='I'):
142+
"""
143+
Returns a sorted tuple of the elements of indices of the nontrivial basis
144+
element label entries for this label.
145+
"""
146+
nonidentity_indices = [i for i in range(len(self.basis_element_labels[0]))
147+
if any([bel[i] != identity_label for bel in self.basis_element_labels])]
148+
149+
return tuple(nonidentity_indices)
83150

84151

85152
class GlobalElementaryErrorgenLabel(ElementaryErrorgenLabel):
@@ -91,7 +158,47 @@ class GlobalElementaryErrorgenLabel(ElementaryErrorgenLabel):
91158

92159
@classmethod
93160
def cast(cls, obj, sslbls=None, identity_label='I'):
94-
""" TODO: docstring - lots in this module """
161+
"""
162+
Method for casting an object to an instance of GlobalElementaryErrorgenLabel
163+
164+
Parameters
165+
----------
166+
obj : `GlobalElementaryErrorgenLabel`, `LocalElementaryErrorgenLabel`, tuple or list
167+
Object to cast. If a `LocalElementaryErrorgenLabel` then a value for the `sslbls`
168+
argument should be passed with the full list of state space labels for the system.
169+
Other castable options include:
170+
171+
-str: Following formatting options are supported.
172+
- A string formatted as '<type>(<bel1>[,<bel2>]:(<sslbls>))' where <sslbls>
173+
is the subset of state-space labels this error generator acts on nontrivially
174+
specified as a comma-separated list. E.g. 'H(XX:0,1)' or 'S(XIY):0,2'.
175+
- A string formatted as <type><bel>:<sslbls>, where <sslbls>
176+
is the subset of state-space labels this error generator acts on nontrivially
177+
specified as a comma-separated list. E.g. 'HXX:0,1' or 'SIX:1'. Note this style
178+
is only compatible with basis element label error generators, and this only H and S.
179+
- A string formatted as <type><bel>. For this style the basis element label
180+
is assumed to correspond to the entire state space, and as such the sslbls kwarg
181+
for this method must also be specified. Like the previous example this is also
182+
only compatible with H and S terms.
183+
-tuple/list: These can be specified either in 'global-style' or 'local-style'.
184+
- local-style: format is (<type>, <bel1>[,<bel2>])
185+
- global-style:format is (<type>, (<bel1>,[<bel2>]), (<sslbls>))
186+
Where sslbls above is specifically the subset of state space labels this error
187+
generator acts on nontrivially. When specifying global-style tuple labels the sslbls kwarg of this method
188+
which contains the complete set of state-space labels must also be specified.
189+
190+
sslbls : tuple or list, optional (default None)
191+
A complete set of state space labels. Used when casting from a LocalElementaryErrorgenLabel
192+
or from a tuple of length 2 (wherein the final element is interpreted as the set of ssblbs the error
193+
generator acts upon).
194+
195+
identity_label : str, optional (default 'I')
196+
An optional string specifying the label used to denote the identity in basis element labels.
197+
198+
Returns
199+
-------
200+
GlobalElementaryErrorgenLabel
201+
"""
95202
if isinstance(obj, GlobalElementaryErrorgenLabel):
96203
return obj
97204
elif isinstance(obj, LocalElementaryErrorgenLabel):
@@ -116,7 +223,7 @@ def cast(cls, obj, sslbls=None, identity_label='I'):
116223
return cls.cast(LocalElementaryErrorgenLabel.cast(obj), sslbls, identity_label)
117224
else: # no parenthesis, assume of form "HXX:Q0,Q1" or local label, e.g. "HXX"
118225
if ':' in obj:
119-
typ_bel_str, sslbl_str = in_parens.split(':')
226+
typ_bel_str, sslbl_str = obj.split(':')
120227
sslbls = [_to_int_or_strip(x) for x in sslbl_str.split(',')]
121228
return cls(typ_bel_str[0], (typ_bel_str[1:],), sslbls)
122229
else: # treat as a local label
@@ -132,6 +239,27 @@ def cast(cls, obj, sslbls=None, identity_label='I'):
132239
raise ValueError("Cannot convert %s to a global elementary errorgen label!" % str(obj))
133240

134241
def __init__(self, errorgen_type, basis_element_labels, sslbls, sort=True):
242+
"""
243+
Parameters
244+
----------
245+
errorgen_type : str
246+
A string corresponding to the error generator sector this error generator label is
247+
an element of. Allowed values are 'H', 'S', 'C' and 'A'.
248+
249+
basis_element_labels : tuple or list
250+
A list or tuple of strings labeling basis elements used to label this error generator.
251+
This is either length-1 for 'H' and 'S' type error generators, or length-2 for 'C' and 'A'
252+
type.
253+
254+
sslbls : tuple or list
255+
A tuple or list of state space labels corresponding to the qudits upon which this error generator
256+
is supported.
257+
258+
sort : bool, optional (default True)
259+
If True then the input state space labels are first sorted, and then the used basis element labels
260+
are sorted to match the order to the newly sorted state space labels.
261+
"""
262+
135263
if sort:
136264
sorted_indices, sslbls = zip(*sorted(enumerate(sslbls), key=lambda x: x[1]))
137265
basis_element_labels = [''.join([bel[i] for i in sorted_indices]) for bel in basis_element_labels]
@@ -141,9 +269,21 @@ def __init__(self, errorgen_type, basis_element_labels, sslbls, sort=True):
141269
self.sslbls = tuple(sslbls)
142270
# Note: each element of basis_element_labels must be an iterable over
143271
# 1-qubit basis labels of length len(self.sslbls) (?)
272+
self._hash = hash((self.errorgen_type, self.basis_element_labels, self.sslbls))
144273

145274
def __hash__(self):
146-
return hash((self.errorgen_type, self.basis_element_labels, self.sslbls))
275+
return self._hash
276+
277+
#pickle management functions
278+
def __getstate__(self):
279+
state_dict = self.__dict__
280+
return state_dict
281+
282+
def __setstate__(self, state_dict):
283+
for k, v in state_dict.items():
284+
self.__dict__[k] = v
285+
#reinitialize the hash
286+
self._hash = hash((self.errorgen_type, self.basis_element_labels, self.sslbls))
147287

148288
def __eq__(self, other):
149289
return (self.errorgen_type == other.errorgen_type

0 commit comments

Comments
 (0)