# qml.operation¶

Warning

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.

## Description¶

### 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).

#### Differentiation¶

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.

Note

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.

#### Differentiation¶

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.

Note

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

## Functions¶

 classproperty(func) The class property decorator operation_derivative(operation) Calculate the derivative of an operation.

## Classes¶

 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. DiagonalOperation(*params[, wires, do_queue, id]) Base class for diagonal quantum operations supported by a device. 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. Tensor(*args) Container class representing tensor products of observables. WiresEnum(value) Integer enumeration class to represent the number of wires an operation acts on

## Variables¶

 AllWires An enumeration which represents all wires in the subsystem. AnyWires An enumeration which represents any wires in the subsystem. Expectation An enumeration which represents returning the expectation value of an observable on specified wires. Probability An enumeration which represents returning probabilities of all computational basis states. Sample An enumeration which represents sampling an observable. State An enumeration which represents returning the state in the computational basis. Variance An enumeration which represents returning the variance of an observable on specified wires. has_gen Returns True if an operator has a generator defined. has_grad_method Returns True if an operator has a grad_method defined. has_multipar Returns True if an operator has more than one parameter according to num_params. has_nopar Returns True if an operator has no parameters according to num_params. has_unitary_gen Returns True if an operator has a unitary_generator according to the has_unitary_generator flag. is_measurement Returns True if an operator is a MeasurementProcess instance. is_trainable Returns True if any of the parameters of an operator is trainable according to qml.math.requires_grad.