Skip to content

Handling Optimization Formats

With aqmodels you can define your optimization problem in a uniform way so that you can switch between standard optimization formats and access various solvers.

Solvers and algorithms often support specific optimization formats, and not all formats are compatible with every solver. To bridge this gap, LunaSolve provides a robust format translation feature. This allows you to use solvers even if they don’t natively support the format your problem is defined in.

For example, you can upload a problem in QUBO format and run it with a solver that expects LP—LunaSolve will automatically handle the conversion for you. This flexibility is powerful, but keep in mind that translations may impact solver performance, especially if the original problem was tailored to a specific format.

Translate Optimization Formats into Aqmodels

With Translators in aqmodels you can easily convert your QUBO, LP, BQM or CQM into a unified Model.

from pathlib import Path
from luna_quantum.translator import LpTranslator

lp_file = Path("path/to/your_model.lp")
model = LpTranslator.to_aq(lp_file)
from luna_quantum.translator import LpTranslator

lp_string = """
Maximize
obj: 3 x + 4 y
Subject To
c1: 2 x + y <= 100
c2: x + 2 y <= 80
Bounds
0 <= x <= 40
0 <= y <= 30
End
"""

model = LpTranslator.to_aq(lp_string)
import numpy as np
from luna_quantum.translator import MatrixTranslator, Vtype

q = np.array([[1.0, -1.0],
            [-1.0, 2.0]])

model = MatrixTranslator.to_aq(q, name="qubo_model", vtype=Vtype.Binary)
from luna_quantum.translator import BqmTranslator
import dimod

# Create BQM from dictionaries
linear = {'x1': -1.0, 'x2': 0.5, 'x3': -0.8}
quadratic = {('x1', 'x2'): 2.0, ('x1', 'x3'): -1.5, ('x2', 'x3'): 1.0}
offset = 0.5

bqm = dimod.BinaryQuadraticModel(linear, quadratic, offset, 'BINARY')

# Convert it into a Model
model = BqmTranslator.to_aq(bqm, name="bqm_model")
from dimod import ConstrainedQuadraticModel, Binary
from luna_quantum.translator import CqmTranslator

# Define 3 toppings
toppings = {    
    'pepperoni': {'cost': 8, 'happiness': 9},    
    'pineapple': {'cost': 6, 'happiness': 6},    
    'cheese': {'cost': 4, 'happiness': 8}
}

# Create binary variables
vars = {name: Binary(name) for name in toppings.keys()}

# Create CQM
cqm = ConstrainedQuadraticModel()

# Objective: maximize happiness (minimize negative happiness)
objective = -sum(toppings[name]['happiness'] * var for name, var in vars.items())
cqm.set_objective(objective)

# Constraint 1: Budget limit ($15)
budget = sum(toppings[name]['cost'] * var for name, var in vars.items())
cqm.add_constraint(budget <= 15, label="budget")

# Constraint 2: Joey's ultimatum - pineapple XOR pepperoni
joey_rule = vars['pineapple'] + vars['pepperoni']
cqm.add_constraint(joey_rule == 1, label="joey_xor")

# Convert to Luna model
model = CqmTranslator.to_aq(cqm, name="pizza_party_cqm")