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 extract_states preservation hermiticity. #2214

Merged
merged 3 commits into from
Aug 15, 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
2 changes: 2 additions & 0 deletions doc/changes/2214.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed issue where `extrac_states` did not preserve hermiticity.
Fixed issue where `rand_herm` did not set the private attribute _isherm to True.
2 changes: 1 addition & 1 deletion qutip/qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,7 @@ def extract_states(self, states_inds, normalize=False):

"""
if self.isoper:
q = Qobj(self.data[states_inds, :][:, states_inds])
q = Qobj(self.data[states_inds, :][:, states_inds], isherm=self._isherm or None)
elif self.isket:
q = Qobj(self.data[states_inds, :])
elif self.isbra:
Expand Down
2 changes: 1 addition & 1 deletion qutip/random_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def rand_herm(N, density=0.75, dims=None, pos_def=False, seed=None):
M = _rand_herm_dense(N, density, pos_def)
else:
raise TypeError('Input N must be an integer or array_like.')
return Qobj(M, dims=dims)
return Qobj(M, dims=dims, isherm=True)


def _rand_herm_sparse(N, density, pos_def):
Expand Down
8 changes: 8 additions & 0 deletions qutip/tests/test_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,14 @@ def test_overlap():
assert np.allclose(Ad.overlap(Bd), ans)
assert np.allclose(A.overlap(Bd), np.conj(ans))

def test_extract_states_hermiticity():
"""Test that hermiticity is preserved after extract_states."""

H_t = rand_herm(10, seed=0)
H_t_reduced = H_t.extract_states(range(3))

assert H_t_reduced._isherm == True


def test_unit():
"""
Expand Down