Skip to main content

BeliefMatching

PyPI version

An implementation of the belief-matching decoder, using pymatching for the minimum-weight perfect matching (MWPM) subroutine and the ldpc library for the belief propagation (BP) subroutine. Belief-matching is more accurate than the MWPM decoder alone when hyperedge error mechanisms are present in the error model. Belief-matching has the same worst-case complexity as minimum-weight perfect matching, and the expected running time is roughly linear in the size of the decoding problem (Tanner graph). See the paper for more details.

However, note that this implementation is much (>100x) slower than just using the pymatching (v2) decoder alone, since it has not been optimised for performance. For example, for each shot, belief propagation is run on the full Tanner graph (stim DetectorErrorModel) with the output used to construct a new instance of a pymatching Matching object. This implementation uses the ldpc library for BP, which uses a parallel BP schedule, and does not support the serial BP schedule shown to have slightly improved accuracy for belief-matching in the appendix of this paper.

Installation

To install beliefmatching, run:

pip install beliefmatching

To install from source, run:

pip install -e .

from the root directory.

Usage

Here is an example of how the decoder can be used directly with Stim:

import stim
import numpy as np
from beliefmatching import BeliefMatching

num_shots = 100
d = 5
p = 0.007
circuit = stim.Circuit.generated(
    "surface_code:rotated_memory_x",
    rounds=d,
    distance=d,
    before_round_data_depolarization=p,
    before_measure_flip_probability=p,
    after_reset_flip_probability=p,
    after_clifford_depolarization=p
)

sampler = circuit.compile_detector_sampler()
shots, observables = sampler.sample(num_shots, separate_observables=True)

bm = BeliefMatching(circuit, max_bp_iters=20)

predicted_observables = bm.decode_batch(shots)
num_mistakes = np.sum(np.any(predicted_observables != observables, axis=1))

print(f"{num_mistakes}/{num_shots}")  # prints 4/100

Note that, as well as loading directly from a stim.Circuit as above, you can also load from a stim.DetectorErrorModel. When using this option it is important that decompose_errors=True is set when calling circuit.detector_error_model. E.g.:

dem = circuit.detector_error_model(decompose_errors=True)
bm = BeliefMatching(dem, max_bp_iters=20)

Sinter integration

To integrate with sinter, you can use the beliefmatching.BeliefMatchingSinterDecoder class, which inherits from sinter.Decoder. To use it, you can use the custom_decoders argument when using sinter.collect:

import sinter
from beliefmatching import BeliefMatchingSinterDecoder

samples = sinter.collect(
    num_workers=4,
    max_shots=1_000_000,
    max_errors=1000,
    tasks=generate_example_tasks(),
    decoders=['beliefmatching'],
    custom_decoders={'beliefmatching': BeliefMatchingSinterDecoder()}
)

A complete example using sinter to compare beliefmatching with pymatching can be found in the examples/surface_code_threshold.py file (this file also includes a definition of generate_example_tasks() used above).

Tests

Tests can be run by installing pytest with

pip install pytest

and running

pytest tests

Attribution

When using beliefmatching for research, please cite the paper:

@article{PhysRevX.13.031007,
  title = {Improved Decoding of Circuit Noise and Fragile Boundaries of Tailored Surface Codes},
  author = {Higgott, Oscar and Bohdanowicz, Thomas C. and Kubica, Aleksander and Flammia, Steven T. and Campbell, Earl T.},
  journal = {Phys. Rev. X},
  volume = {13},
  issue = {3},
  pages = {031007},
  numpages = {20},
  year = {2023},
  month = {Jul},
  publisher = {American Physical Society},
  doi = {10.1103/PhysRevX.13.031007},
  url = {https://link.aps.org/doi/10.1103/PhysRevX.13.031007}
}

Release files for beliefmatching 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for beliefmatching 0.2.0
File Size Uploaded
beliefmatching-0.2.0.tar.gz 13.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for beliefmatching 0.2.0
File Interpreter ABI Platform
beliefmatching-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.2 kB

Release files / beliefmatching-0.2.0.tar.gz

Download URL beliefmatching-0.2.0.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
915175fc6ab4b73cfb948774ef06d71abc045a189dfb21cdf3dbd69389b111cc
BLAKE2b-256 checksum
How to use checksums
43e45b5cc731a4507c7243a90c786390041ceb69dbe304ee17debd589049b6a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / beliefmatching-0.2.0-py3-none-any.whl

Download URL beliefmatching-0.2.0-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
601e5511e34acd00daad6f421a229aa60f3ff58a1c8800f416b2ffa236c7a22a
BLAKE2b-256 checksum
How to use checksums
f24849f5bc2a7ac44226d07381ac553f44966bcee1f4c17d3688d960023ac228
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page