Defining Models
Dynex SDK supports three model types, each suited to a different class of problems. All models wrap a dimod object and pass through to DynexSampler.
Model Selection Guide
| Problem type | Model | Example |
|---|
| Binary variables, no constraints | BQM | QUBO, Ising, MaxCut, graph problems |
| Binary/integer variables with constraints | CQM | Portfolio optimization, scheduling with budget |
| Multi-valued discrete variables | DQM | Multi-class assignment, routing |
BQM — Binary Quadratic Model
The most common model type. Represents an objective function over binary variables (0/1 or ±1).
import dynex
import dimod
# Define BQM with dimod
bqm = dimod.BinaryQuadraticModel(
{0: 1.0, 1: -1.0}, # Linear terms: h_i * x_i
{(0, 1): 0.5}, # Quadratic terms: J_ij * x_i * x_j
0.0, # Constant offset
'BINARY' # Variable type: 'BINARY' (0/1) or 'SPIN' (-1/+1)
)
model = dynex.BQM(bqm)
Using PyQUBO
from pyqubo import Binary
x0, x1, x2 = Binary('x0'), Binary('x1'), Binary('x2')
H = x0 + x1 - 2*x0*x1 + x2
qubo, offset = H.compile().to_qubo()
bqm = dimod.BinaryQuadraticModel.from_qubo(qubo)
model = dynex.BQM(bqm)
Working with named variables
bqm = dimod.BinaryQuadraticModel(
{'a': -1.0, 'b': -1.0, 'c': 1.0},
{('a', 'b'): 2.0, ('b', 'c'): -0.5},
0.0,
'BINARY'
)
model = dynex.BQM(bqm)
CQM — Constrained Quadratic Model
For problems where constraints are first-class: equality and inequality constraints are encoded directly without penalty terms.
import dynex
import dimod
cqm = dimod.ConstrainedQuadraticModel()
# Define binary variables
x = dimod.Binary('x')
y = dimod.Binary('y')
z = dimod.Binary('z')
# Objective: maximize x + 2y + 3z
cqm.set_objective(-x - 2*y - 3*z)
# Constraint: x + y + z <= 2 (budget constraint)
cqm.add_constraint(x + y + z <= 2, label='budget')
# Constraint: x + z >= 1 (minimum requirement)
cqm.add_constraint(x + z >= 1, label='min_req')
model = dynex.CQM(cqm)
CQM is ideal when constraints cannot be easily penalized. The SDK handles constraint-to-QUBO conversion internally.
DQM — Discrete Quadratic Model
For problems with multi-valued variables — where each variable can take one of several discrete values.
import dynex
import dimod
dqm = dimod.DiscreteQuadraticModel()
# Add variable 'color' with 3 possible values (0, 1, 2)
dqm.add_variable(3, label='color')
# Add variable 'size' with 4 possible values (0, 1, 2, 3)
dqm.add_variable(4, label='size')
# Set linear biases: prefer color=1 and size=2
dqm.set_linear('color', [1.0, -1.0, 0.5])
dqm.set_linear('size', [0.0, 0.5, -1.0, 0.5])
# Set quadratic interaction between variables
dqm.set_quadratic('color', 'size', {(0, 0): 1.0, (1, 2): -0.5})
model = dynex.DQM(dqm)
Preprocessing
For QPU backends, preprocessing can improve solution quality by scaling and normalizing coefficients:
from dynex import DynexConfig, ComputeBackend, QPUModel
config = DynexConfig(
compute_backend=ComputeBackend.QPU,
qpu_model=QPUModel.APOLLO_RC1
)
sampler = dynex.DynexSampler(model, config=config)
sampleset = sampler.sample(
num_reads=50, # QPU: 1–100
annealing_time=200, # QPU: 10–1000
preprocess=True # Enable automatic preprocessing
)