Skip to content

Commit e65446f

Browse files
committed
readability and changing of absolute constant used in an assertion
1 parent b90be51 commit e65446f

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

pygsti/modelmembers/operations/lindbladcoefficients.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def set_elementary_errorgens(self, elementary_errorgens, on_missing='ignore', tr
528528
raise ValueError("Missing entry for %s in dictionary of elementary errorgens." % str(eeg_lbl))
529529
flat_data[i] = val
530530

531-
self.block_data[(slice(None, None),) * self.block_data.ndim] = flat_data.reshape(self.block_data.shape)
531+
self.block_data[:] = flat_data.reshape(self.block_data.shape)
532532
self._truncate_block_data(truncate)
533533

534534
#set a flag to indicate that the coefficients (as returned by elementary_errorgens)
@@ -652,7 +652,7 @@ def _block_data_to_params(self, truncate=False):
652652
in the case of `'other'` blocks.
653653
"""
654654
if truncate is False:
655-
ttol = -1e-15 # (was -1e-12) # truncation tolerance
655+
ttol = -1e-14 # (was -1e-12) # truncation tolerance
656656
elif truncate is True:
657657
ttol = -_np.inf
658658
else:
@@ -725,25 +725,24 @@ def _block_data_to_params(self, truncate=False):
725725
nonzero_block_data = perm_block_data[0:num_nonzero, 0:num_nonzero]
726726
assert(_np.isclose(_np.linalg.norm(self.block_data), _np.linalg.norm(nonzero_block_data)))
727727

728-
#evals, U = _np.linalg.eigh(nonzero_block_data) # works too (assert hermiticity above)
729-
evals, U = _np.linalg.eig(nonzero_block_data)
728+
evals, U = _np.linalg.eigh(nonzero_block_data)
730729

731730
assert(all([ev > ttol for ev in evals])), \
732731
("Lindblad coefficients are not CPTP (truncate == %s)! (largest neg = %g)"
733-
% (str(truncate), min(evals.real)))
732+
% (str(truncate), min(evals)))
734733

735734
if ttol < 0: # if we're truncating and assert above allows *negative* eigenvalues
736735
#push any slightly negative evals of other_projs positive so that
737736
# the Cholesky decomp will work.
738-
Ui = _np.linalg.inv(U)
737+
Ui = U.T.conj()
739738
pos_evals = evals.clip(1e-16, None)
740-
nonzero_block_data = _np.dot(U, _np.dot(_np.diag(pos_evals), Ui))
739+
nonzero_block_data = U @ (pos_evals[:, None] * Ui)
741740
try:
742741
nonzero_Lmx = _np.linalg.cholesky(nonzero_block_data)
743742
# if Lmx not postitive definite, try again with 1e-12 (same lines as above)
744-
except _np.linalg.LinAlgError: # pragma: no cover
743+
except _np.linalg.LinAlgError: # pragma: no cover
745744
pos_evals = evals.clip(1e-12, 1e100) # pragma: no cover
746-
nonzero_block_data = _np.dot(U, _np.dot(_np.diag(pos_evals), Ui)) # pragma: no cover
745+
nonzero_block_data = U @ (pos_evals[:, None] * Ui) # pragma: no cover
747746
nonzero_Lmx = _np.linalg.cholesky(nonzero_block_data)
748747
else: # truncate == False or == 0 case
749748
nonzero_Lmx = _np.linalg.cholesky(nonzero_block_data)
@@ -753,15 +752,15 @@ def _block_data_to_params(self, truncate=False):
753752
Lmx = perm.T @ perm_Lmx @ perm
754753

755754
for i in range(num_bels):
756-
assert(_np.linalg.norm(_np.imag(Lmx[i, i])) < IMAG_TOL)
755+
assert(_np.abs(Lmx[i, i].imag) < IMAG_TOL)
757756
params[i, i] = Lmx[i, i].real
758757
for j in range(i):
759758
params[i, j] = Lmx[i, j].real
760759
params[j, i] = Lmx[i, j].imag
761760

762761
elif self._param_mode == "elements": # params mx stores block_data (hermitian) directly
763762
for i in range(num_bels):
764-
assert(_np.linalg.norm(_np.imag(self.block_data[i, i])) < IMAG_TOL)
763+
assert(_np.abs(self.block_data[i, i].imag) < IMAG_TOL)
765764
params[i, i] = self.block_data[i, i].real
766765
for j in range(i):
767766
params[i, j] = self.block_data[i, j].real

0 commit comments

Comments
 (0)