qml.workflow.construct_tape

construct_tape(qnode, level='user')[source]

Constructs the tape for a designated stage in the transform program.

Parameters
  • qnode (QNode) – the qnode we want to get the tapes and post-processing for.

  • level (None, str, int, slice) –

    Specifies which stage of the QNode’s transform program to use for tape construction.

    • None or "device": Uses the entire transformation pipeline.

    • "top": Ignores transformations and returns the original tape as defined.

    • "user": Includes transformations that are manually applied by the user.

    • "gradient": Extracts the gradient-level tape.

    • int: Can also accept an integer, corresponding to a number of transforms in the program.

    • slice: Can also accept a slice object to select an arbitrary subset of the transform program.

Returns

a quantum circuit.

Return type

tape (QuantumScript)

Raises

ValueError – if the level argument corresponds to more than one tape.

See also

pennylane.workflow.get_transform_program() to inspect the contents of the transform program for a specified level.

Example

@qml.qnode(qml.device("default.qubit", shots=10))
def circuit(x):
    qml.RandomLayers(qml.numpy.array([[1.0, 2.0]]), wires=(0,1))
    qml.RX(x, wires=0)
    qml.RX(-x, wires=0)
    qml.SWAP((0,1))
    qml.X(0)
    qml.X(0)
    return qml.expval(qml.X(0) + qml.Y(0))
>>> tape = qml.workflow.construct_tape(circuit)(0.5)
>>> tape.circuit
[RandomLayers(tensor([[1., 2.]], requires_grad=True), wires=[0, 1]),
RX(0.5, wires=[0]),
RX(-0.5, wires=[0]),
SWAP(wires=[0, 1]),
X(0),
X(0),
expval(X(0) + Y(0))]