# Tensorflow simulator backend¶

Module name: strawberryfields.backends.tfbackend

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

The Circuit simulator maintains an internal tensor representation of the quantum state of a multi-mode quantum optical system using a (truncated) Fock basis representation. As its various state manipulation methods are called, the quantum state is updated to reflect these changes. The simulator will try to keep the internal state in a pure (vector) representation for as long as possible. Unitary gates will not change the type of representation, while state preparations and measurements will.

A number of factors determine the shape and dimensionality of the state tensor:

• the underlying state representation being used (either a ket vector or a density matrix)
• the number of modes $$n$$ actively being simulated
• the cutoff dimension $$D$$ for the Fock basis
• whether the circuit is operating in batched mode (with batch size $$B$$)

When not operating in batched mode, the state tensor corresponds to a single multimode quantum system. If the representation is a pure state, the state tensor has shape $$(\underbrace{D,...,D}_{n~\text{times}})$$. In a mixed state representation, the state tensor has shape $$(\underbrace{D,D,...,D,D}_{2n~\text{times}})$$. Indices for the same mode appear consecutively. Hence, for a mixed state, the first two indices are for the first mode, the second are for the second mode, etc.

In batched mode, the state tensor simultaneously encodes an ensemble of $$B$$ multimode quantum systems (indexed using the first axis of the state tensor). Pure states thus have shape $$(B,\underbrace{D,...,D}_{n~\text{times}})$$, while mixed states have shape $$(B,\underbrace{D,D,...,D,D}_{2n~\text{times}})$$.

## Basic quantum simulator methods¶

The TFBackend simulator implements a number of state preparations, gates, and measurements (listed below). The parameters supplied for these operations can be either numeric (float, complex) values or Tensorflow Variables/Tensors. The Tensorflow objects can either be scalars or vectors. For vectors, they must have the same dimension as the declared batch size of the underlying circuit.

 begin_circuit(num_subsystems[, cutoff_dim, …]) Create a quantum circuit (initialized in vacuum state) with the number of modes equal to num_subsystems and a Fock-space cutoff dimension of cutoff_dim. prepare_vacuum_state(mode) Prepare the vacuum state on the specified mode. prepare_coherent_state(alpha, mode) Prepare a coherent state with parameter alpha on the specified mode. prepare_squeezed_state(r, phi, mode) Prepare a coherent state with parameters (r, phi) on the specified mode. prepare_displaced_squeezed_state(alpha, r, …) Prepare a displaced squezed state with parameters (alpha, r, phi) on the specified mode. prepare_fock_state(n, mode) Prepare a Fock state on the specified mode. prepare_ket_state(state, modes) Prepare an arbitrary pure state on the specified mode. prepare_dm_state(state, modes) Prepare an arbitrary mixed state on the specified mode. rotation(phi, mode) Perform a phase shift by angle phi on the specified mode. displacement(alpha, mode) Perform a displacement operation on the specified mode. squeeze(z, mode) Perform a squeezing operation on the specified mode. beamsplitter(t, r, mode1, mode2) Perform a beamsplitter operation on 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. loss(T, mode) Perform a loss channel operation on the specified mode. measure_fock(modes[, select]) Perform a Fock measurement on the specified modes. measure_homodyne(phi, mode[, select]) Perform a homodyne measurement on the specified modes. del_mode(modes) Trace out the specified modes from the underlying circuit state. add_mode([n]) Add n new modes to the underlying circuit state. get_modes() Return a list of the active mode indices for the circuit. state([modes]) Returns the state of the quantum simulation, restricted to the subsystems defined by modes.

## Auxiliary methods¶

 reset([pure]) Resets the circuit state tensor back to an all-vacuum state. get_cutoff_dim() Returns the Hilbert space cutoff dimension used. graph Get the Tensorflow Graph object where the current quantum circuit is defined.

### Code details¶

class strawberryfields.backends.tfbackend.TFBackend(graph=None)[source]

Tensorflow Backend implementation.

begin_circuit(num_subsystems, cutoff_dim=None, hbar=2, pure=True, **kwargs)[source]

Create a quantum circuit (initialized in vacuum state) with the number of modes equal to num_subsystems and a Fock-space cutoff dimension of cutoff_dim.

Parameters: num_subsystems (int) – number of modes the circuit should begin with cutoff_dim (int) – numerical cutoff dimension in Fock space for each mode. cutoff_dim=D represents the Fock states $$|0\rangle,\dots,|D-1\rangle$$. This argument is required for the Tensorflow backend. 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 begin the circuit in a pure state representation **kwargs – optional keyword arguments which will be passed to the underlying circuit class batch_size (None or int): the size of the batch-axis dimension. If None, no batch-axis will be used.
reset(pure=True, **kwargs)[source]

Resets the circuit state tensor back to an all-vacuum state.

Parameters: Keyword Arguments: pure (bool) – whether to use a pure state representation upon reset hard (bool) – whether to reset the underlying tensorflow graph. If hard reset is specified, then resets the underlying tensor graph as well. If False, then the circuit is reset to its initial state, but ops that have already been declared are still accessible. cutoff_dim (int) – new cutoff dimension for the simulated circuit. hbar (float) – New $$\hbar$$ value. See Conventions and formulas for more details.
get_cutoff_dim()[source]

Returns the Hilbert space cutoff dimension used.

Returns: cutoff dimension int
get_modes()[source]

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

Returns: sorted list of active (assigned, not invalid) mode indices list[int]
prepare_vacuum_state(mode)[source]

Prepare the vacuum state on the specified mode. Note: this may convert the state representation to mixed.

Parameters: mode (int) – index of mode where state is prepared
prepare_coherent_state(alpha, mode)[source]

Prepare a coherent state with parameter alpha on the specified mode. Note: this may convert the state representation to mixed.

Parameters: alpha (complex) – coherent state displacement parameter mode (int) – index of mode where state is prepared
prepare_squeezed_state(r, phi, mode)[source]

Prepare a coherent state with parameters (r, phi) on the specified mode. Note: this may convert the state representation to mixed.

Parameters: r (float) – squeezing amplitude phi (float) – squeezing phase mode (int) – index of mode where state is prepared
prepare_displaced_squeezed_state(alpha, r, phi, mode)[source]

Prepare a displaced squezed state with parameters (alpha, r, phi) on the specified mode. Note: this may convert the state representation to mixed.

Parameters: alpha (complex) – displacement parameter r (float) – squeezing amplitude phi (float) – squeezing phase mode (int) – index of mode where state is prepared
prepare_fock_state(n, mode)[source]

Prepare a Fock state on the specified mode. Note: this may convert the state representation to mixed.

Parameters: n (int) – number state to prepare mode (int) – index of mode where state is prepared
prepare_ket_state(state, modes)[source]

Prepare an arbitrary pure state on the specified mode. Note: this may convert the state representation to mixed.

Parameters: state (array) – vector representation of ket state to prepare mode (int) – index of mode where state is prepared
prepare_dm_state(state, modes)[source]

Prepare an arbitrary mixed state on the specified mode. Note: this converts the state representation to mixed.

Parameters: state (array) – matrix representation of the state to prepare mode (int) – index of mode where state is prepared
prepare_thermal_state(nbar, mode)[source]

Prepare a thermal state on the specified mode. Note: this may convert the state representation to mixed.

Parameters: nbar (float) – mean photon number of the thermal state mode (int) – index of mode where state is prepared
rotation(phi, mode)[source]

Perform a phase shift by angle phi on the specified mode.

Parameters: phi (float) – mode (int) – index of mode where operation is carried out
displacement(alpha, mode)[source]

Perform a displacement operation on the specified mode.

Parameters: alpha (float) – displacement parameter mode (int) – index of mode where operation is carried out
squeeze(z, mode)[source]

Perform a squeezing operation on the specified mode.

Parameters: z (complex) – squeezing parameter mode (int) – index of mode where operation is carried out
beamsplitter(t, r, mode1, mode2)[source]

Perform a beamsplitter operation on the specified modes.

Parameters: t (float) – transmittivity parameter r (complex) – reflectivity parameter mode1 (int) – index of first mode where operation is carried out mode2 (int) – index of second mode where operation is carried out
loss(T, mode)[source]

Perform a loss channel operation on the specified mode.

Parameters: T – loss parameter mode (int) – index of mode where operation is carried out
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
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. **kwargs – optional keyword args (session: a Tensorflow session; feed_dict: a Python dictionary feeding the desired numerical values for Tensors) which will be used by the Tensorflow simulator for numerically evaluating the measurement results. An instance of the Strawberry Fields FockStateTF class.
measure_fock(modes, select=None, **kwargs)[source]

Perform a Fock measurement on the specified modes.

Parameters: modes (Sequence[int]) – indices of mode where operation is carried out select (Sequence[int]) – (Optional) desired values of measurement results. Allows user to post-select on specific measurement results instead of randomly sampling. **kwargs – optional keyword args (session: a Tensorflow session; feed_dict: a Python dictionary feeding the desired numerical values for Tensors) which will be used by the Tensorflow simulator for numerically evaluating the measurement results. measurement outcomes tuple[int] or tuple[Tensor]
measure_homodyne(phi, mode, select=None, **kwargs)[source]

Perform a homodyne measurement on the specified modes.

Parameters: phi (float) – angle (relative to x-axis) for the measurement. select (float) – (Optional) desired values of measurement results. Allows user to post-select on specific measurement results instead of randomly sampling. mode (Sequence[int]) – index of mode where operation is carried out **kwargs – optional keyword args (session: a Tensorflow session; feed_dict: a Python dictionary feeding the desired numerical values for Tensors) which will be used by the Tensorflow simulator for numerically evaluating the measurement results. In addition, kwargs can be used to (optionally) pass user-specified numerical parameters max and num_bins. These are used numerically to build the probability distribution function (pdf) for the homodyne measurement. Specifically, the pdf is discretized onto the 1D grid [-max,max], with num_bins equally spaced bins. measurement outcomes tuple[float] or tuple[Tensor]
is_vacuum(tol=0.0, **kwargs)[source]

Test whether the current circuit state is in vacuum (up to tolerance tol). :param tol: numerical tolerance for how close state must be to true vacuum state :type tol: float

Returns: True if vacuum state up to tolerance tol bool
del_mode(modes)[source]

Trace out the specified modes from the underlying circuit state. Note: This will reduce the number of indices used for the state representation, and also convert the state representation to mixed.

Parameters: modes (Sequence[int]) – the modes to be removed from the circuit
add_mode(n=1)[source]

Add n new modes to the underlying circuit state. Indices for new modes always occur at the end of the state tensor. Note: This will increase the number of indices used for the state representation.

Parameters: n (int) – the number of modes to be added to the circuit.
graph

Get the Tensorflow Graph object where the current quantum circuit is defined.

Returns: the circuit’s graph (Graph)

## FockStateTF¶

This class represents the quantum state returned by the Tensorflow backend. It extends BaseFockState with additional functionality unique to the Tensorflow backend. The primary difference between this class and the Base Fock state is that its methods and attributes can return either numerical or symbolic values.

The default representation (numerical or symbolic) is set when creating the state: state = eng.run('backend', eval=True/False). The representation can also be specified on a per-use basis when calling a method, e.g., state.mean_photon(eval=True/False). Along with the boolean eval, acceptable keyword arguments are session (a Tensorflow Session object) and feed_dict (a dictionary mapping Tensorflow objects to numerical values). These will be used when evaluating any Tensors.

 ket(**kwargs) Computes the ket representation of the state. dm(**kwargs) Computes the density matrix representation of the state. reduced_dm(modes, **kwargs) Computes the reduced density matrix representation of the state. trace(**kwargs) Computes the trace of the state. fock_prob(n, **kwargs) Compute the probabilities of a specific Fock-basis matrix element for the state. all_fock_probs(**kwargs) Compute the probabilities of all possible Fock-basis states for the state. fidelity(other_state, mode, **kwargs) Compute the fidelity of the reduced state (on the specified mode) with the state. fidelity_coherent(alpha_list, **kwargs) Compute the fidelity of the state with the coherent states specified by alpha_list. fidelity_vacuum(**kwargs) Compute the fidelity of the state with the vacuum state. is_vacuum([tol]) Computes a boolean which indicates whether the state is the vacuum state. quad_expectation(mode[, phi]) Compute the expectation value of the quadrature operator $$\hat{x}_\phi$$ for the reduced state on the specified mode. mean_photon(mode, **kwargs) Compute the mean photon number for the reduced state on the specified mode. batched The number of batches. cutoff_dim The numerical truncation of the Fock space used by the underlying state. graph The computational graph.

### Code details¶

class strawberryfields.backends.tfbackend.states.FockStateTF(state_data, num_modes, pure, cutoff_dim, graph, batched=False, hbar=2.0, mode_names=None, eval=True)[source]

Class for the representation of quantum states in the Fock basis using the TFBackend.

Parameters: state_data (array) – the state representation in the Fock basis num_modes (int) – the number of modes in the state pure (bool) – True if the state is a pure state, false if the state is mixed cutoff_dim (int) – the Fock basis truncation size hbar (float) – (default 2) The value of $$\hbar$$ in the commutation relation $$[\x,\p]=i\hbar$$ mode_names (Sequence) – (optional) this argument contains a list providing mode names for each mode in the state. eval (bool) – indicates the default return behaviour for the class instance (symbolic when eval=False, numerical when eval=True)
trace(**kwargs)[source]

Computes the trace of the state. May be numerical or symbolic.

Parameters: **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical value, or an unevaluated Tensor object, for the trace. float/Tensor
fock_prob(n, **kwargs)[source]

Compute the probabilities of a specific Fock-basis matrix element for the state. May be numerical or symbolic.

Parameters: n (Sequence[int]) – the Fock state $$\ket{\vec{n}}$$ that we want to measure the probability of **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical values, or an unevaluated Tensor object, for the Fock-state probabilities. float/Tensor
all_fock_probs(**kwargs)[source]

Compute the probabilities of all possible Fock-basis states for the state. May be numerical or symbolic.

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]

Parameters: **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical values, or an unevaluated Tensor object, for the Fock-basis probabilities. array/Tensor
fidelity(other_state, mode, **kwargs)[source]

Compute the fidelity of the reduced state (on the specified mode) with the state. May be numerical or symbolic.

Parameters: other_state (array) – state vector (ket) to compute the fidelity with respect to mode (int) – which subsystem to use for the fidelity computation **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical value, or an unevaluated Tensor object, for the fidelity. float/Tensor
fidelity_coherent(alpha_list, **kwargs)[source]

Compute the fidelity of the state with the coherent states specified by alpha_list. May be numerical or symbolic.

Parameters: alpha_list (Sequence[complex]) – list of coherence parameter values, one for each mode **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical value, or an unevaluated Tensor object, for the fidelity $$\bra{\vec{\alpha}}\rho\ket{\vec{\alpha}}$$. float/Tensor
fidelity_vacuum(**kwargs)[source]

Compute the fidelity of the state with the vacuum state. May be numerical or symbolic.

Parameters: **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical value, or an unevaluated Tensor object, for the fidelity $$\bra{\vec{0}}\rho\ket{\vec{0}}$$. float/Tensor
is_vacuum(tol=0.0, **kwargs)[source]

Computes a boolean which indicates whether the state is the vacuum state. May be numerical or symbolic.

Parameters: tol – numerical tolerance. If the state has fidelity with vacuum within tol, then this method returns True. **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the boolean value, or an unevaluated Tensor object, for whether the state is in vacuum. bool/Tensor
reduced_dm(modes, **kwargs)[source]

Computes the reduced density matrix representation of the state. May be numerical or symbolic.

Parameters: modes (int or Sequence[int]) – specifies the mode(s) to return the reduced density matrix for. **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical value, or an unevaluated Tensor object, for the density matrix. array/Tensor
quad_expectation(mode, phi=0.0, **kwargs)[source]

Compute the expectation value of the quadrature operator $$\hat{x}_\phi$$ for the reduced state on the specified mode. May be numerical or symbolic.

Parameters: mode (int) – which subsystem to take the expectation value of phi (float) – rotation angle for the quadrature operator **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical value, or an unevaluated Tensor object, for the expectation value float/Tensor
mean_photon(mode, **kwargs)[source]

Compute the mean photon number for the reduced state on the specified mode. May be numerical or symbolic.

Parameters: mode (int) – which subsystem to take the mean photon number of **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. tuple containing the numerical value, or an unevaluated Tensor object, for the mean photon number and variance. tuple(float/Tensor)
ket(**kwargs)[source]

Computes the ket representation of the state. May be numerical or symbolic.

Parameters: **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical value, or an unevaluated Tensor object, for the ket. array/Tensor
dm(**kwargs)[source]

Computes the density matrix representation of the state. May be numerical or symbolic.

Parameters: **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. the numerical value, or an unevaluated Tensor object, for the density matrix. array/Tensor
wigner(mode, xvec, pvec)[source]

Calculates the discretized Wigner function of the specified mode.

Warning

Calculation of the Wigner function is currently only supported if eval=True and batched=False.

Note

This code is a modified version of the ‘iterative’ method of the wigner function provided in QuTiP, which is released under the BSD license, with the following copyright notice:

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 2D array of size [len(xvec), len(pvec)], containing reduced Wigner function values for specified x and p values. array
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.

Warning

Calculation of multi-mode quadratic expectation values is currently only supported if eval=True and batched=False.

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 = \langle P(\mathbf{r})^2\rangle - \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 symmetric 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 vector of quadrature operators $$\mathbf{r}$$ is first rotated by angle $$\phi$$ in the phase space. **kwargs – Optional keyword arguments. If this contains the key eval, then the corresponding argument will be used to determine the return behaviour of this function. When eval=True, the return value is numerical; when eval=False, it is symbolic. If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour, which is specified at initialization. A Tensorflow Session or feed_dict may also be passed via the keys session or feed_dict, respectively. If a Session is supplied, then eval is overriden and the numerical evaluation takes place in the provided Session. If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used. expectation value and variance tuple (float, float)
batched

The number of batches.

graph

The computational graph.