microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/algorithms/JointMeasurement.qs
40lines · modecode
| 1 | /// # Sample |
| 2 | /// Joint Measurement |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// Joint measurements, also known as Pauli measurements, are a generalization |
| 6 | /// of 2-outcome measurements to multiple qubits and other bases. |
| 7 | namespace Sample { |
| 8 | open Microsoft.Quantum.Diagnostics; |
| 9 | @EntryPoint() |
| 10 | operation Main() : (Result, Result[]) { |
| 11 | // Prepare an entangled state. |
| 12 | use qs = Qubit[2]; // |00〉 |
| 13 | H(qs[0]); // 1/sqrt(2)(|00〉 + |10〉) |
| 14 | CNOT(qs[0], qs[1]); // 1/sqrt(2)(|00〉 + |11〉) |
| 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 first 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 | let parityResult = Measure([PauliZ, PauliZ], qs[...1]); |
| 24 | DumpMachine(); |
| 25 | |
| 26 | // However, if we perform a measurement just on the first qubit, we can |
| 27 | // see how the state collapses. |
| 28 | let firstQubitResult = M(qs[0]); |
| 29 | DumpMachine(); |
| 30 | |
| 31 | // Measuring the last qubit does not change the quantum state |
| 32 | // since the state of the second qubit collapsed when the first qubit |
| 33 | // was measured because they were entangled. |
| 34 | let secondQubitResult = M(qs[1]); |
| 35 | DumpMachine(); |
| 36 | |
| 37 | ResetAll(qs); |
| 38 | return (parityResult, [firstQubitResult, secondQubitResult]); |
| 39 | } |
| 40 | } |