microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
katas/content/multi_qubit_measurements/examples/MeasuringOne.qs
42lines · modecode
| 1 | namespace Kata { |
| 2 | open Microsoft.Quantum.Convert; |
| 3 | open Microsoft.Quantum.Diagnostics; |
| 4 | open Microsoft.Quantum.Math; |
| 5 | open Microsoft.Quantum.Measurement; |
| 6 | |
| 7 | @EntryPoint() |
| 8 | operation DemoBasisMeasurement() : Unit { |
| 9 | let expected_probabilities = [1. / 9., 4. / 9., 0., 4. / 9.]; |
| 10 | |
| 11 | // Set up counter array for tracking measurement outcomes. |
| 12 | mutable countArray = [0, 0, 0, 0]; |
| 13 | |
| 14 | use qs = Qubit[2]; |
| 15 | let numRuns = 1000; |
| 16 | for i in 1 .. numRuns { |
| 17 | // Prepare the starting state. |
| 18 | Ry(2. * ArcCos(1. / 3.), qs[1]); |
| 19 | Controlled H([qs[1]], qs[0]); |
| 20 | if i == 1 { |
| 21 | Message("The state of the system before measurement is:"); |
| 22 | DumpMachine(); |
| 23 | } |
| 24 | |
| 25 | // Measure the first (most significant) qubit, then measure the second (least significant) qubit, |
| 26 | // and convert the result to an integer, interpreting it as big endian binary notation. |
| 27 | let result = (MResetZ(qs[0]) == One ? 1 | 0) * 2 + (MResetZ(qs[1]) == One ? 1 | 0); |
| 28 | |
| 29 | set countArray w/= result <- countArray[result] + 1; |
| 30 | } |
| 31 | |
| 32 | // Obtain simulated probability of measurement for each outcome. |
| 33 | mutable simulated_probabilities = []; |
| 34 | for i in 0 .. 3 { |
| 35 | set simulated_probabilities += |
| 36 | [IntAsDouble(countArray[i]) / IntAsDouble(numRuns)]; |
| 37 | } |
| 38 | |
| 39 | Message($"Theoretical measurement probabilities are {expected_probabilities}"); |
| 40 | Message($"Simulated measurement probabilities are {simulated_probabilities}"); |
| 41 | } |
| 42 | } |
| 43 | |