microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
katas/content/deutsch_jozsa/examples/DeutschJozsaAlgorithmDemo.qs
30lines · modecode
| 1 | namespace Kata { |
| 2 | open Microsoft.Quantum.Diagnostics; |
| 3 | open Microsoft.Quantum.Math; |
| 4 | |
| 5 | @EntryPoint() |
| 6 | operation DeutschJozsaAlgorithmDemo () : Unit { |
| 7 | for (N, oracle, name) in [(2, qs => (), "f(x) = 0"), |
| 8 | (2, qs => R(PauliI, 2.0 * PI(), qs[0]), "f(x) = 1"), |
| 9 | (3, qs => Z(qs[Length(qs) - 1]), "f(x) = x mod 2"), |
| 10 | (3, qs => Z(qs[0]), "f(x) = most significant bit of x"), |
| 11 | (3, ApplyToEach(Z, _), "f(x) = parity of x")] { |
| 12 | let isConstant = DeutschJozsaAlgorithm(N, oracle); |
| 13 | Message($"{name} identified as {isConstant ? "constant" | "balanced"}"); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | operation DeutschJozsaAlgorithm (N : Int, oracle : Qubit[] => Unit) : Bool { |
| 18 | mutable isConstant = true; |
| 19 | use x = Qubit[N]; |
| 20 | ApplyToEach(H, x); |
| 21 | oracle(x); |
| 22 | ApplyToEach(H, x); |
| 23 | for xi in x { |
| 24 | if MResetZ(xi) == One { |
| 25 | set isConstant = false; |
| 26 | } |
| 27 | } |
| 28 | return isConstant; |
| 29 | } |
| 30 | } |