qml.transforms.match_relative_phase_toffoli¶
- match_relative_phase_toffoli(tape)[source]¶
Replace 4-qubit relative phase Toffoli gates with their equivalent circuit.
The equivalence is 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
This transform will also replace any subcircuits from the full pattern (composed of the 4-qubit relative phase Toffoli and its decomposition) that can be 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
import pennylane as qml @qml.transforms.match_relative_phase_toffoli @qml.qnode(qml.device("default.qubit")) def circuit(): qml.Hadamard(wires=0) qml.Hadamard(wires=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.X(0) return qml.expval(qml.Z(0))
The circuit (containing a 4-qubit relative phase Toffoli) before decomposition:
>>> print(qml.draw(circuit, level=0)()) 0: ──H──X─╭●─╭●─╭●─╭●──X─┤ <Z> 1: ──H────├●─╰S─├●─├●──H─┤ 2: ───────│─────╰S─├●────┤ 3: ───────╰Z───────╰X────┤
The 4-qubit relative phase Toffoli pattern is replaced after the transform:
>>> print(qml.draw(circuit, level=1)()) 0: ──H──X───────────╭●───────────╭●──X────────────────────────┤ <Z> 1: ──H──────────────│─────╭●─────│─────╭●──H──────────────────┤ 2: ───────╭●────────│─────│──────│─────│────────────╭●────────┤ 3: ──H──T─╰X──T†──H─╰X──T─╰X──T†─╰X──T─╰X──T†──H──T─╰X──T†──H─┤