Skip to content

Commit ea6f217

Browse files
authored
Merge pull request #227 from abhinavd/option-partialtrace
Add option to not always normalise
2 parents 52dfa0f + 8e0c9ff commit ea6f217

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

tensorcircuit/quantum.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ def reduced_density_matrix(
16451645
state: Union[Tensor, QuOperator],
16461646
cut: Union[int, List[int]],
16471647
p: Optional[Tensor] = None,
1648+
normalize: bool = True,
16481649
) -> Union[Tensor, QuOperator]:
16491650
"""
16501651
Compute the reduced density matrix from the quantum state ``state``.
@@ -1658,6 +1659,7 @@ def reduced_density_matrix(
16581659
:type p: Optional[Tensor]
16591660
:return: The reduced density matrix.
16601661
:rtype: Union[Tensor, QuOperator]
1662+
:normalize: if True, returns a trace 1 density matrix. Otherwise does not normalize.
16611663
"""
16621664
if isinstance(cut, list) or isinstance(cut, tuple) or isinstance(cut, set):
16631665
traceout = list(cut)
@@ -1700,7 +1702,9 @@ def reduced_density_matrix(
17001702
rho = backend.reshape(
17011703
rho, [2 ** (freedom - len(traceout)), 2 ** (freedom - len(traceout))]
17021704
)
1703-
rho /= backend.trace(rho)
1705+
if normalize:
1706+
rho /= backend.trace(rho)
1707+
17041708

17051709
else:
17061710
w = state / backend.norm(state)
@@ -1715,7 +1719,9 @@ def reduced_density_matrix(
17151719
rho = w @ backend.adjoint(w)
17161720
else:
17171721
rho = w @ backend.diagflat(p) @ backend.adjoint(w)
1718-
rho /= backend.trace(rho)
1722+
if normalize:
1723+
rho /= backend.trace(rho)
1724+
17191725
return rho
17201726

17211727

0 commit comments

Comments
 (0)