Unless you are a PennyLane or plugin developer, you likely do not need to use these classes directly.

See the main operations page for details on available operations and observables.

This module contains the abstract base classes for defining PennyLane operations and observables.


Qubit Operations

The Operator class serves as a base class for operators, and is inherited by both the Observable class and the Operation class. These classes are subclassed to implement quantum operations and measure observables in PennyLane.

  • Each Operator subclass represents a general type of map between physical states. Each instance of these subclasses represents either

    • an application of the operator or

    • an instruction to measure and return the respective result.

    Operators act on a sequence of wires (subsystems) using given parameter values.

  • Each Operation subclass represents a type of quantum operation, for example a unitary quantum gate. Each instance of these subclasses represents an application of the operation with given parameter values to a given sequence of wires (subsystems).

  • Each Observable subclass represents a type of physical observable. Each instance of these subclasses represents an instruction to measure and return the respective result for the given parameter values on a sequence of wires (subsystems).


In general, an Operation is differentiable (at least using the finite-difference method) with respect to a parameter iff

  • the domain of that parameter is continuous.

For an Operation to be differentiable with respect to a parameter using the analytic method of differentiation, it must satisfy an additional constraint:

  • the parameter domain must be real.


These conditions are not sufficient for analytic differentiation. For example, CV gates must also define a matrix representing their Heisenberg linear transformation on the quadrature operators.

For gates that are supported via the analytic method, the gradient recipe works as follows:

\[\frac{\partial}{\partial\phi_k}f = \sum_{i} c_i f(a_i \phi_k+s_i).\]

where \(f\) is the expectation value of an observable on a circuit that has been evolved by the operation being considered with parameter \(\phi_k\), there are multiple terms indexed with \(i\) for each parameter \(\phi\) and the \([c_i, a_i, s_i]\) are coefficients specific to the gate.

The following specific case holds for example for qubit operations that are generated by one of the Pauli matrices and results in an overall positive and negative shift:

\[\frac{\partial}{\partial\phi_k}f = \frac{1}{2}\left[f \left( \phi_k+\frac{\pi}{2} \right) - f \left( \phi_k-\frac{\pi}{2} \right)\right],\]

i.e., so that \([c_0, a_0, s_0]=[1/2, 1, \pi/2]\) and \([c_1, a_1, s_1]=[-1/2, 1, -\pi/2]\).

CV Operation base classes

Due to additional requirements, continuous-variable (CV) operations must subclass the CVOperation or CVObservable classes instead of Operation and Observable.


To enable gradient computation using the analytic method for Gaussian CV operations, in addition, you need to provide the static class method _heisenberg_rep() that returns the Heisenberg representation of the operation given its list of parameters, namely:

  • For Gaussian CV Operations this method should return the matrix of the linear transformation carried out by the operation on the vector of quadrature operators \(\mathbf{r}\) for the given parameter values.

  • For Gaussian CV Observables this method should return a real vector (first-order observables) or symmetric matrix (second-order observables) of coefficients of the quadrature operators \(\x\) and \(\p\).

PennyLane uses the convention \(\mathbf{r} = (\I, \x, \p)\) for single-mode operations and observables and \(\mathbf{r} = (\I, \x_0, \p_0, \x_1, \p_1, \ldots)\) for multi-mode operations and observables.


Non-Gaussian CV operations and observables are currently only supported via the finite-difference method of gradient computation.



The class property decorator


Calculate the derivative of an operation.



A mixin base class denoting a continuous-variable operation.

CVObservable(*params[, wires, do_queue, id])

Base class for continuous-variable observables.

CVOperation(*params[, wires, do_queue, id])

Base class for continuous-variable quantum operations.

Channel(*params[, wires, do_queue, id])

Base class for quantum channels.

Observable(*params[, wires, do_queue, id])

Base class for observables supported by a device.


Enumeration class to represent the return types of an observable.

Operation(*params[, wires, do_queue, id])

Base class for quantum operations supported by a device.

Operator(*params[, wires, do_queue, id])

Base class for quantum operators supported by a device.


Container class representing tensor products of observables.


Integer enumeration class to represent the number of wires an operation acts on



An enumeration which represents all wires in the subsystem.


An enumeration which represents any wires in the subsystem.


An enumeration which represents returning the expectation value of an observable on specified wires.


An enumeration which represents returning probabilities of all computational basis states.


An enumeration which represents sampling an observable.


An enumeration which represents returning the state in the computational basis.


An enumeration which represents returning the variance of an observable on specified wires.


Returns True if an operator has a generator defined.


Returns True if an operator has a grad_method defined.


Returns True if an operator has more than one parameter according to num_params.


Returns True if an operator has no parameters according to num_params.


Returns True if an operator has a unitary_generator according to the has_unitary_generator flag.


Returns True if an operator is a MeasurementProcess instance.


Returns True if any of the parameters of an operator is trainable according to qml.math.requires_grad.


Returns True if the object is not a quantum tape

Class Inheritance Diagram

Inheritance diagram of pennylane.operation.CV, pennylane.operation.CVObservable, pennylane.operation.CVOperation, pennylane.operation.Channel, pennylane.operation.Observable, pennylane.operation.ObservableReturnTypes, pennylane.operation.Operation, pennylane.operation.Operator, pennylane.operation.Tensor, pennylane.operation.WiresEnum

Operation attributes

PennyLane contains a mechanism for storing lists of operations with similar attributes and behaviour (for example, those that are their own inverses). The attributes below are already included, and are used primarily for the purpose of compilation transforms. New attributes can be added by instantiating new Attribute objects.


Class to represent a set of operators with a certain attribute.


Operations for which composing multiple copies of the operation results in an addition (or alternative accumulation) of parameters.


Operations that are diagonal in the computational basis.


Operations that are generated by a unitary operator.


Operations that are their own inverses.


Operations that are the same if you exchange the order of wires.


Controlled operations that are the same if you exchange the order of all but the last (target) wire.