qldpc.decoders.dems

Alternative representations of a Stim detector error model.

Attributes

CircuitError

HashableType

Classes

DetectorErrorModelArrays

Representation of a stim.DetectorErrorModel by a collection of arrays.

FlipPattern

A set of flipped detectors and observables.

Module Contents

class qldpc.decoders.dems.DetectorErrorModelArrays(circuit_or_dem: stim.Circuit | stim.DetectorErrorModel, *, simplify: bool = True, decompose_errors: bool = False)[source]

Representation of a stim.DetectorErrorModel by a collection of arrays.

A DetectorErrorModelArrays object organizes the data in a stim.DetectorErrorModel into:

  1. detector_flip_matrix: a binary matrix that maps circuit errors to detector flips,

  2. observable_flip_matrix: a binary matrix that maps circuit errors to observable flips, and

  3. error_probs: an array of probabilities of occurrence for each circuit error.

In addition, DetectorErrorModelArrays keeps track of any suggestions that a stim.DetectorErrorModel provides for how to decompose errors.

A DetectorErrorModelArrays is _almost_ one-to-one with a stim.DetectorErrorModel instance. The primary differences are that a DetectorErrorModelArrays object

  1. merges circuit errors with identical targets, where an error’s suggested decomposition is part of its targets (which can be disabled with simplify=False), and

  2. does not preserve detector coordinate data.

copy() → DetectorErrorModelArrays[source]

Return an independent copy of this DetectorErrorModelArrays.

static from_arrays(detector_flip_matrix: scipy.sparse.csc_matrix | numpy.typing.NDArray[numpy.int_], observable_flip_matrix: scipy.sparse.csc_matrix | numpy.typing.NDArray[numpy.int_] | None, error_probs: numpy.typing.NDArray[numpy.floating] | float, suggested_decompositions: dict[int, frozenset[FlipPattern]] | None = None, *, simplify: bool = False) → DetectorErrorModelArrays[source]

Initialize from arrays directly.

Parameters:
  • detector_flip_matrix – binary matrix mapping errors (columns) to detector flips (rows).

  • observable_flip_matrix – binary matrix mapping errors to observable flips, or None for zero observables.

  • error_probs – per-error probabilities, or a single number broadcast to all errors.

  • suggested_decompositions (optional) – dictionary that maps an error (by index) into a frozenset of FlipPattern, one per suggested decomposition component.

  • simplify – If True, return a simplified model with equivalent error mechanisms merged (see DetectorErrorModelArrays.simplified). Defaults to False.

The returned object shares no memory with the given arrays.

get_arrays() → tuple[scipy.sparse.csc_matrix, scipy.sparse.csc_matrix, numpy.typing.NDArray[numpy.floating]][source]

The arrays of this DetectorErrorModelArrays.

Returns:

a binary matrix that maps circuit errors to detector flips. observable_flip_matrix: a binary matrix that maps circuit errors to observable flips. error_probs: an array of probabilities of occurrence for each circuit error.

Return type:

detector_flip_matrix

static get_arrays_from_errors(errors: list[CircuitError], num_detectors: int, num_observables: int) → tuple[scipy.sparse.csc_matrix, scipy.sparse.csc_matrix, numpy.typing.NDArray[numpy.floating]][source]

Convert circuit errors into DetectorErrorModelArrays data.

static get_circuit_errors(dem: stim.DetectorErrorModel, *, decompose_errors: bool = False) → list[CircuitError][source]

Collect all circuit errors in a stim.DetectorErrorModel into a list.

Each circuit error is nominally identified by:

  • a probability of occurrence,

  • a set of detectors that are flipped,

  • a set of observables that are flipped.

In addition, a stim.DetectorErrorModel can come equipped with suggested decompositions of errors, which splits the detector/observable targets of an error into groups. To accommodate decomposition suggestions, a circuit error is identified by

  • a probability of occurrence,

  • a set of (detector_set, observable_set) tuples, one per suggested component.

Errors with no suggested decompositions have a single component.

If decompose_errors is True, all errors are decomposed into single-component errors.

If a detector or observable appears multiple times within one component, its occurrences are reduced to the original value mod 2.

static get_merged_circuit_errors(errors: list[CircuitError]) → list[CircuitError][source]

Merge circuit errors that have the same targets.

Targets include suggested decompositions, so two errors that flip the same detectors and observables stay distinct if they suggest different decompositions.

post_selected_on(detectors: collections.abc.Collection[int], *, keep_detectors: bool = False, order: int = 1) → DetectorErrorModelArrays[source]

Condition this detector error model on the given detectors being in 0 (untriggered).

The errors that trigger the post-selected detectors are removed from the DEM. The post-selected detectors are similarly removed unless keep_detectors is True.

If order > 1, combinations of up to ‘order’ removed error mechanisms whose co-occurrence does not trigger any of the post-selected detectors are added back to the DEM as synthetic error mechanisms.

simplified() → DetectorErrorModelArrays[source]

Simplify this DetectorErrorModelArrays object by merging errors.

to_circuit() → stim.Circuit[source]

Convert this DEM to a synthetic stim.Circuit.

Each error mechanism becomes a noisy measurement M(p) on a dedicated qubit. DETECTOR and OBSERVABLE_INCLUDE instructions then reference those measurements.

The detector error model of that circuit reproduces this DEM up to reordering of error mechanisms, merging of mechanisms with identical flips, and omission of mechanisms that cannot be observed – those with zero probability, whose M(0) is deterministic, and those that flip nothing. Error indices and suggested decompositions are not preserved.

to_dem() → stim.DetectorErrorModel[source]

Alias for self.to_detector_error_model().

to_detector_error_model() → stim.DetectorErrorModel[source]

Convert this object into a stim.DetectorErrorModel.

with_decomposed_errors(*, simplify: bool = True) → DetectorErrorModelArrays[source]

Split error mechanisms according to their suggested decompositions.

Each error with a suggested decomposition is replaced by its individual components, each inheriting the same probability. Errors without a decomposition are kept as-is.

with_erasure(bits: int = 1) → DetectorErrorModelArrays[source]

Construct the DetectorErrorModelArrays obtained by adding erasure bits to the DEM.

Each erasure bit is essentially a zero-probability error mechanism that flips no detectors, but flips one newly added observable. The erasure bit thereby allows decoders to indicate erasure by flipping the erasure bit.

Zero probability makes an erasure mechanism inconsequential to sampling, so simplification drops it. Add erasure bits after simplified, without_detectors, without_untriggered_detectors, with_decomposed_errors, post_selected_on with order > 1, and to_circuit.

without_detectors(detectors: collections.abc.Collection[int]) → DetectorErrorModelArrays[source]

Drop the given detectors.

Also drop or merge error mechanisms that are empty or redundant. Remaining detectors get re-indexed to range(num_remaining_detectors).

without_untriggered_detectors() → DetectorErrorModelArrays[source]

Drop all detectors that are not triggered by any error mechanism.

Such detectors are deterministically 0, so removing them changes no sampling outcome – it only shrinks the DEM. Error mechanisms and observables are left untouched.

detector_flip_matrix: scipy.sparse.csc_matrix
error_probs: numpy.typing.NDArray[numpy.floating]
property num_detectors: int

The number of detectors that witness circuit errors.

property num_errors: int

The number of distinct circuit errors.

property num_observables: int

The number of tracked logical observables.

observable_flip_matrix: scipy.sparse.csc_matrix
suggested_decompositions: dict[int, frozenset[FlipPattern]]
class qldpc.decoders.dems.FlipPattern(detectors: collections.abc.Iterable[int] = (), observables: collections.abc.Iterable[int] = ())[source]

A set of flipped detectors and observables.

dem_targets() → tuple[list[stim.DemTarget], list[stim.DemTarget]][source]

Lists of stim.DemTarget objects for the flipped detectors and observables.

classmethod from_data(detectors: collections.abc.Set[int], observables: collections.abc.Set[int]) → FlipPattern[source]

Construct from sets, skipping the mod-2 pass at normal initialization.

detectors: frozenset[int]
observables: frozenset[int]
qldpc.decoders.dems.CircuitError
qldpc.decoders.dems.HashableType