qml.single_tape_transform

class single_tape_transform(transform_fn)[source]

Bases: object

For registering a tape transform that takes a tape and outputs a single new tape.

Warning

Use of single_tape_transform to create a custom transform is deprecated. Instead switch to using the new transform() function. Follow the instructions here for further details

Examples of such transforms include circuit compilation.

Parameters

transform_fn (function) – The function to register as the single tape transform. It can have an arbitrary number of arguments, but the first argument must be the input tape.

Example

A valid single tape transform is a quantum function that satisfies the following:

  • The first argument must be an input tape

  • Depending on the structure of this input tape, various quantum operations, functions, and templates may be called.

  • Any internal classical processing should use the qml.math module to ensure the transform is differentiable.

  • There is no return statement.

For example:

@qml.single_tape_transform
def my_transform(tape, x, y):
    # loop through all operations on the input tape
    for op in tape:
        if op.name == "CRX":
            wires = op.wires
            param = op.parameters[0]

            qml.RX(x * qml.math.abs(param), wires=wires[1])
            qml.RY(y * qml.math.abs(param), wires=wires[1])
            qml.CZ(wires=[wires[1], wires[0]])
        else:
            op.queue()

This transform iterates through the input tape, and replaces any CRX operation with two single qubit rotations and a CZ operation. These newly queued operations will form the output transformed tape.

We can apply this transform to a quantum tape:

>>> with qml.tape.QuantumTape() as tape:
...     qml.Hadamard(wires=0)
...     qml.CRX(-0.5, wires=[0, 1])
>>> new_tape = my_transform(tape, 1., 2.)
>>> print(qml.drawer.tape_text(new_tape, decimals=1))
0: ──H────────────────╭Z─┤
1: ──RX(0.5)──RY(1.0)─╰●─┤