qml.qnodes.BaseQNode

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

Bases: pennylane._queuing_context.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 represeting 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().

append_operator(operator)

Append an operator to the global 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.

print_applied()

Prints the most recently applied operations from the QNode.

remove_operator(operator)

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

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

Wrapper for BaseQNode.evaluate().

classmethod append_operator(operator)

Append an operator to the global queue(s).

Parameters

operator (Operator) – The Operator instance 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]

print_applied()[source]

Prints the most recently applied operations from the QNode.

classmethod remove_operator(operator)

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

Parameters

operator (Operator) – The Operator instance to be removed