Skip to content

dq.purity

purity(x: QArrayLike) -> Array

Returns the purity of a ket or density matrix.

For a ket (a pure state), the purity is \(1\). For a density matrix \(\rho\), it is defined by \(\tr{\rho^2}\).

Parameters

  • x (qarray-like of shape (..., n, 1) or (..., n, n)) –

    Ket or density matrix.

Returns

(array of shape (...)) Real-valued purity.

Examples

>>> psi = dq.fock(2, 0)
>>> dq.purity(psi)
Array(1., dtype=float32)
>>> rho = (dq.fock_dm(2, 0) + dq.fock_dm(2, 1)).unit()
>>> dq.purity(rho)
Array(0.5, dtype=float32)