qml.transforms.match_relative_phase_toffoli

match_relative_phase_toffoli(tape)[source]

Quantum transform to replace 4-qubit relative phase Toffoli gates, given in Maslov, Dmitri. “On the Advantages of Using Relative Phase Toffolis with an Application to Multiple Control Toffoli Optimization”, arXiv:1508.03273, arXiv, 2016. doi:10.48550/arXiv.1508.03273.

Note

Will also replace any subcircuits from the full pattern (composed of the 4-qubit relative phase Toffoli and its decomposition) that can replaced by the rest of the pattern.

Parameters:

tape (QNode or QuantumScript or Callable) – A quantum circuit.

Returns:

The transformed circuit as described in qml.transform.

Return type:

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

Example

def qfunc():
    qml.CCZ(wires=[0, 1, 3])
    qml.ctrl(qml.S(wires=[1]), control=[0])
    qml.ctrl(qml.S(wires=[2]), control=[0, 1])
    qml.MultiControlledX(wires=[0, 1, 2, 3])
    return qml.expval(qml.Z(0))

The circuit (a 4-qubit relative phase Toffoli) before decomposition:

>>> dev = qml.device('default.qubit', wires=4)
>>> qnode = qml.QNode(qfunc, dev)
>>> lowered_qnode = match_relative_phase_toffoli(qnode)
>>> print(qml.draw(lowered_qnode, level=0)())
    0: ─╭●─╭●─╭●─╭●─┤  <Z>
    1: ─├●─╰S─├●─├●─┤
    2: ─│─────╰S─├●─┤
    3: ─╰Z───────╰X─┤

We can replace the relative phase 4-qubit Toffoli by running the transform:

>>> print(qml.draw(lowered_qnode, level=1)())
    0: ─────────────────╭●───────────╭●───────────────────────────┤  <Z>
    1: ─────────────────│─────╭●─────│─────╭●─────────────────────┤
    2: ───────╭●────────│─────│──────│─────│────────────╭●────────┤
    3: ──H──T─╰X──T†──H─╰X──T─╰X──T†─╰X──T─╰X──T†──H──T─╰X──T†──H─┤

Consider the following quantum function:

@match_relative_phase_toffoli
@qml.qnode(device=dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.Hadamard(wires=1)
    qml.Barrier(wires=[0,1])
    qml.X(0)

    # begin relative phase 4-qubit Toffoli

    qml.CCZ(wires=[0, 1, 3])
    qml.ctrl(qml.S(wires=[1]), control=[0])
    qml.ctrl(qml.S(wires=[2]), control=[0, 1])
    qml.MultiControlledX(wires=[0, 1, 2, 3])

    # end relative phase 4-qubit Toffoli

    qml.Hadamard(wires=1)
    qml.Barrier(wires=[0,1])
    qml.X(0)
    return qml.expval(qml.Z(0))

The relative phase 4-qubit Toffoli is then replaced before execution.