qml.transforms.create_decomp_expand_fn

create_decomp_expand_fn(custom_decomps, dev, decomp_depth=10)[source]

Creates a custom expansion function for a device that applies a set of specified custom decompositions.

Parameters
  • custom_decomps (Dict[Union(str, qml.operation.Operation), Callable]) – Custom decompositions to be applied by the device at runtime.

  • dev (pennylane.Device) – A quantum device.

  • decomp_depth – The maximum depth of the expansion.

Returns

A custom expansion function that a device can call to expand its tapes within a context manager that applies custom decompositions.

Return type

Callable

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 [
        qml.Hadamard(wires=wires[1]),
        qml.CZ(wires=[wires[0], wires[1]]),
        qml.Hadamard(wires=wires[1])
    ]

We then create the custom function (passing a device, in order to pick up any additional stopping criteria the expansion should have), and then register the result as a custom function of the device:

>>> custom_decomps = {qml.CNOT : custom_cnot}
>>> expand_fn = qml.transforms.create_decomp_expand_fn(custom_decomps, dev)
>>> dev.custom_expand(expand_fn)