Skip to content

Add PersistentMatcher: build the matching graph once, reweight it per decode - #195

Open
ziofil wants to merge 1 commit into
oscarhiggott:masterfrom
ziofil:persistent-matcher
Open

ziofil wants to merge 1 commit into
oscarhiggott:masterfrom
ziofil:persistent-matcher

Conversation

@ziofil

@ziofil ziofil commented Nov 13, 2026

Copy link
Copy Markdown

Adds pymatching.PersistentMatcher, for decoding many syndromes of the same check matrix with different weights on every shot (for example per-shot soft information). Today each new weight vector needs a new Matching, and building it costs far more than the decode. PersistentMatcher builds the matching graph once and rewrites its weights in place: 17–21× faster per shot, with outputs identical to

pymatching.Matching.from_check_matrix(check_matrix, weights=w, faults_matrix=f).decode(syndrome)

Usage

from pymatching import PersistentMatcher

matcher = PersistentMatcher(check_matrix)            # optionally weights=..., faults_matrix=...
for weights, syndrome in shots:
    matcher.set_weights(weights)
    correction = matcher.decode(syndrome)            # the predicted observables if a faults_matrix was given

Scope: the defaults of Matching.from_check_matrix (merge_strategy="smallest-weight", no virtual boundary node, one repetition), weights rather than error probabilities, at most two non-zero elements per column. Weights may be negative.

Speed

Per shot, with new weights every shot. Surface-code X checks, weights log((1 - p) / p) with p drawn per fault in [0.005, 0.05]; Linux x86-64, one thread. Every output was checked against Matching: all identical.

d check matrix returns new Matching per shot PersistentMatcher speed-up
5 20 × 41 correction 0.102 ms 0.006 ms 17.7×
11 110 × 221 correction 0.378 ms 0.018 ms 21.1×
21 420 × 841 correction 1.227 ms 0.071 ms 17.2×
31 930 × 1861 correction 2.370 ms 0.144 ms 16.4×
31 930 × 1861 1 observable 1.522 ms 0.073 ms 20.8×

Validation

  • New tests/matching/persistent_matcher_test.py compares against a freshly built Matching on every shot: repetition, surface and toric codes, parallel edges, negative and integer weights, 1 to 70 observables, input validation.
  • The full test suite passes on macOS arm64 and Linux x86-64.
How it works

src/pymatching/sparse_blossom/driver/persistent.pybind.cc builds the UserGraph and Mwpm exactly as from_check_matrix does, then records, for every user edge, the columns merged into it and the slots of its weight in the matching graph's adjacency lists. set_weights redoes what a rebuild would do with the new weights:

  • the smallest-weight merge of parallel edges, in column order (the first column with the strictly smallest weight is kept, with its observables);
  • the discretisation of UserGraph::iter_discretized_edges, with the normalising constant of get_edge_weight_normalising_constant (no rescaling when all weights are integers);
  • the negative-weight bookkeeping of MatchingGraph::add_edge / add_boundary_edge, then sync_negative_weight_observables_and_detection_events as to_mwpm does;
  • with more than 64 observables (including the default identity faults matrix, which returns the correction), the search graph's weights, observable lists and sign markers too.

The graph's structure (nodes, neighbour order) does not depend on the weights, so this reproduces a rebuild. A thin Python class (src/pymatching/persistent_matcher.py) validates the inputs.

Co-authored with Claude (Anthropic).

… decode

When many syndromes of the same check matrix are decoded with different
weights, building a new Matching per shot costs far more than the decode.
PersistentMatcher builds the UserGraph and Mwpm once, as
Matching.from_check_matrix does, and set_weights rewrites the edge weights
in place, reproducing the smallest-weight merge, the discretisation and the
negative-weight bookkeeping of a rebuild; decode returns the same result as
a freshly built Matching.
@ziofil

ziofil commented Nov 18, 2026

Copy link
Copy Markdown
Author

@oscarhiggott bump

@ziofil

ziofil commented Dec 4, 2026

Copy link
Copy Markdown
Author

Bump again?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant