mimiqcircuits.backends.compiled

Compiled-circuit types and the metadata they carry.

LocalBackend.compile() turns a Circuit into a backend-specific CompiledCircuit. The compile step must be pure: no randomness, no sampling. Per-trajectory random work belongs in LocalBackend.prepare_trajectory().

Every compiled circuit carries a CompileMetadata, read through its metadata attribute, for compile-time information such as measurement analysis and qubit permutations.

Concrete types:

  • DefaultCompiledCircuit wraps an artifact as it is, for backends whose compile step returns the final form directly.

  • CompiledParametricCircuit wraps a circuit that still has free symbolic parameters; LocalBackend.bind() resolves them.

Classes

CompileMetadata([measures, active_qubits, ...])

Compile-time information carried by a CompiledCircuit.

CompiledCircuit()

Base class for the backend-specific form of a circuit.

CompiledParametricCircuit(_source[, _metadata])

Compiled circuit that still has free symbolic parameters.

DefaultCompiledCircuit(_source[, _metadata])

Compiled circuit that wraps an artifact as it is.

Exceptions

UnboundSymbolicError(backend_name)

Error for a circuit with free symbolic parameters on a backend that does not declare the "parametric" capability.

class CompileMetadata(measures=<factory>, active_qubits=<factory>, qubit_flips=<factory>, initial_layout=None, final_layout=None, measure_action_counts=<factory>, prefix_endpoint=0)[source]

Bases: object

Compile-time information carried by a CompiledCircuit.

Backends fill in the fields that apply to them and leave the others at their defaults. The execute driver of LocalBackend does not read them.

measures

Measurement-analysis entries, one per classical bit, classifying it as unused, read directly, read flipped, or constant 0 or 1. Empty when no analysis ran.

Type:

list

active_qubits

Qubits that must be simulated; the others are known to be in |0⟩ when measured.

Type:

list[int]

qubit_flips

Per qubit, whether a trailing X gate was absorbed into the read-out.

Type:

list[bool]

initial_layout

Where the pass pipeline put each qubit of the input circuit before the first instruction, 0-based, or None when the qubits keep their indices. See PassResult.

Type:

list[int] or None

final_layout

The same after the last instruction.

Type:

list[int] or None

measure_action_counts

Number of bits in each measurement-analysis class, for debugging.

Type:

dict[str, int]

prefix_endpoint

Index of the last instruction in the deterministic prefix, when the backend splits the circuit into a deterministic prefix and a random suffix; 0 otherwise.

Type:

int

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits.backends import CompileMetadata
>>> meta = CompileMetadata(active_qubits=[0, 1])
>>> meta.active_qubits, meta.final_layout
([0, 1], None)
class CompiledCircuit[source]

Bases: object

Base class for the backend-specific form of a circuit.

Returned by LocalBackend.compile() and run by LocalBackend.evolve(). Backends subclass it to carry whatever they need: an MPO for an MPS simulator, a plain Circuit for a state-vector simulator, and so on. Subclasses implement metadata and source; DefaultCompiledCircuit does both and fits most backends.

property metadata: CompileMetadata

The CompileMetadata of this compiled circuit.

property source

The backend-specific artifact wrapped by this compiled circuit (for example a Circuit or an MPO).

class DefaultCompiledCircuit(_source, _metadata=<factory>)[source]

Bases: CompiledCircuit

Compiled circuit that wraps an artifact as it is.

Use it when the backend’s compile step has nothing to lower, for example a state-vector simulator that runs the input Circuit directly.

Parameters:

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits.backends import DefaultCompiledCircuit
>>> c = Circuit().push(GateH(), 0)
>>> compiled = DefaultCompiledCircuit(c)
>>> compiled.source is c
True
>>> compiled.metadata.active_qubits
[]
property metadata: CompileMetadata

The CompileMetadata given at construction.

property source

The wrapped artifact.

class CompiledParametricCircuit(_source, _metadata=<factory>)[source]

Bases: CompiledCircuit

Compiled circuit that still has free symbolic parameters.

A backend that declares the "parametric" capability can return it from LocalBackend.compile() for a symbolic circuit. LocalBackend.bind() resolves the parameters.

Parameters:
  • _source (Any) – The symbolic Circuit, kept so that LocalBackend.bind() can evaluate it.

  • _metadata (CompileMetadata) – The CompileMetadata. Defaults to an empty one.

property metadata: CompileMetadata

The CompileMetadata given at construction.

property source

The symbolic circuit.

exception UnboundSymbolicError(backend_name)[source]

Bases: Exception

Error for a circuit with free symbolic parameters on a backend that does not declare the "parametric" capability.

Backends raise it from LocalBackend.compile().

Parameters:

backend_name (str) – Name of the backend, kept as backend_name.