QNode

Module: pennylane

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

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.
variable_ops = None

Mapping from free parameter index to the list of Operations (in this circuit) that depend on it.

The first element of the tuple is the index of the Operation in the program queue, the second the index of the parameter within the Operation.

Type:dict[int->list[(int, int)]]
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.
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: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.
Keyword Arguments:
 diag_approx (bool) – If True, forces the diagonal approximation. Default is False.

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
Returns:output measured value(s)
Return type:float, array[float]
metric_tensor(*args, **kwargs)[source]

Evaluate the value of the metric tensor.

Parameters:
  • args – qfunc positional arguments
  • kwargs – qfunc keyword arguments
Keyword Arguments:
 

diag_approx (bool) – If True, forces the diagonal approximation. Default is False.

Returns:

measured values

Return type:

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
Returns:

measured values

Return type:

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:
  • 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.
Keyword Arguments:
 
  • 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.
Returns:

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.

Return type:

array[float]

to_torch()[source]

Convert the standard PennyLane QNode into a TorchQNode().

to_tf()[source]

Convert the standard PennyLane QNode into a TFQNode().