Quantum circuit variables

Module name: pennylane.variable

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.

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.

Code details

class Variable(idx=None, name=None)[source]

A reference class to dynamically track and update circuit parameters.

Represents a placeholder variable. This can either be a free quantum circuit parameter (with a non-fixed value) times an optional scalar multiplier, or a placeholder for data/other hard-coded data.

Each time the circuit is executed, it is given a vector of parameter values, and a dictionary of keyword variable values.

Variable is essentially an index into that vector.

Note

Variables currently do not implement any arithmetic operations other than scalar multiplication.

Parameters:
  • idx (int) – parameter index >= 0
  • name (str) – name of the variable (optional)
free_param_values = None

current free parameter values, set in QNode.evaluate()

Type:array[float]
kwarg_values = None

dictionary containing the keyword argument values, set in QNode.evaluate()

Type:dict
idx = None

parameter index

Type:int
name = None

parameter name

Type:str
mult = None

parameter scalar multiplier

Type:int, float
val

Current numerical value of the Variable.

Returns:current value of the Variable
Return type:float