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:


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.

Number of parameters the operation takes.


Number of wires the operation acts on.

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


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.

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


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 the validity of the operation wires.

Parameters:wires (Sequence[int, Variable]) – wires to check
Raises:TypeError – list of wires is invalid
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).

  • 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)

TypeError – parameter is not an element of the expected domain



Return type:

Number, array, Variable


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]

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]

Append the operation to a QNode queue.