Skip to content

LpTranslator

Utility class for converting between LP files and symbolic models.

LpTranslator provides methods to: - Convert an LP file into a symbolic Model - Convert a Model into an Lp file.

These conversions are especially useful when interacting with external solvers or libraries that operate on LP-based problem definitions.

Examples:

>>> from pathlib import Path
>>> from luna_quantum.translator import LpTranslator
>>> lp_filepath = Path("path/to/the/lp_file")
>>> model = LpTranslator.to_aq(lp_filepath)

Convert it back to an LP file:

>>> recovered = LpTranslator.to_file(model)

from_aq staticmethod

from_aq(model: Model) -> str
from_aq(model: Model, *, filepath: Path) -> None
from_aq(model: Model, *, filepath: Path | None = ...) -> None

Convert a symbolic model to an LP file representation.

Parameters:

Name Type Description Default
model Model

The symbolic model to convert.

required
file Path

The filepath to write the model contents to.

required

Returns:

Type Description
str

If no file to write to is given, i.e., the file is None.

Raises:

Type Description
TranslationError

If the translation fails for some reason.

to_aq staticmethod

to_aq(file: Path) -> Model
to_aq(file: str) -> Model
to_aq(file: str | Path) -> Model

Convert an LP file into a symbolic Model.

Parameters:

Name Type Description Default
file str | Path

An LP file representing a symbolic model, either given as a Path object to the LP file or its contents as a string. If you pass the path as a string, it will be interpreted as a model and thus fail to be parsed to a Model.

required

Returns:

Type Description
Model

A symbolic model representing the given lp file structure.

Raises:

Type Description
TypeError

If file is not of type str or Path.

TranslationError

If the translation fails for a different reason.