Haar Measure & Weingarten Calculus, numerical solution.
The SWAP Trick in Action
Background
Many quantities of interest are quadratic in a state, the purity \(\mathrm{Tr}(\rho^2)\) being the basic example, and these are awkward to access directly because they are nonlinear in \(\rho\). The SWAP trick removes the nonlinearity by moving to two identical copies of the system. On the doubled space there is an operator \(F\) that exchanges the two copies, sending \(|i\rangle|j\rangle\) to \(|j\rangle|i\rangle\), and a short index calculation gives \(\mathrm{Tr}(AB) = \mathrm{Tr}[(A\otimes B)\,F]\) for any two operators.
Setting \(A = B = \rho\) turns the purity into the expectation of the swap operator in the state \(\rho\otimes\rho\). This is what makes purities and the related Rényi entropies measurable without reconstructing the full density matrix, since one prepares two copies and estimates a single observable across them. The same replication is the reason two-copy quantities appear so often once randomness enters the picture.
The trick is also the computational engine behind the Weingarten calculus. Averages of two copies of a unitary against two copies of its conjugate reduce to permutations acting on the copies, and the swap is the nontrivial permutation that appears at this order. Counting how swaps and identities wire the copies together is exactly how second-moment Haar integrals are evaluated.
The notebook applies the identity to the Bell state, whose reduced density matrix is maximally mixed, and reads off purity \(1/2\). It then compares against the Haar-averaged purity at equal subsystem dimensions \(m=n=2\), where the typical value is \(4/5\), placing the Bell state below the average and so more entangled than a generic two-qubit reduction.
Mathematical background
The purity is the second spectral moment \(\gamma = \mathrm{Tr}\,\rho_A^2 = \sum_i \lambda_i^2\), and the swap trick rewrites it as a two-copy expectation, \(\mathrm{Tr}\,\rho_A^2 = \mathrm{Tr}\big(\mathbb{F}\,\rho_A \otimes \rho_A\big)\), where \(\mathbb{F}\) is the swap operator on \(\mathcal{H}_A \otimes \mathcal{H}_A\). Averaging over the induced ensemble gives the exact Haar value
\[
\mathbb{E}[\gamma] = \frac{m+n}{mn+1},
\]
together with the per-eigenvalue moments \(\mathbb{E}[\lambda^2] = (m+n)/[m(mn+1)]\) and \(\mathrm{Var}(\lambda) = (m^2-1)/[m^2(mn+1)]\). For a large environment, \(\mathbb{E}[\gamma] \simeq 1/m + 1/n\), approaching the maximally mixed purity \(1/m\) with a correction set by the environment dimension \(n\). Near-maximal mixing therefore requires \(n \gg m\), not merely \(mn \gg 1\), since the equal bipartition \(m = n\) leaves \(\mathbb{E}[\gamma] \simeq 2/m\).
Exercise
(a) Prove \(\mathrm{Tr}[(A \otimes B) F] = \mathrm{Tr}(AB)\) for the SWAP operator \(F\).
(b) For the Bell state \(|\Phi^+\rangle = (|00\rangle + |11\rangle)/\sqrt{2}\), compute \(\rho_A\) and verify \(\mathrm{Tr}(\rho_A^2) = \mathrm{Tr}[(\rho_A \otimes \rho_A) F_A] = 1/2\).
(c) Compare with the Haar-averaged purity \(\mathbb{E}[\gamma] = (m+n)/(mn+1)\) at \(m = n = 2\). Is the Bell state more or less entangled than a typical random state?
Solution
Part (a): Proof of the SWAP trick
The SWAP operator acts as \(F|i,j\rangle = |j,i\rangle\). Therefore:
The Bell state has \(\gamma_{\mathrm{Bell}} = 0.5 < 0.8 = \mathbb{E}[\gamma]\). The Bell state is more entangled than a typical Haar-random state on 2 qubits.
This makes sense: at \(D = 4\), the Page curve prediction is approximate, and the typical state is not yet close to maximal entanglement. Only for \(n \gg m\) does \(\mathbb{E}[\gamma] \to 1/m\) (Page’s result), which would match the maximally entangled value. For the equal split \(m = n \to \infty\) one finds \(\mathbb{E}[\gamma] \to 2/m\), twice the maximally mixed value, which is why the typical two-qubit state above is not yet maximally entangled.
Symbolic Verification (SymPy)
Code
import sympy as spfrom sympy import Matrix, trace, Symbol, I, eye, simplifyfrom sympy.physics.quantum import TensorProduct# The SWAP trick: Tr(A*B) = Tr(SWAP * (A tensor B))# Verify for 2x2 matricesa, b, c, d = sp.symbols('a b c d')e, f, g, h = sp.symbols('e f g h')A = Matrix([[a, b], [c, d]])B = Matrix([[e, f], [g, h]])# Direct computationtr_AB = trace(A * B)print(f'Tr(A*B) = {sp.expand(tr_AB)}')# SWAP operator on C^2 tensor C^2SWAP = Matrix([[1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])# A tensor B as 4x4AtB = TensorProduct(A, B)swap_result = trace(SWAP * AtB)print(f'Tr(SWAP * (A x B)) = {sp.expand(swap_result)}')# Verify equalityassert sp.expand(tr_AB - swap_result) ==0print('\nSWAP trick verified: Tr(AB) = Tr(SWAP * (A tensor B))')# Application: purity Tr(rho^2) = Tr(SWAP * rho^{tensor 2})print('Application: Tr(rho^2) = Tr(SWAP * rho x rho)')print('This connects purity to the 2-copy Hilbert space.')
Tr(A*B) = a*e + b*g + c*f + d*h
Tr(SWAP * (A x B)) = a*e + b*g + c*f + d*h
SWAP trick verified: Tr(AB) = Tr(SWAP * (A tensor B))
Application: Tr(rho^2) = Tr(SWAP * rho x rho)
This connects purity to the 2-copy Hilbert space.
Numerical Verification
Code
import numpy as npfrom scipy.stats import unitary_groupnp.random.seed(42)# Part (b): Verify via SWAP trick and direct computationD =2F = np.zeros((D**2, D**2))for a inrange(D):for b inrange(D): F[a*D+b, b*D+a] =1# Bell state purityrho_A = np.eye(D) / Dpurity_direct = np.trace(rho_A @ rho_A).realpurity_swap = np.trace(np.kron(rho_A, rho_A) @ F).realprint(f"Bell purity (direct): {purity_direct:.4f}")print(f"Bell purity (SWAP): {purity_swap:.4f}")assert np.isclose(purity_direct, 0.5)assert np.isclose(purity_swap, 0.5)print()
Bell purity (direct): 0.5000
Bell purity (SWAP): 0.5000
Code
# Part (c): Haar-averaged puritym, n =2, 2D_total = m * nn_samples =20000purities = []for _ inrange(n_samples): psi = unitary_group.rvs(D_total)[:, 0] rho = np.outer(psi, psi.conj()).reshape(m, n, m, n) rho_A = np.trace(rho, axis1=1, axis2=3) purities.append(np.trace(rho_A @ rho_A).real)E_gamma = np.mean(purities)pred = (m + n) / (m*n +1)print(f"E[γ] = {E_gamma:.4f} (theory (m+n)/(mn+1) = {pred:.4f})")print(f"Bell purity = 0.500 < E[γ] = {pred:.3f}: Bell is MORE entangled than typical.")assertabs(E_gamma - pred) <0.02print("\nSWAP trick and Haar average verified.")
E[γ] = 0.7998 (theory (m+n)/(mn+1) = 0.8000)
Bell purity = 0.500 < E[γ] = 0.800: Bell is MORE entangled than typical.
SWAP trick and Haar average verified.
Takeaway
The SWAP trick renders \(\mathrm{Tr}(\rho^2)\) as a two-copy measurement, and for the Bell state it gives purity \(1/2\). That lies below the Haar average \(4/5\) at \(m = n = 2\), so the Bell state is more entangled than a typical two-qubit reduction.
Connections
The SWAP operator is the nontrivial permutation that the second Haar moment selects, which is why writing a purity as a two-copy expectation is the entry point to the whole second-moment machinery. The same two-copy average gives the Page formula and purity concentration. It reappears far from this setting as the randomized benchmarking twirl and the barren plateau gradient variance.
References
Mele, Introduction to Haar Measure Tools in Quantum Information: A Beginner’s Tutorial, Quantum (2024). doi
Collins and Śniady, Integration with respect to the Haar measure on unitary, orthogonal and symplectic group, Communications in Mathematical Physics (2006).
Brydges et al., Probing Rényi Entanglement Entropy via Randomized Measurements, Science (2019).
Płodzień, Information Scrambling, Quantum Chaos, and Haar-Random States, lecture notes (2025). arXiv:2511.14397