|
1 | | -qperm cut_oracle(cut_size: int, const nodes: qbit[], res: qbit) { |
2 | | - res ^= cut_size <= ((((((0 + ((nodes[0] * (1 - nodes[1])) + (nodes[1] * (1 - nodes[0])))) + ((nodes[0] * (1 - nodes[2])) + (nodes[2] * (1 - nodes[0])))) + ((nodes[1] * (1 - nodes[2])) + (nodes[2] * (1 - nodes[1])))) + ((nodes[1] * (1 - nodes[3])) + (nodes[3] * (1 - nodes[1])))) + ((nodes[2] * (1 - nodes[4])) + (nodes[4] * (1 - nodes[2])))) + ((nodes[3] * (1 - nodes[4])) + (nodes[4] * (1 - nodes[3])))); |
| 1 | +// Solving 3-SAT Problem using Grover's Algorithm |
| 2 | + |
| 3 | +// We demonstrate the use of Grover’s search algorithm for an optimization |
| 4 | +// problem, specifically the Max-Cut problem on a small graph. The oracle |
| 5 | +// marks solutions relative to a specified cut size threshold, which can be |
| 6 | +// dynamically explored. |
| 7 | + |
| 8 | +qperm cut_oracle(cut_size: int, const v: qbit[5]) { // a graph of 5 vertices |
| 9 | + control ( |
| 10 | + (v[0] * (1 - v[1]) + v[1] * (1 - v[0])) // edge (0, 1) |
| 11 | + + (v[0] * (1 - v[2]) + v[2] * (1 - v[0])) // edge (0, 2) |
| 12 | + + (v[1] * (1 - v[2]) + v[2] * (1 - v[1])) // edge (1, 2) |
| 13 | + + (v[1] * (1 - v[3]) + v[3] * (1 - v[1])) // edge (1, 3) |
| 14 | + + (v[2] * (1 - v[4]) + v[4] * (1 - v[2])) // edge (2, 4) |
| 15 | + + (v[3] * (1 - v[4]) + v[4] * (1 - v[3])) // edge (3, 4) |
| 16 | + <= cut_size |
| 17 | + ) { |
| 18 | + phase(pi); // flip the sign on states that satisfy the cut size condition |
| 19 | + } |
3 | 20 | } |
4 | 21 |
|
5 | | -qfunc main(r: int, output nodes: qbit[5]) { |
6 | | - allocate(nodes); |
7 | | - hadamard_transform(nodes); |
| 22 | +CUT_SIZE: int = 4; |
| 23 | + |
| 24 | +qfunc main(r: int, output v: qbit[5]) { |
| 25 | + allocate(v); |
| 26 | + hadamard_transform(v); |
8 | 27 | power (r) { |
9 | 28 | grover_operator(lambda(vars) { |
10 | | - phase_oracle(lambda(vars, res) { |
11 | | - cut_oracle(4, vars, res); |
12 | | - }, vars); |
13 | | - }, hadamard_transform, nodes); |
| 29 | + cut_oracle(CUT_SIZE, vars); |
| 30 | + }, hadamard_transform, v); |
14 | 31 | } |
15 | 32 | } |
0 commit comments