# sf.backends.BaseBosonicState¶

class BaseBosonicState(state_data, num_modes, num_weights, mode_names=None)[source]

Bases: strawberryfields.backends.states.BaseState

Class for the representation of quantum states as linear combinations of Gaussian functions in phase space.

Note that this class uses the basis ordering convention

$\bar{\mathbf{r}} = (\bar{x}_1,\bar{p}_1,\bar{x}_2,\bar{p}_2,\dots,\bar{x}_N,\bar{p}_N)$
Parameters
• state_data (tuple(means, covs, weights)) – A tuple containing the array of all the vectors of means, the array of all the covariance matrices, and the array of all the weights for the linear combination

• num_modes (int) – the number of modes in the state

• num_weights (int) – the number of terms in the linear combination

• mode_names (Sequence) – (optional) this argument contains a list providing mode names for each mode in the state

 EQ_TOLERANCE data Returns the underlying numerical (or symbolic) representation of the state. hbar Returns the value of $$\hbar$$ used in the generation of the state. is_pure Checks whether the state is a pure state. mode_indices Returns a dictionary mapping the mode names to mode indices. mode_names Returns a dictionary mapping the mode index to mode names. num_modes Gets the number of modes that the state represents.
EQ_TOLERANCE = 1e-10
data

Returns the underlying numerical (or symbolic) representation of the state. The form of this data differs for different backends.

hbar

Returns the value of $$\hbar$$ used in the generation of the state.

The value of $$\hbar$$ is a convention chosen in the definition of $$\x$$ and $$\p$$. See Operators for more details.

Returns

$$\hbar$$ value.

Return type

float

is_pure

Checks whether the state is a pure state.

Returns

True if and only if the state is pure.

Return type

bool

mode_indices

Returns a dictionary mapping the mode names to mode indices.

The mode names are determined from the initialization argument mode_names. If these were not supplied, the names are generated automatically based on the mode indices.

Returns

dictionary of the form {"mode name":i,...}

Return type

dict

mode_names

Returns a dictionary mapping the mode index to mode names.

The mode names are determined from the initialization argument mode_names. If these were not supplied, the names are generated automatically based on the mode indices.

Returns

dictionary of the form {i:"mode name",...}

Return type

dict

num_modes

Gets the number of modes that the state represents.

Returns

the number of modes in the state

Return type

int

 all_fock_probs(**kwargs) Probabilities of all possible Fock basis states for the current circuit state. The covariance matrices describing the Bosonic state on N modes. displacement([modes]) Returns the displacement parameter $$\alpha$$ of the modes specified. dm(**kwargs) The numerical density matrix for the quantum state in the Fock basis. fidelity(other_state, mode, **kwargs) Fidelity of the reduced state in the specified mode with a user supplied state. fidelity_coherent(alpha_list, **kwargs) Returns the fidelity to a coherent state. fidelity_vacuum(**kwargs) Returns the fidelity to the vacuum. fock_prob(n, **kwargs) Probability of a particular Fock basis state. ket(**kwargs) The numerical state vector for the quantum state in the Fock basis. marginal(mode, xvec[, phi]) Calculates the discretized marginal distribution of the specified mode along the $$x\cos\phi + p\sin\phi$$ quadrature. mean_photon(mode, **kwargs) Returns the mean photon number of a particular mode. The vectors of means describing the Bosonic state on N modes. number_expectation(modes) Calculates the expectation value of the product of the number operators of the modes. p_quad_values(mode, xvec, pvec) Calculates the discretized p-quadrature probability distribution of the specified mode. parity_expectation(modes) Returns the expectation value of the parity operator for modes. poly_quad_expectation(A[, d, k, phi]) The multi-mode expectation values and variance of arbitrary 2nd order polynomials of quadrature operators. Yields the purity of the state by calculating the integral over phase space of the Wigner function multiplied with itself. quad_expectation(mode[, phi]) The $$\x_{\phi}$$ operator expectation values and variance for the specified mode. reduced_bosonic(modes) Returns the weights, vectors of means and the covariance matrices of the specified modes. reduced_dm(modes, **kwargs) Returns a reduced density matrix in the Fock basis. The weights describing the Bosonic state. wigner(mode, xvec, pvec) Calculates the discretized Wigner function of the specified mode. x_quad_values(mode, xvec, pvec) Calculates the discretized x-quadrature probability distribution of the specified mode.
all_fock_probs(**kwargs)[source]

Probabilities of all possible Fock basis states for the current circuit state.

For example, in the case of 3 modes, this method allows the Fock state probability $$|\braketD{0,2,3}{\psi}|^2$$ to be returned via

probs = state.all_fock_probs()
probs[0,2,3]

Returns

array of dimension $$\underbrace{D\times D\times D\cdots\times D}_{\text{num modes}}$$

containing the Fock state probabilities, where $$D$$ is the Fock basis cutoff truncation

Return type

array

Keyword Arguments

cutoff (int) – Specifies where to truncate the computation (default value is 10). Note that the cutoff argument only applies for Gaussian representation; states represented in the Fock basis will use their own internal cutoff dimension.

covs()[source]

The covariance matrices describing the Bosonic state on N modes.

Returns

an array of shape (num_weights, 2N, 2N)

Return type

array[complex]

displacement(modes=None)[source]

Returns the displacement parameter $$\alpha$$ of the modes specified.

Parameters

modes (int or Sequence[int]) – modes specified

Returns

sequence of complex displacements $$\alpha$$ corresponding to the list of specified modes

Return type

Sequence[complex]

Raises

ValueError – if the phase space coordinate is complex-valued

dm(**kwargs)[source]

The numerical density matrix for the quantum state in the Fock basis.

Keyword Arguments

cutoff (int) – Specifies where to truncate the returned density matrix (default value is 10). Note that the cutoff argument only applies for Gaussian representation; states represented in the Fock basis will use their own internal cutoff dimension.

Returns

the numerical density matrix in the Fock basis

Return type

array

fidelity(other_state, mode, **kwargs)[source]

Fidelity of the reduced state in the specified mode with a user supplied state. Note that this method only supports single-mode states.

Parameters

other_state – a pure state vector array represented in the Fock basis (for Fock backends) or a Sequence (mu, cov) containing the means and covariance matrix (for Gaussian backends)

Returns

The fidelity of the circuit state with other_state.

fidelity_coherent(alpha_list, **kwargs)[source]

Returns the fidelity to a coherent state.

Parameters

alpha_list (array) – amplitudes for coherent states

Returns

fidelity of the state in modes to the coherent state alpha

Return type

float

fidelity_vacuum(**kwargs)[source]

Returns the fidelity to the vacuum.

Returns

fidelity of the state in modes to the vacuum

Return type

float

fock_prob(n, **kwargs)[source]

Probability of a particular Fock basis state.

Parameters

n (Sequence[int]) – the Fock state $$\ket{\vec{n}}$$ that we want to measure the probability of

Keyword Arguments

cutoff (int) – Specifies where to truncate the computation (default value is 10).

Returns

measurement probability

Return type

float

ket(**kwargs)[source]

The numerical state vector for the quantum state in the Fock basis. Note that if the state is mixed, this method returns None.

Keyword Arguments

cutoff (int) – Specifies where to truncate the returned density matrix (default value is 10). Note that the cutoff argument only applies for Gaussian representation; states represented in the Fock basis will use their own internal cutoff dimension.

Returns

the numerical state vector. Returns None if the state is mixed.

Return type

array/None

marginal(mode, xvec, phi=0)[source]

Calculates the discretized marginal distribution of the specified mode along the $$x\cos\phi + p\sin\phi$$ quadrature.

Parameters
• mode (int) – the mode to calculate the Wigner function for

• xvec (array) – array of discretized quadrature values

• phi (float) – $$phi$$ value for qudrature angle in phase space

Returns

1D array of size len(xvec), containing marginal distribution values.

Return type

array

Raises

ValueError – if mode is not an integer or out of range

mean_photon(mode, **kwargs)[source]

Returns the mean photon number of a particular mode.

Parameters

mode (int) – specifies the mode

Returns

the mean photon number and variance

Return type

tuple

Raises

ValueError – if the mean or the variance is complex

means()[source]

The vectors of means describing the Bosonic state on N modes.

Returns

an array of shape (num_weights, 2N)

Return type

array[complex]

number_expectation(modes)[source]

Calculates the expectation value of the product of the number operators of the modes.

This method computes the analytic expectation value $$\langle \hat{n}_{i_0} \hat{n}_{i_1}\dots \hat{n}_{i_{N-1}}\rangle$$ for a (sub)set of modes $$[i_0, i_1, \dots, i_{N-1}]$$ of the system.

Parameters

modes (list) – list of modes for which one wants the expectation of the product of their number operator.

Returns

the expectation value and variance

Return type

tuple[float, float]

Example

Consider the following program:

prog = sf.Program(3)

with prog.context as q:
ops.Sgate(0.5) | q[0]
ops.Sgate(0.5) | q[1]
ops.Sgate(0.5) | q[2]
ops.BSgate(np.pi/3, 0.1) |  (q[0], q[1])
ops.BSgate(np.pi/3, 0.1) |  (q[1], q[2])


Executing this on the Fock backend,

>>> eng = sf.Engine("fock", backend_options={"cutoff_dim": 10})
>>> state = eng.run(prog).state


we can compute the expectation value $$\langle \hat{n}_0\hat{n}_2\rangle$$:

>>> state.number_expectation([0, 2])[0]
0.07252895071309405


Executing the same program on the Gaussian backend,

>>> eng = sf.Engine("gaussian")
>>> state = eng.run(prog).state
>>> state.number_expectation([0, 2])[0]
0.07566984755267293


This slight difference in value compared to the result from the Fock backend above is due to the finite Fock basis truncation when using the Fock backend; this can be avoided by increasing the value of cutoff_dim.

Warning

This method only supports at most two modes in the Gaussian backend.

p_quad_values(mode, xvec, pvec)

Calculates the discretized p-quadrature probability distribution of the specified mode.

Parameters
• mode (int) – the mode to calculate the p-quadrature probability values of

• xvec (array) – array of discretized $$x$$ quadrature values

• pvec (array) – array of discretized $$p$$ quadrature values

Returns

1D array of size len(pvec), containing reduced p-quadrature probability values for a specified range of x and p.

Return type

array

parity_expectation(modes)[source]

Returns the expectation value of the parity operator for modes.

Parameters

mode (list) – the requested modes

Returns

parity of the modes

Return type

float

Raises

ValueError – if any modes are duplicated

poly_quad_expectation(A, d=None, k=0, phi=0, **kwargs)[source]

The multi-mode expectation values and variance of arbitrary 2nd order polynomials of quadrature operators.

An arbitrary 2nd order polynomial of quadrature operators over $N$ modes can always be written in the following form:

$P(\mathbf{r}) = \mathbf{r}^T A\mathbf{r} + \mathbf{r}^T \mathbf{d} + k I$

where:

• $$A\in\mathbb{R}^{2N\times 2N}$$ is a symmetric matrix representing the quadratic coefficients,

• $$\mathbf{d}\in\mathbb{R}^{2N}$$ is a real vector representing the linear coefficients,

• $$k\in\mathbb{R}$$ represents the constant term, and

• $$\mathbf{r} = (\x_1,\dots,\x_N,\p_1,\dots,\p_N)$$ is the vector of quadrature operators in $$xp$$-ordering.

This method returns the expectation value of this second-order polynomial,

$\langle P(\mathbf{r})\rangle,$

as well as the variance

$\Delta P(\mathbf{r})^2 = \braket{P(\mathbf{r})^2} - \braket{P(\mathbf{r})}^2$
Parameters
• A (array) – a real symmetric 2Nx2N NumPy array, representing the quadratic coefficients of the second order quadrature polynomial.

• d (array) – a real length-2N NumPy array, representing the linear coefficients of the second order quadrature polynomial. Defaults to the zero vector.

• k (float) – the constant term. Default 0.

• phi (float) – quadrature angle, clockwise from the positive $$x$$ axis. If provided, the vectori of quadrature operators $$\mathbf{r}$$ is first rotated by angle $$\phi$$ in the phase space.

Returns

expectation value and variance

Return type

tuple (float, float)

purity()[source]

Yields the purity of the state by calculating the integral over phase space of the Wigner function multiplied with itself.

Returns

float: purity of the state

quad_expectation(mode, phi=0, **kwargs)[source]

The $$\x_{\phi}$$ operator expectation values and variance for the specified mode.

The $$\x_{\phi}$$ operator is defined as follows,

$\x_{\phi} = \cos\phi~\x + \sin\phi~\p$

with corresponding expectation value

$\bar{x_{\phi}}=\langle x_{\phi}\rangle = \text{Tr}(\x_{\phi}\rho_{mode})$

and variance

$\Delta x_{\phi}^2 = \langle x_{\phi}^2\rangle - \braket{x_{\phi}}^2$
Parameters
• mode (int) – the requested mode

• phi (float) –

quadrature angle, clockwise from the positive $$x$$ axis.

• $$\phi=0$$ corresponds to the $$x$$ expectation and variance (default)

• $$\phi=\pi/2$$ corresponds to the $$p$$ expectation and variance

Returns

expectation value and variance

Return type

tuple (float, float)

reduced_bosonic(modes)[source]

Returns the weights, vectors of means and the covariance matrices of the specified modes.

Parameters

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

Returns

(weights, means, cov) where weights is an array for the coefficients in the

linear combination, means is an array containing the vectors of means, and covs is an array containing the covariance matrices

Return type

tuple

Raises

ValueError – if modes are duplicated or out of range

reduced_dm(modes, **kwargs)[source]

Returns a reduced density matrix in the Fock basis.

Parameters

modes (int or Sequence[int]) – specifies the mode(s) to return the reduced density matrix for.

Keyword Arguments

cutoff (int) – Specifies where to truncate the returned density matrix (default value is 10).

Returns

the reduced density matrix for the specified modes

Return type

array

Raises

ValueError – if any modes are duplicated or out of range

weights()[source]

The weights describing the Bosonic state.

Returns

an array of length num_weights

Return type

array

wigner(mode, xvec, pvec)[source]

Calculates the discretized Wigner function of the specified mode.

Parameters
• mode (int) – the mode to calculate the Wigner function for

• xvec (array) – array of discretized $$x$$ quadrature values

• pvec (array) – array of discretized $$p$$ quadrature values

Returns

2D array of size [len(xvec), len(pvec)], containing reduced Wigner function values for specified x and p values.

Return type

array

Raises

ValueError – if mode is not an integer or out of range

x_quad_values(mode, xvec, pvec)

Calculates the discretized x-quadrature probability distribution of the specified mode.

Parameters
• mode (int) – the mode to calculate the x-quadrature probability values of

• xvec (array) – array of discretized $$x$$ quadrature values

• pvec (array) – array of discretized $$p$$ quadrature values

Returns

1D array of size len(xvec), containing reduced x-quadrature probability values for a specified range of x and p.

Return type

array