SolveSATWithGrover: Clarify the solution for task 2.2 (#98)
This change addresses issue #92: * describe the intent behind the task and the logic of the reference solution, * add an extra exit condition to prevent infinite loop when trying increasing numbers of iterations.
This commit is contained in:
Родитель
c92a76f712
Коммит
e1476aa12e
|
@ -281,6 +281,13 @@ namespace Quantum.Kata.GroversAlgorithm {
|
|||
|
||||
// Task 2.2. Universal implementation of Grover's algorithm
|
||||
operation GroversAlgorithm_Reference (N : Int, oracle : ((Qubit[], Qubit) => Unit : Adjoint)) : Bool[] {
|
||||
// In this task you don't know the optimal number of iterations upfront,
|
||||
// so it makes sense to try different numbers of iterations.
|
||||
// This way, even if you don't hit the "correct" number of iterations on one of your tries,
|
||||
// you'll eventually get a high enough success probability.
|
||||
|
||||
// This solution tries numbers of iterations that are powers of 2;
|
||||
// this is not the only valid solution, since a lot of sequences will eventually yield the answer.
|
||||
mutable answer = new Bool[N];
|
||||
using ((register, output) = (Qubit[N], Qubit())) {
|
||||
mutable correct = false;
|
||||
|
@ -296,10 +303,13 @@ namespace Quantum.Kata.GroversAlgorithm {
|
|||
set answer = BoolArrFromResultArr(res);
|
||||
}
|
||||
ResetAll(register);
|
||||
} until (correct)
|
||||
} until (correct or iter > 100) // the fail-safe to avoid going into an infinite loop
|
||||
fixup {
|
||||
set iter = iter * 2;
|
||||
}
|
||||
if (not correct) {
|
||||
fail "Failed to find an answer";
|
||||
}
|
||||
}
|
||||
Message($"{answer}");
|
||||
return answer;
|
||||
|
|
|
@ -222,6 +222,11 @@ namespace Quantum.Kata.GroversAlgorithm {
|
|||
// Output:
|
||||
// An array of N boolean values which satisfy the expression implemented by the oracle
|
||||
// (i.e., any basis state marked by the oracle).
|
||||
//
|
||||
// Note that the similar task in the GroversAlgorithm kata required you to implement Grover's algorithm
|
||||
// in a way that would be robust to accidental failures, but you knew the optimal number of iterations
|
||||
// (the number that minimizes the probability of such failure).
|
||||
// In this task you also need to make your implementation robust to not knowing the optimal number of iterations.
|
||||
operation GroversAlgorithm (N : Int, oracle : ((Qubit[], Qubit) => Unit : Adjoint)) : Bool[] {
|
||||
// ...
|
||||
return new Bool[N];
|
||||
|
|
Загрузка…
Ссылка в новой задаче