A Sudoku cell, a square on a chessboard and a region on a map can all represent a binary decision. The common pattern is to choose one value from a set of alternatives, while ruling out combinations that cannot coexist.

The 4colors Optimizer for QUBO accepts binary quadratic objectives with additional linear constraints. These three puzzles illustrate the distinction between feasibility and optimization, and why it matters when you interpret a solver's answer.

Three problems, one modeling pattern

PuzzleBinary variableRulesObjective
SudokuCell and digitOne digit per cell; each digit once per row, column and boxConstant
N-queensBoard squareOne queen per row and column; at most one per diagonalConstant
Map coloringRegion and colorOne color per regionMinimize borders with matching colors, or enforce different colors on every border

For the first two models, a feasible assignment is already a complete solution. In coloring, the decision is different depending on the question. A fixed-palette conflict-minimization model can return an assignment with conflicts; a proper-coloring feasibility model must satisfy every border rule.

Build an N-queens model

Choose a variable for every square. Row and column sums must equal one. For each of the two families of diagonals, constrain the sum to be at most one. There is no need to introduce a penalty weight for these rules when using explicit linear constraints.

The following example uses the Python interface. Configure access using the developer documentation.

import fourcolors.qubo

n = 8
m = fourcolors.qubo.to_model({}, [0] * (n * n), n=n * n)
for r in range(n):
    m.add_constraint({r*n+c: 1 for c in range(n)}, lo=1, hi=1)
for c in range(n):
    m.add_constraint({r*n+c: 1 for r in range(n)}, lo=1, hi=1)
for d in range(1-n, n):
    cells = {r*n+c: 1 for r in range(n) for c in range(n) if r-c == d}
    if len(cells) > 1:
        m.add_constraint(cells, hi=1)
for s in range(2*n-1):
    cells = {r*n+c: 1 for r in range(n) for c in range(n) if r+c == s}
    if len(cells) > 1:
        m.add_constraint(cells, hi=1)
r = fourcolors.qubo.solve(m, time_limit_s=5, seed=1)
if r.solution is not None:
    queens = [(i,j) for i in range(n) for j in range(n) if r.solution[i*n+j]]
    assert len(queens) == n
    assert len({i for i,j in queens}) == len({j for i,j in queens}) == n
    assert len({i-j for i,j in queens}) == len({i+j for i,j in queens}) == n

The objective is constant. Check the decoded board independently: the row indices, column indices and both diagonal indices must each be distinct, and there must be n queens.

# Standalone illustration: validate a four-queen placement.
columns = [1, 3, 0, 2]
n = len(columns)
assert set(columns) == set(range(n))
assert len({r-c for r,c in enumerate(columns)}) == n
assert len({r+c for r,c in enumerate(columns)}) == n

Why a search status differs from a proof

Consider a wheel graph with an odd cycle around a central vertex. The central vertex is adjacent to every vertex of the cycle. If only three colors are available, the surrounding cycle must use the other two colors. An odd cycle cannot alternate between two colors. The wheel therefore needs a fourth color.

This is a mathematical impossibility argument, independent of the search algorithm. A search that finishes without finding a feasible answer reports its search outcome; an impossibility proof follows from the structure of the problem. This distinction applies equally to timetables and assignment models.

Try the other puzzles

For Sudoku, remove digits ruled out by the clues before constructing variables. Each remaining cell must take exactly one digit. Check the decoded solution against every row, column, box and original clue. For a map, count conflicting borders independently of the quadratic objective; keep one-color-per-region constraints explicit.

The example repository contains the associated puzzle models, plotting scripts and tests. Follow the setup instructions there for your chosen example.

These modeling patterns also occur in resource allocation and frequency planning. The key is to write the rules in a form you can check against the original decision.

Try the QUBO solver.