qml.variable

Warning

Unless you are a PennyLane or plugin developer, you likely do not need to use the Variable class.

This module contains the Variable class, which is used to track and identify QNode parameters.

Description

The first time a QNode is evaluated (either by calling evaluate(), __call__(), or jacobian()), the construct() method is called, which performs a ‘just-in-time’ circuit construction on the Device. As part of this construction, all arguments and keyword arguments are wrapped in a Variable as follows:

  • All positional arguments in *args, including those with multiple dimensions, are flattened to a single list, and each element wrapped as a Variable instance, indexed by its position in the list.

    This allows PennyLane to inspect the shape and type of arguments the user wishes to pass. The list can then be unflattened back to the original shape of *args.

  • The same is done for each keyword argument in **kwargs, the only difference being that the name of each contained Variable corresponds with the keyword name.

As a result, the device stores a list of operations and expectations, with all free parameters stored as Variable instances.

Note

The QNode can be differentiated with respect to positional arguments, but not with respect to keyword arguments. This makes keyword arguments a natural location for data placeholders.

Important

If the user defines a keyword argument, then they always have to pass the corresponding variable as a keyword argument, otherwise it won’t register.

For each successive QNode execution, the user-provided values for arguments and keyword arguments are stored in the Variable.free_param_values list and the Variable.kwarg_values dictionary respectively; these are then returned by Variable.val(), using its idx value, and, for keyword arguments, its name, to return the correct value to the operation.

Note

The Operation.parameters() property automates the process of unpacking the Variable value. The attribute Variable.val() should not need to be accessed outside of advanced usage.

Classes

Variable(idx[, name]) A reference to dynamically track and update circuit parameters.