microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
katas/content/distinguishing_states/a_b/Verification.qs
40lines · modecode
| 1 | namespace Kata.Verification { |
| 2 | open Microsoft.Quantum.Convert; |
| 3 | open Microsoft.Quantum.Math; |
| 4 | open Microsoft.Quantum.Katas; |
| 5 | |
| 6 | // |A⟩ = cos(alpha) * |0⟩ + sin(alpha) * |1⟩, |
| 7 | // |B⟩ = - sin(alpha) * |0⟩ + cos(alpha) * |1⟩. |
| 8 | operation StatePrep_IsQubitA(alpha : Double, q : Qubit, state : Int) : Unit is Adj { |
| 9 | if state == 0 { |
| 10 | // convert |0⟩ to |B⟩ |
| 11 | X(q); |
| 12 | Ry(2.0 * alpha, q); |
| 13 | } else { |
| 14 | // convert |0⟩ to |A⟩ |
| 15 | Ry(2.0 * alpha, q); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | // We can use the StatePrep_IsQubitA operation for the testing |
| 20 | operation CheckSolution() : Bool { |
| 21 | for i in 0 .. 10 { |
| 22 | let alpha = (PI() * IntAsDouble(i)) / 10.0; |
| 23 | let isCorrect = DistinguishTwoStates_SingleQubit( |
| 24 | StatePrep_IsQubitA(alpha, _, _), |
| 25 | Kata.IsQubitA(alpha, _), |
| 26 | [$"|B⟩=(-sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩)", $"|A⟩=(cos({i}π/10)|0⟩ + sin({i}π/10)|1⟩)"], |
| 27 | false |
| 28 | ); |
| 29 | |
| 30 | if not isCorrect { |
| 31 | let precision = 3; |
| 32 | Message($"Test fails for alpha={DoubleAsStringWithPrecision(alpha, precision)}"); |
| 33 | return false; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | Message("Correct!"); |
| 38 | true |
| 39 | } |
| 40 | } |
| 41 | |