Skip to content

loqs.tools.stimtools

Helpers for running STIM circuits in LoQS.

build_stim_model() builds the ideal model for a STIM circuit's gates, stim_record_from_outcomes() rebuilds STIM's measurement record, detector events and observable flips from the outcomes of a LoQS run, and build_stim_decoder_instruction() decodes them with PyMatching.

Classes

Functions

build_stim_decoder_instruction

build_stim_decoder_instruction(circuit: STIMPhysicalCircuit | Circuit, dem: DetectorErrorModel | None = None, name: str = 'STIM DEM decoder') -> Instruction

Build an instruction that decodes a STIM circuit's run with PyMatching.

It rebuilds the measurement record from the program's measurement outcomes, and adds the frame keys detectors, observables, predicted_observables (lists of 0/1) and logical_error (whether any prediction differs from the observed flip). It applies no correction. Its data is plain text and lists, so the instruction survives saving and loading a program.

Parameters:

Name Type Description Default
circuit STIMPhysicalCircuit | Circuit

The circuit the program runs. A stim.Circuit is wrapped in a STIMPhysicalCircuit so its qubit labels match the run's.

required
dem DetectorErrorModel | None

The detector error model to decode with. Defaults to circuit.detector_error_model(decompose_errors=True), which is empty when the noise comes from a LoQS model rather than the circuit; pass the DEM of a noisy copy of the circuit then.

None
name str

Instruction name.

'STIM DEM decoder'

Raises:

Type Description
ValueError

If the DEM has no error mechanisms, or its detector count differs from the circuit's.


build_stim_model

build_stim_model(circuit: STIMPhysicalCircuit | Circuit, gaterep: type[GateRep] = StimCircuitGateRep, after_clifford_depolarization: float = 0.0, after_reset_flip_probability: float = 0.0, before_measure_flip_probability: float = 0.0) -> DictNoiseModel

Build the model for the gates a STIM circuit uses.

The model has a generic entry for each unitary gate, reset and measurement in the circuit, plus X, Y and Z so error injection works. The three probabilities place Pauli noise as stim.Circuit.generated does: depolarizing noise after each gate except I, a flip after each reset, and a flip before each measurement (and after the reset in MR-type measurements). The flip is X-type for the Z and Y bases and Z-type for the X basis.

Parameters:

Name Type Description Default
circuit STIMPhysicalCircuit | Circuit

The circuit whose gate set the model covers.

required
gaterep type[GateRep]

StimCircuitGateRep (default) builds STIM text for the STIM state. Any other GateRep class builds dense reps for a dense state; with noise, use KrausGateRep or QSimSuperopGateRep, since UnitaryGateRep can't hold it.

StimCircuitGateRep
after_clifford_depolarization float

Depolarizing strength after each gate except I.

0.0
after_reset_flip_probability float

Flip probability after each reset.

0.0
before_measure_flip_probability float

Flip probability before each measurement.

0.0
A
required
no
required
noise
required

Returns:

Type Description
DictNoiseModel

The model.

Raises:

Type Description
ValueError

If a probability is outside [0, 1].

NotImplementedError

If the circuit has an instruction other than a unitary gate, reset, measurement, annotation, noise channel or MPP, or a gate with Pauli targets; or if before_measure_flip_probability is positive and the circuit has an MPP.


stim_record_from_outcomes

stim_record_from_outcomes(circuit: STIMPhysicalCircuit | Circuit, outcomes: ProgramResults | Sequence[Any]) -> StimRecord

Rebuild STIM's measurement record, detectors and observables.

Parameters:

Name Type Description Default
circuit STIMPhysicalCircuit | Circuit

The circuit that was run. A stim.Circuit is wrapped in a STIMPhysicalCircuit so its qubit labels match the run's.

required
outcomes ProgramResults | Sequence[Any]

A ProgramResults, or a sequence of shots. Each shot is a mapping from qubit label to outcomes, or a sequence of such mappings (or None) with one per frame; the outcomes are joined in frame order.

required

Returns:

Type Description
StimRecord

Boolean arrays of shape (shots, n).

Raises:

Type Description
ValueError

If a qubit's number of outcomes differs from the circuit's, or a qubit the circuit never measures has outcomes.

NotImplementedError

If the circuit writes record bits that LoQS can't attribute to a qubit, such as MPP or MPAD.