# CV quantum expectations¶

Module name: pennylane.expval.cv

This section contains the available built-in continuous-variable quantum operations supported by PennyLane, as well as their conventions.

Note

Currently, all expectation commands return scalars.

### Summary

 MeanPhoton(wires) Returns the photon number expectation value. X(wires) Returns the position expectation value in phase space. P(wires) Returns the momentum expectation value in phase space. Homodyne(phi, wires) Expectation value of homodyne measurement in phase space. PolyXP(q, wires) Expectation value of a second-order polynomial observable. NumberState(n, wires) Expectation value of the number state observable $$\ket{n}\bra{n}$$. Identity(wires) Expectation value of the identity observable $$\I$$.

### Code details

class MeanPhoton(wires)[source]

Returns the photon number expectation value.

This expectation command returns the value $$\braket{\hat{n}}$$ where the number operator is $$\hat{n} = \a^\dagger \a = \frac{1}{2\hbar}(\x^2 +\p^2) -\I/2$$.

Details:

• Number of wires: 1

• Number of parameters: 0

• Expectation order: 2nd order in the quadrature operators.

• Heisenberg representation:

$\begin{split}M = \frac{1}{2\hbar}\begin{bmatrix} -\hbar & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{bmatrix}\end{split}$
Parameters: wires (Sequence[int] or int) – the wire the operation acts on.
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 X(wires)[source]

Returns the position expectation value in phase space.

This expectation command returns the value $$\braket{\x}$$.

Details:

• Number of wires: 1

• Number of parameters: 0

• Expectation order: 1st order in the quadrature operators.

• Heisenberg representation:

$d = [0, 1, 0]$
Parameters: wires (Sequence[int] or int) – the wire the operation acts on.
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 P(wires)[source]

Returns the momentum expectation value in phase space.

This expectation command returns the value $$\braket{\p}$$.

Details:

• Number of wires: 1

• Number of parameters: 0

• Expectation order: 1st order in the quadrature operators.

• Heisenberg representation:

$d = [0, 0, 1]$
Parameters: wires (Sequence[int] or int) – the wire the operation acts on.
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 Homodyne(phi, wires)[source]

Expectation value of homodyne measurement in phase space.

This expectation command returns the value $$\braket{\x_\phi}$$, where $$\x_\phi = \x cos\phi+\p\sin\phi$$ is the generalised quadrature operator.

Details:

• Number of wires: 1

• Number of parameters: 1

• Expectation order: 1st order in the quadrature operators.

• Heisenberg representation:

$d = [0, \cos\phi, \sin\phi]$
Parameters: phi (float) – axis in the phase space at which to calculate the homodyne measurement. wires (Sequence[int] or int) – the wire the operation acts on.
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 PolyXP(q, wires)[source]

Expectation value of a second-order polynomial observable.

Represents an arbitrary observable $$P(\x,\p)$$ that is a second order polynomial in the basis $$\mathbf{r} = (\I, \x_0, \p_0, \x_1, \p_1, \ldots)$$.

For first-order observables the representation is a real vector $$\mathbf{d}$$ such that $$P(\x,\p) = \mathbf{d}^T \mathbf{r}$$.

For second-order observables the representation is a real symmetric matrix $$A$$ such that $$P(\x,\p) = \mathbf{r}^T A \mathbf{r}$$.

Used by QNode._pd_analytic() for evaluating arbitrary order-2 CV observables.

Details:

• Number of wires: None (applied to the entire system).
• Number of parameters: 1
• Expectation order: 2nd order in the quadrature operators.
• Heisenberg representation: $$A$$
Parameters: q (array[float]) – expansion coefficients
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 NumberState(n, wires)[source]

Expectation value of the number state observable $$\ket{n}\bra{n}$$.

Represents the non-Gaussian number state observable

$\ket{n}\bra{n} = \ket{n_0, n_1, \dots, n_P}\bra{n_0, n_1, \dots, n_P}$

where $$n_i$$ is the occupation number of the $$i$$ th wire.

The expectation of this observable is

$E[\ket{n}\bra{n}] = \text{Tr}(\ket{n}\bra{n}\rho) = \text{Tr}(\braketT{n}{\rho}{n}) = \braketT{n}{\rho}{n}$

corresponding to the probability of measuring the quantum state in the state $$\ket{n}=\ket{n_0, n_1, \dots, n_P}$$.

Note

If NumberState is applied to a subset of wires, the unaffected wires are traced out prior to the expectation value calculation.

Details:

• Number of wires: None (applied to any subset of wires).
• Number of parameters: 1
• Expectation order: None (non-Gaussian)
Parameters: n (array) – Array of non-negative integers representing the number state observable $$\ket{n}\bra{n}=\ket{n_0, n_1, \dots, n_P}\bra{n_0, n_1, \dots, n_P}$$. For example, to return the expectation value of observable $$\ket{0,4,2}\bra{0,4,2}$$, on wires 0, 1, and 3 of a QNode, you would call NumberState(np.array([0, 4, 2], wires=[0, 1, 3])). Note that len(n)==len(wires), and that len(n) cannot exceed the total number of wires in the QNode.
class Identity(wires)[source]

Expectation value of the identity observable $$\I$$.

The expectation of this observable

$E[\I] = \text{Tr}(\I \rho)$

corresponds to the trace of the quantum state, which in exact simulators should always be equal to 1.

Note

Can be used to check normalization in approximate simulators such as fock basis based ones.

Details:

• Number of wires: None (applied to any subset of wires).
• Number of parameters: 0
• Expectation order: None (non-Gaussian)