Skip to content

Add option to not always normalise #227

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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