qubosolver.bitstring
qubosolver.Bitstring
module-attribute
1-D int8 tensor of shape (n,) representing a single bitstring of 0s and 1s.
qubosolver.bitstring
Bitstring utilities for QUBO solvers.
A Bitstring is a 1-D torch.int8 tensor whose elements are 0 or 1.
This module provides factory functions and converters for creating and
manipulating bitstrings on the globally configured torch device.
Typical usage:
bs = bitstring.from_string("1010")
s = bitstring.to_string(bs) # "1010"
z = bitstring.zeros(4) # tensor([0, 0, 0, 0], dtype=torch.int8)
f = bitstring.round([1.0, 0.0, 0.9999999]) # from a MIP solver's output
Functions:
-
as_tensorβConvenience wrapper for
torch.as_tensorthat converts data to a bitstring tensor. -
deviceβReturns the globally configured torch device.
-
dtypeβReturns the dtype used for bitstrings (
torch.int8). -
from_stringβCreates a bitstring tensor from a string of '0' and '1' characters.
-
randβCreates a bitstring of length n with independent uniformly random bits.
-
roundβRounds near-integral float values to a bitstring tensor.
-
tensorβCreates a bitstring tensor from the given data.
-
to_stringβConverts a bitstring tensor to its string representation.
-
zerosβCreates a zero-filled bitstring of length n.
-
zeros_fieldβCreates a dataclass field defaulting to a zero-filled bitstring.
as_tensor
Convenience wrapper for torch.as_tensor that converts data to a bitstring 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, list, tuple, etc.).
Returns:
-
BitstringβA 1-D
int8tensor on the global device.
Source code in qubosolver/types/bitstring.py
device
dtype
from_string
Creates a bitstring tensor from a string of '0' and '1' characters.
Parameters:
-
s(str) βA string consisting of '0' and '1' characters.
-
device(torch.device | None, default:None) βTorch device for the tensor.
Returns:
-
BitstringβA 1-D
int8tensor.
Source code in qubosolver/types/bitstring.py
rand
rand(n: int, *, device: torch.device | None = None, rng: torch.Generator | None = None) -> Bitstring
Creates a bitstring of length n with independent uniformly random bits.
Parameters:
-
n(int) βLength of the bitstring.
-
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:
-
BitstringβA 1-D
int8tensor of 0s and 1s.
Source code in qubosolver/types/bitstring.py
round
Rounds near-integral float values to a bitstring 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, list, etc.) of floats, each within atol of 0 or 1.
-
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:
-
BitstringβA 1-D
int8tensor of 0s and 1s.
Raises:
-
ValueErrorβIf any value is further than atol from both 0 and 1.
Source code in qubosolver/types/bitstring.py
tensor
Creates a bitstring tensor from the given data.
Parameters:
-
data(Any) βInput data (list, tuple, 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:
-
BitstringβA 1-D
int8tensor.
Source code in qubosolver/types/bitstring.py
to_string
zeros
Creates a zero-filled bitstring of length n.
Parameters:
-
n(int) βLength of the bitstring.
-
device(torch.device | None, default:None) βTorch device for the tensor.
Returns:
-
BitstringβA 1-D
int8tensor of zeros.
Source code in qubosolver/types/bitstring.py
zeros_field
Creates a dataclass field defaulting to a zero-filled bitstring.
Parameters:
-
n(int) βLength of the bitstring.
-
device(torch.device | None, default:None) βTorch device for the tensor.
Returns:
-
BitstringβA dataclass field (typed as
Bitstringfor the enclosing class) whose -
Bitstringβdefault_factorybuilds a fresh zero tensor per instance.