qml.transforms.single_qubit_fusion

single_qubit_fusion(tape, atol=1e-08, exclude_gates=None)[source]

Quantum function transform to fuse together groups of single-qubit operations into a general single-qubit unitary operation (Rot).

Fusion is performed only between gates that implement the property single_qubit_rot_angles. Any sequence of two or more single-qubit gates (on the same qubit) with that property defined will be fused into one Rot.

Parameters
  • tape (QNode or QuantumTape or Callable) – A quantum circuit.

  • atol (float) – An absolute tolerance for which to apply a rotation after fusion. After fusion of gates, if the fused angles \(\theta\) are such that \(|\theta|\leq \text{atol}\), no rotation gate will be applied.

  • exclude_gates (None or list[str]) – A list of gates that should be excluded from full fusion. If set to None, all single-qubit gates that can be fused will be fused.

Returns

The transformed circuit as described in qml.transform.

Return type

qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]

Example

>>> dev = qml.device('default.qubit', wires=1)

You can apply the transform directly on QNode:

@single_qubit_fusion
@qml.qnode(device=dev)
def qfunc(r1, r2):
    qml.Hadamard(wires=0)
    qml.Rot(*r1, wires=0)
    qml.Rot(*r2, wires=0)
    qml.RZ(r1[0], wires=0)
    qml.RZ(r2[0], wires=0)
    return qml.expval(qml.X(0))

The single qubit gates are fused before execution.

Consider the following quantum function.

def qfunc(r1, r2):
    qml.Hadamard(wires=0)
    qml.Rot(*r1, wires=0)
    qml.Rot(*r2, wires=0)
    qml.RZ(r1[0], wires=0)
    qml.RZ(r2[0], wires=0)
    return qml.expval(qml.X(0))

The circuit before optimization:

>>> qnode = qml.QNode(qfunc, dev)
>>> print(qml.draw(qnode)([0.1, 0.2, 0.3], [0.4, 0.5, 0.6]))
0: ──H──Rot(0.1, 0.2, 0.3)──Rot(0.4, 0.5, 0.6)──RZ(0.1)──RZ(0.4)──┤ ⟨X⟩

Full single-qubit gate fusion allows us to collapse this entire sequence into a single qml.Rot rotation gate.

>>> optimized_qfunc = single_qubit_fusion(qfunc)
>>> optimized_qnode = qml.QNode(optimized_qfunc, dev)
>>> print(qml.draw(optimized_qnode)([0.1, 0.2, 0.3], [0.4, 0.5, 0.6]))
0: ──Rot(3.57, 2.09, 2.05)──┤ ⟨X⟩