microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/getting_started/Superposition.qs
22lines · modecode
| 1 | /// # Summary |
| 2 | /// Superposition sample |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// This Q# program sets a qubit in a superposition of the computational basis |
| 6 | /// states |0〉 and |1〉 by applying a Hadamard transformation. |
| 7 | operation Main() : Result { |
| 8 | // Allocate a qubit. Qubit is in |0〉 state after allocation. |
| 9 | use qubit = Qubit(); |
| 10 | // Qubits are only accessible for the duration of the scope where they |
| 11 | // are allocated and are automatically released at the end of the scope. |
| 12 | |
| 13 | // Set the qubit in superposition by applying a Hadamard transformation. |
| 14 | H(qubit); |
| 15 | |
| 16 | // Show the quantum state (when running on a simulator). |
| 17 | Std.Diagnostics.DumpMachine(); |
| 18 | |
| 19 | // Measure then reset the qubit. Return the result. |
| 20 | // There is a 50% probability of measuring either `Zero` or `One`. |
| 21 | MResetZ(qubit) |
| 22 | } |
| 23 | |