211
Capstones · Prove it

Build a quantum simulator from zero

Nothing teaches a quantum computer like building one, in software. This capstone asks you to write two simulators from scratch: a state-vector simulator that tracks all 2n2^n amplitudes, and a stabilizer simulator that handles Clifford circuits on thousands of qubits by tracking Pauli operators instead. Then you test both against each other and against Qiskit. The book’s own plates have used exactly such a simulator throughout.

On the plate: the book’s simulator running its own test suite.

A state-vector simulator

Store the state as a complex array of length 2n2^n, reshaped as an nn-dimensional tensor of 2s so that each qubit is an axis. A single-qubit gate contracts its 2 × 2 matrix with one axis; a CNOT flips the target axis on the slice where the control is 1. Twenty lines of Python suffice, with qubit 0 as the leftmost (most significant) bit, the convention of this book:

import numpy as np

class StateVector:
    def __init__(self, n):
        self.n = n
        self.psi = np.zeros(2**n, complex); self.psi[0] = 1
    def apply1(self, U, q):
        psi = self.psi.reshape([2] * self.n)
        psi = np.moveaxis(np.tensordot(U, psi, axes=([1], [q])), 0, q)
        self.psi = psi.reshape(-1)
    def cnot(self, c, t):
        psi = self.psi.reshape([2] * self.n).copy()
        idx = [slice(None)] * self.n; idx[c] = 1
        sub = psi[tuple(idx)]
        t2 = t if t < c else t - 1
        psi[tuple(idx)] = np.flip(sub, axis=t2)
        self.psi = psi.reshape(-1)
    def probs(self):
        return np.abs(self.psi) ** 2

H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
s = StateVector(2); s.apply1(H, 0); s.cnot(0, 1)
assert np.allclose(s.probs(), [0.5, 0, 0, 0.5])   # Bell state

This book checked the code above with tests for Bell and GHZ states and for CNOTs in both directions. Established

The test suite

Run the book’s simulator, Book.QSim, through . Tests: a Bell state has probabilities ½, 0, 0, ½; a GHZ state on 3 qubits has ½ on 000 and 111; H applied twice is the identity; X on qubit 2 gives index 1 (qubit 2 is the rightmost bit); a CNOT with control 2 and target 0 maps 001 to 101; total probability is 1 after a random circuit. Your own simulator should pass the same tests. Then compare with Qiskit, remembering its bit strings are little-endian (Chapter 152): your index for X on qubit 0 of three is 4, Qiskit’s count key is “001”. Established

A stabilizer simulator

For Clifford circuits, track the nn stabilizer generators instead of amplitudes (Chapter 108). Scott Aaronson and Daniel Gottesman’s 2004 tableau stores each generator as 2n2n bits plus a sign; H, S and CNOT update columns by simple XOR rules, and measurement uses Gaussian elimination over bits, at a cost of O(n2)O(n^2) per measurement. Build it, and test: prepare GHZ on 1,000 qubits, measure qubit 0, and check that all the others agree. Cross-check small random Clifford circuits against your state-vector simulator, amplitude by amplitude. Craig Gidney’s Stim is the production-grade version used throughout error-correction research. Established

What you now have

A working state-vector simulator, a test suite with an endianness check against Qiskit, the design of a stabilizer tableau simulator, and a plan to cross-check the two.

· · ·

Three questions to carry with you.

How does a single-qubit gate act on the state tensor?tap
By contracting its 2 × 2 matrix with that qubit’s axis.
Why can a stabilizer simulator handle 1,000 qubits?tap
It stores n generators of 2n bits each, not 2ⁿ amplitudes.
What is the commonest bug when comparing with Qiskit?tap
Bit order: Qiskit writes qubit 0 as the rightmost character.

And one to work out. How many complex amplitudes does a 20-qubit state vector hold?

=

Sources: S. Aaronson and D. Gottesman, Phys. Rev. A 70, 052328 (2004). C. Gidney, Quantum 5, 497 (2021) (Stim). Checked by code: the Python listing above passes Bell, GHZ and bidirectional CNOT tests.

Next · Chapter 212
Debate: interpretations of quantum mechanics

Defend one interpretation, then argue the strongest opposing case

Turn the page