QUBOInstance
QUBOInstance(coefficients=None, device='cpu', dtype=torch.float32)
Represents a single instance of a Quadratic Unconstrained Binary Optimization (QUBO) problem.
| ATTRIBUTE | DESCRIPTION |
|---|---|
coefficients |
Tensor of shape (size, size), representing the QUBO coefficients.
TYPE:
|
device |
Device where tensors are allocated (e.g., "cpu" or "cuda").
TYPE:
|
dtype |
Data type of the tensors (e.g., torch.float32).
TYPE:
|
solution |
Solution to the QUBO problem, if available.
TYPE:
|
density |
Fraction of non-zero entries in the coefficient matrix.
TYPE:
|
density_type |
Classification of the density (e.g., sparse, dense).
TYPE:
|
Initializes a QUBOInstance.
| PARAMETER | DESCRIPTION |
|---|---|
coefficients
|
Coefficients of the QUBO problem. Can be a dictionary, array-like object, or None.
TYPE:
|
device
|
Device where tensors are allocated (default: "cpu").
TYPE:
|
dtype
|
Data type of the tensors (default: torch.float32).
TYPE:
|
Source code in qubosolver/qubo_instance.py
coefficients
property
writable
Getter for the QUBO coefficient matrix.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
torch.Tensor: Tensor of shape (size, size) representing the QUBO coefficients. |
device
property
Get the device on which the coefficient tensor is stored.
| RETURNS | DESCRIPTION |
|---|---|
device
|
torch.device: The device (e.g., device("cpu") or device("cuda:0")). |
dtype
property
Get the data type of the coefficient tensor.
| RETURNS | DESCRIPTION |
|---|---|
dtype
|
torch.dtype: The dtype (e.g., torch.float32). |
normalized_coefficients
property
Return the coefficient matrix normalised by the maximum off-diagonal value.
All coefficients are divided by _max_off_diag so that the largest
off-diagonal entry becomes 1.0. Useful for embedding algorithms that
require unit-scaled interactions.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
torch.Tensor: Normalised coefficient matrix of shape (size, size). |
size
property
Get the size of the QUBO matrix (number of variables).
| RETURNS | DESCRIPTION |
|---|---|
int
|
Size of the QUBO matrix.
TYPE:
|
__repr__()
Returns a string representation of the QUBOInstance.
| RETURNS | DESCRIPTION |
|---|---|
str
|
A dictionary-like string summarizing the instance.
TYPE:
|
Source code in qubosolver/qubo_instance.py
evaluate_solution(solution)
Evaluates a solution for the QUBO problem.
| PARAMETER | DESCRIPTION |
|---|---|
solution
|
Solution vector to evaluate.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The cost of the given solution.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the solution size does not match the QUBO size. |
Source code in qubosolver/qubo_instance.py
load(file_like)
staticmethod
Deserialises a QUBOInstance from a binary file-like object.
Reconstructs the instance from a file previously created with
QUBOInstance.save.
| PARAMETER | DESCRIPTION |
|---|---|
file_like
|
A file-like object opened in binary read mode containing the serialised QUBOInstance data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QUBOInstance
|
A new QUBOInstance reconstructed from the file.
TYPE:
|
Example
with open("instance.bin", "rb") as f: ... instance = QUBOInstance.load(f)
Source code in qubosolver/qubo_instance.py
save(file_like, instance)
staticmethod
Serialises a QUBOInstance to a binary file-like object.
Uses torch.save internally to persist the coefficient tensor.
| PARAMETER | DESCRIPTION |
|---|---|
file_like
|
A file-like object opened in binary write mode (e.g., an open file or a BytesIO buffer).
TYPE:
|
instance
|
The QUBOInstance to serialise.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
None |
Example
with open("instance.bin", "wb") as f: ... QUBOInstance.save(f, my_instance)
Source code in qubosolver/qubo_instance.py
set_coefficients(new_coefficients=None)
Updates the coefficients of the QUBO problem.
| PARAMETER | DESCRIPTION |
|---|---|
new_coefficients
|
Dictionary of new coefficients to set. Keys are (row, column) tuples.
TYPE:
|
Source code in qubosolver/qubo_instance.py
update_metrics()
Updates the density metrics of the QUBO problem.