class UnwrapTape(tape, params=None)[source]

Bases: object

A context manager that unwraps a single tape with tensor-like parameters to NumPy arrays. In addition, this context manager also correctly infers the trainable parameters of the tape.

  • tape (QuantumTape) – the quantum tape to unwrap

  • params (List[tensor_like or float] or None) – List of parameters. If provided, these parameters will be applied to the tape within the context.


the unwrapped quantum tape

Return type



>>> with tf.GradientTape():
...     with qml.tape.QuantumTape() as tape:
...         qml.RX(tf.Variable(0.1), wires=0)
...         qml.RY(tf.constant(0.2), wires=0)
...         qml.RZ(tf.Variable(0.3), wires=0)
...     with UnwrapTape(tape) as unwrapped_tape:
...         print("Trainable params:", unwrapped_tape.trainable_params)
...         print("Unwrapped params:", unwrapped_tape.get_parameters())
Trainable params: {0, 2}
Unwrapped params: [0.1, 0.3]
>>> print("Original parameters:", tape.get_parameters())
Original parameters: [<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=0.1>,
  <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=0.3>]