Q&A-1Represent and validate a quantum state for complex amplitudes.
Answer
Core answer (first 90 seconds)
I'd store the state as a dense complex array in a declared basis order, validate that the squared magnitudes sum to one within a tolerance tied to the dtype, and only ever derive probabilities as |amplitude|². Two things I pin down before writing code: the dtype, and the basis-ordering convention.
Assumptions: statevector simulation, n up to ~30 qubits, measurement in the computational (Z) basis, amplitudes in complex128.
Representation: a numpy.ndarray of complex128, shape (2**n,), where index i maps to basis state |i⟩ under a stated bit order. complex128 is 16 bytes per amplitude, so 30 qubits is 2³⁰ × 16 B = 16 GiB: that's the wall for dense simulation.
Validation: normalization is ⟨ψ|ψ⟩ = Σ conj(aᵢ)·aᵢ = 1. Compute np.vdot(psi, psi).real and compare with np.isclose(..., atol=1e-10). complex128 has machine epsilon ≈ 2.2e-16; after ~1000 gate applications the norm error accumulates roughly as O(√gates · ε) ≈ 7e-15, so 1e-10 leaves headroom without accepting a genuinely unnormalized state.
Why |a|²: the Born rule. P(i) = |aᵢ|² = re² + im². A global phase e^{iθ} multiplies every amplitude and cancels in |aᵢ|², so it is unobservable. Relative phase is what matters: amplitudes for the same outcome add before squaring, so it drives interference.
import numpy as np
# |psi> = (|0> + i|1>)/sqrt(2); basis order: index 0 = |0>, index 1 = |1>
psi = np.array([1/np.sqrt(2), 1j/np.sqrt(2)], dtype=np.complex128)
norm = np.vdot(psi, psi).real # <psi|psi>
assert np.isclose(norm, 1.0, atol=1e-10), f"not normalized: {norm}"
probs = np.abs(psi) ** 2 # Born ruleTrace: psi = [0.70710678+0j, 0+0.70710678j]. vdot conjugates its first argument, so conj(psi) = [0.70710678-0j, 0-0.70710678j]. Elementwise product with psi: 0.5 + 0j, and (-0.70710678j)(0.70710678j) = 0.5. Sum = 1.0, .real = 1.0, assertion passes. abs(psi) = [0.70710678, 0.70710678], squared = [0.5, 0.5].
Trade-offs
| representation | 30-qubit memory | wins when | breaks when |
|---|---|---|---|
dense complex128 | 16 GiB | general circuits, high entanglement | >~32 qubits |
dense complex64 | 8 GiB | tolerance ≥ 1e-6 is acceptable | a 1e-10 normalization check is impossible |
| sparse dict | O(nnz) | few nonzero amplitudes (e.g. a GHZ state before measurement) | entangling layers fill it in |
| MPS / tensor network | O(n·χ²) | low-entanglement 1D circuits | volume-law entanglement, χ explodes |
Deciding criteria: qubit count, entanglement structure, and the tightest tolerance the downstream check needs. Pick complex64 only if you also loosen the tolerance: mixing them is a bug.
Failure modes
- Amplitude treated as probability:
probs = psiinstead ofnp.abs(psi)**2. Catch it by assertingnp.allclose(probs, np.abs(psi)**2)and thatprobsis real and nonnegative. - Imaginary part dropped:
psi.realsilently halves the norm. For the example above the norm becomes 0.5, so the normalization check catches it. - Basis unstated: index 1 is |01⟩ or |10⟩ depending on convention. Qiskit uses little-endian (qubit 0 = least significant); most textbooks are big-endian. A mismatch flips results for multi-qubit states while single-qubit checks still pass.
- Rounding drift: after ~10⁴ gates the norm can read 0.9999999997. Renormalize only within tolerance; a state off by 10% is a bug, not drift.
- Unauthorized normalization: silently rescaling a wrong state hides the defect. Reject outside tolerance, renormalize inside it.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.