Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix simdiag not returning orthonormal eigenvectors #2269

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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