microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.3.1

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

17lines · modecode

1namespace Kata {
2 open Microsoft.Quantum.Measurement;
3 operation RandomNBits(N : Int) : Int {
4 mutable result = 0;
5 for i in 0 .. N - 1 {
6 set result = result * 2 + RandomBit();
7 }
8 return result;
9 }
10
11 // You can use the operation defined in the first exercise to implement your solution.
12 operation RandomBit() : Int {
13 use q = Qubit();
14 H(q);
15 return MResetZ(q) == Zero ? 0 | 1;
16 }
17}