microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
katas/content/deutsch_algo/implement_algo/Verification.qs
35lines · modecode
| 1 | namespace Kata.Verification { |
| 2 | import KatasUtils.*; |
| 3 | import Std.Math.*; |
| 4 | |
| 5 | operation CheckSolution() : Bool { |
| 6 | for (oracle, expected, name) in [ |
| 7 | (I, true, "f(x) = 0"), |
| 8 | (R(PauliI, 2.0 * PI(), _), true, "f(x) = 1"), |
| 9 | (Z, false, "f(x) = x"), |
| 10 | (PhaseOracle_OneMinusX, false, "f(x) = 1 - x") |
| 11 | ] { |
| 12 | |
| 13 | let actual = Kata.DeutschAlgorithm(oracle); |
| 14 | if actual != expected { |
| 15 | Message("Incorrect."); |
| 16 | let actualStr = ConstantOrVariable(actual); |
| 17 | let expectedStr = ConstantOrVariable(expected); |
| 18 | Message($"{name} identified as {actualStr} but it is {expectedStr}."); |
| 19 | return false; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | Message("Correct!"); |
| 24 | true |
| 25 | } |
| 26 | |
| 27 | function ConstantOrVariable(value : Bool) : String { |
| 28 | return value ? "constant" | "variable"; |
| 29 | } |
| 30 | |
| 31 | operation PhaseOracle_OneMinusX(x : Qubit) : Unit is Adj + Ctl { |
| 32 | Z(x); |
| 33 | R(PauliI, 2.0 * PI(), x); |
| 34 | } |
| 35 | } |
| 36 | |