mm.math.backend_manager.BackendManager

class mrmustard.math.backend_manager.BackendManager[source]

Bases: object

A class to manage the different backends supported by Mr Mustard.

backend

The backend that is being used.

backend_name

The name of the backend in use.

euclidean_opt

The configured Euclidean optimizer.

instance

backend

The backend that is being used.

backend_name

The name of the backend in use.

euclidean_opt

The configured Euclidean optimizer.

instance = Backend(numpy)

Categorical(probs, name)

Categorical distribution over integers.

DefaultEuclideanOptimizer()

Default optimizer for the Euclidean parameters.

J(num_modes)

Symplectic form.

MultivariateNormalTriL(loc, scale_tril)

Multivariate normal distribution on R^k and parameterized by a (batch of) length-k loc vector (aka "mu") and a (batch of) k x k scale matrix; covariance = scale @ scale.T where @ denotes matrix-multiplication.

Xmat(num_modes)

The matrix \(X_n = \begin{bmatrix}0 & I_n\\ I_n & 0\end{bmatrix}.\)

Zmat(num_modes)

The matrix \(Z_n = \begin{bmatrix}I_n & 0\\ 0 & -I_n\end{bmatrix}.\)

abs(array)

The absolute value of array.

add_at_modes(old, new, modes)

Adds two phase-space tensors (cov matrices, displacement vectors, etc..) on the specified modes.

all_diagonals(rho, real)

Returns all the diagonals of a density matrix.

allclose(array1, array2[, atol])

Whether two arrays are equal within tolerance.

any(array)

Returns True if any element of array is True, False otherwise.

arange(start[, limit, delta, dtype])

Returns an array of evenly spaced values within a given interval.

asnumpy(tensor)

Converts an array to a numpy array.

assign(tensor, value)

Assigns value to tensor.

astensor(array[, dtype])

Converts a numpy array to a tensor.

atleast_1d(array[, dtype])

Returns an array with at least one dimension.

atleast_2d(array[, dtype])

Returns an array with at least two dimensions.

atleast_3d(array[, dtype])

Returns an array with at least three dimensions by eventually inserting new axes at the beginning.

binomial_conditional_prob(success_prob, ...)

\(P(out|in) = binom(in, out) * (1-success_prob)**(in-out) * success_prob**out\).

block(blocks[, axes])

Returns a matrix made from the given blocks.

block_diag(mat1, mat2)

Returns a block diagonal matrix from the given matrices.

boolean_mask(tensor, mask)

Returns a tensor based on the truth value of the boolean mask.

cast(array[, dtype])

Casts array to dtype.

change_backend(name)

Changes the backend to a different one.

cholesky(input)

Computes the Cholesky decomposition of square matrices.

clip(array, a_min, a_max)

Clips array to the interval [a_min, a_max].

concat(values, axis)

Concatenates values along the given axis.

conj(array)

The complex conjugate of array.

constraint_func(bounds)

Returns a constraint function for the given bounds.

convolution(array, filters[, padding, ...])

Performs a convolution on array with filters.

convolve_probs(prob, other)

Convolve two probability distributions (up to 3D) with the same shape.

convolve_probs_1d(prob, other_probs)

Convolution of a joint probability with a list of single-index probabilities.

cos(array)

The cosine of an array.

cosh(array)

The hyperbolic cosine of array.

custom_gradient(func)

A decorator to define a function with a custom gradient.

dagger(array)

The adjoint of array.

det(matrix)

The determinant of matrix.

diag(array[, k])

The array made by inserting the given array along the \(k\)-th diagonal.

diag_part(array[, k])

The array of the main diagonal of array.

eigh(tensor)

The eigenvalues and eigenvectors of a matrix.

eigvals(tensor)

The eigenvalues of a tensor.

einsum(string, *tensors)

The result of the Einstein summation convention on the tensors.

euclidean_to_symplectic(S, dS_euclidean)

Convert the Euclidean gradient to a Riemannian gradient on the tangent bundle of the symplectic manifold.

euclidean_to_unitary(U, dU_euclidean)

Convert the Euclidean gradient to a Riemannian gradient on the tangent bundle of the unitary manifold.

exp(array)

The exponential of array element-wise.

expand_dims(array, axis)

The array with an additional dimension inserted at the given axis.

expm(matrix)

The matrix exponential of matrix.

eye(size[, dtype])

The identity matrix of size.

eye_like(array)

The identity matrix of the same shape and dtype as array.

from_backend(value)

Whether the given tensor is a tensor of the concrete backend.

gather(array, indices[, axis])

The values of the array at the given indices.

hermite_renormalized_1leftoverMode(A, B, C, ...)

First, reorder A and B parameters of Bargmann representation to match conventions in mrmustard.math.compactFock~ Then, calculate the required renormalized multidimensional Hermite polynomial.

hermite_renormalized_batch(A, B, C, shape)

Renormalized multidimensional Hermite polynomial given by the "exponential" Taylor series of \(exp(C + Bx + 1/2*Ax^2)\) at zero, where the series has \(sqrt(n!)\) at the denominator rather than \(n!\).

hermite_renormalized_diagonal(A, B, C, cutoffs)

Firsts, reorder A and B parameters of Bargmann representation to match conventions in mrmustard.math.compactFock~ Then, calculates the required renormalized multidimensional Hermite polynomial.

hermite_renormalized_diagonal_batch(A, B, C, ...)

First, reorder A and B parameters of Bargmann representation to match conventions in mrmustard.math.compactFock~ Then, calculates the required renormalized multidimensional Hermite polynomial.

imag(array)

The imaginary part of array.

inv(tensor)

The inverse of tensor.

is_trainable(tensor)

Whether the given tensor is trainable.

kron(tensor1, tensor2)

The Kroenecker product of the given tensors.

left_matmul_at_modes(a_partial, b_full, modes)

Left matrix multiplication of a partial matrix and a full matrix.

lgamma(x)

The natural logarithm of the gamma function of x.

log(x)

The natural logarithm of x.

make_complex(real, imag)

Given two real tensors representing the real and imaginary part of a complex number, this operation returns a complex tensor.

map_fn(fn, elements)

Transforms elems by applying fn to each element unstacked on axis 0.

matmul(*matrices)

The matrix product of the given matrices.

matvec(a, b)

The matrix vector product of a (matrix) and b (vector).

matvec_at_modes(mat, vec, modes)

Matrix-vector multiplication between a phase-space matrix and a vector in the specified modes.

maximum(a, b)

The element-wise maximum of a and b.

minimum(a, b)

The element-wise minimum of a and b.

new_constant(value, name[, dtype])

Returns a new constant with the given value.

new_variable(value, bounds, name[, dtype])

Returns a new variable with the given value and bounds.

norm(array)

The norm of array.

ones(shape[, dtype])

Returns an array of ones with the given shape and dtype.

ones_like(array)

Returns an array of ones with the same shape and dtype as array.

outer(array1, array2)

The outer product of array1 and array2.

pad(array, paddings[, mode, constant_values])

The padded array.

pinv(matrix)

The pseudo-inverse of matrix.

poisson(max_k, rate)

Poisson distribution up to max_k.

pow(x, y)

Returns \(x^y\).

prod(array[, axis])

The product of all elements in array.

random_orthogonal(N)

A random orthogonal matrix in \(O(N)\).

random_symplectic(num_modes[, max_r])

A random symplectic matrix in Sp(2*num_modes).

random_unitary(N)

a random unitary matrix in \(U(N)\)

real(array)

The real part of array.

reshape(array, shape)

The reshaped array.

right_matmul_at_modes(a_full, b_partial, modes)

Right matrix multiplication of a full matrix and a partial matrix.

rotmat(num_modes)

Rotation matrix from quadratures to complex amplitudes.

round(array, decimals)

The array rounded to the nearest integer.

set_diag(array, diag, k)

The array with the diagonal set to diag.

sin(array)

The sine of array.

single_mode_to_multimode_mat(mat, num_modes)

Apply the same \(2\times 2\) matrix (i.e. single-mode) to a larger number of modes.

single_mode_to_multimode_vec(vec, num_modes)

Apply the same 2-vector (i.e. single-mode) to a larger number of modes.

sinh(array)

The hyperbolic sine of array.

solve(matrix, rhs)

The solution of the linear system \(Ax = b\).

sort(array[, axis])

Sort the array along an axis.

sqrt(x[, dtype])

The square root of x.

sqrtm(tensor[, dtype])

The matrix square root.

squeeze(tensor, axis)

Removes dimensions of size 1 from the shape of a tensor.

sum(array[, axes])

The sum of array.

tensordot(a, b, axes)

The tensordot product of a and b.

tile(array, repeats)

The tiled array.

trace(array[, dtype])

The trace of array.

transpose(a[, perm])

The transposed arrays.

unitary_to_orthogonal(U)

Unitary to orthogonal mapping.

update_add_tensor(tensor, indices, values)

Updates a tensor in place by adding the given values.

update_tensor(tensor, indices, values)

Updates a tensor in place with the given values.

value_and_gradients(cost_fn, parameters)

The loss and gradients of the given cost function.

xlogy(x, y)

Returns 0 if x == 0 elementwise and x * log(y) otherwise.

zeros(shape[, dtype])

Returns an array of zeros with the given shape and dtype.

zeros_like(array)

Returns an array of zeros with the same shape and dtype as array.

Categorical(probs, name)[source]

Categorical distribution over integers.

Parameters:
  • probs (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The unnormalized probabilities of a set of Categorical distributions.

  • name (str) – The name prefixed to operations created by this class.

Returns:

instance of tfp.distributions.Categorical class

Return type:

tfp.distributions.Categorical

DefaultEuclideanOptimizer()[source]

Default optimizer for the Euclidean parameters.

static J(num_modes)[source]

Symplectic form.

MultivariateNormalTriL(loc, scale_tril)[source]

Multivariate normal distribution on R^k and parameterized by a (batch of) length-k loc vector (aka “mu”) and a (batch of) k x k scale matrix; covariance = scale @ scale.T where @ denotes matrix-multiplication.

Parameters:
  • loc (Tensor) – if this is set to None, loc is implicitly 0

  • scale_tril (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – lower-triangular Tensor with non-zero diagonal elements

Returns:

instance of tfp.distributions.MultivariateNormalTriL

Return type:

tfp.distributions.MultivariateNormalTriL

static Xmat(num_modes)[source]

The matrix \(X_n = \begin{bmatrix}0 & I_n\\ I_n & 0\end{bmatrix}.\)

Parameters:

num_modes (int) – positive integer

Returns:

The \(2N\times 2N\) array

static Zmat(num_modes)[source]

The matrix \(Z_n = \begin{bmatrix}I_n & 0\\ 0 & -I_n\end{bmatrix}.\)

Parameters:

num_modes (int) – A positive integer representing the number of modes.

Returns:

The \(2N\times 2N\) array

abs(array)[source]

The absolute value of array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the absolute value of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The absolute value of the given array.

add_at_modes(old, new, modes)[source]

Adds two phase-space tensors (cov matrices, displacement vectors, etc..) on the specified modes.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

all_diagonals(rho, real)[source]

Returns all the diagonals of a density matrix.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

allclose(array1, array2, atol=1e-09)[source]

Whether two arrays are equal within tolerance.

The two arrays are compaired element-wise.

Parameters:
  • array1 (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – An array.

  • array2 (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – Another array.

  • atol – The absolute tolerance.

Return type:

bool

Returns:

Whether two arrays are equal within tolerance.

Raises:

ValueError – If the shape of the two arrays do not match.

any(array)[source]

Returns True if any element of array is True, False otherwise.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to check.

Return type:

bool

Returns:

True if any element of array is True, False otherwise.

arange(start, limit=None, delta=1, dtype=None)[source]

Returns an array of evenly spaced values within a given interval.

Parameters:
  • start (int) – The start of the interval.

  • limit (int) – The end of the interval.

  • delta (int) – The step size.

  • dtype (Any) – The dtype of the returned array.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array of evenly spaced values.

asnumpy(tensor)[source]

Converts an array to a numpy array.

Parameters:

tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to convert.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The corresponidng numpy array.

assign(tensor, value)[source]

Assigns value to tensor.

Parameters:
  • tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to assign to.

  • value (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The value to assign.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The tensor with value assigned

astensor(array, dtype=None)[source]

Converts a numpy array to a tensor.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The numpy array to convert.

  • dtype – The dtype of the tensor. If None, the returned tensor is of type float.

Returns:

The tensor with dtype.

atleast_1d(array, dtype=None)[source]

Returns an array with at least one dimension.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to convert.

  • dtype – The data type of the array. If None, the returned array is of the same type as the given one.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array with at least one dimension.

atleast_2d(array, dtype=None)[source]

Returns an array with at least two dimensions.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to convert.

  • dtype – The data type of the array. If None, the returned array is of the same type as the given one.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array with at least two dimensions.

atleast_3d(array, dtype=None)[source]

Returns an array with at least three dimensions by eventually inserting new axes at the beginning. Note this is not the way atleast_3d works in numpy and tensorflow, where it adds at the beginning and/or end.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to convert.

  • dtype – The data type of the array. If None, the returned array is of the same type as the given one.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array with at least three dimensions.

binomial_conditional_prob(success_prob, dim_out, dim_in)[source]

\(P(out|in) = binom(in, out) * (1-success_prob)**(in-out) * success_prob**out\).

block(blocks, axes=(-2, -1))[source]

Returns a matrix made from the given blocks.

Parameters:
  • blocks (List[List[ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]]]) – A list of lists of compatible blocks.

  • axes – The axes to stack the blocks along.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The matrix made of blocks.

block_diag(mat1, mat2)[source]

Returns a block diagonal matrix from the given matrices.

Parameters:
  • mat1 (ndarray[Tuple[int, int], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – A matrix.

  • mat2 (ndarray[Tuple[int, int], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – A matrix.

Return type:

ndarray[Tuple[int, int], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

A block diagonal matrix from the given matrices.

boolean_mask(tensor, mask)[source]

Returns a tensor based on the truth value of the boolean mask.

Parameters:
  • tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – A tensor.

  • mask (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – A boolean mask.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

A tensor based on the truth value of the boolean mask.

cast(array, dtype=None)[source]

Casts array to dtype.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to cast.

  • dtype – The data type to cast to. If None, the returned array is the same as the given one.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array cast to dtype.

change_backend(name)[source]

Changes the backend to a different one.

Parameters:

name (str) – The name of the new backend.

Return type:

None

cholesky(input)[source]

Computes the Cholesky decomposition of square matrices.

Parameters:

input (Tensor) –

Returns:

tensor with the same type as input

Return type:

Tensor

clip(array, a_min, a_max)[source]

Clips array to the interval [a_min, a_max].

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to clip.

  • a_min (float) – The minimum value.

  • a_max (float) – The maximum value.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The clipped array.

concat(values, axis)[source]

Concatenates values along the given axis.

Parameters:
  • values (Sequence[ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]]) – The values to concatenate.

  • axis (int) – The axis along which to concatenate.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The concatenated values.

conj(array)[source]

The complex conjugate of array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the complex conjugate of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The complex conjugate of the given array.

constraint_func(bounds)[source]

Returns a constraint function for the given bounds.

A constraint function will clip the value to the interval given by the bounds.

Note

The upper and/or lower bounds can be None, in which case the constraint function will not clip the value.

Parameters:

bounds (Tuple[Optional[float], Optional[float]]) – The bounds of the constraint.

Return type:

Optional[Callable]

Returns:

The constraint function.

convolution(array, filters, padding=None, data_format='NWC')[source]

Performs a convolution on array with filters.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to convolve.

  • filters (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The filters to convolve with.

  • padding (Optional[str]) – The padding mode.

  • data_format – The data format of the array.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The convolved array.

convolve_probs(prob, other)[source]

Convolve two probability distributions (up to 3D) with the same shape.

Note that the output is not guaranteed to be a complete joint probability, as it’s computed only up to the dimension of the base probs.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

convolve_probs_1d(prob, other_probs)[source]

Convolution of a joint probability with a list of single-index probabilities.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

cos(array)[source]

The cosine of an array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the cosine of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The cosine of array.

cosh(array)[source]

The hyperbolic cosine of array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the hyperbolic cosine of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The hyperbolic cosine of array.

custom_gradient(func)[source]

A decorator to define a function with a custom gradient.

dagger(array)[source]

The adjoint of array. This operation swaps the first and second half of the indexes and then conjugates the matrix.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the adjoint of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The adjoint of array

det(matrix)[source]

The determinant of matrix.

Parameters:

matrix (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The matrix to take the determinant of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The determinant of matrix.

diag(array, k=0)[source]

The array made by inserting the given array along the \(k\)-th diagonal.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to insert.

  • k (int) – The k-th diagonal to insert array into.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array with array inserted into the k-th diagonal.

diag_part(array, k=0)[source]

The array of the main diagonal of array.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to extract the main diagonal of.

  • k (int) – The diagonal to extract.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array of the main diagonal of array.

eigh(tensor)[source]

The eigenvalues and eigenvectors of a matrix.

Parameters:

tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to calculate the eigenvalues and eigenvectors of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The eigenvalues and eigenvectors of tensor.

eigvals(tensor)[source]

The eigenvalues of a tensor.

Parameters:

tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to calculate the eigenvalues of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The eigenvalues of tensor.

einsum(string, *tensors)[source]

The result of the Einstein summation convention on the tensors.

Parameters:
  • string (str) – The string of the Einstein summation convention.

  • tensors – The tensors to perform the Einstein summation on.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The result of the Einstein summation convention.

euclidean_to_symplectic(S, dS_euclidean)[source]

Convert the Euclidean gradient to a Riemannian gradient on the tangent bundle of the symplectic manifold.

Implemented from:

Wang J, Sun H, Fiori S. A Riemannian‐steepest‐descent approach for optimization on the real symplectic group. Mathematical Methods in the Applied Sciences. 2018 Jul 30;41(11):4273-86.

Parameters:
  • S (Matrix) – symplectic matrix

  • dS_euclidean (Matrix) – Euclidean gradient tensor

Returns:

symplectic gradient tensor

Return type:

Matrix

euclidean_to_unitary(U, dU_euclidean)[source]

Convert the Euclidean gradient to a Riemannian gradient on the tangent bundle of the unitary manifold.

Implemented from:

Y Yao, F Miatto, N Quesada - arXiv preprint arXiv:2209.06069, 2022.

Parameters:
  • U (Matrix) – unitary matrix

  • dU_euclidean (Matrix) – Euclidean gradient tensor

Returns:

unitary gradient tensor

Return type:

Matrix

exp(array)[source]

The exponential of array element-wise.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the exponential of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The exponential of array.

expand_dims(array, axis)[source]

The array with an additional dimension inserted at the given axis.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to expand.

  • axis (int) – The axis to insert the new dimension.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array with an additional dimension inserted at the given axis.

expm(matrix)[source]

The matrix exponential of matrix.

Parameters:

matrix (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The matrix to take the exponential of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The exponential of matrix.

eye(size, dtype=None)[source]

The identity matrix of size.

Parameters:
  • size (int) – The size of the identity matrix

  • dtype – The data type of the identity matrix. If None, the returned matrix is of type float.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The identity matrix.

eye_like(array)[source]

The identity matrix of the same shape and dtype as array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to create the identity matrix of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The identity matrix.

from_backend(value)[source]

Whether the given tensor is a tensor of the concrete backend.

Parameters:

value (Any) – A value.

Return type:

bool

Returns:

Whether given value is a tensor of the concrete backend.

gather(array, indices, axis=None)[source]

The values of the array at the given indices.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to gather values from.

  • indices (Batch[int]) – The indices to gather values from.

  • axis (Optional[int]) – The axis to gather values from.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The values of the array at the given indices.

hermite_renormalized_1leftoverMode(A, B, C, cutoffs)[source]

First, reorder A and B parameters of Bargmann representation to match conventions in mrmustard.math.compactFock~ Then, calculate the required renormalized multidimensional Hermite polynomial.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

hermite_renormalized_batch(A, B, C, shape)[source]

Renormalized multidimensional Hermite polynomial given by the “exponential” Taylor series of \(exp(C + Bx + 1/2*Ax^2)\) at zero, where the series has \(sqrt(n!)\) at the denominator rather than \(n!\). It computes all the amplitudes within the tensor of given shape in case of B is a batched vector with a batched diemnsion on the last index.

Parameters:
  • A (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The A matrix.

  • B (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The batched B vector with its batch dimension on the last index.

  • C (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The C scalar.

  • shape (Tuple[int]) – The shape of the final tensor.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The batched Hermite polynomial of given shape.

hermite_renormalized_diagonal(A, B, C, cutoffs)[source]

Firsts, reorder A and B parameters of Bargmann representation to match conventions in mrmustard.math.compactFock~ Then, calculates the required renormalized multidimensional Hermite polynomial.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

hermite_renormalized_diagonal_batch(A, B, C, cutoffs)[source]

First, reorder A and B parameters of Bargmann representation to match conventions in mrmustard.math.compactFock~ Then, calculates the required renormalized multidimensional Hermite polynomial. Same as hermite_renormalized_diagonal but works for a batch of different B’s.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

imag(array)[source]

The imaginary part of array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the imaginary part of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The imaginary part of array

inv(tensor)[source]

The inverse of tensor.

Parameters:

tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to take the inverse of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The inverse of tensor

is_trainable(tensor)[source]

Whether the given tensor is trainable.

Parameters:

tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to train.

Return type:

bool

Returns:

Whether the given tensor can be trained.

kron(tensor1, tensor2)[source]

The Kroenecker product of the given tensors.

Parameters:
  • tensor1 (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – A tensor.

  • tensor2 (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – Another tensor.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The Kroenecker product.

left_matmul_at_modes(a_partial, b_full, modes)[source]

Left matrix multiplication of a partial matrix and a full matrix.

It assumes that that a_partial is a matrix operating on M modes and that modes is a list of M integers, i.e., it will apply a_partial on the corresponding M modes of b_full from the left.

Parameters:
  • a_partial (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The \(2M\times 2M\) array

  • b_full (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The \(2N\times 2N\) array

  • modes (Sequence[int]) – A list of M modes to perform the multiplication on

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The \(2N\times 2N\) array

lgamma(x)[source]

The natural logarithm of the gamma function of x.

Parameters:

x (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the natural logarithm of the gamma function of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The natural logarithm of the gamma function of x

log(x)[source]

The natural logarithm of x.

Parameters:

x (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the natural logarithm of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The natural logarithm of x

make_complex(real, imag)[source]

Given two real tensors representing the real and imaginary part of a complex number, this operation returns a complex tensor. The input tensors must have the same shape.

Parameters:
  • real (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The real part of the complex number.

  • imag (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The imaginary part of the complex number.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The complex array real + 1j * imag.

map_fn(fn, elements)[source]

Transforms elems by applying fn to each element unstacked on axis 0.

Parameters:
  • fn (func) – The callable to be performed. It accepts one argument, which will have the same (possibly nested) structure as elems.

  • elements (Tensor) – A tensor or (possibly nested) sequence of tensors, each of which will be unstacked along their first dimension. func will be applied to the nested sequence of the resulting slices.

Returns:

applied func on elements

Return type:

Tensor

matmul(*matrices)[source]

The matrix product of the given matrices.

Parameters:

matrices (ndarray[Tuple[int, int], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The matrices to multiply.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The matrix product

matvec(a, b)[source]

The matrix vector product of a (matrix) and b (vector).

Parameters:
  • a (ndarray[Tuple[int, int], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The matrix to multiply

  • b (ndarray[Tuple[int], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The vector to multiply

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The matrix vector product of a and b

matvec_at_modes(mat, vec, modes)[source]

Matrix-vector multiplication between a phase-space matrix and a vector in the specified modes.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

maximum(a, b)[source]

The element-wise maximum of a and b.

Parameters:
  • a (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The first array to take the maximum of

  • b (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The second array to take the maximum of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The element-wise maximum of a and b

minimum(a, b)[source]

The element-wise minimum of a and b.

Parameters:
  • a (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The first array to take the minimum of

  • b (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The second array to take the minimum of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The element-wise minimum of a and b

new_constant(value, name, dtype=None)[source]

Returns a new constant with the given value.

Parameters:
  • value (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The value of the new constant

  • name (str) – name of the new constant

  • dtype (type) – dtype of the array

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The new constant

new_variable(value, bounds, name, dtype=None)[source]

Returns a new variable with the given value and bounds.

Parameters:
  • value (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The value of the new variable.

  • bounds (Tuple[Optional[float], Optional[float]]) – The bounds of the new variable.

  • name (str) – The name of the new variable.

  • dtype – dtype of the new variable. If None, casts it to float.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The new variable.

norm(array)[source]

The norm of array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the norm of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The norm of array

ones(shape, dtype=None)[source]

Returns an array of ones with the given shape and dtype.

Parameters:
  • shape (tuple) – shape of the array

  • dtype (type) – dtype of the array. If None, the returned array is of type float.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array of ones

ones_like(array)[source]

Returns an array of ones with the same shape and dtype as array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the shape and dtype of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array of ones

outer(array1, array2)[source]

The outer product of array1 and array2.

Parameters:
  • array1 (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The first array to take the outer product of

  • array2 (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The second array to take the outer product of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The outer product of array1 and array2

pad(array, paddings, mode='CONSTANT', constant_values=0)[source]

The padded array.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to pad

  • paddings (tuple) – paddings to apply

  • mode (str) – mode to apply the padding

  • constant_values (int) – constant values to use for padding

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The padded array

pinv(matrix)[source]

The pseudo-inverse of matrix.

Parameters:

matrix (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The matrix to take the pseudo-inverse of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The pseudo-inverse of matrix

poisson(max_k, rate)[source]

Poisson distribution up to max_k.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

pow(x, y)[source]

Returns \(x^y\). Broadcasts x and y if necessary. :type x: ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]] :param x: The base :type y: ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]] :param y: The exponent

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The \(x^y\)

prod(array, axis=None)[source]

The product of all elements in array.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array of elements to calculate the product of.

  • axis – The axis along which a product is performed. If None, it calculates the product of all elements in array.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The product of the elements in array.

static random_orthogonal(N)[source]

A random orthogonal matrix in \(O(N)\).

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

random_symplectic(num_modes, max_r=1.0)[source]

A random symplectic matrix in Sp(2*num_modes).

Squeezing is sampled uniformly from 0.0 to max_r (1.0 by default).

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

random_unitary(N)[source]

a random unitary matrix in \(U(N)\)

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

real(array)[source]

The real part of array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the real part of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The real part of array

reshape(array, shape)[source]

The reshaped array.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to reshape

  • shape (tuple) – shape to reshape the array to

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The reshaped array

right_matmul_at_modes(a_full, b_partial, modes)[source]

Right matrix multiplication of a full matrix and a partial matrix.

It assumes that that b_partial is a matrix operating on M modes and that modes is a list of M integers, i.e., it will apply b_partial on the corresponding M modes of a_full from the right.

Parameters:
  • a_full (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The \(2N\times 2N\) array

  • b_partial (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The \(2M\times 2M\) array

  • modes (Sequence[int]) – A list of M modes to perform the multiplication on

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The \(2N\times 2N\) array

static rotmat(num_modes)[source]

Rotation matrix from quadratures to complex amplitudes.

round(array, decimals)[source]

The array rounded to the nearest integer.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to round

  • decimals (int) – number of decimals to round to

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array rounded to the nearest integer

set_diag(array, diag, k)[source]

The array with the diagonal set to diag.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to set the diagonal of

  • diag (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The diagonal to set

  • k (int) – diagonal to set

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array with the diagonal set to diag

sin(array)[source]

The sine of array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the sine of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The sine of array

single_mode_to_multimode_mat(mat, num_modes)[source]

Apply the same \(2\times 2\) matrix (i.e. single-mode) to a larger number of modes.

single_mode_to_multimode_vec(vec, num_modes)[source]

Apply the same 2-vector (i.e. single-mode) to a larger number of modes.

sinh(array)[source]

The hyperbolic sine of array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the hyperbolic sine of

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The hyperbolic sine of array

solve(matrix, rhs)[source]

The solution of the linear system \(Ax = b\).

Parameters:
  • matrix (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The matrix \(A\)

  • rhs (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The vector \(b\)

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The solution \(x\)

sort(array, axis=-1)[source]

Sort the array along an axis.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to sort

  • axis (int) – (optional) The axis to sort along. Defaults to last axis.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

A sorted version of the array in acending order.

sqrt(x, dtype=None)[source]

The square root of x.

Parameters:
  • x (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the square root of

  • dtypedtype of the output array.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The square root of x

sqrtm(tensor, dtype=None)[source]

The matrix square root.

Parameters:
  • tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to take the matrix square root of.

  • dtype – The dtype of the output tensor. If None, the output is of type math.complex128.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The square root of x

squeeze(tensor, axis)[source]

Removes dimensions of size 1 from the shape of a tensor.

Parameters:
  • tensor (Tensor) – the tensor to squeeze

  • axis (Optional[List[int]]) – if specified, only squeezes the dimensions listed, defaults to []

Returns:

tensor with one or more dimensions of size 1 removed

Return type:

Tensor

sum(array, axes=None)[source]

The sum of array.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the sum of

  • axes (tuple) – axes to sum over

Returns:

The sum of array

tensordot(a, b, axes)[source]

The tensordot product of a and b.

Parameters:
  • a (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The first array to take the tensordot product of

  • b (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The second array to take the tensordot product of

  • axes (Sequence[int]) – The axes to take the tensordot product over

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The tensordot product of a and b

tile(array, repeats)[source]

The tiled array.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to tile

  • repeats (tuple) – number of times to tile the array along each axis

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The tiled array

trace(array, dtype=None)[source]

The trace of array.

Parameters:
  • array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the trace of

  • dtype (type) – dtype of the output array

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The trace of array

transpose(a, perm=None)[source]

The transposed arrays.

Parameters:
  • a (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to transpose

  • perm (tuple) – permutation to apply to the array

Returns:

The transposed array

unitary_to_orthogonal(U)[source]

Unitary to orthogonal mapping.

Parameters:

U – The unitary matrix in U(n)

Returns:

The orthogonal matrix in \(O(2n)\)

update_add_tensor(tensor, indices, values)[source]

Updates a tensor in place by adding the given values.

Parameters:
  • tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to update

  • indices (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The indices to update

  • values (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The values to add

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The updated tensor

update_tensor(tensor, indices, values)[source]

Updates a tensor in place with the given values.

Parameters:
  • tensor (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The tensor to update

  • indices (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The indices to update

  • values (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The values to update

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The updated tensor

value_and_gradients(cost_fn, parameters)[source]

The loss and gradients of the given cost function.

Parameters:
  • cost_fn (callable) – cost function to compute the loss and gradients of

  • parameters (dict) – parameters to compute the loss and gradients of

Returns:

loss and gradients (dict) of the given cost function

Return type:

tuple

xlogy(x, y)[source]

Returns 0 if x == 0 elementwise and x * log(y) otherwise.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

zeros(shape, dtype=None)[source]

Returns an array of zeros with the given shape and dtype.

Parameters:
  • shape (Sequence[int]) – The shape of the array.

  • dtype – The dtype of the array. If None, the returned array is of type float.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array of zeros.

zeros_like(array)[source]

Returns an array of zeros with the same shape and dtype as array.

Parameters:

array (ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]) – The array to take the shape and dtype of.

Return type:

ndarray[Tuple[int, ...], Union[TypeVar(R, float16, float32, float64), TypeVar(C, complex64, complex128), TypeVar(Z, int16, int32, int64), TypeVar(N, uint16, uint32, uint64)]]

Returns:

The array of zeros.

Contents