Stim is designed from the ground up to support quantum error correction workflows. TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/quantumlib/Stim/llms.txt
Use this file to discover all available pages before exploring further.
.stim circuit format provides two annotations — DETECTOR and OBSERVABLE_INCLUDE — that bridge the gap between a low-level stabilizer circuit and the high-level objects that decoders consume. Understanding these annotations, and the detector error model (DEM) derived from them, is essential for using Stim in a QEC pipeline.
Detectors
ADETECTOR annotation marks a parity check formed from one or more measurement results. Specifically, a detector declares a combination of bits from the measurement record that should be deterministic when no noise is present. If those bits XOR to 1 on a given shot, an error occurred somewhere, and the detector is said to have “fired” (produced a detection event).
rec[-n] targets. The detector fires when the XOR of all listed bits equals 1:
Coordinates are purely annotations — they have no effect on simulation. Decoders such as PyMatching use them to construct a matching graph with the correct geometry.
Observables
AnOBSERVABLE_INCLUDE annotation declares which measurements, when XORed together, track the state of a logical qubit. The argument is an integer index identifying which logical observable the measurements contribute to.
OBSERVABLE_INCLUDE(k) contributions for the same index k are XORed together.
A circuit can have multiple logical observables.
circuit.num_observables returns one more than the largest observable index declared. Sampling all observables at once via compile_detector_sampler(skip_reference_sample=False) returns both detection events and observable flips.Detection events and syndromes
When Stim samples detection events from a noisy circuit, each shot produces a bit vector whose length equalscircuit.num_detectors. A True (1) entry in this vector means the corresponding detector fired. The full vector is the syndrome for that shot.
Detector error models (DEMs)
A detector error model (DEM) is a compact description of how individual physical error mechanisms cause detectors to fire and observables to flip. Stim derives a DEM automatically from an annotated circuit:Annotated repetition code example
The following is a complete, annotated noisy repetition code withDETECTOR and OBSERVABLE_INCLUDE. Data qubits are at indices 0, 2, 4, 6; measurement qubits are 1, 3, 5.
Why does the first round only have one rec per DETECTOR?
Why does the first round only have one rec per DETECTOR?
In the first round, there are no prior measurements to compare against. Each detector references only the current measurement, asserting it should be 0 (since we reset before measuring). From the second round on, each detector compares the current measurement (
rec[-n]) with the one from the previous round (rec[-n-3]), flagging any change.What does OBSERVABLE_INCLUDE(0) rec[-1] mean?
What does OBSERVABLE_INCLUDE(0) rec[-1] mean?
It declares that the last data qubit measurement (qubit 6’s Z-basis result) contributes to logical observable 0. In a repetition code, the logical Z observable is any single data qubit. A decoder that correctly identifies all errors will predict whether
observable_flips[shot, 0] is True or False.How does Stim determine if a detector is deterministic?
How does Stim determine if a detector is deterministic?
Stim runs a noiseless reference simulation of the circuit, computes the XOR of the measurement bits each detector references, and checks that the result is always 0. This is what
circuit.reference_sample() returns. A detector whose reference parity is 1 must have its measurement targets adjusted (e.g., by prefixing one target with ! to invert its contribution).