microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/tests-integration/resources/adaptive_ri/input/ExpandedTests.qs
124lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Test { |
| 5 | import Std.Intrinsic.*; |
| 6 | import Std.Convert.*; |
| 7 | import Std.Math.*; |
| 8 | import Std.Arrays.*; |
| 9 | import Std.Measurement.*; |
| 10 | import Std.Canon.*; |
| 11 | |
| 12 | @EntryPoint() |
| 13 | operation Main() : (Result[], Result) { |
| 14 | return (SearchForMarkedInput(), VerifyCNOTfromExp()); |
| 15 | } |
| 16 | |
| 17 | operation VerifyCNOTfromExp() : Result { |
| 18 | use (control, target, paired) = (Qubit(), Qubit(), Qubit()); |
| 19 | |
| 20 | within { |
| 21 | H(paired); |
| 22 | CNOT(paired, target); |
| 23 | CNOT(paired, control); |
| 24 | } apply { |
| 25 | // CNOT |
| 26 | let theta = PI() / 4.0; |
| 27 | Rx(-2.0 * theta, target); |
| 28 | Rz(-2.0 * theta, control); |
| 29 | Adjoint Exp([PauliZ, PauliX], theta, [control, target]); |
| 30 | |
| 31 | Adjoint CNOT(control, target); |
| 32 | } |
| 33 | |
| 34 | return M(target); |
| 35 | } |
| 36 | |
| 37 | /// # Summary |
| 38 | /// This operation applies Grover's algorithm to search all possible inputs |
| 39 | /// to an operation to find a particular marked state. |
| 40 | operation SearchForMarkedInput() : Result[] { |
| 41 | let nQubits = 2; |
| 42 | use qubits = Qubit[nQubits] { |
| 43 | // Initialize a uniform superposition over all possible inputs. |
| 44 | PrepareUniform(qubits); |
| 45 | // The search itself consists of repeatedly reflecting about the |
| 46 | // marked state and our start state, which we can write out in Q# |
| 47 | // as a for loop. |
| 48 | for idxIteration in 0..NIterations(nQubits) - 1 { |
| 49 | ReflectAboutMarked(qubits); |
| 50 | ReflectAboutUniform(qubits); |
| 51 | } |
| 52 | // Measure and return the answer. |
| 53 | return MResetEachZ(qubits); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// # Summary |
| 58 | /// Returns the number of Grover iterations needed to find a single marked |
| 59 | /// item, given the number of qubits in a register. |
| 60 | function NIterations(nQubits : Int) : Int { |
| 61 | let nItems = 1 <<< nQubits; // 2^numQubits |
| 62 | // compute number of iterations: |
| 63 | let angle = ArcSin(1. / Sqrt(IntAsDouble(nItems))); |
| 64 | let nIterations = Round(0.25 * PI() / angle - 0.5); |
| 65 | return nIterations; |
| 66 | } |
| 67 | |
| 68 | /// # Summary |
| 69 | /// Reflects about the basis state marked by alternating zeros and ones. |
| 70 | /// This operation defines what input we are trying to find in the main |
| 71 | /// search. |
| 72 | operation ReflectAboutMarked(inputQubits : Qubit[]) : Unit { |
| 73 | use outputQubit = Qubit() { |
| 74 | within { |
| 75 | // We initialize the outputQubit to (|0⟩ - |1⟩) / √2, |
| 76 | // so that toggling it results in a (-1) phase. |
| 77 | X(outputQubit); |
| 78 | H(outputQubit); |
| 79 | // Flip the outputQubit for marked states. |
| 80 | // Here, we get the state with alternating 0s and 1s by using |
| 81 | // the X instruction on every other qubit. |
| 82 | ApplyToEachA(X, inputQubits[...2...]); |
| 83 | } apply { |
| 84 | Controlled X(inputQubits, outputQubit); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// # Summary |
| 90 | /// Reflects about the uniform superposition state. |
| 91 | operation ReflectAboutUniform(inputQubits : Qubit[]) : Unit { |
| 92 | within { |
| 93 | // Transform the uniform superposition to all-zero. |
| 94 | Adjoint PrepareUniform(inputQubits); |
| 95 | // Transform the all-zero state to all-ones |
| 96 | PrepareAllOnes(inputQubits); |
| 97 | } apply { |
| 98 | // Now that we've transformed the uniform superposition to the |
| 99 | // all-ones state, reflect about the all-ones state, then let |
| 100 | // the within/apply block transform us back. |
| 101 | ReflectAboutAllOnes(inputQubits); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// # Summary |
| 106 | /// Reflects about the all-ones state. |
| 107 | operation ReflectAboutAllOnes(inputQubits : Qubit[]) : Unit { |
| 108 | Controlled Z(Most(inputQubits), Tail(inputQubits)); |
| 109 | } |
| 110 | |
| 111 | /// # Summary |
| 112 | /// Given a register in the all-zeros state, prepares a uniform |
| 113 | /// superposition over all basis states. |
| 114 | operation PrepareUniform(inputQubits : Qubit[]) : Unit is Adj + Ctl { |
| 115 | ApplyToEachCA(H, inputQubits); |
| 116 | } |
| 117 | |
| 118 | /// # Summary |
| 119 | /// Given a register in the all-zeros state, prepares an all-ones state |
| 120 | /// by flipping every qubit. |
| 121 | operation PrepareAllOnes(inputQubits : Qubit[]) : Unit is Adj + Ctl { |
| 122 | ApplyToEachCA(X, inputQubits); |
| 123 | } |
| 124 | } |
| 125 | |