qml.transforms.invisible

invisible(fn)[source]

A transform to make a quantum function non-recordable or invisible within a QNode or quantum tape context.

Parameters

fn (function) – A quantum function that applies quantum operations.

Returns

The input function transformed, so that it will not be recorded by QNodes or quantum tapes.

Return type

function

Example

Consider the following quantum function:

>>> def list_of_ops(params, wires):
...     return [
...         qml.RX(params[0], wires=wires),
...         qml.RY(params[1], wires=wires),
...         qml.RZ(params[2], wires=wires)
...     ]

If executed within a QNode or a tape context, these operations will be recorded, even if this is not the intention:

>>> dev = qml.device("default.qubit", wires=2)
>>> @qml.qnode(dev)
... def circuit(params):
...     ops = list_of_ops(params, wires=0)
...     # apply only the last operation from the list
...     ops[-1].queue()
...     return qml.expval(qml.PauliZ(0))
>>> print(qml.draw(circuit)([1, 2, 3]))
 0: ──RX(1)──RY(2)──RZ(3)──┤ ⟨Z⟩

Marking the quantum function as invisible will inhibit any internal quantum operation processing from being recorded by the QNode:

>>> @qml.transforms.invisible
... def list_of_ops(params, wires):
...     return [
...         qml.RX(params[0], wires=wires),
...         qml.RY(params[1], wires=wires),
...         qml.RZ(params[2], wires=wires)
...     ]
>>> @qml.qnode(dev)
... def circuit(params):
...     ops = list_of_ops(params, wires=0)
...     # apply only the last operation from the list
...     ops[-1].queue()
...     return qml.expval(qml.PauliZ(0))
>>> print(qml.draw(circuit)([1, 2, 3]))
 0: ──RZ(3)──┤ ⟨Z⟩