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
|
Run |
|
Validate the circuit-preparation flags and return them normalised, as a dict keyed by flag name. |
|
Inverse of a 1-based permutation. |
|
Circuit preparation, resolved: the backend's |
Coerce a common Python value into a |
Classes
Base class for circuit-transformation passes. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Pass parameter that names one of a fixed set of choices, like an |
|
Inputs a pass needs but should not fetch from global state. |
Base class for the JSON-safe sum used to serialise pass parameters. |
|
|
An ordered, iterable sequence of passes. |
|
Side effects returned from |
|
Declarative summary of a pass. |
Exceptions
|
Raised when an ordered pipeline is submitted to a remote backend that does not advertise the |
|
Raised when a pipeline contains a pass the backend rejects. |
- class mimiqcircuits.backends.passes.PassParam[source]¶
Bases:
objectBase 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"(aPStr) must not silently degrade and come back as aPSym. Equality is tag-sensitive:PSym("x") != PStr("x").
- class mimiqcircuits.backends.passes.PFloat(value: 'float')[source]¶
Bases:
PassParam- __init__(value)¶
- class mimiqcircuits.backends.passes.PSym(value)[source]¶
Bases:
PassParamPass parameter that names one of a fixed set of choices, like an
enum.Enummember. Distinct fromPStrat the tag level.- __init__(value)¶
- class mimiqcircuits.backends.passes.PList(items: 'tuple[PassParam, ...]')[source]¶
Bases:
PassParam- __init__(items)¶
- class mimiqcircuits.backends.passes.PDict(items: 'tuple[tuple[str, PassParam], ...]')[source]¶
Bases:
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:
boolis a subclass ofintin Python, soisinstance(True, int)isTrueand reversing the order would tag every boolean asPInt.
- class mimiqcircuits.backends.passes.PassSpec(name, parameters=(), requires=(), conflicts=())[source]¶
Bases:
objectDeclarative 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.
requiresnames other passes that must run before this one;conflictsnames passes that cannot coexist with this one in the same pipeline. Both are advisory.- __init__(name, parameters=(), requires=(), conflicts=())¶
- class mimiqcircuits.backends.passes.PassResult(qubit_permutation=None, metadata=<factory>)[source]¶
Bases:
objectSide effects returned from
AbstractPass.apply().qubit_permutationisNonewhen 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.metadatais free-form pass-specific information (timings, gate counts, …) surfaced for observability.- __init__(qubit_permutation=None, metadata=<factory>)¶
- class mimiqcircuits.backends.passes.PassContext(backend=None, rng=<factory>, bitstrings=<factory>, features=<factory>)[source]¶
Bases:
objectInputs a pass needs but should not fetch from global state.
backendis the Backend the pipeline is compiling for, orNonefor backend-agnostic runs.rngis the dedicated pass-internal RNG.bitstringsare user-supplied amplitude targets in the original qubit space; the pipeline rewrites them into post-pass space as permutations compose.featuresare detected on the input circuit and filter passes viaAbstractPass.preserves().- __init__(backend=None, rng=<factory>, bitstrings=<factory>, features=<factory>)¶
- class mimiqcircuits.backends.passes.AbstractPass[source]¶
Bases:
ABCBase class for circuit-transformation passes.
Subclasses implement:
spec()— return aPassSpecdescribing 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 returnsTruefor 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".
- class mimiqcircuits.backends.passes.PassPipeline(passes=<factory>)[source]¶
Bases:
objectAn ordered, iterable sequence of passes.
Run via
apply_passes(), which composes each pass’squbit_permutationinto a single permutation. Callers use the inverse of that permutation to map samples andAmplitude/ExpectationValueresults 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,simplifyandreorderqubitsmust beNoneor a bool (NumPy bools becomebool);reorderqubitsmay also be a method name.fuse_thresholdandreorderqubits_seedmust beNoneor an integer other than a bool, and becomeint. Anything else raisesTypeError: a value likefuse=0would 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 ownpasses.Each flag speaks about one kind of pass. Set to
Falseit 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.passesare appended at the end, as given. Invalid flag values raiseTypeError; seecheck_prep_flags().
- exception mimiqcircuits.backends.passes.UnacceptedPassError(backend_name, pass_name)[source]¶
Bases:
ExceptionRaised 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.
- exception mimiqcircuits.backends.passes.RemotePassOrderError(backend_name)[source]¶
Bases:
ExceptionRaised when an ordered pipeline is submitted to a remote backend that does not advertise the
"pass_order_honored"capability whilestrict_pass_order=True.Pass
strict_pass_order=Falseto opt into an unordered submission (recognised passes are translated into the server’s flat option set; unrecognised passes are dropped with a warning).
- mimiqcircuits.backends.passes.apply_passes(pipeline, ctx, circuit)[source]¶
Run
pipelineagainstcircuit.Returns
(transformed_circuit, composed_permutation, per_pass_results).composed_permutationisNonewhen every pass left qubit indices unchanged.Raises
UnacceptedPassErrorif the backend rejects any pass. When the backend reportsdelegates_pass(p) is Truethe pass is not run here: the pipeline records a marker result and the backend handles the pass natively inside itscompileorevolve.