# Calculator#

class BaseCalculator#

The base class for encapsulating a framework.

This class makes dependency injection into Piquasso possible. This way, Piquasso calculations can be made independent of the underlying framework (NumPy, TensorFlow, JAX) using the framework’s implementation of the NumPy API.

Variables:
• np – The implemented NumPy API.

• fallback_np – Regular NumPy, usually.

• forward_pass_np – NumPy API used in forward pass.

See:

Note

Every attribute of this class should be stateless!

Applies modifications to inputs in custom gradients.

hafnian(matrix: ndarray, reduce_on: ndarray) #

Calculates the hafnian of a matrix with prescribed reduction array.

This function first performs a reduction by a reduction array $$S$$, and then calculates the hafnian. Succintly, this function should implement $$A \mapsto \operatorname{haf}(A_{(S)})$$.

Parameters:
Returns:

The hafnian of the matrix.

Return type:

float

loop_hafnian(matrix: ndarray, diagonal: ndarray, reduce_on: ndarray) #

Calculates the hafnian of a matrix with prescribed reduction array.

This function first fills the diagonals with $$D$$, then performs a reduction by a reduction array $$S$$ and then calculates the hafnian. Succintly, this function should implement $$A \mapsto \operatorname{lhaf}(\operatorname{filldiag}(A, D)_{(S)})$$.

Parameters:
Returns:

The hafnian of the matrix.

Return type:

float

assign(array, index, value)#

Item assignment.

Filling an array of a given shape with the given indices and update values.

Equivalent to tf.scatter_nd().

block(arrays)#

Assembling submatrices into a single matrix.

Equivalent to numpy.block().

block_diag(*arrs)#

Putting together matrices as a block diagonal matrix.

Equivalent to scipy.linalg.block_diag().

polar(matrix, side='right')#

Polar decomposition.

Equivalent to scipy.linalg.polar().

Parameters:
• matrix (numpy.ndarray) – The input matrix

• side (str, optional) – The order of the decomposition. Defaults to “right”.

logm(matrix)#

Matrix logarithm.

Equivalent to scipy.linalg.logm().

Parameters:

matrix (numpy.ndarray) – The input matrix.

expm(matrix)#

Matrix exponential.

Equivalent to scipy.linalg.expm().

Parameters:

matrix (numpy.ndarray) – The input matrix.

powm(matrix, power)#

Matrix power.

Equivalent to numpy.linalg.matrix_power().

Parameters:

matrix (numpy.ndarray) – The input matrix.

Parameters:

func – The function for which custom gradient is defined.

accumulator(dtype, size, **kwargs)#

Datatype to collect NumPy arrays.

Common generalization of a Python list and tf.TensorArray.

write_to_accumulator(accumulator, index, value)#

Append an element to the accumulator.

Common generalization of a Python list appending and tf.TensorArray.write.

stack_accumulator(accumulator)#

Stack elements in the accumulator.

Common generalization of numpy.stack() and tf.TensorArray.stack.

decorator(func)#

Decorates heavy computations in Piquasso.

Parameters:

func – Function to decorate.

gather_along_axis_1(array, indices)#

Gathering values along axis 1 of a matrix.

Note

Gather along axis 1 was terribly slow in Tensorflow, see https://github.com/tensorflow/ranking/issues/160.

transpose(matrix)#

Matrix transposition.

Parameters:

matrix (numpy.ndarray) – The input matrix.