qubify = make it QUBO — a constraint-to-QUBO compiler for quantum optimization
Project description
qubify
qubify = make it QUBO
A constraint-to-QUBO compiler for quantum optimization. Convert optimization problems into QUBO matrices — no hardware lock-in, no solver dependency. Feed the output to CIM, D-Wave, Fujitsu DA, simulated annealing, or any QUBO solver.
pip install qubify
Why qubify?
| You have | qubify gives you |
|---|---|
| A TSP with N cities | An N² × N² QUBO matrix |
| A Max-Cut graph | An N × N QUBO matrix |
| A knapsack with items + capacity | An (N + slack) × (N + slack) QUBO matrix |
| Your own constraint problem | A QUBO matrix from a declarative DSL |
The compiler handles variable allocation, constraint-to-penalty conversion, and automatic penalty coefficient estimation so you don't have to hand-derive QUBO matrices.
Quick Start
Using Presets (easiest)
from qubify.presets import tsp, maxcut, knapsack
# Traveling Salesman Problem
distances = [[0, 10, 15], [10, 0, 35], [15, 35, 0]]
qubo_matrix, varmap = tsp(distances)
# Max-Cut
adjacency = [[0, 1, 0], [1, 0, 1], [0, 1, 0]]
qubo_matrix, varmap = maxcut(adjacency)
# Knapsack
qubo_matrix, varmap = knapsack(
values=[60, 100, 120],
weights=[10, 20, 30],
capacity=50,
)
Using the Compiler (flexible)
from qubify import qubify
problem = {
"variables": {"x": ("binary", (5,))},
"objective": [
{"coeff": -1.0, "vars": [0]}, # favor x[0] = 1
{"coeff": 2.0, "vars": [0, 1]}, # penalize x[0]x[1]
],
"constraints": [
{"type": "one_hot", "vars": [0, 1, 2]},
{"type": "cardinality", "vars": [3, 4], "rhs": 1},
],
}
matrix, varmap = qubify(problem)
# matrix is ready for any QUBO/Ising solver
Feed to a Quantum Solver
import kaiwu as kw
# Convert to Ising (if the solver requires it)
ising_matrix = kw.qubo.qubo_to_ising(matrix)[0]
# Solve with simulated annealing
opt = kw.classical.SimulatedAnnealingOptimizer()
solution = opt.solve(ising_matrix)
# Or with CIM quantum hardware
opt = kw.cim.CIMOptimizer(task_name="my-tsp", wait=True)
solution = opt.solve(ising_matrix)
Available Constraint Types
| Constraint | Signature | Description |
|---|---|---|
one_hot |
vars: [i, j, ...] |
Exactly one variable = 1 |
cardinality |
vars: [...], rhs: k |
Exactly k variables = 1 |
at_least_one |
vars: [...] |
At least one variable = 1 |
mutual_exclusive |
vars: [i, j] |
Cannot both be 1 |
implication |
vars: [a, b] |
a ⇒ b |
equality |
vars: [a, b] |
a = b |
at_most_k |
vars: [...], rhs: k |
At most k variables = 1 |
All constraints are compiled into quadratic penalties using exact binary arithmetic — no approximations, no ancilla qubits needed for basic constraints.
Variable System
Declare named variable blocks with shapes:
"variables": {
"x": ("binary", (2, 3)), # 2×3 grid → 6 vars, indices 0–5
"y": ("binary", (4,)), # 1D array → 4 vars, indices 6–9
}
Reference variables in objectives and constraints by:
- Flat index:
0,5 - Named index:
("x", (0, 1))→ flat index 1 - Named block head:
"y"→ flat index 6 (first element of block)
Presets Included
| Preset | Function | Variables | Constraints |
|---|---|---|---|
| TSP | tsp(distances) |
n² | 2n one_hot |
| Max-Cut | maxcut(adjacency) |
n | none |
| Knapsack | knapsack(v, w, C) |
n + slack | capacity (via objective) |
More presets coming: graph coloring, scheduling, portfolio optimization, set covering.
Architecture
Problem (dict) → qubify() → QUBO matrix (numpy)
│
┌───────────┼───────────┐
▼ ▼ ▼
variables objective constraints
(flat idx) (QuboExpr) (one_hot, etc.)
│ │ │
└───────────┴───────────┘
│
▼
penalty estimation
│
▼
Σ → QUBO matrix
qubify is solver-agnostic. It produces a standard numpy QUBO matrix. What you do with it is up to you.
Install
pip install qubify
Dev install with tests:
pip install qubify[dev]
pytest tests/
Related Projects
- kaiwu-cli-mcp — Docker + CLI + MCP wrapper for Kaiwu SDK (玻色量子 CIM). Use qubify to generate matrices, then feed them to kaiwu-cli-mcp for execution on quantum hardware.
- Built as a standalone library — works with any QUBO/Ising solver.
Links
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qubify-0.1.0.tar.gz.
File metadata
- Download URL: qubify-0.1.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47d53e3df1f32bf78e02a36539dbfd97af75d0b3fd5a46b2b7e50aa8e3050193
|
|
| MD5 |
4a023d13a8aa0c43869ef395d2b44e7d
|
|
| BLAKE2b-256 |
6109b2c2ece8088090762e500a2ef13ee1807f4fb2db323c7b9e6534bfb184d1
|
File details
Details for the file qubify-0.1.0-py3-none-any.whl.
File metadata
- Download URL: qubify-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5ae455eeafbcd563bef68092beeb3e9fc9d351505a0f9808b7e312e7ce88c6d
|
|
| MD5 |
f658bda754983ce90dd587043cf95886
|
|
| BLAKE2b-256 |
13c74ee51308b31b86e3bfbd2c12220ee46c9f36751cbba101bfef523f5a4c29
|