-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.qs
39 lines (35 loc) · 1.18 KB
/
Program.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
namespace QuantumRNG {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
operation GenerateRandomBit() : Result {
// Allocate a qubit.
using (q = Qubit()) {
// Put the qubit to superposition
H(q);
// It now a 50% chance of being measured 0 or 1.
// Measure the qubit value.
return MResetZ(q);
}
}
operation SampleRandomNumberInRange(min : Int, max : Int) : Int {
mutable output = min;
repeat {
mutable bits = new Result[0];
for (idxBit in 1..BitSizeI(max)) {
set bits += [GenerateRandomBit()];
}
set output = ResultArrayAsInt(bits);
} until (output <= max and output >= min);
return output;
}
@EntryPoint()
operation SampleRandomNumber() : Int {
let min = 10;
let max = 50;
Message($"Sampling a random number between {min} and {max}: ");
return SampleRandomNumberInRange(min, max);
}
}