# sf.backends.tfbackend.TFBackend¶

class TFBackend[source]

Bases: strawberryfields.backends.base.BaseFock

Implements a simulation of quantum optical circuits in a truncated Fock basis using TensorFlow, returning a FockStateTF state object.

compiler = 'fock'
short_name = 'tf'
 add_mode([n]) Add modes to the circuit. 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) Apply the two mode cross-Kerr interaction $$\exp{(i\kappa \hat{n}_1\hat{n}_2)}$$ to the specified modes. cubic_phase(gamma, mode) Apply the cubic phase operation to the specified mode. del_mode(modes) Delete modes from the circuit. displacement(r, phi, mode) Apply the displacement operation to the specified mode. gaussian_gate(S, d, *modes) Multimode gaussian gate parametrized by its symplectic matrix and displacement vector, the number of modes to be applied is not fixed Returns the Hilbert space cutoff dimension used. Return a list of the active modes for the circuit. is_vacuum([tol]) Test whether the current circuit state is vacuum (up to given tolerance). kerr_interaction(kappa, mode) Apply the Kerr interaction $$\exp{(i\kappa \hat{n}^2)}$$ to the specified mode. loss(T, mode) Perform a loss channel operation on the specified mode. measure_fock(modes[, shots, select]) Measure the given modes in the Fock basis. measure_homodyne(phi, mode[, shots, select]) Perform a homodyne measurement on the specified modes. 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. 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, modes) Prepare the given mixed state in the specified modes. 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 specified modes. 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. squeeze(r, phi, mode) Apply the squeezing operation to the specified mode. state([modes]) Returns the state of the quantum simulation, restricted to the subsystems defined by modes. 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. two_mode_squeeze(r, phi, mode1, mode2) Apply a two-mode squeezing operator to the two specified modes.
add_mode(n=1, **kwargs)[source]

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]

Keyword Arguments

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

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) – Numerical Hilbert space cutoff dimension for the modes. For each mode, the simulator can represent the Fock states $$\ket{0}, \ket{1}, \ldots, \ket{\text{cutoff_dim}-1}$$.

• pure (bool) – If True (default), use a pure state representation (otherwise will use a mixed state representation).

• batch_size (None or int) – Size of the batch-axis dimension. If None, no batch-axis will be used.

• dtype (tf.DType) – Complex Tensorflow Tensor type representation, either tf.complex64 (default) or tf.complex128. Note, tf.complex128 will increase memory usage substantially.

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

cubic_phase(gamma, mode)[source]

Apply the cubic phase operation to the specified mode.

Applies the operation

$\exp\left(i \frac{\gamma}{6} (\hat{a} +\hat{a}^\dagger)^3\right)$

to the specified mode.

Note

This method is $$\hbar$$ independent. The usual definition of the cubic phase gate is $$\hbar$$ dependent:

$V(\gamma') = \exp\left(i \frac{\gamma'}{3\hbar} \hat{x}^3\right) = \exp\left(i \frac{\gamma' \sqrt{\hbar/2}}{6} (\hat{a} +\hat{a}^\dagger)^3\right).$

Hence the cubic phase gate $$V(\gamma')$$ is executed on a backend by scaling the $$\gamma'$$ parameter by $$\sqrt{\hbar/2}$$ and then passing it to this method, much in the way the $$\hbar$$ dependent X and Z gates are implemented through the $$\hbar$$ independent displacement() method.

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) – scaled cubic phase shift, $$\gamma = \gamma' \sqrt{\hbar/2}$$

• mode (int) – which mode to apply it to

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_gate(S, d, *modes)[source]

Multimode gaussian gate parametrized by its symplectic matrix and displacement vector, the number of modes to be applied is not fixed

get_cutoff_dim()[source]

Returns the Hilbert space cutoff dimension used.

Returns

cutoff dimension

Return type

int

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]

is_vacuum(tol=0.0, **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)[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

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

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

Measure the given modes in the Fock basis.

Keyword Args:

Returns

measurement outcomes

Return type

tuple[int] or tuple[Tensor]

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

Perform a homodyne measurement on the specified modes.

Keyword Arguments
• num_bins (int) – Number of equally spaced bins for the probability distribution function (pdf) simulating the homodyne measurement (default: 100000).

• max (float) – The pdf is discretized onto the 1D grid [-max,max] (default: 10).

Returns

measurement outcome

Return type

float or tf.Tensor

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)[source]

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

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, modes)[source]

Prepare the given mixed state in the specified modes.

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

Parameters
• state (array) – Density matrix 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.

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 specified modes.

The requested modes 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) – Ket 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]) – Modes 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_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

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

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, restricted to the subsystems defined by modes.

Returns

state description

Return type

FockStateTF

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

two_mode_squeeze(r, phi, mode1, mode2)[source]

Apply a two-mode squeezing operator to the two specified modes.

Parameters
• r (float) – squeezing magnitude

• phi (float) – squeezing angle

• mode1 (int) – first mode that two-mode squeezing gate acts on

• mode2 (int) – second mode that two-mode squeezing gate acts on

### Contents

Using Strawberry Fields

Development

API