Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Graph Coloring

Source: examples/graph_coloring/README.md

Assign one of C colors to each node of an undirected graph such that no two adjacent nodes share the same color (proper C-coloring).

QUBO formulation

  • Input: number of nodes N, number of colors C, edge list
  • Model: N*C binary variables in an N x C grid. x[v,c] = 1 if node v gets color c.
  • Objective: energy 0 for any valid C-coloring; minimise constraint violations.
  • Constraints:
    • One-hot per node: sum_c x[v,c] = 1 (ONEHOTR, penalty 200)
    • Exclusion per (edge, color): x[u,c] + x[v,c] <= 1 (EXCLUDE, penalty 200)

Encoding strategy

ONEHOTR applies the one-hot row constraint directly: for each node v, a single ONEHOTR instruction constrains all C variables in that row to sum to 1.

EXCLUDE is applied per (edge (u,v), color c) pair via a nested range loop. The 2D coordinates (u, c) and (v, c) are resolved to flat indices using IDXGRID: u * num_colors + c and v * num_colors + c.

DSL methods used

  • problem.define_model(size=N*C, rows=N, cols=C) – 2D grid model layout
  • model.apply_onehot_row(node, penalty) – ONEHOTR per node
  • model.apply_exclude((u, c), (v, c), penalty) – EXCLUDE per edge per color

Pipeline overview

  1. CP (xqcp) – generate a random graph, declare an N x C binary grid, and add ONEHOTR constraints per node plus EXCLUDE constraints per (edge, color) pair.
  2. Assemble.xqasm text to bytecode via xquad.asm
  3. Encode – run encoder on chosen XQVM to produce the XQMX model
  4. Sample – solver runs SA/QPU/GPU over the model
  5. Verify – verifier checks one-hot and exclusion constraints and computes energy
  6. Decode – decoder extracts the color assignment per node

Usage

uv run python examples/graph_coloring/runner.py --seed 42
uv run python examples/graph_coloring/runner.py --n 6 --colors 3 --interpreter rust
FlagDefaultDescription
--n5Number of nodes
--colors3Number of colors
--solverdwave-cpuSolver backend (see Choosing a solver)
--interpreterpythonXQVM backend: python or rust
--seed42Random seed
-ostdoutWrite JSON result to file

Choosing a solver

NameHardwareInstall
dwave-cpuCPU (default)pip install xquad
dwave-qpuD-Wave Leap accountpip install xquad[dwave]
cuda-gpuNVIDIA CUDA GPUpip install xquad[cuda]
metal-gpuApple Silicon (macOS)pip install xquad[metal]

See GPU/QPU installation for driver prerequisites and xqsa solver quick-starts for per-solver parameter tuning.

Non-default solvers will not reproduce the canonical output (different RNG/hardware). example-smoke always runs dwave-cpu.

The canonical output and its invariants are defined in the source README.