mimiqcircuits.backends.fidelity

Typed fidelity values returned by LocalBackend.evolve().

Different simulators report fidelity with different semantics: a state-vector backend can claim exactness, an MPS backend can only return a truncation lower bound, and a randomised-benchmarking backend can return a sample-based estimate with a standard error.

A plain float return would silently lose those distinctions. The tagged variants below preserve them:

Reduce to plain numbers via as_lower_bound() (conservative), as_expected() (central), or as_interval() ((lo, hi)).

Functions

as_expected(f)

Reduce f to a central estimate.

as_interval(f)

Reduce f to a (low, high) interval.

as_lower_bound(f)

Reduce f to a conservative lower bound.

Classes

EstimatedFidelity(mean, stderr)

Sample-based fidelity estimate with a standard error.

ExactFidelity()

The simulator believes the state is exact.

Fidelity()

Base class for typed fidelity values.

LowerBoundPerStep(values)

Per-step lower-bound contributions.

TruncationLowerBound(value)

A single lower bound on the fidelity of the whole circuit.

UnknownFidelity()

The backend does not track fidelity.

class Fidelity[source]

Bases: object

Base class for typed fidelity values.

The subclasses say what a fidelity number means: ExactFidelity, UnknownFidelity, TruncationLowerBound, LowerBoundPerStep and EstimatedFidelity. as_lower_bound(), as_expected() and as_interval() reduce any of them to plain numbers.

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits.backends import *

An MPS simulator that discarded 1% of the norm reports a lower bound, not an exact value.

>>> f = TruncationLowerBound(0.99)
>>> as_lower_bound(f), as_expected(f)
(0.99, 0.99)

An estimate with a standard error is reduced conservatively by as_lower_bound() (mean minus three standard errors).

>>> e = EstimatedFidelity(mean=0.9, stderr=0.01)
>>> round(as_lower_bound(e), 2), as_expected(e)
(0.87, 0.9)
>>> as_lower_bound(UnknownFidelity())
nan
class ExactFidelity[source]

Bases: Fidelity

The simulator believes the state is exact.

BLAS and SIMD round-off are not tracked; this is a structural claim about the algorithm, not a bit-perfect guarantee.

class UnknownFidelity[source]

Bases: Fidelity

The backend does not track fidelity.

Reducers return nan. Prefer this variant over inventing a placeholder value when you genuinely do not know.

class TruncationLowerBound(value)[source]

Bases: Fidelity

A single lower bound on the fidelity of the whole circuit.

Typical for MPS and MPO simulators, where the bound comes from the singular values discarded during compression. Return it even when the bound is exactly 1.0: a plain 1.0 from LocalBackend.evolve() is read as ExactFidelity.

value

The lower bound.

Type:

float

class LowerBoundPerStep(values)[source]

Bases: Fidelity

Per-step lower-bound contributions.

as_lower_bound() returns the product of the values, each clipped to [0, 1]. That product bounds the circuit fidelity only when successive truncation errors are uncorrelated; a backend that cannot assume this returns a single TruncationLowerBound instead.

values

One contribution per step.

Type:

tuple[float, …]

class EstimatedFidelity(mean, stderr)[source]

Bases: Fidelity

Sample-based fidelity estimate with a standard error.

Use it for randomised benchmarking, direct fidelity estimation, or cross-entropy benchmarking. as_lower_bound() returns max(0, mean - 3 * stderr) and as_interval() returns (mean - stderr, mean + stderr).

mean

The estimate.

Type:

float

stderr

Its standard error.

Type:

float

as_lower_bound(f)[source]

Reduce f to a conservative lower bound.

Parameters:

f (Fidelity) – The fidelity.

Returns:

1.0 for ExactFidelity; nan for UnknownFidelity, so that statistics over many results do not count an unknown fidelity as a good one; the value for TruncationLowerBound; the product of the clipped values for LowerBoundPerStep; max(0, mean - 3 * stderr) for EstimatedFidelity.

Raises:

TypeError – If f is not one of these types.

Return type:

float

as_expected(f)[source]

Reduce f to a central estimate.

Parameters:

f (Fidelity) – The fidelity.

Returns:

The same as as_lower_bound(), except for EstimatedFidelity, which gives its mean.

Raises:

TypeError – If f is not a known Fidelity type.

Return type:

float

as_interval(f)[source]

Reduce f to a (low, high) interval.

Parameters:

f (Fidelity) – The fidelity.

Returns:

(mean - stderr, mean + stderr) for EstimatedFidelity, otherwise (as_lower_bound(f), as_expected(f)).

Raises:

TypeError – If f is not a known Fidelity type.

Return type:

tuple[float, float]