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:
Mariia Mykhailova 2019-04-29 16:48:52 -07:00 коммит произвёл GitHub
Родитель c92a76f712
Коммит e1476aa12e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 16 добавлений и 1 удалений

Просмотреть файл

@ -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];