microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/tests-integration/resources/adaptive_ri/input/Functors.qs
51lines · modecode
| 1 | namespace Test { |
| 2 | |
| 3 | import Std.Intrinsic.*; |
| 4 | import Std.Measurement.*; |
| 5 | |
| 6 | // Verifies use of Q# functors. |
| 7 | // Expected simulation output: |
| 8 | // ([0, 0], [1, 0], [0, 0]) -> 0.5 |
| 9 | // ([0, 0], [1, 1], [0, 0]) -> 0.5 |
| 10 | @EntryPoint() |
| 11 | operation Main() : (Result[], Result[], Result[]) { |
| 12 | use targetsA = Qubit[2]; |
| 13 | Unitary(targetsA); |
| 14 | Adjoint Unitary(targetsA); |
| 15 | |
| 16 | use controls = Qubit[2]; |
| 17 | use targetsB = Qubit[2]; |
| 18 | within { |
| 19 | for q in controls { |
| 20 | X(q); |
| 21 | } |
| 22 | } apply { |
| 23 | Controlled Unitary(controls, targetsB); |
| 24 | } |
| 25 | |
| 26 | use targetsC = Qubit[2]; |
| 27 | within { |
| 28 | for q in controls { |
| 29 | X(q); |
| 30 | } |
| 31 | } apply { |
| 32 | Controlled Unitary(controls, targetsC); |
| 33 | Controlled Adjoint Unitary(controls, targetsC); |
| 34 | } |
| 35 | |
| 36 | let rA = MeasureEachZ(targetsA); |
| 37 | let rB = MeasureEachZ(targetsB); |
| 38 | let rC = MeasureEachZ(targetsC); |
| 39 | ResetAll(controls); |
| 40 | ResetAll(targetsA); |
| 41 | ResetAll(targetsB); |
| 42 | ResetAll(targetsC); |
| 43 | return (rA, rB, rC); |
| 44 | } |
| 45 | |
| 46 | operation Unitary(register : Qubit[]) : Unit is Adj + Ctl { |
| 47 | X(register[0]); |
| 48 | H(register[1]); |
| 49 | Z(register[1]); |
| 50 | } |
| 51 | } |