Skip to content

Commit

Permalink
ptrace selecting all always return dm
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericgig committed Jun 27, 2023
1 parent 49a3a70 commit e544940
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/changes/2129.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ptrace ket always return density matrix
7 changes: 5 additions & 2 deletions qutip/qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,9 +1357,12 @@ def ptrace(self, sel, sparse=None):
if sparse:
q = Qobj()
q.data, q.dims, _ = _ptrace(self, sel)
return q.tidyup() if settings.auto_tidyup else q
out = q.tidyup() if settings.auto_tidyup else q
else:
return _ptrace_dense(self, sel)
out = _ptrace_dense(self, sel)
if isket(out):
out = out.proj()
return out

def permute(self, order):
"""Permutes a composite quantum object.
Expand Down
16 changes: 15 additions & 1 deletion qutip/tests/test_ptrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ def test_ptrace_noncompound_rand(sparse, dm):
state = qutip.rand_ket(5)
if dm:
state = state.proj()
assert state.ptrace(0, sparse=sparse) == state
if state.isket:
target = state.proj()
else:
target = state
assert state.ptrace(0, sparse=sparse) == target


@pytest.mark.parametrize('pair', list(itertools.combinations(range(3), 2)))
Expand All @@ -49,9 +53,19 @@ def test_ptrace_unsorted_selection_subset(state, sparse, pair):
assert state_ordered == state_reversed


def test_ptrace_ket():
ket_1 = qutip.rand_ket(3)
ket_2 = qutip.rand_ket(4)
ket = qutip.tensor(ket_1, ket_2)
assert ket.ptrace([0, 1]) == ket.proj()
assert ket.ptrace([0]) == ket_1.proj() * ket_2.norm()


@pytest.mark.parametrize('permutation', list(itertools.permutations(range(3))))
def test_ptrace_unsorted_selection_all(state, sparse, permutation):
state_ptraced = state.ptrace(permutation, sparse=sparse)
if state.isket:
state = state.proj()
assert state.dims == state_ptraced.dims
assert state == state_ptraced

Expand Down

0 comments on commit e544940

Please sign in to comment.