forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferenceImplementation.qs
55 lines (46 loc) · 1.61 KB
/
ReferenceImplementation.qs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains reference solutions to all tasks.
// You should not modify anything in this file.
// We recommend that you try to solve the tasks yourself first,
// but feel free to look up the solution if you get stuck.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.RandomNumberGeneration {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;
// Exercise 1.
operation RandomBit_Reference () : Int {
use q = Qubit();
H(q);
return M(q) == Zero ? 0 | 1;
}
// Exercise 2.
operation RandomTwoBits_Reference () : Int {
return 2 * RandomBit_Reference() + RandomBit_Reference();
}
// Exercise 3.
operation RandomNBits_Reference (N: Int) : Int {
mutable result = 0;
for i in 0..(N - 1) {
set result = result * 2 + RandomBit_Reference();
}
return result;
}
// Exercise 4.
operation WeightedRandomBit_Reference (x : Double) : Int {
let theta = ArcCos(Sqrt(x));
use q = Qubit();
Ry(2.0 * theta, q);
return M(q) == Zero ? 0 | 1;
}
// Exercise 5.
operation RandomNumberInRange_Reference (min : Int, max : Int) : Int {
let nBits = BitSizeI(max - min);
mutable output = 0;
repeat {
set output = RandomNBits_Reference(nBits);
} until output <= max - min;
return output + min;
}
}