microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
katas/content/random_numbers/weighted_random_bit/solution.md
32lines · modecode
| 1 | An arbitrary single-qubit state can be written as: |
| 2 | |
| 3 | $$ |
| 4 | |\psi\rangle = |
| 5 | \cos \frac{\theta}{2} |0 \rangle + e^{i\phi} \sin \frac{\theta}{2} |1\rangle |
| 6 | $$ |
| 7 | |
| 8 | Here, $\theta$ is the angle between the state vector and the $Z$-axis, and $\phi$ is the longitude angle with respect to the $X$-axis on the Bloch sphere. |
| 9 | |
| 10 | Our goal is to generate $0$ or $1$ with the probability of generating a $0$ equal to $x$ and the probability of generating a $1$ equal to $1 - x$. This means that the qubit state should look like |
| 11 | |
| 12 | $$ |
| 13 | |\psi\rangle = |
| 14 | \sqrt x |0 \rangle + \sqrt{1 - x} |1\rangle |
| 15 | $$ |
| 16 | |
| 17 | Comparing the amplitudes of the state $|0 \rangle$ in the two equations, we get |
| 18 | |
| 19 | $$ |
| 20 | \sqrt x = \cos \frac{\theta}{2} \Rightarrow \theta = 2 \arccos\sqrt x |
| 21 | $$ |
| 22 | |
| 23 | Since $\theta$ is the angle between the state vector and the $Z$-axis, we need to apply the $Ry$ gate with the calculated $\theta$ to the starting state $|0 \rangle$ to get the desired qubit state. |
| 24 | |
| 25 | The $Ry$ gate applies a given rotation about the $Y$-axis (i.e., in the $ZX$-plane). Hence, $\phi$ (longitude angle with respect to $X$-axis) is always equal to $0^{\circ}$, which means that the relative phase $e^{i\phi}$ doesn't have any impact on the resulting qubit state. |
| 26 | |
| 27 | > We can also calculate ${\theta}$ by comparing the amplitudes of the state $|1 \rangle$ in the two equations, which is $2 \arcsin\sqrt{1.0 - x}$. The results will be equivalent. |
| 28 | |
| 29 | @[solution]({ |
| 30 | "id": "random_numbers__weighted_random_bit_solution", |
| 31 | "codePath": "Solution.qs" |
| 32 | }) |
| 33 | |