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.


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.


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.


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 the positional and keyword arguments are stored in Variable.positional_arg_values and Variable.kwarg_values respectively; the values are then returned by Variable.val(), using the Variable’s idx attribute, and, for keyword arguments, its name, to return the correct value to the operation.


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.


Variable(idx[, name, is_kwarg])

A reference to dynamically track and update circuit parameters.