microquantum.stdlib.bits¶
Standard library: bitstring and integer utilities (Phase 117).
Bitstring conventions match the rest of the SDK: measurement outcomes are
MSB-first strings ("101") whose unsigned-binary integer value is
int(bitstring, 2). Every helper here adopts the same left-to-right,
most-significant-bit-first order, so int_to_bits / bits_to_int and
int_to_bitstring / bitstring_to_int are exact inverses of one
another.
These are dependency-light, application-independent building blocks intended for user programs, the runtime and the compiler alike.
Module Contents¶
- microquantum.stdlib.bits.int_to_bitstring(value, width=None)[source]¶
Convert a non-negative integer to an MSB-first binary bitstring.
- Parameters:
- Returns:
A
0/1string with no leading zeros (unlesswidthpads it).- Raises:
TypeError – If
value/widthis not anint.ValueError – If
valueis negative, orwidthis too small.
- Return type:
- microquantum.stdlib.bits.bitstring_to_int(bitstring)[source]¶
Convert an MSB-first bitstring to its unsigned integer value.
- Parameters:
bitstring (str) – Non-empty string of
0/1characters.- Returns:
The integer value (MSB first), i.e.
int(bitstring, 2).- Raises:
TypeError – If
bitstringis not astr.ValueError – If
bitstringis empty or contains non-binary chars.
- Return type:
- microquantum.stdlib.bits.int_to_bits(value, width=None)[source]¶
Convert a non-negative integer to an MSB-first tuple of bits.
- Parameters:
- Returns:
A tuple of
0/1entries in MSB-first order (no leading zeros unlesswidthpads them).- Raises:
TypeError – If
value/widthis not anint.ValueError – If
valueis negative, orwidthis too small.
- Return type:
- microquantum.stdlib.bits.bits_to_int(bits)[source]¶
Convert an MSB-first iterable of bits to its integer value.
- Parameters:
bits (Iterable[int]) – A non-empty iterable of integer bits (
0or1), read in MSB-first order.- Returns:
The unsigned integer value the bits represent.
- Raises:
TypeError – If any element is not an
int.ValueError – If
bitsis empty or contains values other than 0/1.
- Return type:
- microquantum.stdlib.bits.hamming_weight(value)[source]¶
Number of set bits in an integer or a
0/1bitstring.- Parameters:
value (int | str) – Either a non-negative integer (its set bits counted) or an MSB-first bitstring (its
'1'characters counted).- Returns:
The Hamming weight.
- Raises:
TypeError – If
valueis neither anintnor astr.ValueError – If the integer is negative, or the bitstring is empty or non-binary.
- Return type:
- microquantum.stdlib.bits.hamming_distance(left, right)[source]¶
Number of positions where two integers or bitstrings differ.
- Parameters:
- Returns:
The Hamming distance.
- Raises:
TypeError – If the arguments have different kinds, or either is not an
int/str.ValueError – If an integer is negative, or a bitstring is empty, non-binary, or the two bitstrings have different lengths.
- Return type: