|q⟩ Bad Qubits

advanced · Programming · Quantum Software Engineering & Resource Estimation

Reproducibility and Seeds

Quantum measurement is intrinsically random — the Born rule gives probabilities, not outcomes — and on top of that, simulators sample those probabilities with a pseudo-random number generator. Two sources of nondeterminism (physical and computational) make raw runs irreproducible: run the same circuit twice and the histograms differ. For science and for debugging, that is unacceptable. The fix is seeding: fixing the PRNG so that "random" becomes deterministic and repeatable.

What is and is not random

It is worth separating the two kinds of variation:

Seeding controls only the second kind. It cannot change the physics; it makes the sampling of the physics repeatable.

How a seed makes sampling deterministic

The simulator draws shots from a pseudo-random generator. A PRNG is a deterministic function of its seed: given the same seed it emits the same stream of numbers, so it produces the same sequence of sampled outcomes. Fix the seed and your histogram is bit-for-bit reproducible:

const c = circuit(2);
c.h(0);
c.measure();
// With shots = 1000 and seed = 42, the sampled counts are identical every run.

The grader for this lesson declares shots: 1000, seed: 42 precisely so the histogram you see is the same one a teammate sees. Grading itself, however, still uses the exact probabilities — the seed governs the display, not the verdict.

Reproducibility as an engineering contract

To reproduce a stochastic result exactly, four things must be pinned together:

  1. The circuit (from versioned generator code, last lesson).
  2. The shot count NN.
  3. The seed.
  4. The simulator / PRNG version — a different PRNG yields a different stream even from the same seed.

Record all four next to the result. With them, anyone can regenerate your histogram identically; without any one of them, "reproducible" quietly degrades to "statistically similar". This is the stochastic complement to the version-control discipline of the previous lesson: there we pinned the deterministic source; here we pin the randomness.

Seeds versus statistics

Seeding gives exact reproducibility but tests only one realisation of the randomness. To characterise an algorithm's typical behaviour — its variance, its tail outcomes — you instead run many shots, or many seeds, and report statistics with error bars. The two practices serve different goals: a fixed seed for reproducing and debugging a specific run, many seeds for estimating the distribution's spread. Mature quantum software uses both deliberately, and always states which it is doing.

Try it

Prepare the 2-qubit state (00+10)/2(|00\rangle + |10\rangle)/\sqrt{2} with a single Hadamard on qubit 0, then measure all qubits. The grader checks the exact distribution (outcomes 0000 and 1010 each at probability 1/21/2); the declared seed and shot count make the displayed histogram reproducible.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.