Skip to content

Commit

Permalink
Merge pull request qutip#2269 from Sola85/simdiag-eigh-fix
Browse files Browse the repository at this point in the history
fix simdiag not returning orthonormal eigenvectors
  • Loading branch information
Ericgig authored Nov 27, 2023
2 parents f0c507e + b954cd3 commit e841a89
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/changes/2269.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed simdiag not returning orthonormal eigenvectors.
2 changes: 1 addition & 1 deletion qutip/simdiag.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _degen(tol, vecs, ops, i=0):
/ (1 - np.abs(dot)**2)**0.5)

subspace = vecs.conj().T @ ops[i].full() @ vecs
eigvals, eigvecs = la.eig(subspace)
eigvals, eigvecs = la.eigh(subspace)

perm = np.argsort(eigvals)
eigvals = eigvals[perm]
Expand Down
20 changes: 20 additions & 0 deletions qutip/tests/test_simdiag.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ def test_simdiag_degen_large():
atol=1e-13
)

def test_simdiag_orthonormal_eigenvectors():

# Special matrix that used to be problematic (see Issue #2268)
a = np.array([[1, 0, 1, -1, 0],
[0, 4, 0, 0, 1],
[1, 0, 4, 1, 0],
[-1, 0, 1, 4, 0],
[0, 1, 0, 0, 4]])

_, evecs = qutip.simdiag([qutip.Qobj(a), qutip.qeye(5)])
evecs = np.array([evec.full() for evec in evecs]).squeeze()

# Check that eigenvectors form an othonormal basis
# (<=> matrix of eigenvectors is unitary)
np.testing.assert_allclose(
evecs@evecs.conj().T,
np.eye(len(evecs)),
atol=1e-13
)


def test_simdiag_no_input():
with pytest.raises(ValueError):
Expand Down

0 comments on commit e841a89

Please sign in to comment.