-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoding.py
More file actions
31 lines (23 loc) · 1.21 KB
/
Copy pathdecoding.py
File metadata and controls
31 lines (23 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Minimal MWPM decoding used to score logical failures.
The full decoder zoo (union-find, belief propagation, ML) lives in the
``decoder-benchmark`` repository. Here we only need a reference decoder to turn
sampled syndromes into a logical error rate, so we use PyMatching's
minimum-weight perfect matching decoder built directly from the circuit's
detector error model.
"""
from __future__ import annotations
import numpy as np
import pymatching
import stim
from .sampling import SyndromeSample
def matching_from_circuit(circuit: stim.Circuit) -> pymatching.Matching:
"""Build a PyMatching MWPM decoder from a circuit's detector error model."""
dem = circuit.detector_error_model(decompose_errors=True)
return pymatching.Matching.from_detector_error_model(dem)
def count_logical_failures(circuit: stim.Circuit, sample: SyndromeSample) -> int:
"""Decode ``sample`` with MWPM and count shots whose observable was mispredicted."""
matcher = matching_from_circuit(circuit)
predictions = matcher.decode_batch(sample.detection_events)
predictions = np.asarray(predictions, dtype=bool)
mismatches = np.any(predictions != sample.observable_flips, axis=1)
return int(np.count_nonzero(mismatches))