qp.transforms.set_decomposition¶
- set_decomposition(custom_decomps, dev)[source]¶
Context manager for setting custom decompositions.
- Parameters:
custom_decomps (Dict[Union(str, qp.operation.Operation), Callable]) – Custom decompositions to be applied by the device at runtime.
dev (pennylane.devices.LegacyDevice) – A quantum device.
Example
Suppose we would like a custom expansion function that decomposes all CNOTs into CZs. We first define a decomposition function:
def custom_cnot(wires, **_): return [ qp.Hadamard(wires=wires[1]), qp.CZ(wires=[wires[0], wires[1]]), qp.Hadamard(wires=wires[1]) ]
This context manager can be used to temporarily change a devices expansion function to one that takes into account the custom decompositions.
dev = qp.device("default.qubit", wires=2) @qp.qnode(dev) def circuit(): qp.CNOT(wires=[0, 1]) return qp.expval(qp.Z(0))
>>> print(qp.draw(circuit, level="device")()) 0: ─╭●─┤ <Z> 1: ─╰X─┤
Now let’s set up a context where the custom decomposition will be applied. To see our change, the circuit is drawn at the device level where the custom decomposition will be applied.
>>> with qp.transforms.set_decomposition({qp.CNOT : custom_cnot}, dev): ... print(qp.draw(circuit, level="device")()) 0: ────╭●────┤ <Z> 1: ──H─╰Z──H─┤
code/api/pennylane.transforms.set_decomposition
Download Python script
Download Notebook
View on GitHub