qml.transforms.single_qubit_fusion

single_qubit_fusion = <function single_qubit_fusion>[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
  • qfunc (function) – A quantum function.

  • 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 quantum function

Return type

function

Example

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.PauliX(0))

The circuit before optimization:

>>> dev = qml.device('default.qubit', wires=1)
>>> 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⟩