Skip to content

Commit 6c1cbfd

Browse files
zazabapclaude
andcommitted
docs: add Knapsack to QUBO reduction in paper
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2ecf986 commit 6c1cbfd

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

docs/paper/reductions.typ

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,43 @@ where $P$ is a penalty weight large enough that any constraint violation costs m
11861186
_Solution extraction._ Discard slack variables: return $bold(x)' [0..n]$.
11871187
]
11881188

1189+
#let ks_qubo = load-example("knapsack_to_qubo")
1190+
#let ks_qubo_r = load-results("knapsack_to_qubo")
1191+
#let ks_qubo_sol = ks_qubo_r.solutions.at(0)
1192+
#reduction-rule("Knapsack", "QUBO",
1193+
example: true,
1194+
example-caption: [$n = 4$ items, capacity $C = 7$],
1195+
extra: [
1196+
*Step 1 -- Source instance.* #ks_qubo.source.instance.num_items items with weights $(#ks_qubo.source.instance.weights.map(str).join(", "))$, values $(#ks_qubo.source.instance.values.map(str).join(", "))$, and capacity $C = #ks_qubo.source.instance.capacity$.
1197+
1198+
*Step 2 -- Introduce slack variables.* The capacity constraint $sum_i w_i x_i lt.eq C$ is converted to equality by adding $B = floor(log_2 C) + 1 = floor(log_2 7) + 1 = 3$ binary slack variables $s_0, s_1, s_2$ encoding the unused capacity in binary:
1199+
$ 2 x_0 + 3 x_1 + 4 x_2 + 5 x_3 + s_0 + 2 s_1 + 4 s_2 = 7 $
1200+
Total QUBO variables: $n + B = 4 + 3 = #ks_qubo.target.instance.num_vars$.
1201+
1202+
*Step 3 -- Construct QUBO objective.* The penalty coefficient $P = 1 + sum_i v_i = 1 + 3 + 4 + 5 + 7 = 20$ exceeds the total value, ensuring that any infeasible solution has higher cost. The QUBO objective is:
1203+
$ H = -(3 x_0 + 4 x_1 + 5 x_2 + 7 x_3) + 20 (2 x_0 + 3 x_1 + 4 x_2 + 5 x_3 + s_0 + 2 s_1 + 4 s_2 - 7)^2 $
1204+
1205+
*Step 4 -- Verify a solution.* The optimal solution is $bold(x) = (#ks_qubo_sol.source_config.map(str).join(", "))$ (items 0 and 3), with slack $bold(s) = (0, 0, 0)$. Check constraint: $2 dot 1 + 5 dot 1 + 0 = 7 = C$ #sym.checkmark. Penalty term: $(7 - 7)^2 = 0$ (feasible). Objective: $H = -(3 + 7) + 0 = -10$. The full QUBO configuration is $(#ks_qubo_sol.target_config.map(str).join(", "))$.
1206+
1207+
A suboptimal feasible solution $bold(x) = (0,1,1,0)$ gives weight $3 + 4 = 7$, value $9$, and $H = -9$. An infeasible selection $bold(x) = (1,1,0,1)$ has weight $10 > 7$; the penalty dominates: $H gt.eq -14 + 20 dot 9 = 166$.
1208+
1209+
*Count:* #ks_qubo_r.solutions.len() optimal solution (items $\{0, 3\}$ is the unique selection achieving value 10).
1210+
],
1211+
)[
1212+
The 0-1 Knapsack capacity inequality $sum_i w_i x_i lt.eq C$ is converted to equality using $B = floor(log_2 C) + 1$ binary slack variables encoding the unused capacity. The penalty method (@sec:penalty-method) combines the negated value objective with a quadratic constraint penalty, producing a QUBO with $n + B$ binary variables.
1213+
][
1214+
_Construction._ Given $n$ items with weights $w_0, dots, w_(n-1)$, values $v_0, dots, v_(n-1)$, and capacity $C$, introduce $B = floor(log_2 C) + 1$ binary slack variables $s_0, dots, s_(B-1)$ to convert the capacity inequality to equality:
1215+
$ sum_(i=0)^(n-1) w_i x_i + sum_(j=0)^(B-1) 2^j s_j = C $
1216+
Let $a_k$ denote the constraint coefficient of the $k$-th binary variable ($a_k = w_k$ for $k < n$, $a_(n+j) = 2^j$ for $j < B$). The QUBO objective is:
1217+
$ f(bold(z)) = -sum_(i=0)^(n-1) v_i x_i + P (sum_k a_k z_k - C)^2 $
1218+
where $bold(z) = (x_0, dots, x_(n-1), s_0, dots, s_(B-1))$ and $P = 1 + sum_i v_i$. Expanding the quadratic penalty using $z_k^2 = z_k$ (binary):
1219+
$ Q_(k k) = P a_k^2 - 2 P C a_k - [k < n] v_k, quad Q_(i j) = 2 P a_i a_j quad (i < j) $
1220+
1221+
_Correctness._ ($arrow.r.double$) If $bold(x)^*$ is a feasible knapsack solution with value $V^*$, then there exist slack values $bold(s)^*$ satisfying the equality constraint (encoding $C - sum w_i x_i^*$ in binary), so $f(bold(z)^*) = -V^*$. ($arrow.l.double$) If the equality constraint is violated, the penalty $(sum a_k z_k - C)^2 gt.eq 1$ contributes at least $P > sum_i v_i$ to the objective, exceeding the entire value range. Among feasible assignments (penalty zero), $f$ reduces to $-sum v_i x_i$, minimized at the knapsack optimum.
1222+
1223+
_Solution extraction._ Discard slack variables: return $bold(z)[0..n]$.
1224+
]
1225+
11891226
#let qubo_ilp = load-example("qubo_to_ilp")
11901227
#let qubo_ilp_r = load-results("qubo_to_ilp")
11911228
#let qubo_ilp_sol = qubo_ilp_r.solutions.at(0)

0 commit comments

Comments
 (0)