qml.qnodes.PassthruQNode

class PassthruQNode(func, device, **kwargs)[source]

Bases: pennylane.qnodes.base.BaseQNode

Differentiable quantum node that appears as a white box to an external autodiff framework.

In PennyLane, the QNode classes work as black box functions with respect to any autodiff (AD) framework (such as TensorFlow or PyTorch). This means that the QNode converts all its inputs (which may come in data types specific to the AD framework used, which we denote ADT here) into plain Python/NumPy types, computes the required quantum function value or Jacobian, and converts the result back into the ADT if necessary.

In contrast, PassthruQNode works as a white box: it preserves the ADT throughout the computation. This requires that the quantum function is computed using a simulator device that is compatible with the AD framework used (typically implemented using that same framework), and returns the result as the ADT instead of plain Python/NumPy types.

The advantages of this approach are that the qfunc can be differentiated using its AD framework without requiring a separate method for computing the Jacobian, and that the internals of the simulation are visible in the computational graph.

Parameters
  • func (callable) – The quantum function of the QNode. A Python function containing Operation constructor calls, and returning a tuple of measured Observable instances.

  • device (Device) – computational device to execute the function on

Keyword Arguments

use_native_type (bool) – If True, return the result in whatever type the device uses internally, otherwise convert it into array[float]. Default: True.

__call__(*args, **kwargs)

Wrapper for BaseQNode.evaluate().

active_context()

Returns the currently active queuing context.

append(obj, **kwargs)

Append an object to the queue(s).

draw([charset, show_variable_names])

Draw the QNode as a circuit diagram.

evaluate(args, kwargs)

Evaluate the quantum function on the specified device.

evaluate_obs(obs, args, kwargs)

Evaluate the value of the given observables.

get_info(obj)

Returns information of an object in the queue.

get_trainable_args()

Returns the indices of quantum function positional arguments that support differentiability.

print_applied()

Prints the most recently applied operations from the QNode.

remove(obj)

Remove an object from the queue(s) if it is in the queue(s).

set_trainable_args(arg_indices)

Store the indices of quantum function positional arguments that support differentiability.

update_info(obj, **kwargs)

Updates information of an object in the active queue.

__call__(*args, **kwargs)

Wrapper for BaseQNode.evaluate().

classmethod active_context()

Returns the currently active queuing context.

classmethod append(obj, **kwargs)

Append an object to the queue(s).

Parameters

obj – the object to be appended

draw(charset='unicode', show_variable_names=False)

Draw the QNode as a circuit diagram.

Consider the following circuit as an example:

@qml.qnode(dev)
def qfunc(a, w):
    qml.Hadamard(0)
    qml.CRX(a, wires=[0, 1])
    qml.Rot(w[0], w[1], w[2], wires=[1])
    qml.CRX(-a, wires=[0, 1])

    return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))

We can draw the circuit after it has been executed:

>>> result = qfunc(2.3, [1.2, 3.2, 0.7])
>>> print(qfunc.draw())
0: ──H──╭C────────────────────────────╭C─────────╭┤ ⟨Z ⊗ Z⟩
1: ─────╰RX(2.3)──Rot(1.2, 3.2, 0.7)──╰RX(-2.3)──╰┤ ⟨Z ⊗ Z⟩
>>> print(qfunc.draw(charset="ascii"))
0: --H--+C----------------------------+C---------+| <Z @ Z>
1: -----+RX(2.3)--Rot(1.2, 3.2, 0.7)--+RX(-2.3)--+| <Z @ Z>
>>> print(qfunc.draw(show_variable_names=True))
0: ──H──╭C─────────────────────────────╭C─────────╭┤ ⟨Z ⊗ Z⟩
1: ─────╰RX(a)──Rot(w[0], w[1], w[2])──╰RX(-1*a)──╰┤ ⟨Z ⊗ Z⟩
Parameters
  • charset (str, optional) – The charset that should be used. Currently, “unicode” and “ascii” are supported.

  • show_variable_names (bool, optional) – Show variable names instead of values.

Raises
  • ValueError – If the given charset is not supported

  • pennylane.QuantumFunctionError – Drawing is impossible because the underlying CircuitGraph has not yet been constructed

Returns

The circuit representation of the QNode

Return type

str

evaluate(args, kwargs)

Evaluate the quantum function on the specified device.

Parameters
  • args (tuple[Any]) – positional arguments to the quantum function (differentiable)

  • kwargs (dict[str, Any]) – auxiliary arguments (not differentiable)

Keyword Arguments

use_native_type (bool) – If True, return the result in whatever type the device uses internally, otherwise convert it into array[float]. Default: False.

Returns

output measured value(s)

Return type

float or array[float]

evaluate_obs(obs, args, kwargs)

Evaluate the value of the given observables.

Assumes construct() has already been called.

Parameters
  • obs (Iterable[Observable]) – observables to measure

  • args (tuple[Any]) – positional arguments to the quantum function (differentiable)

  • kwargs (dict[str, Any]) – auxiliary arguments (not differentiable)

Returns

measured values

Return type

array[float]

classmethod get_info(obj)

Returns information of an object in the queue.

get_trainable_args()

Returns the indices of quantum function positional arguments that support differentiability.

Returns

Differentiable positional argument indices. A

value of None means that all argument indices are differentiable.

Return type

None or Set[int]

print_applied()

Prints the most recently applied operations from the QNode.

classmethod remove(obj)

Remove an object from the queue(s) if it is in the queue(s).

Parameters

obj – the object to be removed

set_trainable_args(arg_indices)

Store the indices of quantum function positional arguments that support differentiability.

Parameters

args (None or Set[int]) – Differentiable positional argument indices. A value of None means that all argument indices are differentiable.

classmethod update_info(obj, **kwargs)

Updates information of an object in the active queue.