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