Connector

class BaseConnector

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 – Usually the original NumPy API for calculations that are not required to be present on the computation graph.

  • forward_pass_np – NumPy API used in the forward pass.

See:

Note

Every attribute of this class should be stateless!

abstract preprocess_input_for_custom_gradient(value)

Applies modifications to inputs in custom gradients.

abstract permanent(matrix: ndarray, rows: Tuple[int, ...], columns: Tuple[int, ...]) float

Calculates the permanent of a matrix with row and column repetitions.

abstract hafnian(matrix: ndarray, reduce_on: ndarray) float

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

abstract loop_hafnian(matrix: ndarray, diagonal: ndarray, reduce_on: ndarray) float

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

abstract loop_hafnian_batch(matrix: ndarray, diagonal: ndarray, reduce_on: ndarray, cutoff: int) float

Batch loop hafnian calculation.

Same as loop_hafnian(), but with batching, according to https://arxiv.org/abs/2108.01622.

abstract assign(array, index, value)

Item assignment.

abstract scatter(indices, updates, shape)

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

Equivalent to tf.scatter_nd().

abstract embed_in_identity(matrix, indices, dim)

Embeds a matrix in identity.

abstract block(arrays)

Assembling submatrices into a single matrix.

Equivalent to numpy.block().

abstract block_diag(*arrs)

Putting together matrices as a block diagonal matrix.

Equivalent to scipy.linalg.block_diag().

abstract 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”.

abstract logm(matrix)

Matrix logarithm.

Equivalent to scipy.linalg.logm().

Parameters:

matrix (numpy.ndarray) – The input matrix.

abstract expm(matrix)

Matrix exponential.

Equivalent to scipy.linalg.expm().

Parameters:

matrix (numpy.ndarray) – The input matrix.

abstract powm(matrix, power)

Matrix power.

Equivalent to numpy.linalg.matrix_power().

Parameters:

matrix (numpy.ndarray) – The input matrix.

abstract custom_gradient(func)

Custom gradient wrapper.

Parameters:

func – The function for which custom gradient is defined.

abstract accumulator(dtype, size, **kwargs)

Datatype to collect NumPy arrays.

Common generalization of a Python list and tf.TensorArray.

abstract write_to_accumulator(accumulator, index, value)

Append an element to the accumulator.

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

abstract stack_accumulator(accumulator)

Stack elements in the accumulator.

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

abstract decorator(func)

Decorates heavy computations in Piquasso.

Parameters:

func – Function to decorate.

abstract 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.

abstract transpose(matrix)

Matrix transposition.

Parameters:

matrix (numpy.ndarray) – The input matrix.

abstract calculate_interferometer_on_fock_space(interferometer, helper_indices)

Calculates the interferometer unitary matrix on the Fock space.

Parameters:
  • interferometer – One-particle unitary corresponding to the interferometer.

  • helper_indices – Separately calculated helper indices.

Returns:

All the n-particle unitary matrices corresponding to the interferometer up to cutoff.

abstract pfaffian(matrix)

Calculates the pfaffian of a matrix.

abstract real_logm(matrix)

Calculates the real logarithm of a matrix.

abstract calculate_interferometer_on_fermionic_fock_space(matrix, cutoff)

Calculates the representation of the interferometer on the Fock space.

abstract apply_fermionic_passive_linear_to_state_vector(representations, state_vector, modes, d, cutoff)

Applies a passive linear gate to a state vector expressed in the Fock basis.