quantumlib / Stim

A fast stabilizer circuit library.
Apache License 2.0
305 stars 88 forks source link

Add `stim.CompiledDetectorSampler.sample(..., dets_out=None, obs_out=None)` #782

Closed Strilanc closed 4 weeks ago

Strilanc commented 4 weeks ago

Benchmarked by taking 250 instances of 1024 shots from a distance 11 surface code running for 33 rounds:

So... the buffer appears to not be hugely significant, but the copy reduction was very useful.

import numpy as np
import stim
import time

circuit = stim.Circuit.generated(
    "surface_code:rotated_memory_x",
    distance=11,
    rounds=33,
    after_clifford_depolarization=1e-3,
    before_measure_flip_probability=1e-3,
    after_reset_flip_probability=1e-3,
    before_round_data_depolarization=1e-3,
)
sampler = circuit.compile_detector_sampler()

det_buf = np.empty((1024, (circuit.num_detectors + 7) // 8), dtype=np.uint8)
obs_buf = np.empty((1024, (circuit.num_observables + 7) // 8), dtype=np.uint8)
t0 = time.monotonic()

if True:
    for _ in range(250):
        sampler.sample(
            shots=1024,
            bit_packed=True,
            dets_out=det_buf,
            obs_out=obs_buf,
        )
else:
    for _ in range(250):
        sampler.sample(
            shots=1024,
            bit_packed=True,
        )
t1 = time.monotonic()
dt = t1 - t0
print(dt)
print(dt / 1024)
print(dt / 1024 / 1024)
print(dt / 1024 / 1024 / circuit.num_detectors)