microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/oracles/bit_pattern_challenge/Verification.qs
43lines · modecode
| 1 | namespace Kata.Verification { |
| 2 | open Microsoft.Quantum.Arrays; |
| 3 | open Microsoft.Quantum.Convert; |
| 4 | open Microsoft.Quantum.Katas; |
| 5 | |
| 6 | operation ArbitraryBitPattern_Oracle_Challenge_Reference(x : Qubit[], pattern : Bool[]) : Unit is Adj + Ctl { |
| 7 | within { |
| 8 | for i in IndexRange(x) { |
| 9 | if not pattern[i] { |
| 10 | X(x[i]); |
| 11 | } |
| 12 | } |
| 13 | } apply { |
| 14 | Controlled Z(Most(x), Tail(x)); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | @EntryPoint() |
| 19 | operation CheckSolution() : Bool { |
| 20 | for N in 1 .. 3 { |
| 21 | for k in 0 .. 2^N -1 { |
| 22 | let pattern = IntAsBoolArray(k, N); |
| 23 | |
| 24 | let sol = Kata.ArbitraryBitPattern_Oracle_Challenge(_, pattern); |
| 25 | let ref = ArbitraryBitPattern_Oracle_Challenge_Reference(_, pattern); |
| 26 | let isCorrect = CheckOperationsEquivalenceStrict(sol, ref, N); |
| 27 | |
| 28 | if not isCorrect { |
| 29 | Message("Incorrect."); |
| 30 | Message("Hint: examine how your solution transforms the given state and compare it with the expected " + |
| 31 | $"transformation for the {N}-bit oracle for pattern = {pattern}"); |
| 32 | use initial = Qubit[N]; |
| 33 | PrepRandomState(initial); |
| 34 | ShowQuantumStateComparison(initial, sol, ref); |
| 35 | ResetAll(initial); |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | Message("All tests passed."); |
| 41 | true |
| 42 | } |
| 43 | } |
| 44 | |