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).
A code: its memory-experiment circuit and where its erasures are. |
|
A decoder: observable flips predicted from detection events and heralds. |
|
The matching graph of a circuit's detector error model ( |
|
One place where an atom can be lost and replaced: a heralded erasure. |
|
Our matching decoder: |
|
The noise of a memory experiment, as probabilities in [0, 0.5). |
|
What a decoder is given (a neutral interface, so qLDPC codes and decoders fit later): |
|
A memory experiment's outcome. |
|
One sampling of a circuit: |
|
A built-in code at distance d ( |
|
A code by name at distance d: "surface" or "repetition" (d = 3 or 5), or a registered one. |
|
A new decoder by name: "loss_aware", "loss_blind" or a registered one. |
|
The decoding graph of a circuit with heralded erasures at |
|
A memory experiment: |
|
The noise a device's calibration and digital profile map to ( |
|
Adds a code: |
|
Adds a decoder: |
|
|
|
Classes
- class cosmiq.qec.Code(*args, **kwargs)#
Bases:
ProtocolA 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 withrounds = distance, which is whatmemory_experimentbuilds.
- class cosmiq.qec.Decoder(*args, **kwargs)#
Bases:
ProtocolA 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) anderased(shots x erasure locations, bool: which heralds fired). Nothing asks for a graphlike error model: a decoder that needs a matching graph builds it.
- class cosmiq.qec.DecodingGraph(dem: DetectorErrorModel, erasure_edges: Sequence[Sequence[int]] = ())#
Bases:
objectThe 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 bydecoding_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) andi(its index).
- shortest(sources: Sequence[int], zero: Sequence[int] = (), targets: Sequence[int] = ()) tuple[NDArray[float64], NDArray[int64]]#
Dijkstra from each source, with the edges
zeroat weight 0: the lengths and the observable masks of the paths, len(sources) x (num_detectors + 1), filled fortargets(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.
- class cosmiq.qec.Erasure(qubit: int, round: int, record: int)#
Bases:
objectOne 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.
- class cosmiq.qec.MatchingDecoder(loss_aware: bool)#
Bases:
objectOur matching decoder:
MatchingDecoder(loss_aware=True)gives the erased locations’ edges weight 0 in each shot;loss_aware=Falseignores 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; aDecodingGraphis 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:
objectThe 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 bynoise_from_device.
- class cosmiq.qec.Problem(circuit: Circuit, erasures: Sequence[Erasure])#
Bases:
objectWhat a decoder is given (a neutral interface, so qLDPC codes and decoders fit later):
circuitanderasures: the memory circuit and its erasure locations;dem: its detector error model without decomposition (approximate_disjoint_errors, as Stim needs forHERALDED_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.
- 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:
objectA 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;samplesandpredictions: the shots themselves and the decoder’s predictions.
- class cosmiq.qec.Samples(detectors: NDArray[bool], observables: NDArray[bool], erased: NDArray[bool])#
Bases:
objectOne sampling of a circuit:
detectorsandobservables(shots x each, bool) anderased(shots x erasure locations: which heralds fired).
- class cosmiq.qec.StimCode(name: str, distance: int)#
Bases:
objectA built-in code at distance d (
qec.code("surface", 5)): its memory circuit, with rounds = d, and its erasure locations.
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 distancedwith rounds = d,shotsshots atnoise, decoded bydecoder(“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 (otherwiseDIGITAL_DEVICE) and the calibration keys the mapping reads (otherwiseQEC_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 callsfactory(d)for aCode. 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 callsfactory()for aDecoder. A name that’s taken is refused (ValueError).
- cosmiq.qec.sample(circuit: Circuit, erasures: Sequence[Erasure], shots: int, seed: int | None = None) Samples#
shotsshots ofcircuit, read the frozen way: the measurement record, its herald columns aterasures‘ record indices, and its detection events and observables.
Values
- cosmiq.qec.DEVICE_MAPPING#