qp.transforms.undo_swaps¶
- undo_swaps(tape)[source]¶
Quantum function transform to remove SWAP gates by running from right to left through the circuit changing the position of the qubits accordingly.
- Parameters:
tape (QNode or QuantumTape or Callable) – A quantum circuit.
- Returns:
The transformed circuit as described in
qp.transform.- Return type:
qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]
Example
>>> dev = qp.device('default.qubit', wires=3)
You can apply the transform directly on a
QNode@undo_swaps @qp.qnode(device=dev) def circuit(): qp.Hadamard(wires=0) qp.X(1) qp.SWAP(wires=[0,1]) qp.SWAP(wires=[0,2]) qp.Y(0) return qp.expval(qp.Z(0))
The SWAP gates are removed before execution.
Usage Details
Consider the following quantum function:
def qfunc(): qp.Hadamard(wires=0) qp.X(1) qp.SWAP(wires=[0,1]) qp.SWAP(wires=[0,2]) qp.Y(0) return qp.expval(qp.Z(0))
The circuit before optimization:
>>> dev = qp.device('default.qubit', wires=3) >>> qnode = qp.QNode(qfunc, dev) >>> print(qp.draw(qnode)()) 0: ──H─╭SWAP─╭SWAP──Y─┤ <Z> 1: ──X─╰SWAP─│────────┤ 2: ──────────╰SWAP────┤
We can remove the SWAP gates by running the
undo_swaptransform:>>> optimized_qfunc = undo_swaps(qfunc) >>> optimized_qnode = qp.QNode(optimized_qfunc, dev) >>> print(qp.draw(optimized_qnode)()) 0: ──Y─┤ <Z> 1: ──H─┤ 2: ──X─┤
code/api/pennylane.transforms.undo_swaps
Download Python script
Download Notebook
View on GitHub