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