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 |
required |
dem
|
DetectorErrorModel | None
|
The detector error model to decode with. Defaults to
|
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
|
after_clifford_depolarization
|
float
|
Depolarizing strength after each gate except |
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 |
NotImplementedError
|
If the circuit has an instruction other than a unitary gate, reset,
measurement, annotation, noise channel or |
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 |
required |
outcomes
|
ProgramResults | Sequence[Any]
|
A |
required |
Returns:
| Type | Description |
|---|---|
StimRecord
|
Boolean arrays of shape |
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 |