mimiqcircuits.fusion

Clustering gate-fusion pass.

Replaces runs of unitary gates spanning at most max_support qubits with a single GateCustom block whose matrix is the ordered product of the run. Non-unitary or opaque operations (measurements, resets, noise channels, Barrier, control flow, and gates with symbolic parameters) are emitted unchanged and act as fusion boundaries: no gate fuses across one on a shared wire.

Runs that are diagonal in the computational basis get their own, usually wider, budget max_diagonal_support and collapse into a GateCustomDiagonal: composing two diagonals is an elementwise product of 2**k entries, so both the block and the work to build it stay linear in the state size rather than quadratic. A cluster keeps that budget only while every member is diagonal; absorbing one dense gate drops it back to max_support.

Clusters grow by absorbing the clusters that own a gate’s wires. Merging is what makes max_support above two useful: on an entangling circuit every wire is owned after the first layer, so a gate that could only ever extend a single cluster would start a fresh one every time.

Merging two clusters is only sound when nothing outside them sits in between, otherwise the contracted DAG gains a cycle and the circuit cannot be reordered. The open flag is what keeps that safe: see seal in fuse_circuit.

Functions

fuse_circuit(circuit[, max_support, ...])

Fuse groups of neighbouring gates in circuit into GateCustom blocks.

Classes

FusePass([max_support, ...])

Pass that fuses neighbouring gates into GateCustom blocks.

fuse_circuit(circuit, max_support=2, max_diagonal_support=None)[source]

Fuse groups of neighbouring gates in circuit into GateCustom blocks.

Gates are grouped into clusters acting on at most max_support qubits, and each cluster becomes one GateCustom whose matrix is the product of its gates. Only gates with numeric parameters are fused. Measurements, resets, noise channels, barriers, control flow and gates with symbolic parameters are copied unchanged, and no gate is fused across one of them on a shared qubit. A cluster of a single gate is left as the original instruction, and qubit indices are never relabeled. Clusters that commute may come out in a different order than their gates had in the circuit.

Raising max_support lets a block cover more wires and so emit fewer, wider blocks. Which gates end up together is decided greedily, so the result is not guaranteed to be the smallest possible circuit.

Runs where every gate is diagonal in the computational basis are budgeted separately, by max_diagonal_support (default: max_support), and collapse into a GateCustomDiagonal instead: such a block is 2**k numbers rather than 4**k, and it is applied without mixing amplitudes, so it is usually worth allowing wider than a dense one. A cluster keeps the diagonal budget only while all of its gates are diagonal — the first dense gate joining it brings it back under max_support.

Parameters:
  • circuit (Circuit) – The circuit to fuse. It is not changed.

  • max_support (int) – The largest number of qubits of a fused block. Defaults to 2.

  • max_diagonal_support (int) – The largest number of qubits of a fused diagonal block. Defaults to max_support.

Returns:

A new circuit with the same unitary and the same non-unitary operations.

Return type:

Circuit

Examples

>>> import mimiqcircuits as mc
>>> c = mc.Circuit()
>>> _ = c.push(mc.GateH(), 0)      # a Hadamard ...
>>> _ = c.push(mc.GateCX(), 0, 1)  # ... feeding a CX on {0, 1}
>>> fused = mc.fuse_circuit(c)      # both act on {0, 1}: one 2-qubit block
>>> len(fused)
1
>>> isinstance(fused[0].operation, mc.GateCustom)
True
>>> d = mc.Circuit()
>>> _ = d.push(mc.GateP(0.1), 0)
>>> _ = d.push(mc.GateCZ(), 0, 1)   # both diagonal, so the block is too
>>> isinstance(mc.fuse_circuit(d)[0].operation, mc.GateCustomDiagonal)
True
class FusePass(max_support=2, max_diagonal_support=None, qubit_threshold=0)[source]

Bases: AbstractPass

Pass that fuses neighbouring gates into GateCustom blocks.

Runs fuse_circuit() as a step of a PassPipeline. It does not relabel qubits, so PassResult.qubit_permutation is None.

Parameters:
  • max_support (int) – The largest number of qubits of a fused block. Defaults to 2.

  • max_diagonal_support (int) – The largest number of qubits of a block made only of gates diagonal in the computational basis, which fuses into a GateCustomDiagonal of 2**k entries rather than 4**k. Defaults to max_support; raise it for backends that apply a diagonal without mixing amplitudes.

  • qubit_threshold (int) – Fuse only circuits with at least this many qubits; smaller ones pass through unchanged, since fusing pays off only when each gate application on the state vector is costly. Defaults to 0, which always fuses.

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits.backends.passes import PassContext
>>> c = Circuit().push(GateH(), 0).push(GateCX(), 0, 1)
>>> fused, result = FusePass().apply(PassContext(), c)
>>> # the H and the CX become one 2-qubit block
>>> fused
2-qubit circuit with 1 instruction:
└── Custom(...) @ q[0:1]
spec()[source]

Return a PassSpec describing the pass.

Its name is how the preparation flags and MimiqRemoteBackend identify the pass.

apply(ctx, circuit)[source]

Run the pass on circuit.

Parameters:
  • ctx – The PassContext of the run.

  • circuit – The input circuit. Return a new circuit rather than changing it in place.

Returns:

A tuple (new_circuit, PassResult). A pass that relabels qubits reports it in PassResult.qubit_permutation.