microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/random_numbers/random_two_bits/solution.qs
23lines · modecode
| 1 | namespace Kata { |
| 2 | operation RandomTwoBits() : Int { |
| 3 | return 2 * RandomBit() + RandomBit(); |
| 4 | } |
| 5 | |
| 6 | operation RandomBit() : Int { |
| 7 | // Allocate single qubit. |
| 8 | use q = Qubit(); |
| 9 | |
| 10 | // Set qubit in superposition state. |
| 11 | H(q); |
| 12 | |
| 13 | // Measuring the qubit and reset. |
| 14 | let result = M(q); |
| 15 | Reset(q); |
| 16 | |
| 17 | // Return integer value of result. |
| 18 | if result == One { |
| 19 | return 1; |
| 20 | } |
| 21 | return 0; |
| 22 | } |
| 23 | } |
| 24 | |