A detector error model (DEM) is a compact, probabilistic description of all the error mechanisms in a circuit, expressed in terms of which detectors each error flips. Conceptually it is a Tanner graph: nodes are detectors and logical observables, and edges (error mechanisms) connect the nodes whose parity they change. Decoders such as PyMatching consume this graph directly, so producing a correct DEM is the first step in any decoding pipeline.Documentation 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.
Basic usage
Callcircuit.detector_error_model() on any stim.Circuit that contains DETECTOR and OBSERVABLE_INCLUDE annotations:
stim.DetectorErrorModel object contains instructions of three types:
| Instruction | Meaning |
|---|---|
error(p) D… | Error with probability p that flips the listed detectors |
error(p) D… L… | Same, but also flips logical observable(s) |
detector(coords) D… | Coordinate metadata for a detector |
logical_observable L… | Declares a logical observable index |
Key options
decompose_errors=True — graphlike decomposition
Matching decoders require that every error flips at most two detectors (graphlike errors). Some circuit errors — like Y errors in a CSS code — naturally flip four detectors. Setting decompose_errors=True asks Stim to split such hyper-errors into pairs of graphlike components, separated by ^ in the output:
approximate_disjoint_errors=True — disjoint error approximation
Some Stim error channels — PAULI_CHANNEL_1, PAULI_CHANNEL_2, ELSE_CORRELATED_ERROR — express disjoint (mutually exclusive) error cases. DEMs require independent mechanisms, so Stim will raise an error when it encounters these unless you enable the approximation:
DEPOLARIZE1 and DEPOLARIZE2 can be converted exactly (without approximation) for physically realistic error rates, so you rarely need this flag for depolarizing noise.allow_gauge_detectors=True — non-deterministic detectors
By default, Stim verifies that every detector is deterministic under noiseless execution. If your circuit contains gauge detectors (detectors that anti-commute with resets or measurements), enable this flag so Stim resolves them via Gaussian elimination rather than raising an error:
flatten_loops=True — unroll REPEAT blocks
By default, Stim watches for periodic steady states inside REPEAT blocks and emits compact repeat blocks in the DEM. Set flatten_loops=True to expand all loops into individual instructions (useful for small circuits or for tools that cannot handle DEM repeat blocks):
Reading a DEM programmatically
Iterate over the instructions to inspect error mechanisms:stim.DemInstruction exposes:
.type—"error","detector", or"logical_observable".args_copy()— list of float arguments (the probability forerrorinstructions).targets_copy()— list ofstim.DemTargetobjects
Finding the shortest graphlike error
circuit.shortest_graphlike_error() (and the equivalent dem.shortest_graphlike_error()) returns the minimum-weight set of graphlike errors that produce an undetected logical flip:
Mapping DEM errors back to circuit locations
circuit.explain_detector_error_model_errors() traces each DEM error mechanism back to the physical circuit instruction(s) that produce it: