microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/deutsch_algo/examples/DeutschAlgorithmDemo.qs
40lines · modecode
| 1 | namespace Kata { |
| 2 | import Std.Diagnostics.*; |
| 3 | import Std.Math.*; |
| 4 | |
| 5 | @EntryPoint() |
| 6 | operation DeutschAlgorithmDemo() : Unit { |
| 7 | for (oracle, name) in [ |
| 8 | (PhaseOracle_Zero, "f(x) = 0"), |
| 9 | (PhaseOracle_One, "f(x) = 1"), |
| 10 | (PhaseOracle_X, "f(x) = x"), |
| 11 | (PhaseOracle_OneMinusX, "f(x) = 1-x") |
| 12 | ] { |
| 13 | let isConstant = DeutschAlgorithm(oracle); |
| 14 | Message($"{name} identified as {isConstant ? "constant" | "variable"}"); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | operation PhaseOracle_Zero(x : Qubit) : Unit {} |
| 19 | |
| 20 | operation PhaseOracle_One(x : Qubit) : Unit { |
| 21 | R(PauliI, 2.0 * PI(), x); |
| 22 | } |
| 23 | |
| 24 | operation PhaseOracle_X(x : Qubit) : Unit { |
| 25 | Z(x); |
| 26 | } |
| 27 | |
| 28 | operation PhaseOracle_OneMinusX(x : Qubit) : Unit { |
| 29 | Z(x); |
| 30 | R(PauliI, 2.0 * PI(), x); |
| 31 | } |
| 32 | |
| 33 | operation DeutschAlgorithm(oracle : Qubit => Unit) : Bool { |
| 34 | use x = Qubit(); |
| 35 | H(x); |
| 36 | oracle(x); |
| 37 | H(x); |
| 38 | return MResetZ(x) == Zero; |
| 39 | } |
| 40 | } |
| 41 | |