← Quantum Store · Guides · Lessons · Glossary · Packages · About · Türkçe

Google Quantum AI — Stim; Oscar Higgott — PyMatching

Surface code error correction experiment (Stim + PyMatching)

ProtocolError correctionLicence: Apache-2.0Version: 1.16.010.6 KB

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.

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

  1. Scan p over 0.001–0.01 and find the threshold where the curves cross.
  2. Add d=7. You may need to increase the number of samples.
  3. 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

Links

Package contents

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

← All packages

Share: X · LinkedIn