microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/algorithms/HypercubeLookup/src/GroverSearch.qs
73lines · modecode
| 1 | import Std.Arrays.*; |
| 2 | import Std.Math.*; |
| 3 | import Std.Convert.*; |
| 4 | |
| 5 | /// # Summary |
| 6 | /// Implements Grover's algorithm, which searches all possible inputs to an |
| 7 | /// operation to find a particular marked state. This is the same implementation |
| 8 | /// as found in the Grover's Search sample in the QDK samples repository. |
| 9 | operation GroverSearch( |
| 10 | nQubits : Int, |
| 11 | iterations : Int, |
| 12 | phaseOracle : Qubit[] => Unit |
| 13 | ) : Result[] { |
| 14 | |
| 15 | use qubits = Qubit[nQubits]; |
| 16 | |
| 17 | // Initialize a uniform superposition over all possible inputs. |
| 18 | PrepareUniform(qubits); |
| 19 | |
| 20 | // The search itself consists of repeatedly reflecting about the marked |
| 21 | // state and our start state, which we can write out in Q# as a for loop. |
| 22 | for _ in 1..iterations { |
| 23 | phaseOracle(qubits); |
| 24 | ReflectAboutUniform(qubits); |
| 25 | } |
| 26 | |
| 27 | // Measure and return the answer. |
| 28 | return MResetEachZ(qubits); |
| 29 | } |
| 30 | |
| 31 | /// # Summary |
| 32 | /// Given a register in the all-zeros state, prepares a uniform |
| 33 | /// superposition over all basis states. |
| 34 | operation PrepareUniform(inputQubits : Qubit[]) : Unit is Adj { |
| 35 | ApplyToEachCA(H, inputQubits); |
| 36 | } |
| 37 | |
| 38 | /// # Summary |
| 39 | /// Reflects about the uniform superposition state. |
| 40 | operation ReflectAboutUniform(inputQubits : Qubit[]) : Unit { |
| 41 | within { |
| 42 | // Transform the uniform superposition to all-zero. |
| 43 | Adjoint PrepareUniform(inputQubits); |
| 44 | // Transform the all-zero state to all-ones |
| 45 | ApplyToEachA(X, inputQubits); |
| 46 | } apply { |
| 47 | // Now that we've transformed the uniform superposition to the |
| 48 | // all-ones state, reflect about the all-ones state, then let the |
| 49 | // within/apply block transform us back. |
| 50 | ReflectAboutAllOnes(inputQubits); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// # Summary |
| 55 | /// Reflects about the all-ones state. |
| 56 | operation ReflectAboutAllOnes(inputQubits : Qubit[]) : Unit { |
| 57 | Controlled Z(Most(inputQubits), Tail(inputQubits)); |
| 58 | } |
| 59 | |
| 60 | /// # Summary |
| 61 | /// Returns the optimal number of Grover iterations needed to find a marked |
| 62 | /// item, given the number of qubits in a register. Setting the number of |
| 63 | /// iterations to a different number may undershoot or overshoot the marked state. |
| 64 | function IterationsToMarked(nQubits : Int) : Int { |
| 65 | if nQubits > 126 { |
| 66 | fail "This sample supports at most 126 qubits."; |
| 67 | } |
| 68 | |
| 69 | let nItems = 2.0^IntAsDouble(nQubits); |
| 70 | let angle = ArcSin(1. / Sqrt(nItems)); |
| 71 | let iterations = Round(0.25 * PI() / angle - 0.5); |
| 72 | iterations |
| 73 | } |
| 74 | |