Skip to content

CqmTranslator

Utility class for converting between dimod.BinaryQuadraticModel (CQM) and symbolic models.

CqmTranslator provides methods to: - Convert a CQM into a symbolic Model - Convert a Model (with quadratic objective) into a CQM

These conversions are especially useful when interacting with external solvers or libraries that operate on CQMs.

Examples:

>>> import dimod
>>> import numpy as np
>>> from luna_quantum import CqmTranslator, Vtype
>>> bqm = dimod.generators.gnm_random_bqm(5, 10, "BINARY")

Create a model from a matrix:

>>> model = CqmTranslator.to_aq(bqm, name="bqm_model")

Convert it back to a dense matrix:

>>> recovered = CqmTranslator.from_aq(model)

from_aq staticmethod

from_aq(model: Model) -> ConstrainedQuadraticModel

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
BinaryQuadraticModel

The resulting CQM.

Raises:

Type Description
TranslationError

If the translation fails for some reason.

to_aq staticmethod

to_aq(cqm: ConstrainedQuadraticModel) -> Model

Convert a CQM into a symbolic Model.

Parameters:

Name Type Description Default
cqm ConstrainedQuadraticModel

The CQM.

required

Returns:

Type Description
Model

A symbolic model representing the given CQM.

Raises:

Type Description
TypeError

If cqm is not of type ConstrainedQuadraticModel.

TranslationError

If the translation fails for some reason.