microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/getting_started/CatStates.qs
44lines · modecode
| 1 | /// # Summary |
| 2 | /// Greenberger–Horne–Zeilinger state sample |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// This Q# program shows how to prepare a generalized Greenberger–Horne–Zeilinger |
| 6 | /// state (aka Cat state) in a register of 5 qubits. |
| 7 | /// |
| 8 | /// # Remarks |
| 9 | /// The Greenberger–Horne–Zeilinger state, or GHZ state, aka Cat state, |
| 10 | /// is a state defined as: |GHZ〉 = (|0...0〉 + |1...1〉)/√2. |
| 11 | /// |
| 12 | /// The GHZ state is said to be a maximally entangled state, a multi-qubit |
| 13 | /// state where the state of any one qubit is not separable from the state |
| 14 | /// of any of the other qubits. |
| 15 | /// |
| 16 | /// # References |
| 17 | /// - [Greenberger–Horne–Zeilinger state](https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state) |
| 18 | /// - [Cat state](https://en.wikipedia.org/wiki/Cat_state) |
| 19 | operation Main() : Result[] { |
| 20 | // Allocate 5 qubits for Cat₅ state. |
| 21 | use cat5 = Qubit[5]; |
| 22 | // Prepare a Cat₅ state in the register. |
| 23 | PrepareGHZState(cat5); |
| 24 | // Show the Cat₅ state. |
| 25 | Std.Diagnostics.DumpMachine(); |
| 26 | // Measure and reset qubits. Return results. |
| 27 | MResetEachZ(cat5) |
| 28 | } |
| 29 | |
| 30 | /// # Summary |
| 31 | /// Prepares GHZ state (|0...0〉 + |1...1〉) / √2 (aka Cat state) in `qs` register. |
| 32 | /// All qubits are assumed to be in |0〉 state on input. |
| 33 | operation PrepareGHZState(qs : Qubit[]) : Unit { |
| 34 | Std.Diagnostics.Fact(Length(qs) > 0, "Qubit register must not be empty."); |
| 35 | |
| 36 | // Set the first qubit into a (|0〉 + |1〉) / √2 superposition. |
| 37 | H(qs[0]); |
| 38 | |
| 39 | // Then apply a CNOT to the remaining qubits using the first qubit as control. |
| 40 | // This entangles first qubit with all the other qubits in the register. |
| 41 | for q in qs[1...] { |
| 42 | CNOT(qs[0], q); |
| 43 | } |
| 44 | } |
| 45 | |