Default Gaussian plugin

Module name: pennylane.plugins.default_gaussian

Short name: "default.gaussian"

The default.gaussian plugin is meant to be used as a template for writing PennyLane device plugins for new CV backends.

It implements the necessary Device methods as well as all built-in continuous-variable Gaussian operations, and provides a very simple simulation of a Gaussian-based quantum circuit architecture.

The following is the technical documentation of the implementation of the plugin. You will not need to read and understand this to use this plugin.

Auxillary functions

partitions(s[, include_singles])

Partitions a sequence into all groupings of pairs and singles of elements.

fock_prob(mu, cov, event[, hbar])

Returns the probability of detection of a particular PNR detection event.

Gates and operations

rotation(phi)

Rotation in the phase space.

displacement(state, wire, alpha[, hbar])

Displacement in the phase space.

squeezing(r, phi)

Squeezing in the phase space.

quadratic_phase(s)

Quadratic phase shift.

beamsplitter(theta, phi)

Beamsplitter.

two_mode_squeezing(r, phi)

Two-mode squeezing.

controlled_addition(s)

CX gate.

controlled_phase(s)

CZ gate.

interferometer(U)

Interferometer

State preparation

squeezed_cov(r, phi[, hbar])

Returns the squeezed covariance matrix of a squeezed state.

vacuum_state(wires[, hbar])

Returns the vacuum state.

coherent_state(a[, phi, hbar])

Returns a coherent state.

squeezed_state(r, phi[, hbar])

Returns a squeezed state.

displaced_squeezed_state(a, phi_a, r, phi_r)

Returns a squeezed coherent state

thermal_state(nbar[, hbar])

Returns a thermal state.

gaussian_state(mu, cov[, hbar])

Returns a Gaussian state.

set_state(state, wire, mu, cov)

Inserts a single mode Gaussian into the state representation of the complete system.

Observables

photon_number(mu, cov, wires, params, …[, …])

Calculates the mean photon number for a given one-mode state.

homodyne([phi])

Function factory that returns the Homodyne expectation of a one mode state.

poly_quad_expectations(mu, cov, wires, …)

Calculates the expectation and variance for an arbitrary polynomial of quadrature operators.

fock_expectation(mu, cov, wires, params, …)

Calculates the expectation and variance of a Fock state probability.

Classes

DefaultGaussian(wires, *[, shots, hbar, …])

Default Gaussian device for PennyLane.

Code details

partitions(s, include_singles=True)[source]

Partitions a sequence into all groupings of pairs and singles of elements.

Parameters
  • s (sequence) – the sequence to partition

  • include_singles (bool) – if False, only partitions into pairs is returned.

Returns

returns a nested tuple, containing all partitions of the sequence.

Return type

tuple

fock_prob(mu, cov, event, hbar=2.0)[source]

Returns the probability of detection of a particular PNR detection event.

For more details, see:

Parameters
  • mu (array) – length-\(2N\) means vector

  • cov (array) – \(2N\times 2N\) covariance matrix

  • event (array) – length-\(N\) array of non-negative integers representing the PNR detection event of the multi-mode system.

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\).

Returns

probability of detecting the event

Return type

float

rotation(phi)[source]

Rotation in the phase space.

Parameters

phi (float) – rotation parameter

Returns

symplectic transformation matrix

Return type

array

displacement(state, wire, alpha, hbar=2)[source]

Displacement in the phase space.

Parameters
  • state (tuple) – contains means vector and covariance matrix

  • wire (int) – wire that the displacement acts on

  • alpha (float) – complex displacement

Returns

contains the vector of means and covariance matrix

Return type

tuple

squeezing(r, phi)[source]

Squeezing in the phase space.

Parameters
  • r (float) – squeezing magnitude

  • phi (float) – rotation parameter

Returns

symplectic transformation matrix

Return type

array

quadratic_phase(s)[source]

Quadratic phase shift.

Parameters

s (float) – gate parameter

Returns

symplectic transformation matrix

Return type

array

beamsplitter(theta, phi)[source]

Beamsplitter.

Parameters
  • theta (float) – transmittivity angle (\(t=\cos\theta\))

  • phi (float) – phase angle (\(r=e^{i\phi}\sin\theta\))

Returns

symplectic transformation matrix

Return type

array

two_mode_squeezing(r, phi)[source]

Two-mode squeezing.

Parameters
  • r (float) – squeezing magnitude

  • phi (float) – rotation parameter

Returns

symplectic transformation matrix

Return type

array

controlled_addition(s)[source]

CX gate.

Parameters

s (float) – gate parameter

Returns

symplectic transformation matrix

Return type

array

controlled_phase(s)[source]

CZ gate.

Parameters

s (float) – gate parameter

Returns

symplectic transformation matrix

Return type

array

interferometer(U)[source]

Interferometer

Parameters

U (array) – unitary matrix

Returns

symplectic transformation matrix

Return type

array

squeezed_cov(r, phi, hbar=2)[source]

Returns the squeezed covariance matrix of a squeezed state.

Parameters
  • r (float) – the squeezing magnitude

  • p (float) – the squeezing phase \(\phi\)

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the squeezed state

Return type

array

vacuum_state(wires, hbar=2.0)[source]

Returns the vacuum state.

Parameters
  • basis (str) – Returns the vector of means and the covariance matrix

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the vacuum state

Return type

array

coherent_state(a, phi=0, hbar=2.0)[source]

Returns a coherent state.

Parameters
  • a (complex) – the displacement

  • phi (float) – the phase

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the coherent state

Return type

array

squeezed_state(r, phi, hbar=2.0)[source]

Returns a squeezed state.

Parameters
  • r (float) – the squeezing magnitude

  • phi (float) – the squeezing phase \(\phi\)

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the squeezed state

Return type

array

displaced_squeezed_state(a, phi_a, r, phi_r, hbar=2.0)[source]

Returns a squeezed coherent state

Parameters
  • a (real) – the displacement magnitude

  • phi_a (real) – the displacement phase

  • r (float) – the squeezing magnitude

  • phi_r (float) – the squeezing phase \(\phi_r\)

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the squeezed coherent state

Return type

array

thermal_state(nbar, hbar=2.0)[source]

Returns a thermal state.

Parameters
  • nbar (float) – the mean photon number

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the thermal state

Return type

array

gaussian_state(mu, cov, hbar=2.0)[source]

Returns a Gaussian state.

This is simply a bare wrapper function, since the means vector and covariance matrix can be passed via the parameters unchanged.

Note that both the means vector and covariance matrix should be in \((\x_1,\dots, \x_N, \p_1, \dots, \p_N)\) ordering.

Parameters
  • mu (array) – vector means. Must be length-\(2N\), where N is the number of modes

  • cov (array) – covariance matrix. Must be dimension \(2N\times 2N\), where N is the number of modes

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the mean and covariance matrix of the Gaussian state

Return type

tuple

set_state(state, wire, mu, cov)[source]

Inserts a single mode Gaussian into the state representation of the complete system.

Parameters
  • state (tuple) – contains means vector and covariance matrix of existing state

  • wire (int) – wire corresponding to the new Gaussian state

  • mu (array) – vector of means to insert

  • cov (array) – covariance matrix to insert

Returns

contains the vector of means and covariance matrix.

Return type

tuple

photon_number(mu, cov, wires, params, total_wires, hbar=2.0)[source]

Calculates the mean photon number for a given one-mode state.

Parameters
  • mu (array) – length-2 vector of means

  • cov (array) – \(2\times 2\) covariance matrix

  • wires (Sequence[int]) – wires to calculate the expectation for

  • params (None) – no parameters are used for this expectation value

  • total_wires (int) – total number of wires in the system

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

contains the photon number expectation and variance

Return type

tuple

homodyne(phi=None)[source]

Function factory that returns the Homodyne expectation of a one mode state.

Parameters

phi (float) – the default phase space axis to perform the Homodyne measurement

Returns

A function that accepts a single mode means vector, covariance matrix, and phase space angle phi, and returns the quadrature expectation value and variance.

Return type

function

poly_quad_expectations(mu, cov, wires, params, total_wires, hbar=2.0)[source]

Calculates the expectation and variance for an arbitrary polynomial of quadrature operators.

Parameters
  • mu (array) – vector of means

  • cov (array) – covariance matrix

  • wires (Sequence[int]) – wires to calculate the expectation for

  • params (array) – a \((2N+1)\times (2N+1)\) array containing the linear and quadratic coefficients of the quadrature operators \((\I, \x_0, \p_0, \x_1, \p_1,\dots)\)

  • total_wires (int) – total number of wires in the system

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the mean and variance of the quadrature-polynomial observable

Return type

tuple

fock_expectation(mu, cov, wires, params, total_wires, hbar=2.0)[source]

Calculates the expectation and variance of a Fock state probability.

Parameters
  • mu (array) – length-\(2N\) vector of means

  • cov (array) – \(2N\times 2N\) covariance matrix

  • wires (Sequence[int]) – wires to calculate the expectation for

  • params (Sequence[int]) – the Fock state to return the expectation value for

  • total_wires (int) – total number of wires in the system

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

Returns

the Fock state expectation and variance

Return type

tuple

identity(*_, **__)[source]

Returns 1.

Returns

the Fock state expectation and variance

Return type

tuple

class DefaultGaussian(wires, *, shots=1000, hbar=2, analytic=True)[source]

Default Gaussian device for PennyLane.

Parameters
  • wires (int) – the number of modes to initialize the device in

  • shots (int) – How many times the circuit should be evaluated (or sampled) to estimate the expectation values. If analytic == True, then the number of shots is ignored in the calculation of expectation values and variances, and only controls the number of samples returned by sample.

  • hbar (float) – (default 2) the value of \(\hbar\) in the commutation relation \([\x,\p]=i\hbar\)

  • analytic (bool) – indicates if the device should calculate expectations and variances analytically

pre_apply()[source]

Called during execute() before the individual operations are executed.

apply(operation, wires, par)[source]

Apply a quantum operation.

For plugin developers: this function should apply the operation on the device.

Parameters
  • operation (str) – name of the operation

  • wires (Sequence[int]) – subsystems the operation is applied on

  • par (tuple) – parameters for the operation

expand(S, wires)[source]

Expands a Symplectic matrix S to act on the entire subsystem.

Parameters
  • S (array) – a \(2M\times 2M\) Symplectic matrix

  • wires (Sequence[int]) – the wires of the modes that S acts on

Returns

the resulting \(2N\times 2N\) Symplectic matrix

Return type

array

expval(observable, wires, par)[source]

Returns the expectation value of observable on specified wires.

Parameters
  • observable (str) – name of the observable

  • wires (Sequence[int]) – target subsystems

  • par (tuple[float]) – parameter values

Returns

expectation value \(\expect{A} = \bra{\psi}A\ket{\psi}\)

Return type

float

var(observable, wires, par)[source]

Returns the variance of observable on specified wires.

Parameters
  • observable (str) – name of the observable

  • wires (Sequence[int]) – target subsystems

  • par (tuple[float]) – parameter values

Returns

variance \(\mathrm{var}(A) = \bra{\psi}A^2\ket{\psi} - \bra{\psi}A\ket{\psi}^2\)

Return type

float

sample(observable, wires, par)[source]

Return a sample of an observable.

Note

The default.gaussian plugin only supports sampling from X, P, and QuadOperator observables.

Parameters
  • observable (str) – name of the observable

  • wires (Sequence[int]) – subsystems the observable is to be measured on

  • par (tuple) – parameters for the observable

Returns

samples in an array of dimension (n, num_wires)

Return type

array[float]

reset()[source]

Reset the device

reduced_state(wires)[source]

Returns the vector of means and the covariance matrix of the specified wires.

Parameters

wires (int of Sequence[int]) – indices of the requested wires

Returns

means is an array containing the vector of means, and cov is a square array containing the covariance matrix

Return type

tuple (means, cov)

property operations

Get the supported set of operations.

Returns

the set of PennyLane operation names the device supports

Return type

set[str]

property observables

Get the supported set of observables.

Returns

the set of PennyLane observable names the device supports

Return type

set[str]