Symbolic quantum operations¶

Module name: pennylane.operation

Operation base classes¶

This module contains the symbolic base class for performing quantum operations and measuring observables in PennyLane.

• 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 (with multiplier $$c_k$$, parameter shift $$s_k$$ for parameter $$\phi_k$$) works as follows:

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

Summary¶

 Operation(*args[, wires, do_queue]) Base class for quantum operations supported by a device. Observable(*args[, wires, do_queue]) Base class for observables supported by a device.

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.

Summary¶

 CV A mixin base class denoting a continuous-variable operation. CVOperation(*args[, wires, do_queue]) Base class for continuous-variable quantum operations. CVObservable(*args[, wires, do_queue]) Base class for continuous-variable observables.

Code details¶

class ClassPropertyDescriptor(fget, fset=None)[source]

Allows a class property to be defined

setter(func)[source]

Set the function as a class method, and store as an attribute.

classproperty(func)[source]

The class property decorator

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.

Parameters: Keyword Arguments: args (tuple[float, int, array, Variable]) – operation parameters 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

• '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.

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 TypeError – list of wires is invalid p Number, array, Variable
check_domain(p, flattened=False)[source]

Check the validity of a parameter.

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) TypeError – parameter is not an element of the expected domain p 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 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 list[float]
queue()[source]

Append the operation to a QNode queue.

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

Base class for observables supported by a device.

Observable is used to describe Hermitian quantum observables.

As with Operation, the following class attributes must be defined for all observables:

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

Parameters: Keyword Arguments: args (tuple[float, int, array, Variable]) – observable parameters wires (Sequence[int]) – subsystems it acts on. Currently, only one subsystem is supported. do_queue (bool) – Indicates whether the operation should be immediately pushed into a QNode observable queue. This flag is useful if there is some reason to call an observable outside of a QNode context.
class CV[source]

A mixin base class denoting a continuous-variable operation.

heisenberg_expand(U, num_wires)[source]

Expand the given local Heisenberg-picture array into a full-system one.

Parameters: U (array[float]) – array to expand (expected to be of the dimension 1+2*self.num_wires) num_wires (int) – total number of wires in the quantum circuit. If zero, return U as is. expanded array, dimension 1+2*num_wires array[float]
static _heisenberg_rep(p)[source]

Heisenberg picture representation of the operation.

• For Gaussian CV gates, this method returns the matrix of the linear transformation carried out by the gate for the given parameter values. The method is not defined for non-Gaussian gates.

The existence of this method is equivalent to setting grad_method = 'A'.

• For observables, returns a real vector (first-order observables) or symmetric matrix (second-order observables) of expansion coefficients of the observable.

For single-mode Operations we use the basis $$\mathbf{r} = (\I, \x, \p)$$. For multi-mode Operations we use the basis $$\mathbf{r} = (\I, \x_0, \p_0, \x_1, \p_1, \ldots)$$.

Note

For gates, we assume that the inverse transformation is obtained by negating the first parameter.

Parameters: p (Sequence[float]) – parameter values for the transformation $$\tilde{U}$$ or $$q$$ array[float]
class CVOperation(*args, wires=None, do_queue=True)[source]

Base class for continuous-variable quantum operations.

heisenberg_pd(idx)[source]

Partial derivative of the Heisenberg picture transform matrix.

Parameters: idx (int) – index of the parameter with respect to which the partial derivative is computed. partial derivative array[float]
heisenberg_tr(num_wires, inverse=False)[source]

Heisenberg picture representation of the linear transformation carried out by the gate at current parameter values.

Given a unitary quantum gate $$U$$, we may consider its linear transformation in the Heisenberg picture, $$U^\dagger(\cdot) U$$.

If the gate is Gaussian, this linear transformation preserves the polynomial order of any observables that are polynomials in $$\mathbf{r} = (\I, \x_0, \p_0, \x_1, \p_1, \ldots)$$. This also means it maps $$\text{span}(\mathbf{r})$$ into itself:

$U^\dagger \mathbf{r}_i U = \sum_j \tilde{U}_{ij} \mathbf{r}_j$

For Gaussian CV gates, this method returns the transformation matrix for the current parameter values of the Operation. The method is not defined for non-Gaussian (and non-CV) gates.

Parameters: num_wires (int) – total number of wires in the quantum circuit inverse (bool) – if True, return the inverse transformation instead $$\tilde{U}$$, the Heisenberg picture representation of the linear transformation array[float]
class CVObservable(*args, wires=None, do_queue=True)[source]

Base class for continuous-variable observables.

The class attribute ev_order can be defined to indicate to PennyLane whether the corresponding CV observable is a polynomial in the quadrature operators. If so,

• ev_order = 1 indicates a first order polynomial in quadrature operators $$(\x, \p)$$.
• ev_order = 2 indicates a second order polynomial in quadrature operators $$(\x, \p)$$.

If ev_order is not None, then the Heisenberg representation of the observable should be defined in the static method _heisenberg_rep(), returning an array of the correct dimension.

ev_order = None

if not None, the observable is a polynomial of the given order in (x, p).

Type: None, int
heisenberg_obs(num_wires)[source]

Representation of the observable in the position/momentum operator basis.

Returns the expansion $$q$$ of the observable, $$Q$$, in the basis $$\mathbf{r} = (\I, \x_0, \p_0, \x_1, \p_1, \ldots)$$.

• For first-order observables returns a real vector such that $$Q = \sum_i q_i \mathbf{r}_i$$.
• For second-order observables returns a real symmetric matrix such that $$Q = \sum_{ij} q_{ij} \mathbf{r}_i \mathbf{r}_j$$.
Parameters: num_wires (int) – total number of wires in the quantum circuit $$q$$ array[float]