microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/oracles/examples/PhaseOracleAltBit.qs
30lines · modecode
| 1 | namespace Kata { |
| 2 | open Microsoft.Quantum.Diagnostics; |
| 3 | |
| 4 | // This operation implements the oracle; we will learn how to implement oracles later in the kata |
| 5 | operation AlternatingBitPattern_PhaseOracle(x : Qubit[]) : Unit is Adj + Ctl { |
| 6 | use q = Qubit(); |
| 7 | X(q); |
| 8 | ApplyControlledOnBitString([false, true, false], Z, x, q); |
| 9 | ApplyControlledOnBitString([true, false, true], Z, x, q); |
| 10 | X(q); |
| 11 | } |
| 12 | |
| 13 | @EntryPoint() |
| 14 | operation PhaseOracle_Demo() : Unit { |
| 15 | use q = Qubit[3]; |
| 16 | ApplyToEachA(H, q); |
| 17 | |
| 18 | Message("Starting state (equal superposition of all basis states):"); |
| 19 | DumpMachine(); |
| 20 | |
| 21 | // Apply the oracle |
| 22 | AlternatingBitPattern_PhaseOracle(q); |
| 23 | |
| 24 | // Print the resulting state; notice which phases changed |
| 25 | Message("State after applying the phase oracle:"); |
| 26 | DumpMachine(); |
| 27 | |
| 28 | ResetAll(q); |
| 29 | } |
| 30 | } |
| 31 | |