# qml.QNode¶

class QNode(func, device, cache=False)[source]

Bases: object

Quantum node in the hybrid computational graph.

Parameters: func (callable) – a Python function containing Operation constructor calls, returning a tuple of measured Observable instances. device (Device) – device to execute the function on cache (bool) – If True, the quantum function used to generate the QNode will only be called to construct the quantum circuit once, on first execution, and this circuit structure (i.e., the placement of templates, gates, measurements, etc.) will be cached for all further executions. The circuit parameters can still change with every call. Only activate this feature if your quantum circuit structure will never change.
 interface String representing the QNode interface
interface

String representing the QNode interface

 __call__(*args, **kwargs) Wrapper for evaluate(). construct(args[, kwargs]) Constructs a representation of the quantum circuit. construct_metric_tensor(args, **kwargs) Create metric tensor subcircuits for each parameter. evaluate(**kwargs) Evaluates the quantum function on the specified device. evaluate_obs(obs, args, **kwargs) Evaluate the value of the given observables. jacobian(params[, which, method, h, order]) Compute the Jacobian of the QNode. metric_tensor(*args, **kwargs) Evaluate the value of the metric tensor. print_applied() Prints the most recently applied operations from the QNode. to_tf() Convert the standard PennyLane QNode into a TFQNode(). to_torch() Convert the standard PennyLane QNode into a TorchQNode().
__call__(*args, **kwargs)[source]

Wrapper for evaluate().

construct(args, kwargs=None)[source]

Constructs a representation of the quantum circuit.

The user should never have to call this method.

This method is called automatically the first time QNode.evaluate() or QNode.jacobian() is called. It executes the quantum function, stores the resulting sequence of Operation instances, and creates the variable mapping.

Parameters: args (tuple) – Represent the free parameters passed to the circuit. Here we are not concerned with their values, but with their structure. Each free param is replaced with a Variable instance. kwargs (dict) – Additional keyword arguments may be passed to the quantum circuit function, however PennyLane does not support differentiating with respect to keyword arguments. Instead, keyword arguments are useful for providing data or ‘placeholders’ to the quantum circuit function. QuantumFunctionError – if the pennylane.QNode’s _current_context is attempted to be modified inside of this method, the quantum function returns incorrect values or if both continuous and discrete operations are specified in the same quantum circuit
construct_metric_tensor(args, **kwargs)[source]

Create metric tensor subcircuits for each parameter.

If the parameter appears in a gate $$G$$, the subcircuit contains all gates which precede $$G$$, and $$G$$ is replaced by the variance value of its generator.

Parameters: Keyword Arguments: args (tuple) – Represent the free parameters passed to the circuit. Here we are not concerned with their values, but with their structure. Each free param is replaced with a Variable instance. diag_approx (bool) – If True, forces the diagonal approximation. Default is False. QuantumFunctionError – if a metric tensor cannot be generated because no generator was defined

Note

Additional keyword arguments may be passed to the quantum circuit function, however PennyLane does not support differentiating with respect to keyword arguments. Instead, keyword arguments are useful for providing data or ‘placeholders’ to the quantum circuit function.

evaluate(**kwargs)

Evaluates the quantum function on the specified device.

Parameters: args (tuple) – input parameters to the quantum function QuantumFunctionError – if there is an error while measuring output measured value(s) float, 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 (array[float]) – circuit input parameters measured values array[float]
jacobian(params, which=None, *, method='B', h=1e-07, order=1, **kwargs)[source]

Compute the Jacobian of the QNode.

Returns the Jacobian of the parametrized quantum circuit encapsulated in the QNode.

The Jacobian can be computed using several methods:

• Finite differences ('F'). The first order method evaluates the circuit at $$n+1$$ points of the parameter space, the second order method at $$2n$$ points, where n = len(which).

• Analytic method ('A'). Works for all one-parameter gates where the generator only has two unique eigenvalues; this includes one-parameter qubit gates, as well as Gaussian circuits of order one or two. Additionally, can be used in CV systems for Gaussian circuits containing first- and second-order observables.

The circuit is evaluated twice for each incidence of each parameter in the circuit.

• Best known method for each parameter ('B'): uses the analytic method if possible, otherwise finite difference.

Note

The finite difference method is sensitive to statistical noise in the circuit output, since it compares the output at two points infinitesimally close to each other. Hence the ‘F’ method requires exact expectation values, i.e., analytic=True in simulation plugins.

Parameters: Keyword Arguments: params (nested Sequence[Number], Number) – point in parameter space at which to evaluate the gradient which (Sequence[int], None) – return the Jacobian with respect to these parameters. None (the default) means with respect to all parameters. Note that keyword arguments to the QNode are always treated as fixed values and not included in the Jacobian calculation. method (str) – Jacobian computation method, see above. h (float) – finite difference method step size order (int) – finite difference method order, 1 or 2 shots (int) – How many times the circuit should be evaluated (or sampled) to estimate the expectation values. QuantumFunctionError – if sampling is specified ValueError – if params is incorrect, differentiation is not possible, or if method is unknown Jacobian matrix, with shape (n_out, len(which)), where len(which) is the number of free parameters, and n_out is the number of expectation values returned by the QNode. array[float]
metric_tensor(*args, **kwargs)[source]

Evaluate the value of the metric tensor.

Parameters: Keyword Arguments: args – qfunc positional arguments kwargs – qfunc keyword arguments diag_approx (bool) – If True, forces the diagonal approximation. Default is False. measured values array[float]
print_applied()[source]

Prints the most recently applied operations from the QNode.

to_tf()[source]

Convert the standard PennyLane QNode into a TFQNode().

Raises: QuantumFunctionError – if TensorFlow >= 1.12 is not installed
to_torch()[source]

Convert the standard PennyLane QNode into a TorchQNode().

Raises: QuantumFunctionError – if PyTorch is not installed