microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/algorithms/BellState.qs
69lines · 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 | namespace Sample { |
| 10 | open Microsoft.Quantum.Diagnostics; |
| 11 | open Microsoft.Quantum.Measurement; |
| 12 | |
| 13 | @EntryPoint() |
| 14 | operation BellStates() : (Result, Result)[] { |
| 15 | // This array contains a label and a preparation operation for each one |
| 16 | // of the four Bell states. |
| 17 | let bellStateTuples = [ |
| 18 | ("|Φ+〉", PreparePhiPlus), |
| 19 | ("|Φ-〉", PreparePhiMinus), |
| 20 | ("|Ψ+〉", PreparePsiPlus), |
| 21 | ("|Ψ-〉", PreparePsiMinus) |
| 22 | ]; |
| 23 | |
| 24 | // Prepare all Bell states, show them using the `DumpMachine` operation |
| 25 | // and measure the Bell state qubits. |
| 26 | mutable measurements = []; |
| 27 | for (label, prepare) in bellStateTuples { |
| 28 | // Allocate the two qubits that will be used to create a Bell state. |
| 29 | use register = Qubit[2]; |
| 30 | prepare(register); |
| 31 | Message($"Bell state {label}:"); |
| 32 | DumpMachine(); |
| 33 | set measurements += [(MResetZ(register[0]), MResetZ(register[1]))]; |
| 34 | } |
| 35 | return measurements; |
| 36 | } |
| 37 | |
| 38 | /// # Summary |
| 39 | /// Prepares |Φ+⟩ = (|00⟩+|11⟩)/√2 state assuming `register` is in |00⟩ state. |
| 40 | operation PreparePhiPlus(register : Qubit[]) : Unit { |
| 41 | H(register[0]); // |+0〉 |
| 42 | CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 + |11〉) |
| 43 | } |
| 44 | |
| 45 | /// # Summary |
| 46 | /// Prepares |Φ−⟩ = (|00⟩-|11⟩)/√2 state assuming `register` is in |00⟩ state. |
| 47 | operation PreparePhiMinus(register : Qubit[]) : Unit { |
| 48 | H(register[0]); // |+0〉 |
| 49 | Z(register[0]); // |-0〉 |
| 50 | CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 - |11〉) |
| 51 | } |
| 52 | |
| 53 | /// # Summary |
| 54 | /// Prepares |Ψ+⟩ = (|01⟩+|10⟩)/√2 state assuming `register` is in |00⟩ state. |
| 55 | operation PreparePsiPlus(register : Qubit[]) : Unit { |
| 56 | H(register[0]); // |+0〉 |
| 57 | X(register[1]); // |+1〉 |
| 58 | CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 + |10〉) |
| 59 | } |
| 60 | |
| 61 | /// # Summary |
| 62 | /// Prepares |Ψ−⟩ = (|01⟩-|10⟩)/√2 state assuming `register` is in |00⟩ state. |
| 63 | operation PreparePsiMinus(register : Qubit[]) : Unit { |
| 64 | H(register[0]); // |+0〉 |
| 65 | Z(register[0]); // |-0〉 |
| 66 | X(register[1]); // |-1〉 |
| 67 | CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 - |10〉) |
| 68 | } |
| 69 | } |
| 70 | |