qubosolver.bitstrings
qubosolver.Bitstrings
module-attribute
2-D int8 tensor of shape (n, m) representing a batch of n bitstrings each of
length m.
qubosolver.bitstrings
Batch bitstring utilities for QUBO solvers.
A Bitstrings collection is a 2-D torch.int8 tensor of shape
(count, n_bits), where each row is an individual bitstring.
This module provides factory functions and converters for creating and
manipulating batches of bitstrings on the globally configured torch device.
Typical usage:
bs = bitstrings.from_strings(["1010", "0110", "1100"])
ss = bitstrings.to_strings(bs) # ["1010", "0110", "1100"]
z = bitstrings.zeros(4, 8) # 4 zero bitstrings of length 8
f = bitstrings.round([[1.0, 0.0, 0.9999999]]) # from a MIP solver's output
See also qubosolver.bitstring for single-bitstring operations.
Functions:
-
as_tensorβConvenience wrapper for
torch.as_tensorthat converts data to a bitstrings tensor. -
deviceβReturns the globally configured torch device.
-
dtypeβReturns the dtype used for bitstrings (
torch.int8). -
from_stringsβCreates a 2-D bitstrings tensor from a sequence of '0'/'1' strings.
-
randβCreates a 2-D bitstrings tensor with independent uniformly random bits.
-
roundβRounds near-integral float values to a 2-D bitstrings tensor.
-
tensorβCreates a 2-D bitstrings tensor from the given data.
-
to_stringsβConverts a 2-D bitstrings tensor into a list of '0'/'1' strings.
-
zerosβCreates a zero-filled 2-D bitstrings tensor.
-
zeros_fieldβCreates a dataclass field defaulting to a zero-filled bitstrings tensor.
as_tensor
as_tensor(data: Any) -> Bitstrings
Convenience wrapper for torch.as_tensor that converts data to a bitstrings tensor.
Avoids a copy when possible. If data is already a tensor with the right dtype and on
the right device, it is returned as-is, sharing the same underlying memory. A numpy
array is also shared rather than copied if it already has int8 dtype and the global
device is cpu (numpy arrays only live on CPU, so any other dtype or device forces a
copy). Lists, tuples, and other array-like inputs are always copied.
Parameters:
-
data(Any) βInput data (tensor, numpy array, nested list, etc.).
Returns:
-
BitstringsβA 2-D
int8tensor on the global device.
Source code in qubosolver/types/bitstrings.py
device
dtype
from_strings
from_strings(strings: Sequence[str], *, device: torch.device | None = None) -> Bitstrings
Creates a 2-D bitstrings tensor from a sequence of '0'/'1' strings.
Parameters:
-
strings(Sequence[str]) βA sequence of strings, each consisting of '0' and '1' characters. All strings must have the same length.
-
device(torch.device | None, default:None) βTorch device for the tensor.
Returns:
-
BitstringsβA 2-D
int8tensor of shape(len(strings), len(strings[0])), possibly empty.
Raises:
-
ValueErrorβIf the strings have differing lengths.
Source code in qubosolver/types/bitstrings.py
rand
rand(count: int, n_bits: int, *, device: torch.device | None = None, rng: torch.Generator | None = None) -> Bitstrings
Creates a 2-D bitstrings tensor with independent uniformly random bits.
Parameters:
-
count(int) βNumber of bitstrings (rows).
-
n_bits(int) βLength of each bitstring (columns).
-
device(torch.device | None, default:None) βTorch device for the tensor.
-
rng(torch.Generator | None, default:None) βPyTorch random number generator controlling the sampling.
Returns:
-
BitstringsβA 2-D
int8tensor of shape(count, n_bits)containing 0s and 1s.
Source code in qubosolver/types/bitstrings.py
round
round(data: Any, *, atol: float = 1e-06, device: torch.device | None = None) -> Bitstrings
Rounds near-integral float values to a 2-D bitstrings tensor.
Values are compared in float64 regardless of the globally configured
float dtype, so atol keeps its meaning even when the global dtype is
narrower (e.g. float32, which would round 0.9999999998 to exactly
1.0 before the check could see it).
Parameters:
-
data(Any) βInput data (tensor, numpy array, nested list, etc.) of floats, each within atol of 0 or 1. Nested sequences must not be ragged.
-
atol(float, default:1e-06) βMaximum absolute distance from 0 or 1 tolerated before raising.
-
device(torch.device | None, default:None) βTorch device for the tensor.
Returns:
-
BitstringsβA 2-D
int8tensor of shape(count, n_bits).
Raises:
-
ValueErrorβIf any value is further than atol from both 0 and 1, or if the input is a ragged nested sequence.
Source code in qubosolver/types/bitstrings.py
tensor
tensor(data: Any, *, device: torch.device | None = None, **kwargs: Any) -> Bitstrings
Creates a 2-D bitstrings tensor from the given data.
Parameters:
-
data(Any) βInput data (nested list or array-like of 0s and 1s).
-
device(torch.device | None, default:None) βTorch device for the tensor.
-
**kwargs(Any, default:{}) βExtra keyword arguments forwarded to
torch.tensor.
Returns:
-
BitstringsβA 2-D
int8tensor; an empty list gives a(0, 0)tensor.
Source code in qubosolver/types/bitstrings.py
to_strings
to_strings(bitstrings: Bitstrings) -> list[str]
Converts a 2-D bitstrings tensor into a list of '0'/'1' strings.
Parameters:
-
bitstrings(Bitstrings) βA 2-D
int8tensor of shape(n, m)containing 0s and 1s.
Returns:
Source code in qubosolver/types/bitstrings.py
zeros
zeros(count: int, n_bits: int, *, device: torch.device | None = None) -> Bitstrings
Creates a zero-filled 2-D bitstrings tensor.
Parameters:
-
count(int) βNumber of bitstrings (rows).
-
n_bits(int) βLength of each bitstring (columns).
-
device(torch.device | None, default:None) βTorch device for the tensor.
Returns:
-
BitstringsβA 2-D
int8tensor of shape(count, n_bits).
Source code in qubosolver/types/bitstrings.py
zeros_field
zeros_field(count: int, n_bits: int, *, device: torch.device | None = None) -> Bitstrings
Creates a dataclass field defaulting to a zero-filled bitstrings tensor.
Parameters:
-
count(int) βNumber of bitstrings (rows).
-
n_bits(int) βLength of each bitstring (columns).
-
device(torch.device | None, default:None) βTorch device for the tensor.
Returns:
-
BitstringsβA dataclass field (typed as
Bitstringsfor the enclosing class) whose -
Bitstringsβdefault_factorybuilds a fresh zero tensor per instance.