QUBODataset(coefficients, solutions=None)
Bases: Dataset
Represents a dataset for Quadratic Unconstrained Binary Optimization (QUBO) problems.
ATTRIBUTE | DESCRIPTION |
---|---|
coefficients |
Tensor of shape (size, size, num_instances), containing the QUBO coefficient matrices.
TYPE:
|
solutions |
Optional list of QUBOSolution objects corresponding to each instance in the dataset.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__len__ |
Returns the number of instances in the dataset. |
__getitem__ |
Retrieves the coefficient matrix and optionally the solution for the specified index. |
from_random |
Class method to generate a QUBODataset with random coefficient matrices. |
Initializes a QUBODataset.
PARAMETER | DESCRIPTION |
---|---|
coefficients
|
Tensor of shape (size, size, num_instances), containing the QUBO coefficient matrices.
TYPE:
|
solutions
|
Optional list of QUBOSolution objects corresponding to each instance in the dataset.
TYPE:
|
Source code in qubosolver/data.py
__getitem__(idx)
Retrieves the coefficient matrix and optionally the solution for the specified index.
PARAMETER | DESCRIPTION |
---|---|
idx
|
Index of the dataset instance to retrieve.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Tensor, QUBOSolution | None]
|
tuple[torch.Tensor, QUBOSolution | None]: The coefficient matrix of shape (size, size) and optionally the corresponding QUBOSolution. |
Source code in qubosolver/data.py
__len__()
Returns the number of instances in the dataset.
RETURNS | DESCRIPTION |
---|---|
int
|
The number of coefficient matrices (num_instances).
TYPE:
|
from_random(n_matrices, matrix_dim, densities=[0.5], coefficient_bounds=(-10.0, 10.0), device='cpu', dtype=torch.float32, seed=None)
classmethod
Generates a QUBODataset with random QUBO coefficient matrices.
Generation Steps: 1. Initialize a reproducible random generator. 2. Create a storage tensor for coefficients. 3. For each density: a. Compute the exact target number of non-zero elements. b. For each instance: i. Generate a symmetric boolean mask with an exact number of True elements. ii. Generate random values within the coefficient_bounds. iii. Apply the mask to zero out unselected elements. iv. Symmetrize the matrix by mirroring the upper triangle onto the lower triangle. v. Force all off-diagonal coefficients to be positive. vi. Ensure that at least one diagonal element is negative. vii. Ensure at least one coefficient equals the upper bound, excluding any diagonal already at the lower bound. 4. Return a QUBODataset instance containing the generated matrices.
PARAMETER | DESCRIPTION |
---|---|
n_matrices
|
Number of QUBO matrices to generate for each density.
TYPE:
|
matrix_dim
|
The dimension of each QUBO matrix.
TYPE:
|
densities
|
List of densities (ratio of non-zero elements). Defaults to [0.5].
TYPE:
|
coefficient_bounds
|
Range (min, max) of random values for the coefficients. Defaults to (-10.0, 10.0).
TYPE:
|
device
|
Device for the tensors. Defaults to "cpu".
TYPE:
|
dtype
|
Data type for the coefficient tensors. Defaults to torch.float32.
TYPE:
|
seed
|
Seed for reproducibility. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
QUBODataset
|
A dataset containing the generated coefficient matrices.
TYPE:
|
Source code in qubosolver/data.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
QUBOSolution(bitstrings, costs, counts=None, probabilities=None, solution_status=SolutionStatusType.UNPROCESSED)
dataclass
Represents a solution to a QUBO problem.
ATTRIBUTE | DESCRIPTION |
---|---|
bitstrings |
Tensor of shape (num_solutions, bitstring_length), containing the bitstring solutions. Each entry is an integer tensor with 0s and 1s.
TYPE:
|
counts |
Tensor of shape (num_solutions,), containing the count of occurrences of each bitstring. Optional, can be None.
TYPE:
|
probabilities |
Tensor of shape (num_solutions,), containing the probability of each bitstring solution. Optional, can be None.
TYPE:
|
costs |
Tensor of shape (num_solutions,), containing the cost associated with each bitstring solution.
TYPE:
|
compute_costs(instance)
Computes the cost for each bitstring solution based on the provided QUBO instance.
PARAMETER | DESCRIPTION |
---|---|
instance
|
The QUBO instance containing the QUBO matrix.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: A tensor of costs for each bitstring. |
Source code in qubosolver/data.py
compute_probabilities()
Computes the probabilities of each bitstring solution based on their counts.
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: A tensor of probabilities for each bitstring. |
Source code in qubosolver/data.py
sort_by_cost()
Sorts the QUBOSolution in-place by increasing cost.
Reorders bitstrings, costs, counts, and probabilities (if available) based on the ascending order of the costs.
Source code in qubosolver/data.py
Utility functions
generate_symmetric_mask(size, target, device, generator)
Generate a symmetric boolean mask with an exact number of True elements
to match a certain density of QUBO.
Used in the from_random
method of QUBODataset
.
PARAMETER | DESCRIPTION |
---|---|
size
|
Size of problem.
TYPE:
|
target
|
Target number of elements.
TYPE:
|
device
|
Torch device.
TYPE:
|
generator
|
generator for randomness.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: Mask. |
Source code in qubosolver/data_utils.py
load_qubo_dataset(filepath)
Loads a QUBODataset from a file.
Notes:
The file should contain data saved in the format used by save_qubo_dataset
.
PARAMETER | DESCRIPTION |
---|---|
filepath
|
Path to the file from which the QUBODataset will be loaded.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
QUBODataset
|
The loaded QUBODataset object.
TYPE:
|
Source code in qubosolver/saveload.py
load_qubo_instance(filepath)
Loads a QUBOInstance from a file.
PARAMETER | DESCRIPTION |
---|---|
filepath
|
Path to the file from which the QUBOInstance will be loaded.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
QUBOInstance
|
The loaded QUBOInstance object.
TYPE:
|
Notes
The file should contain data saved in the format used by save_qubo_instance
.
Source code in qubosolver/saveload.py
save_qubo_dataset(dataset, filepath)
Saves a QUBODataset to a file.
PARAMETER | DESCRIPTION |
---|---|
dataset
|
The QUBODataset object to save.
TYPE:
|
filepath
|
Path to the file where the QUBODataset will be saved.
TYPE:
|
Notes
The saved data includes: - Coefficients (size x size x num_instances tensor) - Solutions (optional, includes bitstrings, counts, probabilities, and costs)
Source code in qubosolver/saveload.py
save_qubo_instance(instance, filepath)
Saves a QUBOInstance to a file.
PARAMETER | DESCRIPTION |
---|---|
instance
|
The QUBOInstance object to save.
TYPE:
|
filepath
|
Path to the file where the QUBOInstance will be saved.
TYPE:
|
Notes
The saved data includes: - Coefficients (N x N matrix) - Device (e.g., 'cpu' or 'cuda') - Data type (e.g., torch.float32) - Solution (optional)