Quantum Circuit Lab / math engine
Lesson 07 · math engine

The Matrix Trap: Subspace Multiplication in Simulators

Why full 2ⁿ by 2ⁿ matrix multiplication is a useful teaching model but the wrong inner loop for a scalable state-vector simulator.

Math heavy20 min readsoftware engineering · quantum computing

The tempting implementation

With two qubits, a one-qubit gate can be lifted into a 4 × 4 matrix using a Kronecker product. With three qubits it becomes 8 × 8. This is elegant, but the operator grows as 2ⁿ × 2ⁿ while the state vector is already 2ⁿ.

# Teaching version
full_H = np.kron(H, np.eye(2))
state = full_H @ state

The local update

A one-qubit gate acts on amplitude pairs. For target bit k, pair index i has that bit clear and partner j = i | (1 << k) has it set.

def apply_one_qubit(state, gate, target):
    step = 1 << target
    out = state.copy()
    for i in range(len(state)):
        if i & step: continue
        j = i | step
        a, b = state[i], state[j]
        out[i] = gate[0, 0] * a + gate[0, 1] * b
        out[j] = gate[1, 0] * a + gate[1, 1] * b
    return out

The memory and work per gate remain linear in the number of amplitudes. The exponential limit has not disappeared—you still track 2ⁿ values—but you stop allocating a mostly-zero operator.

Library design: expose a readable vector API to learners while keeping subspace kernels behind it.

Use the idea in the Qiskit Bell-state example, then compare that abstraction with the simulator on this site.