diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index 0c36ec3eb1..84113d1e7a 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -87,12 +87,21 @@ circuit as groups of gates have been fused to a single is equivalent to simulating the original one but in most cases more efficient since less gates need to be applied to the state vector. -All one-qubit gates acting on the same qubit in a row are fused together. -Once a two-qubit gate acting on a specific pair ``(q0, q1)`` is found then it is -fused together with the existing fused one-qubit gates on ``q0`` and ``q1``. -If a new two-qubit gate is found which acts on either ``q0`` or ``q1`` but not -both then the fusion round for the pair ``(q0, q1)`` stops and a new fusion -round starts for the target qubits of the new two-qubit gate. +The fusion algorithm works as follows: First all gates in the circuit are +transformed to unmarked :class:`qibo.abstractions.gates.FusedGate`. The gates +are then processed in the order they were added in the circuit. For each gate +we identify the neighbors forth and back in time and attempt to fuse them to +the gate. Two gates can be fused if their total number of target qubits is +smaller than the fusion maximum qubits (specified by the user) and there are +no other gates between acting on the same target qubits. Gates that are fused +to others are marked. The new circuit queue contains the gates that remain +unmarked after the above operations finish. + +Gates are processed in the original order given by user. There are no +additional simplifications performed such as commuting gates acting on the same +qubit or canceling gates even when such simplifications are mathematically possible. +The user can specify the maximum number of qubits in a fused gate using +the ``max_qubits`` flag in :meth:`qibo.core.circuit.Circuit.fuse`. For example the following: @@ -134,16 +143,6 @@ and the second will act to ``(1, 2)`` corresponding to [Y(1), Z(2), CNOT(1, 2), H(1), H(2)] -Currently the maximum number of qubits in a fused gate is two. If a gate acting -on more qubits is found it is applied as it is without participating in the -fusion and new fusion groups are created for the gates that follow and act on -its target qubits. - -The fusion algorithm fuses gates in the original order given by user. There -are no additional simplifications performed such as commuting gates acting -on the same qubit or canceling gates even when such simplifications are -mathematically possible. - Density matrix circuit """""""""""""""""""""" diff --git a/src/qibo/abstractions/gates.py b/src/qibo/abstractions/gates.py index 8cf27da79b..9bd399ceeb 100644 --- a/src/qibo/abstractions/gates.py +++ b/src/qibo/abstractions/gates.py @@ -1665,9 +1665,6 @@ class FusedGate(Gate): This gate is constructed automatically by :meth:`qibo.core.circuit.Circuit.fuse` and should not be used by user. - :class:`qibo.abstractions.gates.FusedGate` works with arbitrary number of - target qubits however the backend implementation - :class:`qibo.core.gates.FusedGate` assumes two target qubits. """ def __init__(self, *q): diff --git a/src/qibo/core/circuit.py b/src/qibo/core/circuit.py index 9097e43b02..a7527ab3d6 100644 --- a/src/qibo/core/circuit.py +++ b/src/qibo/core/circuit.py @@ -47,7 +47,7 @@ def _add_layer(self, gate): self.queue.append(gate.additional_unitary) def fuse(self, max_qubits=2): - """Creates an equivalent circuit with the gates fused up to two-qubits. + """Creates an equivalent circuit by fusing gates for increased simulation performance. Args: max_qubits (int): Maximum number of qubits in the fused gates.