Squaring a linear constraint can turn it into part of a binary quadratic objective. It can also change the structure of a model substantially. An assignment problem with a linear cost acquires interactions between alternatives; a capacity rule can introduce additional binary variables.
The 4colors Optimizer for QUBO supports explicit linear constraints. This tutorial compares the mathematical formulations before showing how to express a constrained model through the Python interface.
Assignment: preserve the original decision
Let xᵢⱼ indicate whether worker i is assigned job j. Minimize the sum of assignment costs, subject to one job per worker and one worker per job. With n workers and n jobs, the linear model has n² variables and 2n equality rows.
In an unconstrained encoding, each row contributes a term of the form P(∑ x − 1)2. Each group of n alternatives contributes a quadratic term for every pair. Before combining coefficients, the assignment penalties introduce 2nC(n, 2) pair terms.
Knapsack: capacity without extra binary variables
For nonnegative integer weights and capacity C, choose binary items to maximize their total value, while their weights sum to at most C. One conventional penalty encoding introduces a nonnegative integer slack s representing unused capacity, and adds P(∑ wi xi + s − C)2 to the negative value objective. Encode s in binary with enough range to represent every unused capacity from zero to C.
If values are nonnegative, a penalty P greater than their sum is sufficient to make a nonzero integer residual worse than the feasible empty selection. This is a sufficient mathematical bound for the stated encoding. It does not identify the best weight for a finite heuristic search. Explicit constraints retain the original linear value objective and capacity row.
Submit the model directly
import fourcolors.qubo
weights = [2, 3, 4]
values = [3, 4, 5]
capacity = 5
model = fourcolors.qubo.to_model({}, [-v for v in values], n=len(values))
model.add_constraint(dict(enumerate(weights)), hi=capacity)
result = fourcolors.qubo.solve(model, time_limit_s=5, seed=1) Read the returned status and independently check the capacity and total value. The service checks every row exactly on the returned assignment and reports it in result.constraints; model.feasible(result.solution) repeats the check locally.
Compare the formulations on a small instance
from itertools import product
weights, values, capacity = [2,3,4], [3,4,5], 5
P = sum(values)+1
xs = list(product((0,1),repeat=len(weights)))
value = lambda x: sum(v*b for v,b in zip(values,x))
load = lambda x: sum(w*b for w,b in zip(weights,x))
best = max((x for x in xs if load(x)<=capacity),key=value)
penalty = min(((-value(x)+P*(load(x)+s-capacity)**2,x,s)
for x in xs for s in range(capacity+1)))
assert value(best)==7 and penalty[1]==best In this illustration both representations identify the same selection. The explicit version uses only the original item variables. The unconstrained version includes slack and penalty interactions.
Reproduce a larger comparison
The assignment and knapsack example repository includes model generators, a penalty-weight sweep and independent assignment and dynamic-programming references. Evaluate every decoded answer on the original objective and capacity or assignment rules. If you vary penalty weights, record the budget spent choosing them separately from the final search.
The developer documentation provides the Python entry point. Use explicit constraints where you want the original objective and rules to remain visible.