The problem
Ever wondered how delivery companies figure out the best route for their drivers?
It's called the Travelling Salesman Problem (TSP) — and it's brutally hard to solve
optimally as the number of stops grows.
I wanted to explore quantum-inspired approaches to this problem — algorithms that
borrow ideas from quantum physics but run on regular CPUs. No quantum computer needed.
What is QUBO?
QUBO stands for Quadratic Unconstrained Binary Optimization. It's a mathematical
framework where you express any optimization problem as:
- Binary variables (0 or 1)
- A matrix of coefficients
- Minimize the total "energy"
Quantum annealers (like D-Wave) solve QUBO natively. But quantum-inspired algorithms
simulate this classically — and that's exactly what I built.
What I built
quasar-solver — a pure Python QUBO optimizer with:
-
QUBOclass: sparse dict representation, energy computation -
SimulatedAnnealingSolver: multiple restarts, tunable beta schedule - TSP converter: encodes city routing as a QUBO matrix
- Interactive web demo — click to place cities, watch the solver find the route
🔴 Live Demo — no install needed, runs in browser
Quick usage
from quasar_solver import QUBO, SimulatedAnnealingSolver
q = QUBO()
q.add(0, 0, -1.0) # linear term
q.add(1, 1, 2.0) # linear term
q.add(0, 1, -3.0) # quadratic term
result = SimulatedAnnealingSolver(seed=42).solve(q)
print(result.best_sample) # [1, 1]
print(result.best_energy) # -2.0
How TSP becomes QUBO
The key insight: use one-hot encoding. Variable x[i][t] = 1 means city i
is visited at timestep t.
Two constraint types become penalty terms:
- Each city visited exactly once
- Exactly one city per timestep
The solver minimizes total distance + penalties simultaneously.
Results
On a 6-city random instance, the solver finds a valid tour in under 10ms on CPU.
The web demo uses 2-opt moves in JavaScript for instant visual feedback.
What's next
- VRP support (multiple vehicles, capacity constraints)
- Schedule optimization demo
- FastAPI deployment
- PyPI release (
pip install quasar-solver)
GitHub
⭐ github.com/abdulazizbalu/quasar-solver
Feedback welcome — especially on penalty tuning and SA parameters!
Top comments (0)