qml.qnodes.BaseQNode

class BaseQNode(func, device, *, mutable=True, **kwargs)[source]

Bases: pennylane._queuing.QueuingContext

Base class for quantum nodes in the hybrid computational graph.

A quantum node encapsulates a quantum function (corresponding to a variational circuit) and the computational device it is executed on.

The QNode calls the quantum function to construct a CircuitGraph instance representing the quantum circuit. The circuit can be either

  • mutable, which means the quantum function is called each time the QNode is evaluated, or

  • immutable, which means the quantum function is called only once, on first evaluation, to construct the circuit representation.

If the circuit is mutable, its auxiliary parameters can undergo any kind of classical processing inside the quantum function. It can also contain classical flow control structures that depend on the auxiliary parameters, potentially resulting in a different circuit on each call. The auxiliary parameters may also determine the wires on which operators act.

For immutable circuits the quantum function must build the same circuit graph consisting of the same Operator instances regardless of its parameters; they can only appear as the arguments of the Operators in the circuit. Immutable circuits are slightly faster to execute, and can be optimized, but require that the layout of the circuit is fixed.

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

  • mutable (bool) – whether the circuit is mutable, see above

Keyword Arguments
  • vis_check (bool) – whether to check for operations that cannot affect the output

  • par_check (bool) – whether to check for unused positional params

__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.

unwrap_tensor_kwargs(kwargs)

Unwraps the pennylane.numpy.tensor objects that were passed as keyword arguments so that they can be handled as gate parameters by arbitrary devices.

update_info(obj, **kwargs)

Updates information of an object in the active queue.

__call__(*args, **kwargs)[source]

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)[source]

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)[source]

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)[source]

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()[source]

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()[source]

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)[source]

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.

static unwrap_tensor_kwargs(kwargs)[source]

Unwraps the pennylane.numpy.tensor objects that were passed as keyword arguments so that they can be handled as gate parameters by arbitrary devices.

Parameters

kwargs (dict[str, Any]) – Auxiliary arguments passed to the quantum function.

Returns

Auxiliary arguments passed to the quantum function in an unwrapped form (if applicable).

Return type

dict[str, Any]

classmethod update_info(obj, **kwargs)

Updates information of an object in the active queue.