microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
katas/content/deutsch_algo/examples/DeutschAlgorithmDemo.qs
40lines · modecode
| 1 | namespace Kata { |
| 2 | open Microsoft.Quantum.Diagnostics; |
| 3 | open Microsoft.Quantum.Math; |
| 4 | open Microsoft.Quantum.Measurement; |
| 5 | |
| 6 | @EntryPoint() |
| 7 | operation DeutschAlgorithmDemo () : Unit { |
| 8 | for (oracle, name) in [(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 | let isConstant = DeutschAlgorithm(oracle); |
| 13 | Message($"{name} identified as {isConstant ? "constant" | "variable"}"); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | operation PhaseOracle_Zero (x : Qubit) : Unit { |
| 18 | } |
| 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 | } |