microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/random_numbers/weighted_random_bit/Solution.qs
14lines · modecode
| 1 | namespace Kata { |
| 2 | open Microsoft.Quantum.Math; |
| 3 | open Microsoft.Quantum.Measurement; |
| 4 | |
| 5 | operation WeightedRandomBit(x : Double) : Int { |
| 6 | let theta = 2.0 * ArcCos(Sqrt(x)); // (or) 2.0 * ArcSin(Sqrt(1.0 - x)); |
| 7 | |
| 8 | use q = Qubit(); |
| 9 | Ry(theta, q); |
| 10 | |
| 11 | let result = MResetZ(q); |
| 12 | return result == Zero ? 0 | 1; |
| 13 | } |
| 14 | } |
| 15 | |