← Quantum Store · Guides · Lessons · Glossary · Packages · About · Türkçe
Google Quantum AI — Stim; Oscar Higgott — PyMatching
Surface code error correction experiment (Stim + PyMatching)
Official source: quantumlib/Stim
Download ZIP (free)A tested protocol that simulates a rotated surface code memory experiment with Stim, decodes it with PyMatching and compares the logical error rates of d=3 and d=5.
What does this protocol do?
Quantum computers are noisy. The surface code combines many physical qubits so
that they behave like a single "logical" qubit, and catches errors with parity
(stabiliser) checks that are measured continuously. As the code distance d
grows, the logical error falls, provided the physical error rate is below the
threshold.
- Stim (Google Quantum AI) samples these circuits millions of times in seconds.
- PyMatching estimates the most likely error from the measured error signatures (detection events) with minimum-weight matching.
Test result
With 20,000 samples, yuzey_kodu_deneyi.py gave these logical error rates:
| physical p | d=3 | d=5 |
|---|---|---|
| 0.002 | 0.00260 | 0.00085 |
| 0.005 | 0.01660 | 0.01445 |
At p=0.002, raising the distance from 3 to 5 cuts the error by about a factor of three. At p=0.005 the gain is small because it is close to the threshold.
Next steps
- Scan
pover 0.001–0.01 and find the threshold where the curves cross. - Add
d=7. You may need to increase the number of samples. - For real Google hardware data, see this store's "Google surface code experiment data" package.
Installation and running
pip install stim pymatching numpy
python yuzey_kodu_deneyi.py
Tested with: stim 1.16.0, pymatching 2.4.0, Python 3.12.
Official source
- Repository: quantumlib/Stim
- Latest release: v1.16.0 — 22 May 2026
- Licence: Apache-2.0
- Stars: 832
- Default branch commit when the catalogue was built:
e51d0482abba(12 October 2026)
- Repository: oscarhiggott/PyMatching
- Latest release: v2.3.1 — 25 September 2025
- Licence: Apache-2.0
- Stars: 365
- Default branch commit when the catalogue was built:
6f63b2b9474b(22 May 2026)
Links
Package contents
yuzey_kodu_deneyi.pyREADME.en.md— this fileREADME.md— the same guide in TurkishLICENSE— the licence of the files in this packageKAYNAK.json— a machine-readable summary of the official sourceUPSTREAM-LICENSE.txt— the licence text of the official projectCITATION.cff— citation information of the official project
Licence
The example code and the guide in this package were written by the Quantum Store editors and are MIT licensed. The project it uses is subject to its own licence (Apache-2.0).
Quantum Store — https://hakanerdogan.com/en
Example code
The example shipped in the package, run and checked. Comments and printed messages are in Turkish; the code is the same in any language. Everything is in the ZIP.
yuzey_kodu_deneyi.py
"""Yüzey kodu (surface code) hafıza deneyi: Stim ile simüle et, PyMatching ile çöz.
Farklı kod mesafeleri (d = 3, 5) ve fiziksel hata oranları için mantıksal
hata oranını ölçer. Hata oranı eşiğin altındayken, mesafeyi büyütmek
mantıksal hatayı azaltır; bu kuantum hata düzeltmenin temel fikridir.
Çalıştırma:
pip install stim pymatching numpy
python yuzey_kodu_deneyi.py
"""
import numpy as np
import pymatching
import stim
def logical_error_rate(distance: int, p: float, shots: int = 20_000) -> float:
circuit = stim.Circuit.generated(
"surface_code:rotated_memory_z",
rounds=distance,
distance=distance,
after_clifford_depolarization=p,
after_reset_flip_probability=p,
before_measure_flip_probability=p,
before_round_data_depolarization=p,
)
sampler = circuit.compile_detector_sampler(seed=7)
detections, observables = sampler.sample(shots, separate_observables=True)
dem = circuit.detector_error_model(decompose_errors=True)
matcher = pymatching.Matching.from_detector_error_model(dem)
predictions = matcher.decode_batch(detections)
return float(np.mean(np.any(predictions != observables, axis=1)))
rates = {}
print("fiziksel p d=3 d=5")
for p in (0.002, 0.005):
rates[p] = {d: logical_error_rate(d, p) for d in (3, 5)}
print(f"{p:<11} {rates[p][3]:<10.5f} {rates[p][5]:<10.5f}")
assert rates[0.002][5] < rates[0.002][3], rates
print("\nTamam: eşiğin altında d=5, d=3'ten daha az mantıksal hata yapıyor.")
Related concepts
- Quantum error correction · Quantum error correction spreads one logical qubit over many physical qubits so that errors caused by noise can be found and fixed without measuring the qubits directly.
- Surface code · The surface code arranges qubits on a two-dimensional grid and uses only operations between neighbouring qubits; it is the most widely tested quantum error-correcting code today.