microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/getting_started/BellStates.qs
72lines · modecode
| 1 | /// # Summary |
| 2 | /// Bell States sample |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// This Q# sample shows how to prepare the four different Bell states. |
| 6 | /// |
| 7 | /// # Remarks |
| 8 | /// Bell states or EPR pairs are specific quantum states of two qubits |
| 9 | /// that represent the simplest (and maximal) examples of quantum entanglement. |
| 10 | /// |
| 11 | /// # References |
| 12 | /// - [Bell state](https://en.wikipedia.org/wiki/Bell_state) |
| 13 | operation Main() : (Result, Result)[] { |
| 14 | // Prepare and measure each pair. Return an array of these results. |
| 15 | [ |
| 16 | PrepareAndMeasurePair(PreparePhiPlus, "|Φ+〉"), |
| 17 | PrepareAndMeasurePair(PreparePhiMinus, "|Φ-〉"), |
| 18 | PrepareAndMeasurePair(PreparePsiPlus, "|Ψ+〉"), |
| 19 | PrepareAndMeasurePair(PreparePsiMinus, "|Ψ-〉") |
| 20 | ] |
| 21 | } |
| 22 | |
| 23 | /// # Summary |
| 24 | /// Allocates a pair of qubits, prepares them using `preparation` operation, |
| 25 | /// Then measures and resets them. Returns a pair of results. |
| 26 | operation PrepareAndMeasurePair( |
| 27 | preparation : Qubit[] => Unit, |
| 28 | name : String |
| 29 | ) : (Result, Result) { |
| 30 | // Allocate a pair of qubits |
| 31 | use pair = Qubit[2]; |
| 32 | // Prepare them using the preparation operation |
| 33 | preparation(pair); |
| 34 | // Show the name of the prepared state |
| 35 | Message($"Bell state {name}:"); |
| 36 | // Show the prepared state |
| 37 | Std.Diagnostics.DumpMachine(); |
| 38 | // Measure, reset and return |
| 39 | (MResetZ(pair[0]), MResetZ(pair[1])) |
| 40 | } |
| 41 | |
| 42 | /// # Summary |
| 43 | /// Prepares |Φ+⟩ = (|00⟩+|11⟩)/√2 state assuming `register` is in |00⟩ state. |
| 44 | operation PreparePhiPlus(register : Qubit[]) : Unit { |
| 45 | H(register[0]); // |+0〉 |
| 46 | CNOT(register[0], register[1]); // (|00〉 + |11〉)/sqrt(2) |
| 47 | } |
| 48 | |
| 49 | /// # Summary |
| 50 | /// Prepares |Φ−⟩ = (|00⟩-|11⟩)/√2 state assuming `register` is in |00⟩ state. |
| 51 | operation PreparePhiMinus(register : Qubit[]) : Unit { |
| 52 | H(register[0]); // |+0〉 |
| 53 | Z(register[0]); // |-0〉 |
| 54 | CNOT(register[0], register[1]); // (|00〉 - |11〉)/sqrt(2) |
| 55 | } |
| 56 | |
| 57 | /// # Summary |
| 58 | /// Prepares |Ψ+⟩ = (|01⟩+|10⟩)/√2 state assuming `register` is in |00⟩ state. |
| 59 | operation PreparePsiPlus(register : Qubit[]) : Unit { |
| 60 | H(register[0]); // |+0〉 |
| 61 | X(register[1]); // |+1〉 |
| 62 | CNOT(register[0], register[1]); // (|01〉 + |10〉)/sqrt(2) |
| 63 | } |
| 64 | |
| 65 | /// # Summary |
| 66 | /// Prepares |Ψ−⟩ = (|01⟩-|10⟩)/√2 state assuming `register` is in |00⟩ state. |
| 67 | operation PreparePsiMinus(register : Qubit[]) : Unit { |
| 68 | H(register[0]); // |+0〉 |
| 69 | Z(register[0]); // |-0〉 |
| 70 | X(register[1]); // |-1〉 |
| 71 | CNOT(register[0], register[1]); // (|01〉 - |10〉)/sqrt(2) |
| 72 | } |
| 73 | |