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 groups of neighbouring gates in |
Classes
|
Pass that fuses neighbouring gates into |
- fuse_circuit(circuit, max_support=2, max_diagonal_support=None)[source]¶
Fuse groups of neighbouring gates in
circuitintoGateCustomblocks.Gates are grouped into clusters acting on at most
max_supportqubits, and each cluster becomes oneGateCustomwhose 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_supportlets 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 aGateCustomDiagonalinstead: such a block is2**knumbers rather than4**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 undermax_support.- Parameters:
- Returns:
A new circuit with the same unitary and the same non-unitary operations.
- Return type:
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:
AbstractPassPass that fuses neighbouring gates into
GateCustomblocks.Runs
fuse_circuit()as a step of aPassPipeline. It does not relabel qubits, soPassResult.qubit_permutationisNone.- 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
GateCustomDiagonalof2**kentries rather than4**k. Defaults tomax_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]