QuboTranslator ¶
Utility class for converting between dense QUBO matrices and symbolic models.
QuboTranslator
provides methods to:
- Convert a NumPy-style QUBO matrix into a symbolic Model
- Convert a Model
(with quadratic objective) into a dense QUBO matrix
These conversions are especially useful when interacting with external solvers or libraries that operate on matrix-based problem definitions.
Examples:
>>> import numpy as np
>>> from luna_quantum import QuboTranslator, Vtype
>>> q = np.array([[1.0, -1.0], [-1.0, 2.0]])
Create a model from a matrix:
Convert it back to a dense matrix:
from_aq
staticmethod
¶
from_aq(model: Model) -> Qubo
Convert a symbolic model to a dense QUBO matrix representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Model
|
The symbolic model to convert. The objective must be quadratic-only and unconstrained. |
required |
Returns:
Type | Description |
---|---|
Qubo
|
An object representing a QUBO with information additional to the square NumPy array
representing the QUBO matrix derived from the model's objective. This object also
includes the |
Raises:
Type | Description |
---|---|
TranslationError
|
Generally if the translation fails. Might be specified by one of the four following errors. |
ModelNotQuadraticError
|
If the objective contains higher-order (non-quadratic) terms. |
ModelNotUnconstrainedError
|
If the model contains any constraints. |
ModelSenseNotMinimizeError
|
If the model's optimization sense is 'maximize'. |
ModelVtypeError
|
If the model contains different vtypes or vtypes other than binary and spin. |
to_aq
staticmethod
¶
to_aq(
qubo: NDArray,
*,
offset: float | None = ...,
variable_names: list[str] | None = ...,
name: str | None = ...,
vtype: Vtype | None = ...,
) -> Model
Convert a dense QUBO matrix into a symbolic Model
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qubo
|
NDArray
|
A square 2D NumPy array representing the QUBO matrix. Diagonal entries correspond to linear coefficients; off-diagonal entries represent pairwise quadratic terms. |
required |
name
|
str
|
An optional name to assign to the resulting model. |
...
|
vtype
|
Vtype
|
The variable type to assign to all variables (e.g. Binary, Spin). |
...
|
Returns:
Type | Description |
---|---|
Model
|
A symbolic model representing the given QUBO structure. |
Raises:
Type | Description |
---|---|
TranslationError
|
Generally if the translation fails. Might be specified by the following error. |
VariableNamesError
|
If a list of variable names is provided but contains duplicates or has an incorrect length. |