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!

is_abstract(value: Any) bool

Checks whether a value is abstract in the given framework.

Parameters:

value – The value to check.

Returns:

Whether the value is abstract.

Return type:

bool

abstractmethod preprocess_input_for_custom_gradient(value)

Applies modifications to inputs in custom gradients.

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

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

abstractmethod 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

abstractmethod 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

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

abstractmethod assign(array, index, value)

Item assignment.

abstractmethod scatter(indices, updates, shape)

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

Equivalent to tf.scatter_nd().

abstractmethod embed_in_identity(matrix, indices, dim)

Embeds a matrix in identity.

abstractmethod block(arrays)

Assembling submatrices into a single matrix.

Equivalent to numpy.block().

abstractmethod block_diag(*arrs)

Putting together matrices as a block diagonal matrix.

Equivalent to scipy.linalg.block_diag().

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

abstractmethod logm(matrix)

Matrix logarithm.

Equivalent to scipy.linalg.logm().

Parameters:

matrix (numpy.ndarray) – The input matrix.

abstractmethod expm(matrix)

Matrix exponential.

Equivalent to scipy.linalg.expm().

Parameters:

matrix (numpy.ndarray) – The input matrix.

abstractmethod powm(matrix, power)

Matrix power.

Equivalent to numpy.linalg.matrix_power().

Parameters:

matrix (numpy.ndarray) – The input matrix.

abstractmethod custom_gradient(func)

Custom gradient wrapper.

Parameters:

func – The function for which custom gradient is defined.

abstractmethod accumulator(dtype, size, **kwargs)

Datatype to collect NumPy arrays.

Common generalization of a Python list and tf.TensorArray.

abstractmethod write_to_accumulator(accumulator, index, value)

Append an element to the accumulator.

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

abstractmethod stack_accumulator(accumulator)

Stack elements in the accumulator.

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

abstractmethod decorator(func)

Decorates heavy computations in Piquasso.

Parameters:

func – Function to decorate.

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

abstractmethod transpose(matrix)

Matrix transposition.

Parameters:

matrix (numpy.ndarray) – The input matrix.

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

abstractmethod pfaffian(matrix)

Calculates the pfaffian of a matrix.

abstractmethod real_logm(matrix)

Calculates the real logarithm of a matrix.

abstractmethod calculate_interferometer_on_fermionic_fock_space(matrix, cutoff)

Calculates the representation of the interferometer on the Fock space.

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

abstractmethod permanent_laplace(matrix: ndarray, rows: Tuple[int, ...], cols: Tuple[int, ...]) ndarray

Calculates the permanent of a matrix corresponding to the Laplace expansion.

Here, the sum of rows is equal to the sum of cols plus one, and all the permanents corresponding to the row multiplicities given by rows and column multiplicities given by cols minus one from each column are calculated.

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

  • rows (Tuple[int, ...]) – Multiplicity vector specifying how many times each row is repeated (i.e., the row multiplicities).

  • cols (Tuple[int, ...]) – Multiplicity vector specifying how many times each column is repeated (i.e., the column multiplicities).

Returns:

The permanents of the submatrices corresponding to the

Laplace expansion.

Return type:

numpy.ndarray