microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/algorithms/CatState.qs
35lines · modecode
| 1 | /// # Sample |
| 2 | /// Cat State |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// A cat state is a highly entangled state where the qubits are in a |
| 6 | /// superposition of all |0...0〉 or all |1...1〉. |
| 7 | /// |
| 8 | /// This Q# program implements a cat state of 5 qubits. |
| 9 | import Std.Diagnostics.*; |
| 10 | |
| 11 | operation Main() : Result[] { |
| 12 | use register = Qubit[5]; |
| 13 | |
| 14 | // Prepare a cat state using the allocated register. |
| 15 | PrepareCatState(register); |
| 16 | |
| 17 | // Show the cat state. |
| 18 | DumpMachine(); |
| 19 | |
| 20 | // Measure and reset qubits before releasing them. |
| 21 | MResetEachZ(register) |
| 22 | } |
| 23 | |
| 24 | /// # Summary |
| 25 | /// Prepares state (|0...0〉 + |1...1〉) / √2 (a generalized GHZ state |
| 26 | /// or a cat state) across the `register` of qubits. |
| 27 | /// All qubits are assumed to be in |0〉 state on input. |
| 28 | operation PrepareCatState(register : Qubit[]) : Unit { |
| 29 | Fact(Length(register) > 0, "Qubit register must not be empty."); |
| 30 | |
| 31 | // Set the first qubit in the register into a (|0〉 + |1〉) / √2 superposition. |
| 32 | // Then apply a CNOT to the remaining qubits using the first qubit as control. |
| 33 | H(register[0]); |
| 34 | ApplyToEach(CNOT(register[0], _), register[1...]); |
| 35 | } |
| 36 | |