microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
minestarks-patch-1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

katas/content/deutsch_jozsa/examples/DeutschJozsaAlgorithmDemo.qs

32lines · modecode

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