# sf.backends.BosonicBackend¶

class BosonicBackend[source]

Bases: strawberryfields.backends.base.BaseBosonic

The BosonicBackend implements a simulation of quantum optical circuits in NumPy by representing states as linear combinations of Gaussian functions in phase space., returning a BosonicState state object.

The primary component of the BosonicBackend is a BosonicModes object which is used to simulate a multi-mode quantum optical system. BosonicBackend provides the basic API-compatible interface to the simulator, while the BosonicModes object actually carries out the mathematical simulation.

The BosonicModes simulators maintain internal sets of weights, means and covariance matrices for all the Gaussian functions in the linear combination. Note these can be complex-valued quantities.

circuit_spec = None
compiler = 'bosonic'
short_name = 'bosonic'
 add_mode([n]) Adds new modes to the circuit each with a number of Gaussian peaks specified by peaks. beamsplitter(theta, phi, mode1, mode2) Apply the beamsplitter operation to the specified modes. begin_circuit(num_subsystems, **kwargs) Instantiate a quantum circuit. cross_kerr_interaction(kappa, mode1, mode2) cubic_phase(gamma, mode) del_mode(modes) Delete modes from the circuit. displacement(r, phi, mode) Apply the displacement operation to the specified mode. gaussian_cptp(modes, X[, Y]) Transforms the state according to a deterministic Gaussian CPTP map. Return a list of the active modes for the circuit. init_circuit(prog) Instantiate the circuit and initialize weights, means, and covs depending on the Preparation classes. is_vacuum([tol]) Test whether the current circuit state is vacuum (up to given tolerance). kerr_interaction(kappa, mode) loss(T, mode) Perform a loss channel operation on the specified mode. mb_squeeze_avg(mode, r, phi, r_anc, eta_anc) Squeeze mode by the amount $$re^{i\phi}$$ using measurement-based squeezing. mb_squeeze_single_shot(mode, r, phi, r_anc, …) Squeeze mode by the amount $$re^{i\phi}$$ using measurement-based squeezing. measure_fock(modes[, shots, select]) Measure the given modes in the Fock basis. measure_heterodyne(mode[, shots, select]) Perform a heterodyne measurement on the given mode. measure_homodyne(phi, mode[, shots, select]) Measure a phase space quadrature of the given mode. measure_threshold(modes[, shots, select]) Measure the given modes in the thresholded Fock basis, i.e., zero or nonzero photons). mzgate(phi_in, phi_ex, mode1, mode2) Apply the Mach-Zehnder interferometer operation to the specified modes. passive(T, modes) Perform an arbitrary multimode passive operation prepare_cat(alpha, phi, representation, …) Prepares the arrays of weights, means and covs for a cat state: prepare_cat_real_rep(alpha, phi, ampl_cutoff, D) Prepares the arrays of weights, means and covs for a cat state: prepare_coherent_state(r, phi, mode) Prepare a coherent state in the specified mode. prepare_displaced_squeezed_state(r_d, phi_d, …) Prepare a displaced squeezed state in the specified mode. prepare_dm_state(state, mode) prepare_fock(n[, r]) Prepares the arrays of weights, means and covs of a Fock state. prepare_fock_state(n, mode) prepare_gaussian_state(r, V, modes) Prepare a Gaussian state. prepare_gkp(state, epsilon, ampl_cutoff[, …]) Prepares the arrays of weights, means and covs for a finite energy GKP state. prepare_ket_state(state, mode) prepare_squeezed_state(r, phi, mode) Prepare a squeezed vacuum state in the specified mode. prepare_thermal_state(nbar, mode) Prepare a thermal state in the specified mode. Prepare the vacuum state in the specified mode. reset([pure]) Reset the circuit so that all the modes are in the vacuum state. rotation(phi, mode) Apply the phase-space rotation operation to the specified mode. run_prog(prog, **kwargs) Runs a strawberryfields program using the bosonic backend. squeeze(r, phi, mode) Apply the squeezing operation to the specified mode. state([modes]) Returns the state of the quantum simulation. supports(name) Check whether the backend supports the given operating mode. thermal_loss(T, nbar, mode) Perform a thermal loss channel operation on the specified mode.
add_mode(n=1, **kwargs)[source]

Adds new modes to the circuit each with a number of Gaussian peaks specified by peaks.

Parameters

n (int) – number of new modes to add

Keyword Arguments

peaks (list) – number of Gaussian peaks for each new mode

Raises

ValueError – if the length of the list of peaks is different than the number of modes

beamsplitter(theta, phi, mode1, mode2)[source]

Apply the beamsplitter operation to the specified modes.

Parameters
• theta (float) – transmissivity is cos(theta)

• phi (float) – phase angle

• mode1 (int) – first mode that beamsplitter acts on

• mode2 (int) – second mode that beamsplitter acts on

begin_circuit(num_subsystems, **kwargs)[source]

Instantiate a quantum circuit.

Instantiates a representation of a quantum optical state with num_subsystems modes. The state is initialized to vacuum.

The modes in the circuit are indexed sequentially using integers, starting from zero. Once an index is assigned to a mode, it can never be re-assigned to another mode. If the mode is deleted its index becomes invalid. An operation acting on an invalid or unassigned mode index raises an IndexError exception.

Parameters

num_subsystems (int) – number of modes in the circuit

Keyword Arguments
• cutoff_dim (int) – Hilbert space truncation dimension (for Fock basis backends only)

• batch_size (int) – (optional) batch-axis dimension, enables batched operation if > 1 (for the TF backend only)

cross_kerr_interaction(kappa, mode1, mode2)
cubic_phase(gamma, mode)
del_mode(modes)[source]

Delete modes from the circuit.

The deleted modes are traced out. As a result the state may have to be described using a density matrix.

The indices of the deleted modes become invalid for the lifetime of the circuit object. They will never be reassigned to other modes. Deleting a mode that has already been deleted raises an IndexError exception.

Parameters

modes (Sequence[int]) – mode numbers to delete

displacement(r, phi, mode)[source]

Apply the displacement operation to the specified mode.

Parameters
• r (float) – displacement amplitude

• phi (float) – displacement angle

• mode (int) – which mode to apply the displacement to

gaussian_cptp(modes, X, Y=None)[source]

Transforms the state according to a deterministic Gaussian CPTP map.

Parameters
• modes (list) – list of modes on which (X,Y) act

• X (array) – matrix for multiplicative part of transformation

• Y (array) – matrix for additive part of transformation

get_cutoff_dim()
get_modes()[source]

Return a list of the active modes for the circuit.

A mode is active if it has been created and has not been deleted.

Returns

sorted list of active (assigned, not invalid) mode indices

Return type

list[int]

init_circuit(prog)[source]

Instantiate the circuit and initialize weights, means, and covs depending on the Preparation classes.

Parameters

prog (object) – Program instance

Raises
• NotImplementedError – if Ket or DensityMatrix preparation used

• CircuitError – if any of the parameters for non-Gaussian state preparation are symbolic

is_vacuum(tol=1e-10, **kwargs)[source]

Test whether the current circuit state is vacuum (up to given tolerance).

Returns True iff $$|\bra{0} \rho \ket{0} -1| \le$$ tol, i.e., the fidelity of the current circuit state with the vacuum state is within the given tolerance from 1.

Parameters

tol (float) – numerical tolerance

Returns

True iff current state is vacuum up to tolerance tol

Return type

bool

kerr_interaction(kappa, mode)
loss(T, mode)[source]

Perform a loss channel operation on the specified mode.

Parameters
• T (float) – loss parameter, $$0\leq T\leq 1$$.

• mode (int) – index of mode where operation is carried out

mb_squeeze_avg(mode, r, phi, r_anc, eta_anc)[source]

Squeeze mode by the amount $$re^{i\phi}$$ using measurement-based squeezing.

Here, the average, deterministic Gaussian CPTP map is applied.

Parameters
• mode (int) – mode to be squeezed

• r (float) – target squeezing magnitude

• phi (float) – target squeezing phase

• r_anc (float) – squeezing magnitude of the ancillary mode

• eta_anc (float) – detection efficiency of the ancillary mode

mb_squeeze_single_shot(mode, r, phi, r_anc, eta_anc)[source]

Squeeze mode by the amount $$re^{i\phi}$$ using measurement-based squeezing.

Here, the single-shot map is applied, returning the ancillary measurement outcome.

Parameters
• mode (int) – mode to be squeezed

• r (float) – target squeezing magnitude

• phi (float) – target squeezing phase

• r_anc (float) – squeezing magnitude of the ancillary mode

• eta_anc (float) – detection efficiency of the ancillary mode

Returns

the measurement outcome of the ancilla

Return type

float

measure_fock(modes, shots=1, select=None, **kwargs)[source]

Measure the given modes in the Fock basis.

Note

When shots == 1, updates the current system state to the conditional state of that measurement result. When shots > 1, the system state is not updated.

Parameters
• modes (Sequence[int]) – which modes to measure

• shots (int) – number of measurement samples to obtain

• select (None or Sequence[int]) – If not None: desired values of the measurement results. Enables post-selection on specific measurement results instead of random sampling. len(select) == len(modes) is required.

Returns

measurement results

Return type

tuple[int]

measure_heterodyne(mode, shots=1, select=None)[source]

Perform a heterodyne measurement on the given mode.

Updates the current state of the circuit such that the measured mode is reset to the vacuum state.

Parameters
• mode (int) – which mode to measure

• shots (int) – number of measurement samples to obtain

• select (None or complex) – If not None: desired value of the measurement result. Enables post-selection on specific measurement results instead of random sampling.

Returns

measured value

Return type

complex

measure_homodyne(phi, mode, shots=1, select=None, **kwargs)[source]

Measure a phase space quadrature of the given mode.

For the measured mode, samples the probability distribution $$f(q) = \bra{q_\phi} \rho \ket{q_\phi}$$ and returns the sampled value. Here $$\ket{q_\phi}$$ is the eigenstate of the operator

$\hat{q}_\phi = \sqrt{2/\hbar}(\cos(\phi)\hat{x} +\sin(\phi)\hat{p}) = e^{-i\phi} \hat{a} +e^{i\phi} \hat{a}^\dagger.$

Note

This method is $$\hbar$$ independent. The returned values can be converted to conventional position/momentum eigenvalues by multiplying them with $$\sqrt{\hbar/2}$$.

Updates the current state such that the measured mode is reset to the vacuum state. This is because we cannot represent exact position or momentum eigenstates in any of the backends, and experimentally the photons are destroyed in a homodyne measurement.

Parameters
• phi (float) – phase angle of the quadrature to measure (x: $$\phi=0$$, p: $$\phi=\pi/2$$)

• mode (int) – which mode to measure

• shots (int) – number of measurement samples to obtain

• select (None or float) – If not None: desired value of the measurement result. Enables post-selection on specific measurement results instead of random sampling.

Keyword arguments can be used to pass additional parameters to the backend. Options for such arguments will be documented in the respective subclasses.

Returns

measured value

Return type

float

measure_threshold(modes, shots=1, select=None, **kwargs)[source]

Measure the given modes in the thresholded Fock basis, i.e., zero or nonzero photons).

Note

When :code:shots == 1, updates the current system state to the conditional state of that measurement result. When :code:shots > 1, the system state is not updated.

Parameters
• modes (Sequence[int]) – which modes to measure

• shots (int) – number of measurement samples to obtain

• select (None or Sequence[int]) – If not None: desired values of the measurement results. Enables post-selection on specific measurement results instead of random sampling. len(select) == len(modes) is required.

Returns

measurement results

Return type

tuple[int]

mzgate(phi_in, phi_ex, mode1, mode2)

Apply the Mach-Zehnder interferometer operation to the specified modes.

Parameters
• phi_in (float) – internal phase

• phi_ex (float) – external phase

• mode1 (int) – first mode that MZ interferometer acts on

• mode2 (int) – second mode that MZ interferometer acts on

passive(T, modes)

Perform an arbitrary multimode passive operation

Parameters
• T (array) – an NxN matrix acting on a N mode state

• modes (int or Sequence[int]) – Which modes to prepare the state in.

Acts the following transformation on the state:

$a^{\dagger}_i \to \sum_j T_{ij} a^{\dagger}_j$
prepare_cat(alpha, phi, representation, ampl_cutoff, D)[source]

Prepares the arrays of weights, means and covs for a cat state:

$$\ket{\text{cat}(\alpha)} = \frac{1}{N} (\ket{\alpha} +e^{i\phi\pi} \ket{-\alpha})$$.

Parameters
• alpha (complex) – alpha value of cat state

• phi (float) – phi value of cat state

• representation (str) – whether to use the 'real' or 'complex' representation

• ampl_cutoff (float) – if using the 'real' representation, this determines how many terms to keep

• D (float) – for 'real' representation, quality parameter of approximation

Returns

arrays of the weights, means and covariances for the state

Return type

tuple

prepare_cat_real_rep(alpha, phi, ampl_cutoff, D)[source]

Prepares the arrays of weights, means and covs for a cat state:

$$\ket{\text{cat}(\alpha)} = \frac{1}{N} (\ket{\alpha} +e^{i\phi\pi} \ket{-\alpha})$$.

For this representation, weights, means and covariances are real-valued.

Parameters
• alpha (complex) – alpha value of cat state

• phi (float) – phi value of cat state

• ampl_cutoff (float) – this determines how many terms to keep

• D (float) – quality parameter of approximation

Returns

arrays of the weights, means and covariances for the state

Return type

tuple

prepare_coherent_state(r, phi, mode)[source]

Prepare a coherent state in the specified mode.

The requested mode is traced out and replaced with the coherent state $$\ket{r e^{i\phi}}$$. As a result the state may have to be described using a density matrix.

Parameters
• r (float) – coherent state displacement amplitude

• phi (float) – coherent state displacement phase

• mode (int) – which mode to prepare the coherent state in

prepare_displaced_squeezed_state(r_d, phi_d, r_s, phi_s, mode)[source]

Prepare a displaced squeezed state in the specified mode.

The requested mode is traced out and replaced with the displaced squeezed state $$\ket{\alpha, z}$$, where $$\alpha=r_d e^{i\phi_d}$$ and $$z=r_s e^{i\phi_s}$$. As a result the state may have to be described using a density matrix.

Parameters
• r_d (float) – displacement amplitude

• phi_d (float) – displacement angle

• r_s (float) – squeezing amplitude

• phi_s (float) – squeezing angle

• mode (int) – which mode to prepare the squeezed state in

prepare_dm_state(state, mode)
prepare_fock(n, r=0.05)[source]

Prepares the arrays of weights, means and covs of a Fock state.

Parameters
• n (int) – photon number

• r (float) – quality parameter for the approximation

Returns

arrays of the weights, means and covariances for the state

Return type

tuple

Raises

ValueError – if $$1/r^2$$ is less than $$n$$

prepare_fock_state(n, mode)
prepare_gaussian_state(r, V, modes)[source]

Prepare a Gaussian state.

Note the different basis-ordering from the GaussianBackend.

The specified modes are traced out and replaced with a Gaussian state provided via a vector of means and a covariance matrix.

Parameters
• r (array) – vector of means in $$(x_1,p_1,x_2,p_2,\dots)$$ ordering

• V (array) – covariance matrix in $$(x_1,p_1,x_2,p_2,\dots)$$ ordering

• modes (int or Sequence[int]) – Which modes to prepare the state in. If the modes are not sorted, this is taken into account when preparing the state. I.e., when a two mode state is prepared with modes=[3,1], the first mode of the given state goes into mode 3 and the second mode goes into mode 1.

Raises

ValueError – if the shapes of r or V do not match the number of modes.

prepare_gkp(state, epsilon, ampl_cutoff, representation='real', shape='square')[source]

Prepares the arrays of weights, means and covs for a finite energy GKP state.

GKP states are qubits, with the qubit state defined by:

$$\ket{\psi}_{gkp} = \cos\frac{\theta}{2}\ket{0}_{gkp} + e^{-i\phi}\sin\frac{\theta}{2}\ket{1}_{gkp}$$

where the computational basis states are $$\ket{\mu}_{gkp} = \sum_{n} \ket{(2n+\mu)\sqrt{\pi\hbar}}_{q}$$.

Parameters
• state (list) – [theta,phi] for qubit definition above

• epsilon (float) – finite energy parameter of the state

• ampl_cutoff (float) – this determines how many terms to keep

• representation (str) – 'real' or 'complex' reprsentation

• shape (str) – shape of the lattice; default ‘square’

Returns

arrays of the weights, means and covariances for the state

Return type

tuple

Raises

NotImplementedError – if the complex representation or a non-square lattice is attempted

prepare_ket_state(state, mode)
prepare_squeezed_state(r, phi, mode)[source]

Prepare a squeezed vacuum state in the specified mode.

The requested mode is traced out and replaced with the squeezed state $$\ket{z}$$, where $$z=re^{i\phi}$$. As a result the state may have to be described using a density matrix.

Parameters
• r (float) – squeezing amplitude

• phi (float) – squeezing angle

• mode (int) – which mode to prepare the squeezed state in

prepare_thermal_state(nbar, mode)[source]

Prepare a thermal state in the specified mode.

The requested mode is traced out and replaced with the thermal state $$\rho(nbar)$$. As a result the state may have to be described using a density matrix.

Parameters
• nbar (float) – thermal population (mean photon number) of the mode

• mode (int) – which mode to prepare the thermal state in

prepare_vacuum_state(mode)[source]

Prepare the vacuum state in the specified mode.

The requested mode is traced out and replaced with the vacuum state. As a result the state may have to be described using a density matrix.

Parameters

mode (int) – which mode to prepare the vacuum state in

reset(pure=True, **kwargs)[source]

Reset the circuit so that all the modes are in the vacuum state.

After the reset the circuit is in the same state as it was after the last begin_circuit() call. It will have the original number of modes, all initialized in the vacuum state. Some circuit parameters may be changed during the reset, see the keyword args below.

Parameters

pure (bool) – if True, initialize the circuit in a pure state representation (will use a mixed state representation if pure is False)

Keyword Arguments

cutoff_dim (int) – new Hilbert space truncation dimension (for Fock basis backends only)

rotation(phi, mode)[source]

Apply the phase-space rotation operation to the specified mode.

Parameters
• phi (float) – rotation angle

• mode (int) – which mode to apply the rotation to

run_prog(prog, **kwargs)[source]

Runs a strawberryfields program using the bosonic backend.

Parameters

prog (object) – sf.Program instance

Returns

a tuple of the list of applied commands, the dictionary of measurement samples, and the dictionary of ancilla measurement samples

Return type

tuple

Raises
• NotApplicableError – if an op in the program does not apply to the bosonic backend

• NotImplementedError – if an op in the program is not implemented in the bosonic backend

squeeze(r, phi, mode)[source]

Apply the squeezing operation to the specified mode.

Parameters
• r (float) – squeezing amplitude

• phi (float) – squeezing angle

• mode (int) – which mode to apply the squeeze to

state(modes=None, **kwargs)[source]

Returns the state of the quantum simulation.

For the bosonic backend, mode indices are sorted in ascending order.

Returns

object containing all state information

Return type

BosonicState

supports(name)

Check whether the backend supports the given operating mode.

Currently supported operating modes are:

• “gaussian”: for manipulations in the Gaussian representation using the displacements and covariance matrices

• “fock_basis”: for manipulations in the Fock representation

• “bosonic”: for manipulations of states represented as linear combinations of Gaussian functions in phase space

• “mixed_states”: for representations where the quantum state is mixed

• “batched”: allows for a multiple circuits to be simulated in parallel

Parameters

name (str) – name of the operating mode which we are checking support for

Returns

True if this backend supports that operating mode.

Return type

bool

thermal_loss(T, nbar, mode)[source]

Perform a thermal loss channel operation on the specified mode.

Parameters
• T (float) – loss parameter, $$0\leq T\leq 1$$.

• nbar (float) – mean photon number of the environment thermal state

• mode (int) – index of mode where operation is carried out