|
| 1 | +# # Grover Search |
1 | 2 | using Yao |
2 | | -using QuAlgorithmZoo: groveriter!, inference_oracle, prob_match_oracle |
| 3 | +using LinearAlgebra |
| 4 | +using QuAlgorithmZoo: groveriter, inference_oracle, prob_match_oracle |
3 | 5 |
|
| 6 | +# ## Target Space and Evidense |
4 | 7 | num_bit = 12 |
5 | | -oracle(reg::DefaultRegister) = (reg.state[100:101,:]*=-1; reg) |
6 | | -target_state = zeros(1<<num_bit); target_state[100:101] = sqrt(0.5) |
| 8 | +oracle = matrixgate(Diagonal((v = ones(1<<num_bit); v[100:101]*=-1; v))) |
| 9 | +target_state = zeros(1<<num_bit); target_state[100:101] .= sqrt(0.5) |
7 | 10 |
|
| 11 | +# now we want to search the subspace with [1,3,5,8,9,11,12] fixed to 1 and [4,6] fixed to 0. |
| 12 | +evidense = [1, 3, -4, 5, -6, 8, 9, 11, 12] |
| 13 | + |
| 14 | +# ## Search |
8 | 15 | # then solve the above problem |
9 | | -it = groveriter!(uniform_state(num_bit), oracle) |
| 16 | +it = groveriter(uniform_state(num_bit), oracle) |
10 | 17 | for (i, psi) in enumerate(it) |
11 | 18 | overlap = abs(statevec(psi)'*target_state) |
12 | 19 | println("step $(i-1), overlap = $overlap") |
13 | 20 | end |
14 | 21 |
|
| 22 | +# ## Inference Example |
15 | 23 | # we have a state psi0, we know how to prepair it |
16 | 24 | psi = rand_state(num_bit) |
17 | 25 |
|
18 | | -# now we want to search the subspace with [1,3,5,8,9,11,12] fixed to 1 and [4,6] fixed to 0. |
19 | | -evidense = [1, 3, -4, 5, -6, 8, 9, 11, 12] |
20 | | - |
21 | 26 | """ |
22 | 27 | Doing Inference, psi is the initial state, we want to search target space with specific evidense. |
23 | 28 | e.g. evidense [1, -3, 6] means the [1, 3, 6]-th bits take value [1, 0, 1]. |
24 | 29 | """ |
25 | 30 | oracle_infer = inference_oracle(evidense)(nqubits(psi)) |
26 | | -it = groveriter!(psi, oracle_infer) |
| 31 | +it = groveriter(psi, oracle_infer) |
27 | 32 | for (i, psi) in enumerate(it) |
28 | 33 | p_target = prob_match_oracle(psi, oracle_infer) |
29 | 34 | println("step $(i-1), overlap^2 = $p_target") |
|
0 commit comments