mrmustard.math

The math module

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

Functions

Categorical(name)

Categorical distribution over integers.

DefaultEuclideanOptimizer()

Default optimizer for the Euclidean parameters.

J(num_modes)

Symplectic form.

MultivariateNormalTriL(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()

The absolute value of array.

add_at_modes(new, modes)

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

all_diagonals(real)

Returns all the diagonals of a density matrix.

allclose(array2[, atol])

Whether two arrays are equal within tolerance.

any()

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

arange([limit, delta, dtype])

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

asnumpy()

Converts an array to a numpy array.

assign(value)

Assigns value to tensor.

astensor([dtype])

Converts a numpy array to a tensor.

atleast_1d([dtype])

Returns an array with at least one dimension.

atleast_2d([dtype])

Returns an array with at least two dimensions.

atleast_3d([dtype])

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

binomial_conditional_prob(dim_out, dim_in)

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

block([axes])

Returns a matrix made from the given blocks.

block_diag(mat2)

Returns a block diagonal matrix from the given matrices.

boolean_mask(mask)

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

cast([dtype])

Casts array to dtype.

change_backend()

Changes the backend to a different one.

cholesky()

Computes the Cholesky decomposition of square matrices.

clip(a_min, a_max)

Clips array to the interval [a_min, a_max].

concat(axis)

Concatenates values along the given axis.

conj()

The complex conjugate of array.

constraint_func()

Returns a constraint function for the given bounds.

convolution(filters[, padding, data_format])

Performs a convolution on array with filters.

convolve_probs(other)

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

convolve_probs_1d(other_probs)

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

cos()

The cosine of an array.

cosh()

The hyperbolic cosine of array.

custom_gradient(func)

A decorator to define a function with a custom gradient.

dagger()

The adjoint of array.

det()

The determinant of matrix.

diag([k])

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

diag_part([k])

The array of the main diagonal of array.

eigh()

The eigenvalues and eigenvectors of a matrix.

eigvals()

The eigenvalues of a tensor.

einsum(*tensors)

The result of the Einstein summation convention on the tensors.

euclidean_to_symplectic(dS_euclidean)

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

euclidean_to_unitary(dU_euclidean)

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

exp()

The exponential of array element-wise.

expand_dims(axis)

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

expm()

The matrix exponential of matrix.

eye([dtype])

The identity matrix of size.

eye_like()

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

from_backend()

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

gather(indices[, axis])

The values of the array at the given indices.

hermite_renormalized(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_1leftoverMode(B, C, cutoffs)

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_1leftoverMode_reorderedAB(B, ...)

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

hermite_renormalized_batch(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_binomial(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(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(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.

hermite_renormalized_diagonal_reorderedAB(B, ...)

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

imag()

The imaginary part of array.

inv()

The inverse of tensor.

is_trainable()

Whether the given tensor is trainable.

kron(tensor2)

The Kroenecker product of the given tensors.

left_matmul_at_modes(b_full, modes)

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

lgamma()

The natural logarithm of the gamma function of x.

log()

The natural logarithm of x.

make_complex(imag)

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

map_fn(elements)

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

matmul()

The matrix product of the given matrices.

matvec(b)

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

matvec_at_modes(vec, modes)

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

maximum(b)

The element-wise maximum of a and b.

minimum(b)

The element-wise minimum of a and b.

new_constant(name[, dtype])

Returns a new constant with the given value.

new_variable(bounds, name[, dtype])

Returns a new variable with the given value and bounds.

norm()

The norm of array.

ones([dtype])

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

ones_like()

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

outer(array2)

The outer product of array1 and array2.

pad(paddings[, mode, constant_values])

The padded array.

pinv()

The pseudo-inverse of matrix.

poisson(rate)

Poisson distribution up to max_k.

pow(y)

Returns \(x^y\).

prod([axis])

The product of all elements in array.

random_orthogonal(N)

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

random_symplectic([max_r])

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

random_unitary()

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

real()

The real part of array.

reshape(shape)

The reshaped array.

right_matmul_at_modes(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(decimals)

The array rounded to the nearest integer.

set_diag(diag, k)

The array with the diagonal set to diag.

sin()

The sine of array.

single_mode_to_multimode_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(num_modes)

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

sinh()

The hyperbolic sine of array.

solve(rhs)

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

sort([axis])

Sort the array along an axis.

sqrt([dtype])

The square root of x.

sqrtm([dtype])

The matrix square root.

squeeze(axis)

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

sum([axes])

The sum of array.

tensordot(b, axes)

The tensordot product of a and b.

tile(repeats)

The tiled array.

trace([dtype])

The trace of array.

transpose([perm])

The transposed arrays.

unitary_to_orthogonal(U)

Unitary to orthogonal mapping.

update_add_tensor(indices, values)

Updates a tensor in place by adding the given values.

update_tensor(indices, values)

Updates a tensor in place with the given values.

value_and_gradients(parameters)

The loss and gradients of the given cost function.

xlogy(y)

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

zeros([dtype])

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

zeros_like()

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

Variables

backend

backend_name

euclidean_opt

instance