microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dmitryv/select-updated

Branches

Tags

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

Clone

HTTPS

Download ZIP

katas/content/random_numbers/random_n_bits/solution.qs

27lines · modecode

1namespace Kata {
2 operation RandomNBits(N : Int) : Int {
3 mutable result = 0;
4 for i in 0..(N - 1) {
5 set result = result * 2 + RandomBit();
6 }
7 return result;
8 }
9
10 operation RandomBit() : Int {
11 // Allocate single qubit.
12 use q = Qubit();
13
14 // Set qubit in superposition state.
15 H(q);
16
17 // Measuring the qubit and reset.
18 let result = M(q);
19 Reset(q);
20
21 // Return integer value of result.
22 if result == One {
23 return 1;
24 }
25 return 0;
26 }
27}
28