Density Matrices in Code
A density matrix is a complex array together with a basis convention and physical constraints. Storing a square matrix is easy; preserving its interpretation as a state requires checking Hermiticity, unit trace, and positivity separately. This page develops those checks and subsystem index contractions. Circuit evolution and master-equation integration use this representation.
Helpful background. Reduced density operators explains the physical meaning of a mixed state; tensor products in code fixes array ordering.
Density operators as complex arrays
Section titled “Density operators as complex arrays”Use an orthonormal basis and store . For an ensemble of normalized columns with nonnegative weights summing to one,
The conjugation is essential. An outer product without complex conjugation need not be Hermitian. For any vector , , which establishes positivity without diagonalization. A low-rank factor with columns gives and can reduce storage when rank is small.
For a dense -dimensional state, complex double precision costs approximately bytes before work arrays. Hermiticity does not make a generic matrix sparse. Sparse storage helps only while the physical representation has few nonzero entries.
Diagnostics with scale-aware tolerances
Section titled “Diagnostics with scale-aware tolerances”Compute the anti-Hermitian residual before calling a Hermitian eigensolver:
Then diagonalize the Hermitian part for diagnostic eigenvalues. Record and the negative weight . Small roundoff-scale negative eigenvalues are different from a negative weight that grows with time or persists under refinement.
For a physical state, . Purity within these bounds does not prove positivity: the eigenvalues sum to one and have purity . Likewise, trace normalization cannot repair a wrong channel.
A useful tolerance combines machine precision, matrix size, the scale of the computation, and the measured solver error. Do not impose one absolute tolerance on all state sizes. If projecting negative eigenvalues to zero is part of an estimator, report the projection and its magnitude; do not hide an unstable evolution behind it.
The negative weight gives a quantitative repair diagnostic. For a Hermitian, trace-one matrix , write with positive parts on orthogonal eigenspaces and . Clipping and renormalizing gives , because . Consequently
The positive and negative spectral sectors each contribute to the trace norm. For the example , the repair changes the matrix by in this measure; it cannot be described as negligible roundoff. This calculation quantifies one specified repair, not an error bound relative to the unknown physical state.
Partial trace as an index contraction
Section titled “Partial trace as an index contraction”With composite ordering , reshape into . The reduced state is
For a pure state reshape into and use , avoiding the full square matrix. Nonadjacent subsystems require an explicit axis permutation before reshaping. Tracing arbitrary axes of an array without documenting their labels is a common source of plausible wrong answers.
import numpy as np
def reduce_pure_state(psi, da, db): c = np.asarray(psi, dtype=complex).reshape(da, db) return c @ c.conj().T
psi = np.array([1, 0, 0, 1j], dtype=complex) / np.sqrt(2)rho_a = reduce_pure_state(psi, 2, 2)assert np.allclose(rho_a, np.eye(2) / 2)The phase makes this a stronger conjugation test than an entirely real Bell state. Test also a product state: its reduction must have rank one. These tests exercise different failure modes.
An equal-dimension Bell pair does not test which subsystem was retained. Use a fixture,
The two reductions must have different shapes: and . In the same coefficient convention, ; is its transpose and is generally different for complex states. Compare a general complex fixture against the explicit index sum as well as the simple analytic example.
Expectations and truncation
Section titled “Expectations and truncation”Evaluate , equivalently . A Hermitian observable should have a real expectation to numerical accuracy. An unexpected imaginary part is a useful signal of mismatched conjugations or index order.
In a truncated oscillator basis, a positive unit-trace matrix is only a valid state within the truncated model. Monitor boundary occupation and increase the cutoff. These physical truncation errors are independent of matrix roundoff and time-step error.
Exercise
Section titled “Exercise”For , derive the determinant and purity and identify the pure cases.
Solution
In the ordered basis , the diagonal entries are and , and the off-diagonal entries are . Hence and . Positivity holds for ; the state is pure only at . Sampling intermediate values tests mixtures of nonorthogonal states.
References
Section titled “References”- Johansson, J. R., Nation, P. D., and Nori, F., “QuTiP: An open-source Python framework for the dynamics of open quantum systems,” Computer Physics Communications 183, 1760–1772 (2012), doi:10.1016/j.cpc.2012.02.021 — density-operator computation and solver context.
- QuTiP developers, “Lindblad Master Equation Solver”, QuTiP documentation — state representations and expectation-value calculations.
- Watrous, J., The Theory of Quantum Information, Cambridge University Press (2018), author’s text — reduced states and trace-norm diagnostics.