cosmiq.qec#

Quantum error correction with atom loss in decoding (see the QEC guide): Stim’s surface and repetition memory circuits, loss as a heralded erasure, and our own matching decoder, loss-blind or loss-aware, with plug-in seams for other codes and decoders. Needs the extra, pip install "cosmiq[qec]" (Stim and networkx).

from cosmiq import qec

r = qec.memory_experiment("surface", d=5, noise=qec.Noise(p=1e-3, p_loss=0.01),
                          shots=20_000, decoder="loss_aware", seed=1)
r.logical_error_rate, r.sigma, r.heralds_per_shot

import cosmiq never imports this package (or Stim).

Code

A code: its memory-experiment circuit and where its erasures are.

Decoder

A decoder: observable flips predicted from detection events and heralds.

DecodingGraph

The matching graph of a circuit's detector error model (decompose_errors=True): one edge per detector pair (or detector and boundary), its probability the independent combination of every error component on it, its weight log((1 - p) / p), and which observables it flips.

Erasure

One place where an atom can be lost and replaced: a heralded erasure.

MatchingDecoder

Our matching decoder: MatchingDecoder(loss_aware=True) gives the erased locations' edges weight 0 in each shot; loss_aware=False ignores the heralds.

Noise

The noise of a memory experiment, as probabilities in [0, 0.5).

Problem

What a decoder is given (a neutral interface, so qLDPC codes and decoders fit later):

Result

A memory experiment's outcome.

Samples

One sampling of a circuit: detectors and observables (shots x each, bool) and erased (shots x erasure locations: which heralds fired).

StimCode

A built-in code at distance d (qec.code("surface", 5)): its memory circuit, with rounds = d, and its erasure locations.

code

A code by name at distance d: "surface" or "repetition" (d = 3 or 5), or a registered one.

decoder

A new decoder by name: "loss_aware", "loss_blind" or a registered one.

decoding_graph

The decoding graph of a circuit with heralded erasures at erasures, cached by the circuit's text.

memory_experiment

A memory experiment: code ("surface", "repetition" or a registered one) at distance d with rounds = d, shots shots at noise, decoded by decoder ("loss_aware", "loss_blind" or a registered one).

noise_from_device

The noise a device's calibration and digital profile map to (DEVICE_MAPPING): a modelling choice, not the device's QEC performance.

register_code

Adds a code: memory_experiment(name, d=..., ...) then calls factory(d) for a Code.

register_decoder

Adds a decoder: memory_experiment(..., decoder=name) then calls factory() for a Decoder.

sample

shots shots of circuit, read the frozen way: the measurement record, its herald columns at erasures' record indices, and its detection events and observables.

DEVICE_MAPPING

Classes

class cosmiq.qec.Code(*args, **kwargs)#

Bases: Protocol

A code: its memory-experiment circuit and where its erasures are.

circuit(noise, rounds) returns a Stim circuit whose measurement record holds one herald bit per erasure location; erasure_locations() lists those locations for the circuit built with rounds = distance, which is what memory_experiment builds.

property distance: int#

The code distance.

circuit(noise: Noise, rounds: int) → Any#

The memory experiment as a stim.Circuit, with its heralded erasures.

erasure_locations() → tuple[Erasure, ...]#

Every erasure location of circuit(noise, rounds=distance), by herald record index.

class cosmiq.qec.Decoder(*args, **kwargs)#

Bases: Protocol

A decoder: observable flips predicted from detection events and heralds.

Both methods take the Problem (the circuit, its erasure locations, its undecomposed detector error model and the erasure map keyed by error mechanism), detectors (shots x detectors, bool) and erased (shots x erasure locations, bool: which heralds fired). Nothing asks for a graphlike error model: a decoder that needs a matching graph builds it.

decode(problem: Problem, detectors: NDArray[bool], erased: NDArray[bool]) → NDArray[bool]#

The predicted observable flips, shots x observables.

weight(problem: Problem, detectors: NDArray[bool], erased: NDArray[bool]) → NDArray[float64]#

The total weight of each shot’s correction (the exits compare it to a reference’s).

class cosmiq.qec.DecodingGraph(dem: DetectorErrorModel, erasure_edges: Sequence[Sequence[int]] = ())#

Bases: object

The matching graph of a circuit’s detector error model (decompose_errors=True): one edge per detector pair (or detector and boundary), its probability the independent combination of every error component on it, its weight log((1 - p) / p), and which observables it flips. erasure_edges[i] holds the edges erasure location i’s Paulis land on. Built by decoding_graph; read it, don’t change it.

erased_edges(erased: NDArray[bool]) → tuple[int, ...]#

The edges of the erased locations in one shot (erased: one bool per location).

keys(edges: Iterable[int]) → frozenset[tuple[int, ...]]#

The detector keys of these edges ((u, v), or (u,) for the boundary).

network() → Graph#

The networkx graph: nodes 0..num_detectors (the last is the boundary); each edge has w (its weight), obs (its observable mask) and i (its index).

shortest(sources: Sequence[int], zero: Sequence[int] = (), targets: Sequence[int] = ()) → tuple[NDArray[float64], NDArray[int64]]#

Dijkstra from each source, with the edges zero at weight 0: the lengths and the observable masks of the paths, len(sources) x (num_detectors + 1), filled for targets (every node when empty; unreachable ones stay inf). The shared graph is never changed: the zeroed weights come from a weight function, so decoding is safe across threads.

all_pairs() → tuple[NDArray[float64], NDArray[int64]]#

shortest from every detector, with the fixed weights, computed once.

class cosmiq.qec.Erasure(qubit: int, round: int, record: int)#

Bases: object

One place where an atom can be lost and replaced: a heralded erasure. When it fires, its herald bit in the measurement record is 1 and the qubit gets I, X, Y or Z, each with probability 1/4.

qubit: int#
round: int#

0-based.

record: int#

The herald’s absolute index in the circuit’s measurement record.

class cosmiq.qec.MatchingDecoder(loss_aware: bool)#

Bases: object

Our matching decoder: MatchingDecoder(loss_aware=True) gives the erased locations’ edges weight 0 in each shot; loss_aware=False ignores the heralds.

solve(problem: Problem | DecodingGraph, detectors: NDArray[bool], erased: NDArray[bool]) → tuple[NDArray[bool], NDArray[float64]]#

The predicted observable flips (shots x observables) and each shot’s matching weight. Given a Problem, it builds (and caches) its matching graph from the circuit; a DecodingGraph is used as it is.

static graph(problem: Problem) → DecodingGraph#

The matching graph of a problem’s circuit (decoding_graph, cached).

decode(problem: Problem | DecodingGraph, detectors: NDArray[bool], erased: NDArray[bool]) → NDArray[bool]#

The predicted observable flips, shots x observables.

weight(problem: Problem | DecodingGraph, detectors: NDArray[bool], erased: NDArray[bool]) → NDArray[float64]#

Each shot’s matching weight: the sum of its matched shortest paths.

class cosmiq.qec.Noise(p: float, p_loss: float = 0.0, p_measure: float | None = None, p_reset: float | None = None, p_idle: float | None = None, sources: tuple[tuple[str, str], ...] = ())#

Bases: object

The noise of a memory experiment, as probabilities in [0, 0.5).

  • p: depolarising after every Clifford gate; also the default of the three below;

  • p_loss: a heralded erasure on every qubit in every round (data qubits at the round’s start, ancillas just before their measurement);

  • p_measure: a flip before each measurement;

  • p_reset: a flip after each reset;

  • p_idle: depolarising on the data qubits at the start of each round;

  • sources: (field, source) pairs, set by noise_from_device.

p: float#
p_loss: float = 0.0#
p_measure: float | None = None#
p_reset: float | None = None#
p_idle: float | None = None#
sources: tuple[tuple[str, str], ...] = ()#
class cosmiq.qec.Problem(circuit: Circuit, erasures: Sequence[Erasure])#

Bases: object

What a decoder is given (a neutral interface, so qLDPC codes and decoders fit later):

  • circuit and erasures: the memory circuit and its erasure locations;

  • dem: its detector error model without decomposition (approximate_disjoint_errors, as Stim needs for HERALDED_ERASE), so hyperedges stay whole;

  • erasure_map[i]: the error mechanisms erasure location i’s Paulis cause, each keyed by its (detectors, observables), from the error model of the circuit with only that erasure left. Computed on first use.

property erasure_map: tuple[frozenset[tuple[frozenset[int], frozenset[int]]], ...]#

Per erasure location, the mechanisms its X, Y and Z cause.

erased_mechanisms(erased: NDArray[bool]) → frozenset[tuple[frozenset[int], frozenset[int]]]#

The mechanisms of the erased locations in one shot (one bool per location).

class cosmiq.qec.Result(code: str, distance: int, noise: Noise, decoder: str, shots: int, seed: int | None, logical_error_rate: float, sigma: float, heralds_per_shot: float, samples: Samples, predictions: NDArray[bool])#

Bases: object

A memory experiment’s outcome.

  • logical_error_rate: the fraction of shots where a predicted observable flip was wrong;

  • sigma: its binomial standard error, sqrt(r (1 - r) / shots);

  • heralds_per_shot: the mean number of heralds that fired;

  • samples and predictions: the shots themselves and the decoder’s predictions.

code: str#
distance: int#
noise: Noise#
decoder: str#
shots: int#
seed: int | None#
logical_error_rate: float#
sigma: float#
heralds_per_shot: float#
samples: Samples#
predictions: NDArray[bool]#
property failures: NDArray[bool]#

Per shot: True where the prediction was wrong.

class cosmiq.qec.Samples(detectors: NDArray[bool], observables: NDArray[bool], erased: NDArray[bool])#

Bases: object

One sampling of a circuit: detectors and observables (shots x each, bool) and erased (shots x erasure locations: which heralds fired).

detectors: NDArray[bool]#
observables: NDArray[bool]#
erased: NDArray[bool]#
class cosmiq.qec.StimCode(name: str, distance: int)#

Bases: object

A built-in code at distance d (qec.code("surface", 5)): its memory circuit, with rounds = d, and its erasure locations.

property distance: int#

The code distance: 3 or 5.

circuit(noise: Noise, rounds: int) → Circuit#

Stim’s generated circuit at noise, with a heralded erasure on every qubit in every round. Only rounds = d is tested; any other is refused (QEC_DISTANCE).

erasure_locations() → tuple[Erasure, ...]#

Every erasure location, by herald record index: the same at every noise.

Functions

cosmiq.qec.code(name: str, d: int) → Code#

A code by name at distance d: “surface” or “repetition” (d = 3 or 5), or a registered one. Any other name is refused (QEC_CODE).

cosmiq.qec.decoder(name: str) → Decoder#

A new decoder by name: “loss_aware”, “loss_blind” or a registered one.

cosmiq.qec.decoding_graph(circuit: Circuit, erasures: Sequence[Erasure]) → DecodingGraph#

The decoding graph of a circuit with heralded erasures at erasures, cached by the circuit’s text. Where some erasure can fire, each location’s edges come from the error model of the circuit with only that erasure left (_erasure.isolated).

cosmiq.qec.memory_experiment(code: str, d: int, noise: Noise, shots: int, decoder: str = 'loss_aware', seed: int | None = None) → Result#

A memory experiment: code (“surface”, “repetition” or a registered one) at distance d with rounds = d, shots shots at noise, decoded by decoder (“loss_aware”, “loss_blind” or a registered one). The same seed gives the same detection events and decisions on the same Stim version, machine architecture and SIMD width (as Stim documents its seeds); nothing is claimed across machines.

cosmiq.qec.noise_from_device(device: str) → Noise#

The noise a device’s calibration and digital profile map to (DEVICE_MAPPING): a modelling choice, not the device’s QEC performance. A device needs a digital profile (otherwise DIGITAL_DEVICE) and the calibration keys the mapping reads (otherwise QEC_CALIBRATION): cosmiq-ref-1 has both; cosmiq-ref-1-ideal carries no calibration.

cosmiq.qec.register_code(name: str, factory: Callable[[int], Code]) → None#

Adds a code: memory_experiment(name, d=..., ...) then calls factory(d) for a Code. A name that’s taken is refused (ValueError).

cosmiq.qec.register_decoder(name: str, factory: Callable[[], Decoder]) → None#

Adds a decoder: memory_experiment(..., decoder=name) then calls factory() for a Decoder. A name that’s taken is refused (ValueError).

cosmiq.qec.sample(circuit: Circuit, erasures: Sequence[Erasure], shots: int, seed: int | None = None) → Samples#

shots shots of circuit, read the frozen way: the measurement record, its herald columns at erasures‘ record indices, and its detection events and observables.

Values

cosmiq.qec.DEVICE_MAPPING#