Skip to content

Commit

Permalink
upgraded knapsack notebook, moved to algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
orsa-classiq committed Dec 18, 2024
1 parent 2bba551 commit 937b7b7
Show file tree
Hide file tree
Showing 9 changed files with 574 additions and 1,163 deletions.
490 changes: 490 additions & 0 deletions algorithms/qaoa/knapsack/qaoa_knapsack.ipynb

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions algorithms/qaoa/knapsack/qaoa_knapsack.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"friendly_name": "Constrained QAOA: Knapsack",
"description": "Optimizing the Knapsack problem using the QAOA Algorithm",
"problem_domain_tags": ["optimization"],
"qmod_type": ["algorithms"],
"level": ["demos"]
}
32 changes: 32 additions & 0 deletions algorithms/qaoa/knapsack/qaoa_knapsack.qmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
qstruct KnapsackVars {
a: qnum<3>;
b: qnum<2>;
}



qfunc eval_constraint(v: KnapsackVars, output aux: qbit) {
aux = ((v.a * 2) + (v.b * 3)) <= 12;
}

qfunc apply_cost(gamma: real, v: KnapsackVars) {
aux: qbit;
within {
eval_constraint(v, aux);
} apply {
control (aux) {
phase (-((v.a * 3) + (v.b * 5)), gamma);
}
}
}

qfunc main(params: real[6], output v: KnapsackVars) {
allocate(v.size, v);
hadamard_transform(v);
repeat (i: 3) {
apply_cost(params[2 * i], v);
apply_to_all(lambda(q) {
RX(params[(2 * i) + 1], q);
}, v);
}
}
43 changes: 43 additions & 0 deletions algorithms/qaoa/knapsack/qaoa_knapsack.synthesis_options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"constraints": {
"max_gate_count": {},
"optimization_parameter": "no_opt"
},
"preferences": {
"machine_precision": 8,
"custom_hardware_settings": {
"basis_gates": [
"x",
"p",
"tdg",
"sx",
"rz",
"sxdg",
"sdg",
"ry",
"cy",
"t",
"y",
"s",
"u1",
"cz",
"z",
"rx",
"u2",
"h",
"u",
"r",
"id",
"cx"
],
"is_symmetric_connectivity": true
},
"debug_mode": true,
"synthesize_all_separately": false,
"output_format": ["qasm"],
"pretty_qasm": true,
"transpilation_option": "auto optimize",
"timeout_seconds": 300,
"random_seed": 3016072189
}
}
Loading

0 comments on commit 937b7b7

Please sign in to comment.