qldpc.codes.monte_carlo

Monte-Carlo helpers for code-capacity logical error rate estimation.

These utilities turn the failure and discard counts collected by the .get_logical_error_rate_func methods of the code classes into logical error and discard rate estimates, and support the sampling that those methods perform. They depend only on the decoder interface and on the binomial weight distribution, not on the code classes themselves, so they live in their own module.

Attributes

OneOrManyFloats

Classes

ErrorRateFunc

Container for raw simulation data used to compute logical error and discard rates.

Functions

get_error_and_erasure(→ tuple[galois.FieldArray, bool])

Decode a syndrome and return the inferred error together with an erasure flag.

get_sample_allocation(→ numpy.typing.NDArray[numpy.int_])

Construct an allocation of samples by error weight.

jeffreys_variance(→ numpy.typing.NDArray[numpy.floating])

Posterior variance of a binomial rate under a Jeffreys prior.

Module Contents

class qldpc.codes.monte_carlo.ErrorRateFunc[source]

Container for raw simulation data used to compute logical error and discard rates.

An instance of this class is built and returned by the .get_logical_error_rate_func method of ClassicalCode, QuditCode, and CSSCode. If:

func = code.get_logical_error_rate_func(...),

then “func” takes a physical error rate “p” as an argument, and returns two numbers:

  1. A logical error rate, estimated over the error weights that were sampled.

  2. A statistical uncertainty in that rate: the standard deviation propagated from the

    per-weight Jeffreys posterior variances.

If called with an array of physical error rates, this function returns two arrays. If called with discard_rate=True, it computes a discard rate instead of an error rate.

Errors of weight above the max_error_weight are never sampled, and this class counts every one of them as a failure. The reported error rate therefore sits above the true error rate, by at most func.truncation_error_bound(p), which is the probability of drawing such a heavy error. A plot of the reported rate usually carries a vertical bar spanning the range in which the true rate might lie. That bar is asymmetric here: the statistical uncertainty spreads in both directions, but counting the unsampled errors as failures only pushes the reported rate up. Writing value, error = func(p), the bottom and top of the bar are:

``lower = max(value - error - func.truncation_error_bound(p), 0)``,
``upper = value + error``.

The error bar defined by lower and upper is almost, but not exactly a confidence interval: its lower edge mixes a posterior standard deviation with a bound that is not statistical at all, and its center is the observed failure rate rather than the mean of the posterior that supplies its width (see infidelities). Read it as a rough indication of what is unknown.

No truncation bound enters a discard rate, because an error counted as a failure is not counted as a discard. A discard rate’s bar runs from value - error to value + error.

Errors of weight below min_error_weight are assumed to decode perfectly, which keeps light errors from dominating the reported uncertainty at small physical error rates.

truncation_error_bound(error_rate: OneOrManyFloats) → OneOrManyFloats[source]

Contribution to the reported infidelity from errors too heavy to have been sampled.

Errors heavier than max_error_weight are all treated as failures, so this is the probability of such an error. Takes one physical error rate or an iterable of them, and returns one bound or an array of them to match, as the constructed function itself does.

The probability of an error heavier than a given weight is the upper tail of a binomial distribution, which the regularized incomplete beta function gives in closed form. With n = num_error_locations error locations each erring with probability p, and weights up to k covered,

sum_(j=k+1)^(n) (n choose j) p**j (1-p)**(n-j) = I_p(k + 1, n - k).

property discard_rate_variances: numpy.typing.NDArray[numpy.floating]

The Jeffreys posterior variance of the discard rate at each error weight.

See help(qldpc.codes.monte_carlo.jeffreys_variance) for additional info.

property discard_rates: numpy.typing.NDArray[numpy.floating]

Discard rate at each error weight.

property infidelities: numpy.typing.NDArray[numpy.floating]

Mean infidelity at each error weight.

This is the observed failure rate. The posterior whose variance infidelity_variances reports sits above it, by 1/(2n + 2) at a weight with n kept samples and no observed failure, so the reported rate and its uncertainty do not come from the same estimator.

property infidelity_variances: numpy.typing.NDArray[numpy.floating]

The Jeffreys posterior variance of the infidelity at each error weight.

See help(qldpc.codes.monte_carlo.jeffreys_variance) for additional info.

max_error_rate: float
property max_error_weight: int

Max error weight considered.

min_error_weight: int = 1
num_discards: numpy.typing.NDArray[numpy.int_]
num_error_locations: int
num_failures: numpy.typing.NDArray[numpy.int_]
num_samples: numpy.typing.NDArray[numpy.int_]
qldpc.codes.monte_carlo.get_error_and_erasure(decoder: qldpc.decoders.Decoder, syndrome: galois.FieldArray) → tuple[galois.FieldArray, bool][source]

Decode a syndrome and return the inferred error together with an erasure flag.

If the decoder has a has_erasure_bit attribute set to True (e.g., a LookupDecoder constructed with add_erasure_bit=True), the last element of the decoded vector is treated as the erasure bit: 1 means the syndrome was not recognized and the sample should be discarded, 0 means a correction was found normally. The erasure bit is stripped before returning the error.

qldpc.codes.monte_carlo.get_sample_allocation(num_samples: int, block_length: int, max_error_rate: float, min_error_weight: int = 1) → numpy.typing.NDArray[numpy.int_][source]

Construct an allocation of samples by error weight.

This method returns an array whose k-th entry is the number of samples to devote to errors of weight k, given a maximum error rate that we care about.

A single allocation has to serve every physical error rate p <= max_error_rate, so samples are apportioned in proportion to the largest probability that each error weight is ever assigned over that range of p (see _get_max_error_probs_by_weight). Every sampled weight is guaranteed at least one sample, so no weight below the maximum sampled weight is silently recorded as failure-free for want of data.

The heaviest weight sampled is the heaviest whose share of the budget reaches half a sample (see _get_max_error_weight), so a larger budget covers more weights and leaves less probability above them. Covering a weight costs at least one sample, so a budget spread thinly over many weights can be overspent.

Weights below min_error_weight are taken to be decoded perfectly and get no samples: there is nothing to learn about them, so spending samples there would only take samples away from the weights that the decoder can actually fail on. The default of one excludes weight 0, the no-error case.

qldpc.codes.monte_carlo.jeffreys_variance(num_events: numpy.typing.NDArray[numpy.int_], num_trials: numpy.typing.NDArray[numpy.int_]) → numpy.typing.NDArray[numpy.floating][source]

Posterior variance of a binomial rate under a Jeffreys prior.

With x events observed in n trials, the rate posterior is Beta(x + 1/2, n - x + 1/2), whose mean is (x + 1/2) / (n + 1) and whose variance is mean * (1 - mean) / (n + 2). Unlike the plug-in variance f (1 - f) / n, this is positive at x = 0, so a weight with no observed events still carries uncertainty, and finite at n = 0, where it reverts to the prior variance 1/8 for a weight with no data at all.

See Brown, Cai & DasGupta, “Interval Estimation for a Binomial Proportion,” Statist. Sci. 16 (2001) 101-133, https://doi.org/10.1214/ss/1009213286, for this posterior and its behaviour at small counts. That work recommends quoting an interval between two quantiles of the posterior, which callers here cannot use: they need one uncertainty on a weighted sum over error weights, and variances add in quadrature across that sum while quantiles do not. A variance is therefore what this function returns, at the price of a symmetric half-width that does not inherit the coverage of the recommended interval.

qldpc.codes.monte_carlo.OneOrManyFloats