Backend API

Module name: strawberryfields.backends.base

This module implements the backend API. It contains the classes

as well as a few methods which apply only to the Gaussian backend.

Note

The Strawberry Fields backends by default assume \(\hbar=2\), however different conventions may be chosen when calling begin_circuit()

Note

Keyword arguments are denoted **kwargs, and allow additional options to be passed to the backends - these are documented where available. For more details on available keyword arguments, please consult the backends directly.

Hierarchy for backends

Inheritance diagram of base.BaseBackend, fockbackend.backend.FockBackend, gaussianbackend.backend.GaussianBackend, tfbackend.backend.TFBackend

Base backend

supports(name) Check whether the backend supports the given operating mode.
begin_circuit(num_subsystems[, cutoff_dim, …]) Instantiate a quantum circuit.
add_mode([n]) Add one or more modes to the circuit.
del_mode(modes) Delete one or more modes from the circuit.
get_modes() Return a list of the active mode indices for the circuit.
reset([pure]) Reset the circuit so that all the modes are in the vacuum state.
prepare_vacuum_state(mode) Prepare the vacuum state in the specified mode.
prepare_coherent_state(alpha, mode) Prepare a coherent state in the specified mode.
prepare_squeezed_state(r, phi, mode) Prepare a squeezed vacuum state in the specified mode.
prepare_displaced_squeezed_state(alpha, r, …) Prepare a displaced squeezed state in the specified mode.
prepare_thermal_state(nbar, mode) Prepare a thermal state in the specified mode.
rotation(phi, mode) Apply the phase-space rotation operation to the specified mode.
displacement(alpha, mode) Apply the displacement operation to the specified mode.
squeeze(z, mode) Apply the squeezing operation to the specified mode.
beamsplitter(t, r, mode1, mode2) Apply the beamsplitter operation to the specified modes.
loss(T, mode) Perform a loss channel operation on the specified mode.
thermal_loss(T, nbar, mode) Perform a thermal loss channel operation on the specified mode.
measure_homodyne(phi, mode[, select]) Measure a phase space quadrature of the given mode.
state([modes]) Returns the state of the quantum simulation, restricted to the subsystems defined by modes.
is_vacuum([tol]) Test whether the current circuit state is in vacuum (up to tolerance tol).

Fock backends

Some methods are only implemented in the subclass FockBackend, which is the base class for simulators using a Fock-state representation for quantum optical circuits.

get_cutoff_dim() Returns the Hilbert space cutoff dimension used.
prepare_fock_state(n, mode) Prepare a Fock state in the specified mode.
prepare_ket_state(state, modes) Prepare the given ket state (in the Fock basis) in the specified modes.
prepare_dm_state(state, modes) Prepare the given dm state (in the Fock basis) in the specified modes.
cubic_phase(gamma, mode) Apply the cubic phase operation to the specified mode.
kerr_interaction(kappa, mode) Apply the Kerr interaction \(\exp{(i\kappa \hat{n}^2)}\) to the specified mode.
cross_kerr_interaction(kappa, mode1, mode2) Apply the two mode cross-Kerr interaction \(\exp{(i\kappa \hat{n}_1\hat{n}_2)}\) to the specified modes.
measure_fock(modes[, select]) Measure the given modes in the Fock basis.

Gaussian backends

Likewise, some methods are only implemented in subclass BaseGaussian, which is the base class for simulators using a Gaussian symplectic representation for quantum optical circuits.

measure_heterodyne(mode[, select]) Perform a heterodyne measurement on the given mode.

Code details

exception strawberryfields.backends.base.NotApplicableError[source]

Exception raised by the backend when the user attempts an unsupported operation. E.g. measure_fock() on a Gaussian backend. Conceptually different from NotImplementedError (which means “not implemented, but at some point may be”).

class strawberryfields.backends.base.ModeMap(num_subsystems)[source]

Simple internal class for maintaining a map of existing modes.

reset()[source]

reset the modemap to the initial state

remap(modes)[source]

Remaps the mode list

valid(modes)[source]

checks if the mode list is valid

show()[source]

Returns the mapping

delete(modes)[source]

Deletes a mode

add(num_modes)[source]

Adds a mode

class strawberryfields.backends.base.BaseBackend[source]

Abstract base class for backends.

supports(name)[source]

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
  • “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
begin_circuit(num_subsystems, cutoff_dim=None, hbar=2, pure=True, **kwargs)[source]

Instantiate a quantum circuit.

Instantiates a circuit with num_subsystems modes to track and update a quantum optical state. The state of the circuit 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
  • cutoff_dim (int) – numerical Hilbert space cutoff dimension (used for circuits operating in Fock basis)
  • hbar (float) – The value of \(\hbar\) to initialise the circuit with, depending on the conventions followed. By default, \(\hbar=2\). See Conventions and formulas for more details.
  • pure (bool) – whether to initialize the circuit in a pure state (will use a mixed state if pure is False)
add_mode(n=1)[source]

Add one or more modes to the circuit.

The new modes are initialized to the vacuum state. They are assigned mode indices sequentially, starting from the first unassigned index.

Parameters:n (int) – number of modes to add
Returns:indices of the newly added modes
Return type:list[int]
del_mode(modes)[source]

Delete one or more 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]) – list of mode numbers to delete
get_modes()[source]

Return a list of the active mode indices for the circuit.

Returns:sorted list of active (assigned, not invalid) mode indices
Return type:list[int]
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 (will use a mixed state if pure is False)
Keyword Arguments:
 cutoff_dim (int) – new Hilbert space truncation dimension (for Fock basis backends only)
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
prepare_coherent_state(alpha, mode)[source]

Prepare a coherent state in the specified mode.

The requested mode is traced out and replaced with the coherent state \(\ket{\alpha}\). As a result the state may have to be described using a density matrix.

Parameters:
  • alpha (complex) – coherent state displacement parameter
  • mode (int) – which mode to prepare the coherent state in
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_displaced_squeezed_state(alpha, r, phi, mode)[source]

Prepare a displaced squeezed state in the specified mode.

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

Parameters:
  • alpha (complex) – displacement parameter
  • 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 will be described using a density matrix.

Parameters:
  • nbar (float) – thermal population of the mode
  • mode (int) – which mode to prepare the thermal state in
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
displacement(alpha, mode)[source]

Apply the displacement operation to the specified mode.

Parameters:
  • alpha (complex) – displacement parameter
  • mode (int) – which mode to apply the displacement to
squeeze(z, mode)[source]

Apply the squeezing operation to the specified mode.

Parameters:
  • z (complex) – squeezing parameter
  • mode (int) – which mode to apply the squeeze to
beamsplitter(t, r, mode1, mode2)[source]

Apply the beamsplitter operation to the specified modes.

Parameters:
  • t (float) – transmitted amplitude
  • r (complex) – reflected amplitude (with phase)
  • mode1 (int) – first mode that beamsplitter acts on
  • mode2 (int) – second mode that beamsplitter acts on
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
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
measure_homodyne(phi, mode, 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}_x R^\dagger(\phi) \rho R(\phi) \ket{q}_x\) and returns the sampled value.

Updates the current state of the circuit 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
  • select (float) – (Optional) desired values of measurement results. Allows user to post-select on specific measurement results instead of randomly sampling.
  • **kwargs – can be used to pass user-specified numerical parameters to the backend. Options for such arguments will be documented in the respective subclasses.
Returns:

measured value

Return type:

float

is_vacuum(tol=0.0, **kwargs)[source]

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

Parameters:tol (float) – numerical tolerance for how close state must be to true vacuum state
Returns:True if vacuum state up to tolerance tol
Return type:bool
state(modes=None, **kwargs)[source]

Returns the state of the quantum simulation, restricted to the subsystems defined by modes.

Parameters:modes (int or Sequence[int]) – specifies the mode(s) to restrict the return state to This argument is optional; the default value modes=None returns the state containing all modes. If modes is not ordered, the returned state contains the requested modes in the given order, i.e., requesting the modes=[3,1] results in a two mode state being returned with the first mode being subsystem 3 and the second mode being subsystem 1 of simulator.
Returns:An instance of the Strawberry Fields State class, suited to the particular backend.
class strawberryfields.backends.base.BaseFock[source]

Abstract base class for backends capable of Fock state manipulation.

get_cutoff_dim()[source]

Returns the Hilbert space cutoff dimension used.

Returns:cutoff dimension
Return type:int
prepare_fock_state(n, mode)[source]

Prepare a Fock state in the specified mode.

The requested mode is traced out and replaced with the Fock state \(\ket{n}\). As a result the state may have to be described using a density matrix.

Parameters:
  • n (int) – Fock state to prepare
  • mode (int) – which mode to prepare the fock state in
prepare_ket_state(state, modes)[source]

Prepare the given ket state (in the Fock basis) in the specified modes.

The requested mode(s) is/are traced out and replaced with the given ket state (in the Fock basis). As a result the state may have to be described using a density matrix.

Parameters:
  • state (array) – state in the Fock basis The state can be given in either vector form, with one index, or tensor form, with one index per mode. For backends supporting batched mode, state can be a batch of such vectors or tensors.
  • modes (int or Sequence[int]) – which mode to prepare the state in If modes is not ordered this is taken into account when preparing the state, i.e., when a two mode state is prepared in modes=[3,1], then the first mode of state goes into mode 3 and the second mode goes into mode 1 of the simulator.
prepare_dm_state(state, modes)[source]

Prepare the given dm state (in the Fock basis) in the specified modes.

The requested mode(s) is/are traced out and replaced with the given dm state (in the Fock basis). As a result the state will be described using a density matrix.

Parameters:
  • state (array) – state in the Fock basis The state can be given in either matrix form, with two indices, or tensor form, with two indices per mode. For backends supporting batched mode, state can be a batch of such matrices or tensors.
  • modes (int or Sequence[int]) – which mode to prepare the state in If modes is not ordered this is take into account when preparing the state, i.e., when a two mode state is prepared in modes=[3,1], then the first mode of state goes into mode 3 and the second mode goes into mode 1 of the simulator.
cubic_phase(gamma, mode)[source]

Apply the cubic phase operation to the specified mode.

Warning

The cubic phase gate can suffer heavily from numerical inaccuracies due to finite-dimensional cutoffs in the Fock basis. The gate implementation in Strawberry Fields is unitary, but it does not implement an exact cubic phase gate. The Kerr gate provides an alternative non-Gaussian gate.

Parameters:
  • gamma (float) – cubic phase shift
  • mode (int) – which mode to apply it to
kerr_interaction(kappa, mode)[source]

Apply the Kerr interaction \(\exp{(i\kappa \hat{n}^2)}\) to the specified mode.

Parameters:
  • kappa (float) – strength of the interaction
  • mode (int) – which mode to apply it to
cross_kerr_interaction(kappa, mode1, mode2)[source]

Apply the two mode cross-Kerr interaction \(\exp{(i\kappa \hat{n}_1\hat{n}_2)}\) to the specified modes.

Parameters:
  • kappa (float) – strength of the interaction
  • mode1 (int) – first mode that cross-Kerr interaction acts on
  • mode2 (int) – second mode that cross-Kerr interaction acts on
measure_fock(modes, select=None, **kwargs)[source]

Measure the given modes in the Fock basis.

Updates the current state of the circuit to the conditional state of this measurement result.

Parameters:
  • modes (Sequence[int]) – which modes to measure
  • select (Sequence[int]) – (Optional) desired values of measurement results. Allows user to post-select on specific measurement results instead of randomly sampling.
Returns:

corresponding measurement results

Return type:

tuple[int]

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

Returns the state of the quantum simulation, restricted to the subsystems defined by modes.

Parameters:modes (int or Sequence[int]) – specifies the mode or modes to restrict the return state to. This argument is optional; the default value modes=None returns the state containing all modes.
Returns:An instance of the Strawberry Fields FockState class.
class strawberryfields.backends.base.BaseGaussian[source]

Abstract base class for backends that are only capable of Gaussian state manipulation.

measure_heterodyne(mode, 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:
  • modes (Sequence[int]) – which modes to measure
  • select (complex) – (Optional) desired values of measurement result. Allows user to post-select on specific measurement results instead of randomly sampling.
Returns:

measured values

Return type:

complex

prepare_gaussian_state(r, V, modes)[source]

Prepare the given Gaussian state (via the provided vector of means and the covariance matrix) in the specified modes.

The requested mode(s) is/are traced out and replaced with the given Gaussian state.

Parameters:
  • r (array) – the vector of means in xp ordering.
  • V (array) – the covariance matrix in xp ordering.
  • modes (int or Sequence[int]) – which mode to prepare the state in If the modes are not sorted, this is take into account when preparing the state. i.e., when a two mode state is prepared in modes=[3,1], then the first mode of state goes into mode 3 and the second mode goes into mode 1 of the simulator.