Getting started

In this tutorial we run a small Clifford circuit two ways, directly and under Clifford Noise Reduction (CliNR), and compare the logical error rates. Along the way we compute the analytic bounds, measure the cost in operations, and let the package choose the protocol’s parameters. Everything runs in a few seconds, and every result comes from one seeded generator, so running the page again from the top on the same installation gives the same numbers.

Install

The package needs Python 3.13 or later and is not on PyPI; install it from GitHub:

pip install git+https://github.com/Daminark/clifford-noise-reduction

A circuit and a noise model

Every random draw in the package takes a NumPy generator, so we seed one first, then draw a random Clifford circuit of 30 gates on 3 qubits.

import numpy as np

from clinr import accounting, bounds, circuits, estimators, noise_models

rng = np.random.default_rng(2026)
n = 3
circuit = circuits.random_gate_sequence(n, 30, rng)
s1, s2 = accounting.gate_counts(circuit)
print(f"{s1} single-qubit gates, {s2} two-qubit gates")
19 single-qubit gates, 11 two-qubit gates

Next, a noise model: two-qubit gates fail with probability 1e-3, and single-qubit gates, measurements and preparations ten times less often.

noise = noise_models.asymmetric_noise(1e-3)
noise
NoiseModel(p1=0.0001, p2=0.001, p_m=0.0001, p_prep=0.0001)

Run it directly

First we sample the circuit as it stands. The logical error rate is the fraction of shots in which the implemented channel is not the circuit’s; the estimate gives it with a 95% interval.

direct = estimators.estimate_direct(circuit, n, noise, 200_000, rng)
rate, low, high = direct.interval()
print(f"direct: {rate:.4f}  [{low:.4f}, {high:.4f}]")
direct: 0.0129  [0.0124, 0.0134]

Run it with CliNR

CliNR splits the circuit into t chunks and applies each through a resource state guarded by r stabilizer checks. We take one chunk and four checks. A shot counts only if every check passes, so the estimate also reports the fraction of shots accepted.

clinr = estimators.estimate_clinr(circuit, n, 1, 4, noise, 200_000, rng)
rate, low, high = clinr.interval()
print(f"CliNR, t = 1, r = 4: {rate:.4f}  [{low:.4f}, {high:.4f}]")
print(f"acceptance: {clinr.acceptance:.3f}")
CliNR, t = 1, r = 4: 0.0075  [0.0072, 0.0079]
acceptance: 0.967

Notice that the logical error rate is well below the direct one, and that nearly every shot is accepted. With one draw of the checks, the interval is for the rate given these checks; the protocol draws new checks for every attempt, and Average over repeated draws of the checks estimates its rate over many draws. The price is operations: the gate overhead is the expected number of operations per gate of the circuit, restarts included.

print(f"gate overhead: {estimators.gate_overhead(clinr):.2f}")
gate overhead: 3.12

Compare with the bounds

The package also bounds both quantities analytically, from the circuit’s gate counts alone.

plog = bounds.plog_bound(circuit, n, 1, 4, noise)
overhead = bounds.overhead_bound(circuit, n, 1, 4, noise)
print(f"logical error bound: {plog:.4f}")
print(f"gate overhead bound: {overhead:.2f}")
logical error bound: 0.0176
gate overhead bound: 3.25

Both bounds lie above what we measured, and the logical error bound lies above even the direct rate: it is a guarantee, and a loose one.

Vary the number of checks

Now we repeat the CliNR run with 0 to 8 checks. More checks catch more faults in the resource state, but every check is noisy too, and more checks mean more restarts.

for r in range(0, 9, 2):
    estimate = estimators.estimate_clinr(circuit, n, 1, r, noise, 100_000, rng)
    print(
        f"r = {r}: measured {estimate.p_log:.4f}, "
        f"bound {bounds.plog_bound(circuit, n, 1, r, noise):.4f}, "
        f"overhead {estimators.gate_overhead(estimate):.2f}"
    )
r = 0: measured 0.0188, bound 0.0205, overhead 1.80
r = 2: measured 0.0098, bound 0.0182, overhead 2.40
r = 4: measured 0.0071, bound 0.0176, overhead 2.90
r = 6: measured 0.0068, bound 0.0177, overhead 3.55
r = 8: measured 0.0070, bound 0.0178, overhead 4.29

The output should show the measured rate falling sharply over the first few checks and then leveling off, while the overhead keeps climbing and the bound barely moves. With no checks, CliNR does worse than running the circuit directly: the resource state and the teleportation only add operations, and so faults.

Let the package choose

Finally, clinr.bounds.choose_tr() compares the direct implementation with every pair (t, r) whose overhead bound stays within a ceiling we set, and returns the one with the smallest logical error bound, or None for the direct implementation.

print(bounds.choose_tr(circuit, n, noise, max_overhead=10, r_max=10))
None

It keeps the direct implementation, even though we measured CliNR well below it. The choice is made from the bounds alone, and for a circuit this small every CliNR bound within the ceiling sits above the direct implementation’s, the probability that one of its gates faults.

What we did

We ran a Clifford circuit under CliNR, brought its logical error rate well below the direct rate at about three operations per gate, compared both figures with their analytic bounds, and saw that a choice made from the bounds alone can miss that gain. Every function used here, with all its options, is in the API reference.