qml.operation.CVObservable

class CVObservable(*params, wires=None, do_queue=True, id=None)[source]

Bases: pennylane.operation.CV, pennylane.operation.Observable

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.

eigvals

Eigenvalues of an instantiated observable.

ev_order

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

hash

returns an integer hash uniquely representing the operator

id

String for the ID of the operator.

matrix

Matrix representation of an instantiated operator in the computational basis.

name

String for the name of the operator.

num_params

Number of trainable parameters that this operator expects to be fed via the dynamic *params argument.

num_wires

Number of wires the operator acts on.

parameters

Current parameter values.

return_type

supports_heisenberg

wires

Wires of this operator.

eigvals

Eigenvalues of an instantiated observable.

The order of the eigenvalues needs to match the order of the computational basis vectors when the observable is diagonalized using diagonalizing_gates. This is a requirement for using qubit observables in quantum functions.

Example:

>>> U = qml.PauliZ(wires=1)
>>> U.eigvals
>>> array([1, -1])
Returns

eigvals representation

Return type

array

ev_order = None

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

Type

None, int

hash

returns an integer hash uniquely representing the operator

Type

int

id

String for the ID of the operator.

matrix

Matrix representation of an instantiated operator in the computational basis.

Example:

>>> U = qml.RY(0.5, wires=1)
>>> U.matrix
>>> array([[ 0.96891242+0.j, -0.24740396+0.j],
           [ 0.24740396+0.j,  0.96891242+0.j]])
Returns

matrix representation

Return type

array

name

String for the name of the operator.

num_params

Number of trainable parameters that this operator expects to be fed via the dynamic *params argument.

By default, this property returns as many parameters as were used for the operator creation. If the number of parameters for an operator subclass is fixed, this property can be overwritten to return the fixed value.

Returns

number of parameters

Return type

int

num_wires

Number of wires the operator acts on.

parameters

Current parameter values.

return_type = None
supports_heisenberg = False
wires

Wires of this operator.

Returns

wires

Return type

Wires

compare(other)

Compares with another Hamiltonian, Tensor, or Observable, to determine if they are equivalent.

decompose()

Decomposes this operator into products of other operators.

decomposition(*params, wires)

Defines a decomposition of this operator into products of other operators.

diagonalizing_gates()

Returns the list of operations such that they diagonalize the observable in the computational basis.

heisenberg_expand(U, wires)

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

heisenberg_obs(wires)

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

label([decimals, base_label])

A customizable string representation of the operator.

queue([context])

Append the operator to the Operator queue.

compare(other)

Compares with another Hamiltonian, Tensor, or Observable, to determine if they are equivalent.

Observables/Hamiltonians are equivalent if they represent the same operator (their matrix representations are equal), and they are defined on the same wires.

Warning

The compare method does not check if the matrix representation of a Hermitian observable is equal to an equivalent observable expressed in terms of Pauli matrices. To do so would require the matrix form of Hamiltonians and Tensors be calculated, which would drastically increase runtime.

Returns

True if equivalent.

Return type

(bool)

Examples

>>> ob1 = qml.PauliX(0) @ qml.Identity(1)
>>> ob2 = qml.Hamiltonian([1], [qml.PauliX(0)])
>>> ob1.compare(ob2)
True
>>> ob1 = qml.PauliX(0)
>>> ob2 = qml.Hermitian(np.array([[0, 1], [1, 0]]), 0)
>>> ob1.compare(ob2)
False
decompose()

Decomposes this operator into products of other operators.

Returns

list[Operation]

static decomposition(*params, wires)

Defines a decomposition of this operator into products of other operators.

Parameters
  • params (tuple[float, int, array]) – operator parameters

  • wires (Union(Sequence[int], Wires)) – wires the operator acts on

Returns

list[Operation]

diagonalizing_gates()

Returns the list of operations such that they diagonalize the observable in the computational basis.

Returns

A list of gates that diagonalize the observable in the computational basis.

Return type

list(qml.Operation)

heisenberg_expand(U, wires)

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)

  • wires (Wires) – wires on the device the array U should be expanded to apply to

Raises

ValueError – if the size of the input matrix is invalid or num_wires is incorrect

Returns

expanded array, dimension 1+2*num_wires

Return type

array[float]

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

wires (Wires) – wires on the device that the observable gets applied to

Returns

\(q\)

Return type

array[float]

label(decimals=None, base_label=None)

A customizable string representation of the operator.

Parameters
  • decimals=None (int) – If None, no parameters are included. Else, specifies how to round the parameters.

  • base_label=None (str) – overwrite the non-parameter component of the label

Returns

label to use in drawings

Return type

str

Example:

>>> op = qml.RX(1.23456, wires=0)
>>> op.label()
"RX"
>>> op.label(decimals=2)
"RX\n(1.23)"
>>> op.label(base_label="my_label")
"my_label"
>>> op.label(decimals=2, base_label="my_label")
"my_label\n(1.23)"
>>> op.inv()
>>> op.label()
"RX⁻¹"
queue(context=<class 'pennylane.queuing.QueuingContext'>)

Append the operator to the Operator queue.