dq.ptrace
ptrace(
x: QArrayLike, keep: int | tuple[int, ...], dims: tuple[int, ...] | None = None
) -> QArray
Returns the partial trace of a ket, bra or density matrix.
Parameters
-
x
(qarray-like of shape (..., n, 1) or (..., 1, n) or (..., n, n))
–
Ket, bra or density matrix of a composite system.
-
keep
(int or tuple of ints)
–
Dimensions to keep after partial trace.
-
dims
(tuple of ints or None)
–
Dimensions of each subsystem in the composite system Hilbert space tensor product. Defaults to
None
(x.dims
if available, single Hilbert spacedims=(n,)
otherwise).
Returns
(qarray of shape (..., m, m)) Density matrix (with m <= n
).
Note
The returned object is always a density matrix, even if the input is a ket or a bra.
Examples
>>> psi_abc = dq.fock((3, 4, 5), (0, 2, 1))
>>> psi_abc.dims
(3, 4, 5)
>>> psi_abc.shape
(60, 1)
>>> rho_a = dq.ptrace(psi_abc, 0)
>>> rho_a.dims
(3,)
>>> rho_a.shape
(3, 3)
>>> rho_bc = dq.ptrace(psi_abc, (1, 2))
>>> rho_bc.dims
(4, 5)
>>> rho_bc.shape
(20, 20)
If the input qarray-like x
does not hold Hilbert space dimensions, you
can specify them with the argument dims
. For example, to trace out the second
subsystem of the Bell state \((\ket{00}+\ket{11})/\sqrt2\):
>>> bell_state = np.array([1, 0, 0, 1])[:, None] / np.sqrt(2)
>>> bell_state.shape
(4, 1)
>>> dq.ptrace(bell_state, 0, dims=(2, 2))
QArray: shape=(2, 2), dims=(2,), dtype=float32, layout=dense
[[0.5 0. ]
[0. 0.5]]