Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit e1476aa

Browse files
authored
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.
1 parent c92a76f commit e1476aa

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

SolveSATWithGrover/ReferenceImplementation.qs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ namespace Quantum.Kata.GroversAlgorithm {
281281

282282
// Task 2.2. Universal implementation of Grover's algorithm
283283
operation GroversAlgorithm_Reference (N : Int, oracle : ((Qubit[], Qubit) => Unit : Adjoint)) : Bool[] {
284+
// In this task you don't know the optimal number of iterations upfront,
285+
// so it makes sense to try different numbers of iterations.
286+
// This way, even if you don't hit the "correct" number of iterations on one of your tries,
287+
// you'll eventually get a high enough success probability.
288+
289+
// This solution tries numbers of iterations that are powers of 2;
290+
// this is not the only valid solution, since a lot of sequences will eventually yield the answer.
284291
mutable answer = new Bool[N];
285292
using ((register, output) = (Qubit[N], Qubit())) {
286293
mutable correct = false;
@@ -296,10 +303,13 @@ namespace Quantum.Kata.GroversAlgorithm {
296303
set answer = BoolArrFromResultArr(res);
297304
}
298305
ResetAll(register);
299-
} until (correct)
306+
} until (correct or iter > 100) // the fail-safe to avoid going into an infinite loop
300307
fixup {
301308
set iter = iter * 2;
302309
}
310+
if (not correct) {
311+
fail "Failed to find an answer";
312+
}
303313
}
304314
Message($"{answer}");
305315
return answer;

SolveSATWithGrover/Tasks.qs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ namespace Quantum.Kata.GroversAlgorithm {
222222
// Output:
223223
// An array of N boolean values which satisfy the expression implemented by the oracle
224224
// (i.e., any basis state marked by the oracle).
225+
//
226+
// Note that the similar task in the GroversAlgorithm kata required you to implement Grover's algorithm
227+
// in a way that would be robust to accidental failures, but you knew the optimal number of iterations
228+
// (the number that minimizes the probability of such failure).
229+
// In this task you also need to make your implementation robust to not knowing the optimal number of iterations.
225230
operation GroversAlgorithm (N : Int, oracle : ((Qubit[], Qubit) => Unit : Adjoint)) : Bool[] {
226231
// ...
227232
return new Bool[N];

0 commit comments

Comments
 (0)