You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/quantum/qsharp-create-first-quantum-development-kit/includes/4-random-number-generator.md
+66-65Lines changed: 66 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,22 +58,22 @@ For the complete random number generator, you're going to reuse the operation de
58
58
The `GenerateRandomBit` operation should look like this:
59
59
60
60
```qsharp
61
-
operation GenerateRandomBit() : Result {
62
-
// Allocate a qubit.
63
-
use q = Qubit();
61
+
operation GenerateRandomBit() : Result {
62
+
// Allocate a qubit.
63
+
use q = Qubit();
64
64
65
-
// Set the qubit into superposition of 0 and 1 using the Hadamard
66
-
H(q);
65
+
// Set the qubit into superposition of 0 and 1 using the Hadamard
66
+
H(q);
67
67
68
-
// Measure the qubit and store the result.
69
-
let result = M(q);
68
+
// Measure the qubit and store the result.
69
+
let result = M(q);
70
70
71
-
// Reset qubit to the |0〉 state.
72
-
Reset(q);
71
+
// Reset qubit to the |0〉 state.
72
+
Reset(q);
73
73
74
-
// Return the result of the measurement.
75
-
return result;
76
-
}
74
+
// Return the result of the measurement.
75
+
return result;
76
+
}
77
77
```
78
78
79
79
### Define the quantum random number operation
@@ -83,30 +83,30 @@ Here, you define the `GenerateRandomNumberInRange` operation. This operation rep
83
83
Copy the following code and paste it before the `GenerateRandomBit` operation into your `Main.qs` file:
84
84
85
85
```qsharp
86
-
/// Generates a random number between 0 and `max`.
87
-
operation GenerateRandomNumberInRange(max : Int) : Int {
88
-
// Determine the number of bits needed to represent `max` and store it
89
-
// in the `nBits` variable. Then generate `nBits` random bits which will
90
-
// represent the generated random number.
91
-
mutable bits = [];
92
-
let nBits = BitSizeI(max);
93
-
for idxBit in 1..nBits {
94
-
set bits += [GenerateRandomBit()];
95
-
}
96
-
let sample = ResultArrayAsInt(bits);
97
-
98
-
// Return random number if it is within the requested range.
99
-
// Generate it again if it is outside the range.
100
-
return sample > max ? GenerateRandomNumberInRange(max) | sample;
86
+
/// Generates a random number between 0 and `max`.
87
+
operation GenerateRandomNumberInRange(max : Int) : Int {
88
+
// Determine the number of bits needed to represent `max` and store it
89
+
// in the `nBits` variable. Then generate `nBits` random bits which will
90
+
// represent the generated random number.
91
+
mutable bits = [];
92
+
let nBits = BitSizeI(max);
93
+
for idxBit in 1..nBits {
94
+
set bits += [GenerateRandomBit()];
101
95
}
96
+
let sample = ResultArrayAsInt(bits);
97
+
98
+
// Return random number if it is within the requested range.
99
+
// Generate it again if it is outside the range.
100
+
return sample > max ? GenerateRandomNumberInRange(max) | sample;
101
+
}
102
102
```
103
103
104
104
Let's take a moment to review the new code.
105
105
106
-
* You need to calculate the number of bits needed to express integers up to `max`. The `BitSizeI` function from the `Microsoft.Quantum.Math` library converts an integer to the number of bits needed to represent it.
107
-
* The `GenerateRandomNumberInRange` operation uses a `for` loop to generate random numbers until it generates one that's equal to or less than `max`. The `for` loop works exactly the same as a `for` loop in other programming languages.
108
-
* The variable `bits` is a mutable variable. A mutable variable is one that can change during the computation. You use the `set` directive to change a mutable variable's value.
109
-
* The `ResultArrayAsInt` function comes from the `Microsoft.Quantum.Convert` library. This function converts the bit string to a positive integer.
106
+
- You need to calculate the number of bits needed to express integers up to `max`. The `BitSizeI` function from the `Microsoft.Quantum.Math` library converts an integer to the number of bits needed to represent it.
107
+
- The `GenerateRandomNumberInRange` operation uses a `for` loop to generate random numbers until it generates one that's equal to or less than `max`. The `for` loop works exactly the same as a `for` loop in other programming languages.
108
+
- The variable `bits` is a mutable variable. A mutable variable is one that can change during the computation. You use the `set` directive to change a mutable variable's value.
109
+
- The `ResultArrayAsInt` function comes from the `Microsoft.Quantum.Convert` library. This function converts the bit string to a positive integer.
110
110
111
111
### Add an entry point
112
112
@@ -118,6 +118,7 @@ Copy and paste the following code to your `Main.qs` file:
118
118
operation Main() : Int {
119
119
let max = 100;
120
120
Message($"Sampling a random number between 0 and {max}: ");
121
+
121
122
// Generate random number in the 0..max range.
122
123
return GenerateRandomNumberInRange(max);
123
124
}
@@ -131,48 +132,48 @@ Your `Main.qs` file should look like this:
131
132
import Microsoft.Quantum.Convert.*;
132
133
import Microsoft.Quantum.Math.*;
133
134
134
-
operation Main() : Int {
135
-
let max = 100;
136
-
Message($"Sampling a random number between 0 and {max}: ");
137
-
138
-
// Generate random number in the 0..max range.
139
-
return GenerateRandomNumberInRange(max);
140
-
}
135
+
operation Main() : Int {
136
+
let max = 100;
137
+
Message($"Sampling a random number between 0 and {max}: ");
141
138
142
-
/// Generates a random number between 0 and `max`.
143
-
operation GenerateRandomNumberInRange(max : Int) : Int {
144
-
// Determine the number of bits needed to represent `max` and store it
145
-
// in the `nBits` variable. Then generate `nBits` random bits which will
146
-
// represent the generated random number.
147
-
mutable bits = [];
148
-
let nBits = BitSizeI(max);
149
-
for idxBit in 1..nBits {
150
-
set bits += [GenerateRandomBit()];
151
-
}
152
-
let sample = ResultArrayAsInt(bits);
139
+
// Generate random number in the 0..max range.
140
+
return GenerateRandomNumberInRange(max);
141
+
}
153
142
154
-
// Return random number if it is within the requested range.
155
-
// Generate it again if it is outside the range.
156
-
return sample > max ? GenerateRandomNumberInRange(max) | sample;
143
+
/// Generates a random number between 0 and `max`.
144
+
operation GenerateRandomNumberInRange(max : Int) : Int {
145
+
// Determine the number of bits needed to represent `max` and store it
146
+
// in the `nBits` variable. Then generate `nBits` random bits which will
147
+
// represent the generated random number.
148
+
mutable bits = [];
149
+
let nBits = BitSizeI(max);
150
+
for idxBit in 1..nBits {
151
+
set bits += [GenerateRandomBit()];
157
152
}
153
+
let sample = ResultArrayAsInt(bits);
158
154
159
-
operation GenerateRandomBit() : Result {
160
-
// Allocate a qubit.
161
-
use q = Qubit();
155
+
// Return random number if it is within the requested range.
156
+
// Generate it again if it is outside the range.
157
+
return sample > max ? GenerateRandomNumberInRange(max) | sample;
158
+
}
162
159
163
-
// Set the qubit into superposition of 0 and 1 using the Hadamard operation
164
-
H(q);
160
+
operation GenerateRandomBit() : Result {
161
+
// Allocate a qubit.
162
+
use q = Qubit();
165
163
166
-
// Measure the qubit value using the `M` operation, and store the
167
-
// measurement value in the `result` variable.
168
-
let result = M(q);
164
+
// Set the qubit into superposition of 0 and 1 using the Hadamard operation
165
+
H(q);
169
166
170
-
// Reset qubit to the |0〉 state.
171
-
Reset(q);
167
+
// Measure the qubit value using the `M` operation, and store the
Copy file name to clipboardExpand all lines: learn-pr/quantum/qsharp-create-first-quantum-development-kit/includes/6-summary.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ In this module, we covered a lot. Here are a few key concepts to keep in mind:
12
12
13
13
## Next steps
14
14
15
-
The Quantum Development Kit includes a set of built-in Q# samples that you can use to learn more about Q# and quantum computing. To view the samples, open a blank Q# file in Visual Studio Code and type `sample`, then select the sample you want to view from the list of options. For example, you can find the Q# code we used in unit 2 by selecting *Random Bit sample*.
15
+
The Quantum Development Kit includes a set of built-in Q# samples that you can use to learn more about Q# and quantum computing. To view the samples, open a blank Q# file in Visual Studio Code and type `sample`, then select the sample you want to view from the list of options. For example, you can find the Q# code we used in unit 2 by selecting the *Random Bits sample*.
0 commit comments