microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

katas/content/deutsch_jozsa/examples/DeutschJozsaAlgorithmDemo.qs

31lines · modecode

1namespace Kata {
2 open Microsoft.Quantum.Diagnostics;
3 open Microsoft.Quantum.Math;
4 open Microsoft.Quantum.Measurement;
5
6 @EntryPoint()
7 operation DeutschJozsaAlgorithmDemo () : Unit {
8 for (N, oracle, name) in [(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 let isConstant = DeutschJozsaAlgorithm(N, oracle);
14 Message($"{name} identified as {isConstant ? "constant" | "balanced"}");
15 }
16 }
17
18 operation DeutschJozsaAlgorithm (N : Int, oracle : Qubit[] => Unit) : Bool {
19 mutable isConstant = true;
20 use x = Qubit[N];
21 ApplyToEach(H, x);
22 oracle(x);
23 ApplyToEach(H, x);
24 for xi in x {
25 if MResetZ(xi) == One {
26 set isConstant = false;
27 }
28 }
29 return isConstant;
30 }
31}