Skip to content

Commit 1032706

Browse files
committed
make RB theory functions gracefully handle linear algebra errors
1 parent d3b91e6 commit 1032706

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

pygsti/tools/rbtheory.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def predicted_rb_number(model, target_model, weights=None, d=None, rtype='EI'):
9090
"""
9191
if d is None: d = int(round(_np.sqrt(model.dim)))
9292
p = predicted_rb_decay_parameter(model, target_model, weights=weights)
93-
r = _rbtls.p_to_r(p, d=d, rtype=rtype)
93+
r = _rbtls.p_to_r(p, d=d, rtype=rtype) if p != _np.nan else _np.nan
9494
return r
9595

9696

@@ -132,15 +132,18 @@ def predicted_rb_decay_parameter(model, target_model, weights=None):
132132
The second largest eigenvalue of L. This is the RB decay parameter
133133
for various types of RB.
134134
"""
135-
L = L_matrix(model, target_model, weights=weights)
136-
E = _np.absolute(_np.linalg.eigvals(L))
137-
E = _np.flipud(_np.sort(E))
138-
if abs(E[0] - 1) > 10**(-12):
139-
_warnings.warn("Output may be unreliable because the model is not approximately trace-preserving.")
140-
141-
if E[1].imag > 10**(-10):
142-
_warnings.warn("Output may be unreliable because the RB decay constant has a significant imaginary component.")
143-
p = abs(E[1])
135+
try:
136+
L = L_matrix(model, target_model, weights=weights)
137+
E = _np.absolute(_np.linalg.eigvals(L))
138+
E = _np.flipud(_np.sort(E))
139+
if abs(E[0] - 1) > 10**(-12):
140+
_warnings.warn("Output may be unreliable because the model is not approximately trace-preserving.")
141+
142+
if E[1].imag > 10**(-10):
143+
_warnings.warn("Output may be unreliable because the RB decay constant has a significant imaginary component.")
144+
p = abs(E[1])
145+
except _np.linalg.LinAlgError:
146+
p = _np.nan
144147
return p
145148

146149

0 commit comments

Comments
 (0)