# CV quantum operations¶

Module name: pennylane.ops.cv

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

Note

For the Heisenberg matrix representation of CV operations, we use the ordering $$(\hat{\mathbb{1}}, \hat{x}, \hat{p})$$ for single modes and $$(\hat{\mathbb{1}}, \hat{x}_1, \hat{p}_2, \hat{x}_1,\hat{p}_2)$$ for two modes .

## Gates¶

 Rotation(phi, wires) Phase space rotation Squeezing(r, phi, wires) Phase space squeezing. Displacement(a, phi, wires) Phase space displacement. Beamsplitter(theta, phi, wires) Beamsplitter interaction. TwoModeSqueezing(r, phi, wires) Phase space two-mode squeezing. QuadraticPhase(s, wires) Quadratic phase shift. ControlledAddition(s, wires) Controlled addition operation. ControlledPhase(s, wires) Controlled phase operation. Kerr(kappa, wires) Kerr interaction. CrossKerr(kappa, wires) Cross-Kerr interaction. CubicPhase(gamma, wires) Cubic phase shift. Interferometer(U, wires) A linear interferometer transforming the bosonic operators according to the unitary matrix $$U$$.

## State preparation¶

 CoherentState(a, phi, wires) Prepares a coherent state. SqueezedState(r, phi, wires) Prepares a squeezed vacuum state. DisplacedSqueezedState(a, phi_a, r, phi_r, wires) Prepares a displaced squeezed vacuum state. ThermalState(nbar, wires) Prepares a thermal state. GaussianState(r, V, wires) Prepare subsystems in a given Gaussian state. FockState(n, wires) Prepares a single Fock state. FockStateVector(state, wires) Prepare subsystems using the given ket vector in the Fock basis. FockDensityMatrix(state, wires) Prepare subsystems using the given density matrix in the Fock basis. CatState(a, phi, p, wires) Prepares a cat state.

## Observables¶

 NumberOperator(wires) The photon number observable $$\langle \hat{n}\rangle$$. X(wires) The position quadrature observable $$\hat{x}$$. P(wires) The momentum quadrature observable $$\hat{p}$$. QuadOperator(phi, wires) The generalized quadrature observable $$\x_\phi = \x cos\phi+\p\sin\phi$$. PolyXP(q, wires) An arbitrary second-order polynomial observable. FockStateProjector(n, wires) The number state observable $$\ket{n}\bra{n}$$.

### Code details¶

class Rotation(phi, wires)[source]

Phase space rotation

$R(\phi) = \exp\left(i \phi \ad \a\right)=\exp\left(i \frac{\phi}{2} \left(\frac{\x^2+ \p^2}{\hbar}-\I\right)\right).$

Details:

• Number of wires: 1

• Number of parameters: 1

• Gradient recipe: $$\frac{d}{dr}f(R(r)) = \frac{1}{2} \left[f(R(\phi+\pi/2)) - f(R(\phi-\pi/2))\right]$$ where $$f$$ is an expectation value depending on $$R(r)$$.

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0 & 0\\ 0 & \cos\phi & -\sin\phi\\ 0 & \sin\phi & \cos\phi \end{bmatrix}\end{split}$
Parameters: phi (float) – the rotation angle
class Squeezing(r, phi, wires)[source]

Phase space squeezing.

$S(z) = \exp\left(\frac{1}{2}(z^* \a^2 -z {\a^\dagger}^2)\right).$

where $$z = r e^{i\phi}$$.

Details:

• Number of wires: 1

• Number of parameters: 2

• Gradient recipe: $$\frac{d}{dr}f(S(r,\phi)) = \frac{1}{2\sinh s} \left[f(S(r+s, \phi)) - f(S(r-s, \phi))\right]$$, where $$s$$ is an arbitrary real number ($$0.1$$ by default) and $$f$$ is an expectation value depending on $$S(r,\phi)$$.

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cosh r - \cos\phi \sinh r & -\sin\phi\sinh r \\ 0 & -\sin\phi\sinh r & \cosh r+\cos\phi\sinh r \end{bmatrix}\end{split}$
Parameters: r (float) – squeezing amount phi (float) – squeezing phase angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class Displacement(a, phi, wires)[source]

Phase space displacement.

$D(a,\phi) = D(\alpha) = \exp(\alpha \ad -\alpha^* \a) = \exp\left(-i\sqrt{\frac{2}{\hbar}}(\re(\alpha) \hat{p} -\im(\alpha) \hat{x})\right).$

where $$\alpha = ae^{i\phi}$$ has magnitude $$a\geq 0$$ and phase $$\phi$$. The result of applying a displacement to the vacuum is a coherent state $$D(\alpha)\ket{0} = \ket{\alpha}$$.

Details:

• Number of wires: 1

• Number of parameters: 2

• Gradient recipe: $$\frac{d}{da}f(D(a,\phi)) = \frac{1}{2s} \left[f(D(a+s, \phi)) - f(D(a-s, \phi))\right]$$, where $$s$$ is an arbitrary real number ($$0.1$$ by default) and $$f$$ is an expectation value depending on $$D(a,\phi)$$.

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0 & 0 \\ 2a\cos\phi & 1 & 0 \\ 2a\sin\phi & 0 & 1\end{bmatrix}\end{split}$
Parameters: a (float) – displacement magnitude $$a=|\alpha|$$ phi (float) – phase angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class Beamsplitter(theta, phi, wires)[source]

Beamsplitter interaction.

$B(\theta,\phi) = \exp\left(\theta (e^{i \phi} \a \hat{b}^\dagger -e^{-i \phi}\ad \hat{b}) \right).$

Details:

• Number of wires: 2

• Number of parameters: 2

• Gradient recipe: $$\frac{d}{d \theta}f(B(\theta,\phi)) = \frac{1}{2} \left[f(B(\theta+\pi/2, \phi)) - f(B(\theta-\pi/2, \phi))\right]$$ where $$f$$ is an expectation value depending on $$B(\theta,\phi)$$.

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0 & 0 & 0 & 0\\ 0 & \cos\theta & 0 & -\cos\phi\sin\theta & -\sin\phi\sin\theta \\ 0 & 0 & \cos\theta & \sin\phi\sin\theta & -\cos\phi\sin\theta\\ 0 & \cos\phi\sin\theta & -\sin\phi\sin\theta & \cos\theta & 0\\ 0 & \sin\phi\sin\theta & \cos\phi\sin\theta & 0 & \cos\theta \end{bmatrix}\end{split}$
Parameters: theta (float) – Transmittivity angle $$\theta$$. The transmission amplitude of the beamsplitter is $$t = \cos(\theta)$$. The value $$\theta=\pi/4$$ gives the 50-50 beamsplitter. phi (float) – Phase angle $$\phi$$. The reflection amplitude of the beamsplitter is $$r = e^{i\phi}\sin(\theta)$$. The value $$\phi = \pi/2$$ gives the symmetric beamsplitter. wires (Sequence[int] or int) – the wire the operation acts on
class TwoModeSqueezing(r, phi, wires)[source]

Phase space two-mode squeezing.

$S_2(z) = \exp\left(z^* \a \hat{b} -z \ad \hat{b}^\dagger \right) = \exp\left(r (e^{-i\phi} \a\hat{b} -e^{i\phi} \ad \hat{b}^\dagger \right).$

where $$z = r e^{i\phi}$$.

Details:

• Number of wires: 2

• Number of parameters: 2

• Gradient recipe: $$\frac{d}{dr}f(S_2(r,\phi)) = \frac{1}{2\sinh s} \left[f(S_2(r+s, \phi)) - f(S_2(r-s, \phi))\right]$$, where $$s$$ is an arbitrary real number ($$0.1$$ by default) and $$f$$ is an expectation value depending on $$S_2(r,\phi)$$.

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 \\ 0 & \cosh r & 0 & \sinh r \cos \phi & \sinh r \sin \phi\\ 0 & 0 & \cosh r & \sinh r \sin \phi & -\sinh r \cos \phi\\ 0 & \sinh r \cos \phi & \sinh r \sin \phi & \cosh r & 0\\ 0 & \sinh r \sin \phi & -\sinh r \cos \phi & 0 & \cosh r \end{bmatrix}\end{split}$
Parameters: r (float) – squeezing amount phi (float) – squeezing phase angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class QuadraticPhase(s, wires)[source]

Quadratic phase shift.

$P(s) = e^{i \frac{s}{2} \hat{x}^2/\hbar}.$

Details:

• Number of wires: 1

• Number of parameters: 1

• Gradient recipe: $$\frac{d}{ds}f(P(s)) = \frac{1}{2 a} \left[f(P(s+a)) - f(P(s-a))\right]$$, where $$a$$ is an arbitrary real number ($$0.1$$ by default) and $$f$$ is an expectation value depending on $$P(s)$$.

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & s & 1 \\ \end{bmatrix}\end{split}$
Parameters: s (float) – parameter wires (Sequence[int] or int) – the wire the operation acts on
class ControlledAddition(s, wires)[source]

Controlled addition operation.

$\text{CX}(s) = \int dx \ket{x}\bra{x} \otimes D\left({\frac{1}{\sqrt{2\hbar}}}s x\right) = e^{-i s \: \hat{x} \otimes \hat{p}/\hbar}.$

Details:

• Number of wires: 2

• Number of parameters: 1

• Gradient recipe: $$\frac{d}{ds}f(\text{CX}(s)) = \frac{1}{2 a} \left[f(\text{CX}(s+a)) - f(\text{CX}(s-a))\right]$$, where $$a$$ is an arbitrary real number ($$0.1$$ by default) and $$f$$ is an expectation value depending on $$\text{CX}(s)$$.

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & -s \\ 0 & s & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \end{bmatrix}\end{split}$
Parameters: s (float) – addition multiplier wires (Sequence[int] or int) – the wire the operation acts on
class ControlledPhase(s, wires)[source]

Controlled phase operation.

$\text{CZ}(s) = \iint dx dy \: e^{i sxy/\hbar} \ket{x,y}\bra{x,y} = e^{i s \: \hat{x} \otimes \hat{x}/\hbar}.$

Details:

• Number of wires: 2

• Number of parameters: 1

• Gradient recipe: $$\frac{d}{ds}f(\text{CZ}(s)) = \frac{1}{2 a} \left[f(\text{CZ}(s+a)) - f(\text{CZ}(s-a))\right]$$, where $$a$$ is an arbitrary real number ($$0.1$$ by default) and $$f$$ is an expectation value depending on $$\text{CZ}(s)$$.

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & s & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & s & 0 & 0 & 1 \end{bmatrix}\end{split}$
Parameters: s (float) – phase shift multiplier wires (Sequence[int] or int) – the wire the operation acts on
class Kerr(kappa, wires)[source]

Kerr interaction.

$K(\kappa) = e^{i \kappa \hat{n}^2}.$

Details:

• Number of wires: 1
• Number of parameters: 1
• Gradient recipe: None (uses finite difference)
Parameters: kappa (float) – parameter wires (Sequence[int] or int) – the wire the operation acts on
class CrossKerr(kappa, wires)[source]

Cross-Kerr interaction.

$CK(\kappa) = e^{i \kappa \hat{n}_1\hat{n}_2}.$

Details:

• Number of wires: 2
• Number of parameters: 1
• Gradient recipe: None (uses finite difference)
Parameters: kappa (float) – parameter wires (Sequence[int] or int) – the wire the operation acts on
class CubicPhase(gamma, wires)[source]

Cubic phase shift.

$V(\gamma) = e^{i \frac{\gamma}{3} \hat{x}^3/\hbar}.$

Details:

• Number of wires: 1
• Number of parameters: 1
• Gradient recipe: None (uses finite difference)
Parameters: gamma (float) – parameter wires (Sequence[int] or int) – the wire the operation acts on
class Interferometer(U, wires)[source]

A linear interferometer transforming the bosonic operators according to the unitary matrix $$U$$.

Note

This operation implements a fixed linear interferometer given a known unitary matrix.

If you instead wish to parameterize the interferometer, and calculate the gradient/optimize with respect to these parameters, consider instead the pennylane.template.Interferometer() template, which constructs an interferometer from a combination of beamsplitters and rotation gates.

Details:

• Number of wires: Any

• Number of parameters: 1

• Gradient recipe: None

• Heisenberg representation:

$\begin{split}M = \begin{bmatrix} 1 & 0\\ 0 & S\\ \end{bmatrix}\end{split}$

where $$S$$ is the Gaussian symplectic transformation representing the interferometer.

Parameters: U (array) – A shape (len(wires), len(wires)) complex unitary matrix wires (Sequence[int] or int) – the wires the operation acts on
class CoherentState(a, phi, wires)[source]

Prepares a coherent state.

Details:

• Number of wires: 1
• Number of parameters: 2
• Gradient recipe: None (uses finite difference)
Parameters: a (float) – displacement magnitude $$r=|\alpha|$$ phi (float) – phase angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class SqueezedState(r, phi, wires)[source]

Prepares a squeezed vacuum state.

Details:

• Number of wires: 1
• Number of parameters: 2
• Gradient recipe: None (uses finite difference)
Parameters: r (float) – squeezing magnitude phi (float) – squeezing angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class DisplacedSqueezedState(a, phi_a, r, phi_r, wires)[source]

Prepares a displaced squeezed vacuum state.

A displaced squeezed state is prepared by squeezing a vacuum state, and then applying a displacement operator,

$\ket{\alpha,z} = D(\alpha)\ket{0,z} = D(\alpha)S(z)\ket{0},$

with the displacement parameter $$\alpha=ae^{i\phi_a}$$ and the squeezing parameter $$z=re^{i\phi_r}$$.

Details:

• Number of wires: 1
• Number of parameters: 3
• Gradient recipe: None (uses finite difference)
Parameters: a (float) – displacement magnitude $$a=|\alpha|$$ phi_a (float) – displacement angle $$\phi_a$$ r (float) – squeezing magnitude $$r=|z|$$ phi_r (float) – squeezing angle $$\phi_r$$ wires (Sequence[int] or int) – the wire the operation acts on
class ThermalState(nbar, wires)[source]

Prepares a thermal state.

Details:

• Number of wires: 1
• Number of parameters: 1
• Gradient recipe: None (uses finite difference)
Parameters: nbar (float) – mean thermal population of the mode wires (Sequence[int] or int) – the wire the operation acts on
class GaussianState(r, V, wires)[source]

Prepare subsystems in a given Gaussian state.

Details:

• Number of wires: Any
• Number of parameters: 1
• Gradient recipe: None
Parameters: r (array) – a length $$2N$$ vector of means, of the form $$(\x_0,\dots,\x_{N-1},\p_0,\dots,\p_{N-1})$$ V (array) – the $$2N\times 2N$$ (real and positive definite) covariance matrix
class FockState(n, wires)[source]

Prepares a single Fock state.

Details:

• Number of wires: 1
• Number of parameters: 1
• Gradient recipe: None (not differentiable)
Parameters: n (int) – Fock state to prepare wires (Sequence[int] or int) – the wire the operation acts on
class FockStateVector(state, wires)[source]

Prepare subsystems using the given ket vector in the Fock basis.

Details:

• Number of wires: Any
• Number of parameters: 1
• Gradient recipe: None (uses finite difference)
Parameters: state (array) – a single ket vector, for single mode state preparation, or a multimode ket, with one array dimension per mode
class FockDensityMatrix(state, wires)[source]

Prepare subsystems using the given density matrix in the Fock basis.

Details:

• Number of wires: Any
• Number of parameters: 1
• Gradient recipe: None (uses finite difference)
Parameters: state (array) – a single mode matrix $$\rho_{ij}$$, or a multimode tensor $$\rho_{ij,kl,\dots,mn}$$, with two indices per mode
class CatState(a, phi, p, wires)[source]

Prepares a cat state.

A cat state is the coherent superposition of two coherent states,

$\ket{\text{cat}(\alpha)} = \frac{1}{N} (\ket{\alpha} +e^{ip\pi} \ket{-\alpha}),$

where $$\ket{\pm\alpha} = D(\pm\alpha)\ket{0}$$ are coherent states with displacement parameters $$\pm\alpha=\pm ae^{i\phi}$$ and $$N = \sqrt{2 (1+\cos(p\pi)e^{-2|\alpha|^2})}$$ is the normalization factor.

Details:

• Number of wires: 1
• Number of parameters: 3
• Gradient recipe: None (uses finite difference)
Parameters: a (float) – displacement magnitude $$a=|\alpha|$$ phi (float) – displacement angle $$\phi$$ p (float) – parity, where $$p=0$$ corresponds to an even cat state, and $$p=1$$ an odd cat state. wires (Sequence[int] or int) – the wire the operation acts on
class NumberOperator(wires)[source]

The photon number observable $$\langle \hat{n}\rangle$$.

The number operator is defined as $$\hat{n} = \a^\dagger \a = \frac{1}{2\hbar}(\x^2 +\p^2) -\I/2$$.

When used with the expval() function, the mean photon number $$\braket{\hat{n}}$$ is returned.

Details:

• Number of wires: 1

• Number of parameters: 0

• Observable 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
class X(wires)[source]

The position quadrature observable $$\hat{x}$$.

When used with the expval() function, the position expectation value $$\braket{\hat{n}}$$ is returned. This corresponds to the mean displacement in the phase space along the $$x$$ axis.

Details:

• Number of wires: 1

• Number of parameters: 0

• Observable 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
class P(wires)[source]

The momentum quadrature observable $$\hat{p}$$.

When used with the expval() function, the momentum expectation value $$\braket{\hat{p}}$$ is returned. This corresponds to the mean displacement in the phase space along the $$p$$ axis.

Details:

• Number of wires: 1

• Number of parameters: 0

• Observable 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
class QuadOperator(phi, wires)[source]

The generalized quadrature observable $$\x_\phi = \x cos\phi+\p\sin\phi$$.

When used with the expval() function, the expectation value $$\braket{\hat{\x_\phi}}$$ is returned. This corresponds to the mean displacement in the phase space along axis at angle $$\phi$$.

Details:

• Number of wires: 1

• Number of parameters: 1

• Observable 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 generalized quadrature observable wires (Sequence[int] or int) – the wire the operation acts on
class PolyXP(q, wires)[source]

An arbitrary 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 expectation values.

Details:

• Number of wires: Any
• Number of parameters: 1
• Observable order: 2nd order in the quadrature operators
• Heisenberg representation: $$A$$
Parameters: q (array[float]) – expansion coefficients
class FockStateProjector(n, wires)[source]

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 expval(FockStateProjector) is applied to a subset of wires, the unaffected wires are traced out prior to the expectation value calculation.

Details:

• Number of wires: Any
• Number of parameters: 1
• Observable 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 observable $$\ket{0,4,2}\bra{0,4,2}$$ acting on wires 0, 1, and 3 of a QNode, you would call FockStateProjector(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.