bitbasis

Functions

get_local_size(n, rank, world_size)

Given global size n, and a rank in [0, world_size), return the size of

get_local_range(n, rank, world_size)

Given global size n, and a rank in [0, world_size), return the range of

get_nth_bit(val, n)

Get the nth bit of val.

flip_nth_bit(val, n)

Flip the nth bit of val.

_recursively_fill_flatconfigs(flatconfigs, n, k, c, r)

get_all_equal_weight_flatconfigs(n, k)

Get every flat configuration of length n with k bits set.

flatconfig_to_bit(flatconfig)

Given a flat configuration, return the corresponding bitstring.

comb(n, k)

Compute the binomial coefficient n choose k.

get_all_equal_weight_bits(n, k[, dtype])

Get an array of all 'bits' (integers), with n bits, and k of them set.

bit_to_rank(b, n, k)

Given a bitstring b, return the rank of the bitstring in the

rank_to_bit(r, n, k)

Given a rank r, return the bitstring of length n with k bits set

product_of_bits(b1, b2, n2[, dtype])

Get the outer product of two bit arrays.

get_number_bitbasis(*nk_pairs[, dtype])

Create a bit basis with number conservation.

build_bitmap(configs)

Build of map of bits to linear indices, suitable for use with numba.

coupled_bits_numba(bi, coupling_map)

_build_coo_numba_core(bits, coupling_map[, bitmap, dtype])

build_coo_numba(bits, coupling_map[, dtype, parallel])

Build an operator in COO form, using the basis bits and the

config_to_bit(self, config)

Encode a 'configuration' as a bit.

bit_to_config(self, bit)

Decode a bit to a configuration.

Module Contents

bitbasis.get_local_size(n, rank, world_size)

Given global size n, and a rank in [0, world_size), return the size of the portion assigned to this rank.

bitbasis.get_local_range(n, rank, world_size)

Given global size n, and a rank in [0, world_size), return the range of indices assigned to this rank.

bitbasis.get_nth_bit(val, n)

Get the nth bit of val.

Examples

>>> get_nth_bit(0b101, 1)
0
bitbasis.flip_nth_bit(val, n)

Flip the nth bit of val.

Examples

>>> bin(flip_nth_bit(0b101, 1))
0b111
bitbasis._recursively_fill_flatconfigs(flatconfigs, n, k, c, r)
bitbasis.get_all_equal_weight_flatconfigs(n, k)

Get every flat configuration of length n with k bits set.

bitbasis.flatconfig_to_bit(flatconfig)

Given a flat configuration, return the corresponding bitstring.

bitbasis.comb(n, k)

Compute the binomial coefficient n choose k.

bitbasis.get_all_equal_weight_bits(n, k, dtype=np.int64)

Get an array of all ‘bits’ (integers), with n bits, and k of them set.

bitbasis.bit_to_rank(b, n, k)

Given a bitstring b, return the rank of the bitstring in the basis of all bitstrings of length n with k bits set. Adapted from https://dlbeer.co.nz/articles/kwbs.html.

bitbasis.rank_to_bit(r, n, k)

Given a rank r, return the bitstring of length n with k bits set that has rank r in the basis of all bitstrings of length n with k bits set. Adapted from https://dlbeer.co.nz/articles/kwbs.html.

bitbasis.product_of_bits(b1, b2, n2, dtype=np.int64)

Get the outer product of two bit arrays.

Parameters:
  • b1 (array) – The bit arrays to take the outer product of.

  • b2 (array) – The bit arrays to take the outer product of.

  • n2 (int) – The number of bits in b2

bitbasis.get_number_bitbasis(*nk_pairs, dtype=np.int64)

Create a bit basis with number conservation.

Parameters:

nk_pairs (sequence of (int, int)) – Each element is a pair (n, k) where n is the number of bits, and k is the number of bits that are set. The basis will be the product of all supplied pairs.

Returns:

basis – An array of integers, each representing a bit string. The size will be prod(comb(n, k) for n, k in nk_pairs).

Return type:

ndarray

Examples

A single number conserving basis:

>>> for i, b in enumerate(get_number_bitbasis((4, 2))):
>>>    print(f"{i}: {b:0>4b}")
0: 0011
1: 0101
2: 0110
3: 1001
4: 1010
5: 1100

A product of two number conserving bases, e.g. n_up and n_down:

>>> for b in get_number_bitbasis((3, 2), (3, 1)):
>>>     print(f"{b:0>6b}")
011001
011010
011100
101001
101010
101100
110001
110010
110100
bitbasis.build_bitmap(configs)

Build of map of bits to linear indices, suitable for use with numba.

bitbasis.coupled_bits_numba(bi, coupling_map)
bitbasis._build_coo_numba_core(bits, coupling_map, bitmap=None, dtype=np.float64)
bitbasis.build_coo_numba(bits, coupling_map, dtype=None, parallel=False)
Build an operator in COO form, using the basis bits and the

coupling_map, optionally multithreaded.

Parameters:
  • bits (array) – An array of integers, each representing a bit string.

  • coupling_map (Dict[int, Dict[int, Dict[int, Tuple[int, float]]]]) – A nested numba dictionary of couplings. The outermost key is the term index, the next key is the register, and the innermost key is the bit index. The value is a tuple of (coupled bit index, coupling coefficient).

  • parallel (bool or int, optional) – Whether to parallelize the computation. If an integer is given, it specifies the number of threads to use.

Returns:

  • data (array) – The non-zero elements of the operator.

  • cis (array) – The row indices of the non-zero elements.

  • cjs (array) – The column indices of the non-zero elements.

bitbasis.config_to_bit(self, config)

Encode a ‘configuration’ as a bit.

Parameters:

config (dict[hashable, int]) – A dictionary mapping sites to their occupation number / spin.

Returns:

bit – The bit corresponding to this configuration.

Return type:

int

bitbasis.bit_to_config(self, bit)

Decode a bit to a configuration.

Parameters:

bit (int) – The bit to decode.

Returns:

config – A dictionary mapping sites to their occupation number / spin.

Return type:

dict[hashable, int]