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