ADocumentation 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 file is a UTF-8 encoded, human-readable specification of an annotated stabilizer circuit. It describes the gates applied to qubits, any noise processes to include during simulation, and annotations that enable detection-event sampling, circuit drawing, and spacetime layout hints. The file format is executed top-to-bottom, one instruction at a time.
Encoding
Stim circuit files are always encoded using UTF-8. Non-ASCII characters may only appear inside comments and inside instruction tags; they are not valid in gate names, arguments, or targets.Syntax
A.stim file is a sequence of lines. Each line is one of:
- Blank — ignored
- Instruction — a gate applied to targets
- Block initiator — an instruction followed by
{on the same line - Block terminator — a
}on its own line
#. Indentation and comments are purely decorative and carry no semantic meaning.
Instructions
An instruction consists of:- A name — starts with a letter, followed by letters, digits, and underscores. Names are case-insensitive.
- An optional tag — an arbitrary string inside square brackets, e.g.
[100ns]. - An optional parenthesized argument list — comma-separated floating-point numbers.
- A space-separated target list.
X_ERROR[test](0.1) 5 6 — name X_ERROR, tag test, argument 0.1, targets 5 and 6.
Tag escape sequences
Inside a tag, the following characters must be escaped:| Character | Escape sequence |
|---|---|
] | \C |
\r (carriage return) | \r |
\n (line feed) | \n |
\ (backslash) | \B |
TICK[100ns] to learn the duration of a time step.
Target types
- Qubit targets
- Measurement record targets
- Sweep bit targets
- Pauli targets
A qubit target is a non-negative integer identifying a qubit by index. Qubits are implicitly allocated; the number of qubits is determined by the largest qubit index referenced in the file.A
! prefix inverts the recorded measurement result for that qubit. For example, M 0 !1 measures qubit 0 and qubit 1, recording the result for qubit 1 as flipped.Broadcasting
When a gate receives more targets than a single application requires, it is broadcast over those targets.- Single-qubit gates — applied to each target in order.
H 0 1 2is equivalent toH 0thenH 1thenH 2. - Two-qubit gates — applied to aligned target pairs in order.
CNOT 0 1 2 3is equivalent toCNOT 0 1thenCNOT 2 3. Giving a two-qubit gate an odd number of targets is an error.
State space
A Stim simulator executing a circuit tracks three things:- The qubits — all start in the |0⟩ state. The qubit count is inferred from the largest qubit index in the circuit.
- The measurement record — an immutable append-only log of all measurement results. Referenced by
rec[-n]targets. - The “correlated error occurred” flag — a hidden boolean used by
ELSE_CORRELATED_ERRORto condition on whether the precedingCORRELATED_ERRORfired.
SHIFT_COORDS, affects DETECTOR coordinate arguments and QUBIT_COORDS annotations but has no effect on simulation results.
REPEAT blocks
AREPEAT K { ... } block repeats its body exactly K times. K must be a positive integer — REPEAT 0 is a syntax error because it creates ambiguity about whether detectors and observables declared inside the block exist.
{ must appear on the same line as the instruction; the } must appear on its own line.
Examples
Quantum teleportation
Quantum teleportation
This circuit distributes a Bell pair, performs a Bell-basis measurement on the sender’s side, and applies feed-forward corrections to the receiver’s qubit (qubit 99).
Repetition code
Repetition code
This circuit measures the parities of adjacent data qubits (0, 2, 4, 6) using measurement qubits (1, 3, 5), then repeats 1000 more rounds. The
DETECTOR annotations declare detection events and the OBSERVABLE_INCLUDE annotation declares the logical observable.Noisy repetition code (annotated)
Noisy repetition code (annotated)
This is the output of
stim --gen repetition_code --task memory --rounds 1000 --distance 3 --after_clifford_depolarization 0.001. It shows noise operations, TICK separators, and coordinate annotations.