microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
samples/algorithms/CatState.qs
40lines · modeblame
ffd3f875César Zaragoza Cortés2 years ago | 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 | open Microsoft.Quantum.Diagnostics; | |
| 11 | open Microsoft.Quantum.Measurement; | |
| 12 | | |
| 13 | @EntryPoint() | |
| 14 | operation Main() : Result[] { | |
| 15 | use register = Qubit[5]; | |
| 16 | | |
| 17 | // Prepare a cat state using the allocated register. | |
| 18 | PrepareCatState(register); | |
| 19 | | |
| 20 | // Show the cat state. | |
| 21 | DumpMachine(); | |
| 22 | | |
| 23 | // Measure and reset qubits before releasing them. | |
| 24 | let results = MeasureEachZ(register); | |
| 25 | ResetAll(register); | |
| 26 | return results; | |
| 27 | } | |
| 28 | | |
| 29 | // Prepares a cat state 1/sqrt(2)(|0...0〉 + |1...1〉) using a qubit register | |
| 30 | // in the zero state. | |
| 31 | operation PrepareCatState(register : Qubit[]) : Unit { | |
| 32 | Fact(Length(register) > 0, "Qubit register must not be empty."); | |
| 33 | Fact(CheckAllZero(register), "Qubits are not in the |0〉 state."); | |
| 34 | | |
| 35 | // Set the first qubit in the register into a 1/sqrt(2)(|0〉 + |1〉) superposition. | |
| 36 | // Then apply a CNOT to the remaining qubits using the first qubit as control. | |
| 37 | H(register[0]); | |
| 38 | ApplyToEach(CNOT(register[0], _), register[1...]); | |
| 39 | } | |
| 40 | } |