microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
katas/content/random_numbers/random_n_bits/Solution.qs
17lines · modecode
| 1 | namespace 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 | } |