Utilities

batch_indexer_info

Process the indexer passed to a __getitem__ call and return the batch index and the number of removed and inserted batch dimensions.

generate_batch_str

Generate a string of characters to represent the batch dimensions.

lin_sup_batch_str

Given a batch string, appends the linear superposition batch dimension to the end.

outer_product_batch_str

Creates the einsum string for the outer product of the given tuple of dimensions.

reshape_args_to_batch_string

Reshapes arguments to match the batch string by inserting singleton dimensions where needed so that they are broadcastable.

zip_batch_strings

Creates a batch string for zipping over the batch dimensions.

batch_indexer_info

mrmustard.physics.utils.batch_indexer_info(indexer, batch_dims)[source]

Process the indexer passed to a __getitem__ call and return the batch index and the number of removed and inserted batch dimensions.

Parameters:
  • indexer (int | slice | None | ellipsis | Sequence[int | slice | None | ellipsis]) – The indexer passed to a __getitem__ call.

  • batch_dims (int) – The number of batch dimensions of the ansatz (including eventual linear superposition dimension)

Returns:

A tuple containing the final index, the number of removed batch dimensions, and the number of inserted batch dimensions.

Raises:
  • TypeError – If the indexer contains any types other than int, slice, None, or Ellipsis.

  • IndexError – If the indexer contains more than one Ellipsis.

  • IndexError – If the indexer consumes more batch dimensions than the ansatz has.

  • IndexError – If the indexer contains an Ellipsis in the middle of the indexer.

Return type:

tuple[tuple[int | slice, …], int, int]

generate_batch_str

mrmustard.physics.utils.generate_batch_str(batch_dim, offset=0)[source]

Generate a string of characters to represent the batch dimensions.

Parameters:
  • batch_dim (int) – The number of batch dimensions.

  • offset (int) – The offset of the characters.

Returns:

A string of characters to represent the batch dimensions.

Return type:

str

lin_sup_batch_str

mrmustard.physics.utils.lin_sup_batch_str(batch_str)[source]

Given a batch string, appends the linear superposition batch dimension to the end.

Parameters:

batch_str (str) – The batch string to append the linear superposition batch dimension to.

Returns:

The batch string with the linear superposition batch dimension appended to the end.

Return type:

str

outer_product_batch_str

mrmustard.physics.utils.outer_product_batch_str(*batch_dims, lin_sup=None)[source]

Creates the einsum string for the outer product of the given tuple of dimensions. E.g. for (2,1,3) it returns ab,c,def->abcdef. If lin_sup is provided, the linear superposition dimensions are moved to the end. E.g. for (2,1,3) and lin_sup=(0,1) it returns ab,c,def->adefbc, as b and c are the linear superposition dimensions of the 0th and 1st tensors.

Parameters:
  • batch_dims (int)

  • lin_sup (tuple[int, ...] | None)

Return type:

str

reshape_args_to_batch_string

mrmustard.physics.utils.reshape_args_to_batch_string(args, batch_string)[source]

Reshapes arguments to match the batch string by inserting singleton dimensions where needed so that they are broadcastable. E.g. given two arrays of shape (2,7) and (3,7) and string ab,cb->abc, it reshapes them to shape (2,7,1) and (1,7,3).

Parameters:
  • args (list[ArrayLike])

  • batch_string (str)

Return type:

tuple[list[ArrayLike], tuple[int, …]]

zip_batch_strings

mrmustard.physics.utils.zip_batch_strings(*batch_dims)[source]

Creates a batch string for zipping over the batch dimensions.

Parameters:

batch_dims (int)

Return type:

str