mimiqcircuits.backends.passes

Optimization-pass framework.

A pass transforms a Circuit before evolution; the PassPipeline runs them in order and tracks qubit permutations so amplitude and expectation queries can be unscrambled back to the user’s original qubit space at the end.

Parameters travel over the wire (to remote backends) through a JSON-safe ADT, PassParam, instead of a loose dict: the tagged variants preserve type information that a plain dict would silently lose.

Custom passes subclass AbstractPass and implement spec() (a declarative PassSpec) and apply() (the actual rewrite). See Implementing a Custom Backend for a worked example.

Functions

apply_passes(pipeline, ctx, circuit)

Run pipeline against circuit.

check_prep_flags(*[, fuse, fuse_threshold, ...])

Validate the circuit-preparation flags and return them normalised, as a dict keyed by flag name.

invert_perm(perm)

Inverse of a 1-based permutation.

resolve_pipeline(backend, passes, *[, fuse, ...])

Circuit preparation, resolved: the backend's default_passes(), adjusted by the flags, then extended with the caller's own passes.

to_pass_param(x)

Coerce a common Python value into a PassParam.

Classes

AbstractPass()

Base class for circuit-transformation passes.

PBool(value)

PDict(items)

PFloat(value)

PInt(value)

PList(items)

PStr(value)

PSym(value)

Pass parameter that names one of a fixed set of choices, like an enum.Enum member.

PassContext([backend, rng, bitstrings, features])

Inputs a pass needs but should not fetch from global state.

PassParam()

Base class for the JSON-safe sum used to serialise pass parameters.

PassPipeline([passes])

An ordered, iterable sequence of passes.

PassResult([qubit_permutation, metadata])

Side effects returned from AbstractPass.apply().

PassSpec(name[, parameters, requires, conflicts])

Declarative summary of a pass.

Exceptions

RemotePassOrderError(backend_name)

Raised when an ordered pipeline is submitted to a remote backend that does not advertise the "pass_order_honored" capability while strict_pass_order=True.

UnacceptedPassError(backend_name, pass_name)

Raised when a pipeline contains a pass the backend rejects.

class mimiqcircuits.backends.passes.PassParam[source]

Bases: object

Base class for the JSON-safe sum used to serialise pass parameters.

The tagged variants (PStr, PInt, PFloat, PBool, PSym, PList, PDict) keep type information a plain dict would lose on the wire — e.g. "greedy" (a PStr) must not silently degrade and come back as a PSym. Equality is tag-sensitive: PSym("x") != PStr("x").

class mimiqcircuits.backends.passes.PStr(value: 'str')[source]

Bases: PassParam

value: str
__init__(value)
class mimiqcircuits.backends.passes.PInt(value: 'int')[source]

Bases: PassParam

value: int
__init__(value)
class mimiqcircuits.backends.passes.PFloat(value: 'float')[source]

Bases: PassParam

value: float
__init__(value)
class mimiqcircuits.backends.passes.PBool(value: 'bool')[source]

Bases: PassParam

value: bool
__init__(value)
class mimiqcircuits.backends.passes.PSym(value)[source]

Bases: PassParam

Pass parameter that names one of a fixed set of choices, like an enum.Enum member. Distinct from PStr at the tag level.

value: str
__init__(value)
class mimiqcircuits.backends.passes.PList(items: 'tuple[PassParam, ...]')[source]

Bases: PassParam

items: tuple[PassParam, ...]
__init__(items)
class mimiqcircuits.backends.passes.PDict(items: 'tuple[tuple[str, PassParam], ...]')[source]

Bases: PassParam

items: tuple[tuple[str, PassParam], ...]
__init__(items)
mimiqcircuits.backends.passes.to_pass_param(x)[source]

Coerce a common Python value into a PassParam.

The bool-before-int check is load-bearing: bool is a subclass of int in Python, so isinstance(True, int) is True and reversing the order would tag every boolean as PInt.

class mimiqcircuits.backends.passes.PassSpec(name, parameters=(), requires=(), conflicts=())[source]

Bases: object

Declarative summary of a pass.

Used for equality / memoisation (two passes with the same spec have the same hash and compare equal), for remote dispatch (the spec is the only thing shipped over the wire), and for observability. Equality is structural.

requires names other passes that must run before this one; conflicts names passes that cannot coexist with this one in the same pipeline. Both are advisory.

name: str
parameters: tuple[tuple[str, PassParam], ...] = ()
requires: tuple[str, ...] = ()
conflicts: tuple[str, ...] = ()
static from_dict(name, parameters=None, requires=(), conflicts=())[source]
__init__(name, parameters=(), requires=(), conflicts=())
class mimiqcircuits.backends.passes.PassResult(qubit_permutation=None, metadata=<factory>)[source]

Bases: object

Side effects returned from AbstractPass.apply().

qubit_permutation is None when the pass leaves qubit indices unchanged. Any pass that does relabel qubits must return the relabel here so the pipeline can compose permutations and un-shuffle downstream outputs.

metadata is free-form pass-specific information (timings, gate counts, …) surfaced for observability.

qubit_permutation: list[int] | None = None
metadata: dict[str, Any]
__init__(qubit_permutation=None, metadata=<factory>)
class mimiqcircuits.backends.passes.PassContext(backend=None, rng=<factory>, bitstrings=<factory>, features=<factory>)[source]

Bases: object

Inputs a pass needs but should not fetch from global state.

backend is the Backend the pipeline is compiling for, or None for backend-agnostic runs. rng is the dedicated pass-internal RNG. bitstrings are user-supplied amplitude targets in the original qubit space; the pipeline rewrites them into post-pass space as permutations compose. features are detected on the input circuit and filter passes via AbstractPass.preserves().

backend: Any | None = None
rng: Random
bitstrings: list
features: set[str]
__init__(backend=None, rng=<factory>, bitstrings=<factory>, features=<factory>)
class mimiqcircuits.backends.passes.AbstractPass[source]

Bases: ABC

Base class for circuit-transformation passes.

Subclasses implement:

  • spec() — return a PassSpec describing the pass (used for equality, serialisation, and remote dispatch).

  • apply() — return (new_circuit, PassResult).

  • preserves() — override when the pass breaks a circuit feature; the default returns True for every feature. The pipeline filters passes by feature when the input circuit has a feature that the pass must preserve.

Recognised feature tokens: "feed_forward", "midcircuit_measure", "parametric", "loss", "exact_equivalence", "strict_qubit_count".

abstract spec()[source]
abstract apply(ctx, circuit)[source]
preserves(feature)[source]
class mimiqcircuits.backends.passes.PassPipeline(passes=<factory>)[source]

Bases: object

An ordered, iterable sequence of passes.

Run via apply_passes(), which composes each pass’s qubit_permutation into a single permutation. Callers use the inverse of that permutation to map samples and Amplitude / ExpectationValue results back to the user’s original qubit space.

passes: list[AbstractPass]
__init__(passes=<factory>)
mimiqcircuits.backends.passes.check_prep_flags(*, fuse=None, fuse_threshold=None, canonicaldecompose=None, reorderqubits=None, remove_swaps=None, simplify=None, reorderqubits_seed=None)[source]

Validate the circuit-preparation flags and return them normalised, as a dict keyed by flag name.

fuse, canonicaldecompose, remove_swaps, simplify and reorderqubits must be None or a bool (NumPy bools become bool); reorderqubits may also be a method name. fuse_threshold and reorderqubits_seed must be None or an integer other than a bool, and become int. Anything else raises TypeError: a value like fuse=0 would otherwise read as “set” and install the pass.

mimiqcircuits.backends.passes.resolve_pipeline(backend, passes, *, fuse=None, fuse_threshold=None, canonicaldecompose=None, reorderqubits=None, remove_swaps=None, simplify=None, reorderqubits_seed=None)[source]

Circuit preparation, resolved: the backend’s default_passes(), adjusted by the flags, then extended with the caller’s own passes.

Each flag speaks about one kind of pass. Set to False it drops that pass from the backend’s declaration; set to anything else it replaces it with the pass the backend builds for that flag (fuse_pass(), reorder_pass(), canonical_decompose_pass(), remove_swaps_pass(), simplify_pass()), or adds that one if the backend declared none. Everything the flags say nothing about is left exactly as the backend wants it.

passes are appended at the end, as given. Invalid flag values raise TypeError; see check_prep_flags().

exception mimiqcircuits.backends.passes.UnacceptedPassError(backend_name, pass_name)[source]

Bases: Exception

Raised when a pipeline contains a pass the backend rejects.

The backend’s Backend.accepts_pass() is the gate; the pipeline fails loudly rather than silently dropping a pass the user explicitly requested.

__init__(backend_name, pass_name)[source]
exception mimiqcircuits.backends.passes.RemotePassOrderError(backend_name)[source]

Bases: Exception

Raised when an ordered pipeline is submitted to a remote backend that does not advertise the "pass_order_honored" capability while strict_pass_order=True.

Pass strict_pass_order=False to opt into an unordered submission (recognised passes are translated into the server’s flat option set; unrecognised passes are dropped with a warning).

__init__(backend_name)[source]
mimiqcircuits.backends.passes.invert_perm(perm)[source]

Inverse of a 1-based permutation.

mimiqcircuits.backends.passes.apply_passes(pipeline, ctx, circuit)[source]

Run pipeline against circuit.

Returns (transformed_circuit, composed_permutation, per_pass_results). composed_permutation is None when every pass left qubit indices unchanged.

Raises UnacceptedPassError if the backend rejects any pass. When the backend reports delegates_pass(p) is True the pass is not run here: the pipeline records a marker result and the backend handles the pass natively inside its compile or evolve.