This repository was archived by the owner on Aug 21, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed
Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 ];
You can’t perform that action at this time.
0 commit comments