microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/algorithms/BellState.qs
65lines · modecode
| 1 | /// # Sample |
| 2 | /// Bell States |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// Bell states or EPR pairs are specific quantum states of two qubits |
| 6 | /// that represent the simplest (and maximal) examples of quantum entanglement. |
| 7 | /// |
| 8 | /// This Q# program implements the four different Bell states. |
| 9 | import Std.Diagnostics.*; |
| 10 | |
| 11 | operation Main() : (Result, Result)[] { |
| 12 | // This array contains a label and a preparation operation for each one |
| 13 | // of the four Bell states. |
| 14 | let bellStateTuples = [ |
| 15 | ("|Φ+〉", PreparePhiPlus), |
| 16 | ("|Φ-〉", PreparePhiMinus), |
| 17 | ("|Ψ+〉", PreparePsiPlus), |
| 18 | ("|Ψ-〉", PreparePsiMinus) |
| 19 | ]; |
| 20 | |
| 21 | // Prepare all Bell states, show them using the `DumpMachine` operation |
| 22 | // and measure the Bell state qubits. |
| 23 | mutable measurements = []; |
| 24 | for (label, prepare) in bellStateTuples { |
| 25 | // Allocate the two qubits that will be used to create a Bell state. |
| 26 | use register = Qubit[2]; |
| 27 | prepare(register); |
| 28 | Message($"Bell state {label}:"); |
| 29 | DumpMachine(); |
| 30 | set measurements += [(MResetZ(register[0]), MResetZ(register[1]))]; |
| 31 | } |
| 32 | return measurements; |
| 33 | } |
| 34 | |
| 35 | /// # Summary |
| 36 | /// Prepares |Φ+⟩ = (|00⟩+|11⟩)/√2 state assuming `register` is in |00⟩ state. |
| 37 | operation PreparePhiPlus(register : Qubit[]) : Unit { |
| 38 | H(register[0]); // |+0〉 |
| 39 | CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 + |11〉) |
| 40 | } |
| 41 | |
| 42 | /// # Summary |
| 43 | /// Prepares |Φ−⟩ = (|00⟩-|11⟩)/√2 state assuming `register` is in |00⟩ state. |
| 44 | operation PreparePhiMinus(register : Qubit[]) : Unit { |
| 45 | H(register[0]); // |+0〉 |
| 46 | Z(register[0]); // |-0〉 |
| 47 | CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 - |11〉) |
| 48 | } |
| 49 | |
| 50 | /// # Summary |
| 51 | /// Prepares |Ψ+⟩ = (|01⟩+|10⟩)/√2 state assuming `register` is in |00⟩ state. |
| 52 | operation PreparePsiPlus(register : Qubit[]) : Unit { |
| 53 | H(register[0]); // |+0〉 |
| 54 | X(register[1]); // |+1〉 |
| 55 | CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 + |10〉) |
| 56 | } |
| 57 | |
| 58 | /// # Summary |
| 59 | /// Prepares |Ψ−⟩ = (|01⟩-|10⟩)/√2 state assuming `register` is in |00⟩ state. |
| 60 | operation PreparePsiMinus(register : Qubit[]) : Unit { |
| 61 | H(register[0]); // |+0〉 |
| 62 | Z(register[0]); // |-0〉 |
| 63 | X(register[1]); // |-1〉 |
| 64 | CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 - |10〉) |
| 65 | } |
| 66 | |