Operation

Module: pennylane

class Operation(*args, wires=None, do_queue=True)[source]

Base class for quantum operations supported by a device.

The following class attributes must be defined for all Operations:

The following two class attributes are optional, but in most cases should be clearly defined to avoid unexpected behavior during differentiation.

Finally, there are some additional optional class attributes that may be set, and used by certain quantum optimizers:

Parameters:

args (tuple[float, int, array, Variable]) – operation parameters

Keyword Arguments:
 
  • wires (Sequence[int]) – Subsystems it acts on. If not given, args[-1] is interpreted as wires.
  • do_queue (bool) – Indicates whether the operation should be immediately pushed into a QNode circuit queue. This flag is useful if there is some reason to run an Operation outside of a QNode context.
num_params

Number of parameters the operation takes.

num_wires

Number of wires the operation acts on.

The value 0 allows the operation to act on any number of wires.

par_domain

Domain of the gate parameters.

  • 'N': natural numbers (including zero).
  • 'R': floats.
  • 'A': arrays of real or complex values.
  • None: if there are no parameters.
grad_method

Gradient computation method.

  • 'A': analytic differentiation.
  • 'F': finite difference numerical differentiation.
  • None: the operation may not be differentiated.

Default is 'F', or None if the Operation has zero parameters.

generator

Generator of the operation.

A length-2 list [generator, scaling_factor], where

  • generator is an existing PennyLane operation class or \(2\times 2\) Hermitian array that acts as the generator of the current operation
  • scaling_factor represents a scaling factor applied to the generator operation

For example, if \(U(\theta)=e^{i0.7\theta \sigma_x}\), then \(\sigma_x\), with scaling factor \(s\), is the generator of operator \(U(\theta)\):

generator = [PauliX, 0.7]

Default is [None, 1], indicating the operation has no generator.

name = None

name of the operation

Type:str
grad_recipe

Gradient recipe for the analytic differentiation method.

This is a list with one tuple per operation parameter. For parameter \(k\), the tuple is of the form \((c_k, s_k)\), resulting in a gradient recipe of

\[\frac{\partial}{\partial\phi_k}O = c_k\left[O(\phi_k+s_k)-O(\phi_k-s_k)\right].\]

If this property returns None, the default gradient recipe \((c_k, s_k)=(1/2, \pi/2)\) is assumed for every parameter.

check_wires(wires)[source]

Check the validity of the operation wires.

Parameters:wires (Sequence[int, Variable]) – wires to check
Raises:TypeError – list of wires is invalid
Returns:p
Return type:Number, array, Variable
check_domain(p, flattened=False)[source]

Check the validity of a parameter.

Variable instances can represent any real scalars (but not arrays).

Parameters:
  • p (Number, array, Variable) – parameter to check
  • flattened (bool) – True means p is an element of a flattened parameter sequence (affects the handling of ‘A’ parameters)
Raises:

TypeError – parameter is not an element of the expected domain

Returns:

p

Return type:

Number, array, Variable

wires

Current wire values.

Fixed wires are returned as is, free wires represented by Variable instances are replaced by their current numerical value.

Returns:wire values
Return type:list[int]
parameters

Current parameter values.

Fixed parameters are returned as is, free parameters represented by Variable instances are replaced by their current numerical value.

Returns:parameter values
Return type:list[float]
queue()[source]

Append the operation to a QNode queue.