microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/random_numbers/index.md
91lines · modecode
| 1 | # Quantum Random Number Generation |
| 2 | |
| 3 | @[section]({"id": "random_numbers__overview", "title": "Overview"}) |
| 4 | |
| 5 | True random number generation is a notoriously difficult problem. Many "random" generators today are actually pseudo-random, using a starting seed to spawn seemingly-random numbers that are actually a repeatable function of that seed. Most true random number generators are based on measurements of some natural phenomenon, such as atmospheric noise or atomic decay. You can read more about it <a href="https://en.wikipedia.org/wiki/Random_number_generation" target="_blank">here</a>. |
| 6 | |
| 7 | Quantum random number generators (QRNGs) are truly random. Of course, this only applies to the case when they run on a real quantum device, relying on the randomness of the quantum state collapse during measurement to produce the random numbers. When QRNGs run on a simulator, the source of randomness is the same as for other classical programs, so the generated numbers are pseudo-random. |
| 8 | |
| 9 | The quantum algorithm for random number generation is one of the simplest applications of quantum computing principles, requiring very few qubits to run. |
| 10 | |
| 11 | **This kata covers the following topics:** |
| 12 | |
| 13 | - Quantum random number generation and the principles behind it |
| 14 | - Implementation of a variety of QRNGs with equal probability of any given number |
| 15 | - Implementation a single-bit QRNG with weighted probabilities of generated bits |
| 16 | |
| 17 | **What you should know to start working on this kata:** |
| 18 | |
| 19 | - The concept of qubit and measurement |
| 20 | - Single-qubit gates |
| 21 | |
| 22 | @[section]({"id": "random_numbers__introduction", "title": "Introduction"}) |
| 23 | |
| 24 | Recall from the Qubit kata that a qubit state $|\psi\rangle$ is defined via the basis states $|0\rangle$ and $|1\rangle$ as $|\psi\rangle = \begin{bmatrix} \alpha \\\ \beta \end{bmatrix} = \alpha|0\rangle + \beta|1\rangle$, where $|\alpha|^2 + |\beta|^2 = 1$. |
| 25 | |
| 26 | We call $\alpha$ and $\beta$ the probability amplitudes of states $|0\rangle$ and $|1\rangle$, respectively. When $|\psi\rangle$ is measured in the $\\{|0\rangle, |1\rangle\\}$ basis (the computational basis), the probabilities of the outcomes are defined based on the state amplitudes: there is a $|\alpha|^2$ probability that the measurement result will be $0$, and a $|\beta|^2$ probability that the measurement result will be $1$. |
| 27 | |
| 28 | > For example, a qubit in state $\begin{bmatrix} \frac{1}{\sqrt{2}} \\\ \frac{1}{\sqrt{2}} \end{bmatrix}$ will yield measurement results $0$ or $1$ with equal probability, while a qubit in state $\begin{bmatrix} \frac{1}{2} \\\ \frac{\sqrt3}{2} \end{bmatrix}$ will yield measurement result $0$ only 25% of the time, and $1$ 75% of the time. |
| 29 | |
| 30 | This knowledge is sufficient to implement a simple random number generator! |
| 31 | |
| 32 | > Remember that you can refer to the Single-Qubit Gates kata if you need a refresher on the various quantum gates and their usage in Q#. |
| 33 | |
| 34 | @[exercise]({ |
| 35 | "id": "random_numbers__random_bit", |
| 36 | "title": "Generate a Single Random Bit", |
| 37 | "path": "./random_bit/", |
| 38 | "qsDependencies": [ |
| 39 | "../KatasLibrary.qs", |
| 40 | "./Common.qs" |
| 41 | ] |
| 42 | }) |
| 43 | |
| 44 | @[exercise]({ |
| 45 | "id": "random_numbers__random_two_bits", |
| 46 | "title": "Generate a Random Two-Bit Number", |
| 47 | "path": "./random_two_bits/", |
| 48 | "qsDependencies": [ |
| 49 | "../KatasLibrary.qs", |
| 50 | "./Common.qs" |
| 51 | ] |
| 52 | }) |
| 53 | |
| 54 | @[exercise]({ |
| 55 | "id": "random_numbers__random_n_bits", |
| 56 | "title": "Generate a Number of Arbitrary Size", |
| 57 | "path": "./random_n_bits/", |
| 58 | "qsDependencies": [ |
| 59 | "../KatasLibrary.qs", |
| 60 | "./Common.qs" |
| 61 | ] |
| 62 | }) |
| 63 | |
| 64 | @[exercise]({ |
| 65 | "id": "random_numbers__weighted_random_bit", |
| 66 | "title": "Generate a Weighted Bit", |
| 67 | "path": "./weighted_random_bit/", |
| 68 | "qsDependencies": [ |
| 69 | "../KatasLibrary.qs", |
| 70 | "./Common.qs" |
| 71 | ] |
| 72 | }) |
| 73 | |
| 74 | @[exercise]({ |
| 75 | "id": "random_numbers__random_number", |
| 76 | "title": "Generate a Random Number Between Min and Max", |
| 77 | "path": "./random_number/", |
| 78 | "qsDependencies": [ |
| 79 | "../KatasLibrary.qs", |
| 80 | "./Common.qs" |
| 81 | ] |
| 82 | }) |
| 83 | |
| 84 | @[section]({"id": "random_numbers__whats_next", "title": "What's Next?"}) |
| 85 | |
| 86 | Congratulations! In this kata you have created a random number generator. Here are a few key concepts to keep in mind: |
| 87 | |
| 88 | - This code will generate truly random numbers when executed on a true quantum computer. Random numbers obtained when executing on a simulator are only as good as the source of randomness used by the simulator. |
| 89 | - You can generate a random bit by preparing a qubit in superposition and then measuring it in the computational basis. |
| 90 | The amplitudes of the basis states will define the probability distribution of the generated bits. |
| 91 | - The Q# library function `BitSizeI` returns the number of bits in the binary representation of an integer. |
| 92 | |