Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions tensorcircuit/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,7 @@ def reduced_density_matrix(
state: Union[Tensor, QuOperator],
cut: Union[int, List[int]],
p: Optional[Tensor] = None,
normalize: bool = True,
) -> Union[Tensor, QuOperator]:
"""
Compute the reduced density matrix from the quantum state ``state``.
Expand All @@ -1658,6 +1659,7 @@ def reduced_density_matrix(
:type p: Optional[Tensor]
:return: The reduced density matrix.
:rtype: Union[Tensor, QuOperator]
:normalize: if True, returns a trace 1 density matrix. Otherwise does not normalize.
"""
if isinstance(cut, list) or isinstance(cut, tuple) or isinstance(cut, set):
traceout = list(cut)
Expand Down Expand Up @@ -1700,7 +1702,9 @@ def reduced_density_matrix(
rho = backend.reshape(
rho, [2 ** (freedom - len(traceout)), 2 ** (freedom - len(traceout))]
)
rho /= backend.trace(rho)
if normalize:
rho /= backend.trace(rho)


else:
w = state / backend.norm(state)
Expand All @@ -1715,7 +1719,9 @@ def reduced_density_matrix(
rho = w @ backend.adjoint(w)
else:
rho = w @ backend.diagflat(p) @ backend.adjoint(w)
rho /= backend.trace(rho)
if normalize:
rho /= backend.trace(rho)

return rho


Expand Down