microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/getting_started/JointMeasurement.qs
43lines · modecode
| 1 | import Std.Diagnostics.*; |
| 2 | |
| 3 | /// # Summary |
| 4 | /// Joint Measurement sample |
| 5 | /// |
| 6 | /// # Description |
| 7 | /// This Q# program demonstrates how to use Joint measurements. |
| 8 | /// Joint measurement, also known as Pauli measurements, are a generalization |
| 9 | /// of 2-outcome measurements to multiple qubits and other bases. |
| 10 | operation Main() : (Result, Result[]) { |
| 11 | // Prepare an entangled state. |
| 12 | use qs = Qubit[2]; // |00〉 |
| 13 | H(qs[0]); // (|00〉 + |10〉)/sqrt(2) |
| 14 | CNOT(qs[0], qs[1]); // (|00〉 + |11〉)/sqrt(2) |
| 15 | |
| 16 | // Show the quantum state before performing the joint measurement. |
| 17 | DumpMachine(); |
| 18 | |
| 19 | // The below code uses a joint measurement as a way to check the parity |
| 20 | // of the two qubits. In this case, the parity measurement result |
| 21 | // will always be `Zero`. |
| 22 | // Notice how the state was not collapsed by the joint measurement |
| 23 | // because this state is the eigenvector of the Z⊗Z operator. |
| 24 | let parityResult = Measure([PauliZ, PauliZ], qs); |
| 25 | DumpMachine(); |
| 26 | |
| 27 | // However, if we perform a measurement just on the first qubit, we can |
| 28 | // see how the state collapses. |
| 29 | let firstQubitResult = M(qs[0]); |
| 30 | DumpMachine(); |
| 31 | |
| 32 | // Measuring the last qubit does not change the quantum state |
| 33 | // since the state of the second qubit collapsed when the first qubit |
| 34 | // was measured because they were entangled. |
| 35 | let secondQubitResult = M(qs[1]); |
| 36 | DumpMachine(); |
| 37 | |
| 38 | // Reset qubits before they are released. |
| 39 | ResetAll(qs); |
| 40 | |
| 41 | // Return results |
| 42 | (parityResult, [firstQubitResult, secondQubitResult]) |
| 43 | } |
| 44 | |