Skip to content

dq.ptrace

ptrace(x: ArrayLike, keep: int | tuple[int, ...], dims: tuple[int, ...]) -> Array

Returns the partial trace of a ket, bra or density matrix.

Parameters

  • x (array_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) –

    Dimensions of each subsystem in the composite system Hilbert space tensor product.

Returns

(array of shape (..., m, m)) Density matrix (with m <= n).

Raises

  • ValueError –

    If x is not a ket, bra or density matrix.

  • ValueError –

    If dims does not match the shape of x, or if keep is incompatible with dims.

Note

The returned object is always a density matrix, even if the input is a ket or a bra.

Examples

>>> psi_abc = dq.tensor(dq.fock(3, 0), dq.fock(4, 2), dq.fock(5, 1))
>>> psi_abc.shape
(60, 1)
>>> rho_a = dq.ptrace(psi_abc, 0, (3, 4, 5))
>>> rho_a.shape
(3, 3)
>>> rho_bc = dq.ptrace(psi_abc, (1, 2), (3, 4, 5))
>>> rho_bc.shape
(20, 20)