-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operation.qs
81 lines (74 loc) · 1.41 KB
/
Operation.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace Quantum.RandomGenerator
{
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
operation SetValue (desired: Result, q1: Qubit) : ()
{
body
{
let current = M(q1);
if (desired != current)
{
X(q1);
}
}
}
operation GenerateRandomMultiBool(numberofBools : Int) : Bool[]
{
body
{
mutable result = new Bool[numberofBools];
for (i in 1..numberofBools)
{
set result[i-1] = GenerateRandomBool();
}
return result;
}
}
function BoolsToInt(numberofBools : Int, bools : Bool[]) : Int
{
mutable multiplier = 1;
mutable result = 0;
for (i in 1..numberofBools)
{
if (bools[i-1])
{
set result = result + multiplier;
}
set multiplier = multiplier * 2;
}
return result;
}
operation GenerateRandomBool () : Bool
{
body
{
mutable res = false;
using (register = Qubit[1])
{
SetValue(Zero,register[0]);
H(register[0]);
let measurement = M(register[0]);
if (measurement == Zero)
{
set res = false;
}
else
{
set res = true;
}
Reset(register[0]);
}
return res;
}
}
operation GenerateRandomInt(numberofBits : Int) : Int
{
body
{
let allBools = GenerateRandomMultiBool(numberofBits);
let result = BoolsToInt(numberofBits,allBools);
return result;
}
}
}