microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
katas/content/marking_oracles/num_div_3/Solution.qs
23lines · modecode
| 1 | namespace Kata { |
| 2 | operation Oracle_DivisibleBy3 (x : Qubit[], y : Qubit) : Unit is Adj + Ctl { |
| 3 | use counter = Qubit[2]; |
| 4 | within { |
| 5 | for i in 0 .. Length(x) - 1 { |
| 6 | if i % 2 == 0 { |
| 7 | Controlled IncrementMod3([x[i]], counter); |
| 8 | } else { |
| 9 | Controlled Adjoint IncrementMod3([x[i]], counter); |
| 10 | } |
| 11 | } |
| 12 | } apply { |
| 13 | ApplyControlledOnInt(0, X, counter, y); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | operation IncrementMod3 (counterRegister : Qubit[]) : Unit is Adj + Ctl { |
| 18 | let sum = counterRegister[0]; |
| 19 | let carry = counterRegister[1]; |
| 20 | ApplyControlledOnInt(0, X, [carry], sum); |
| 21 | ApplyControlledOnInt(0, X, [sum], carry); |
| 22 | } |
| 23 | } |
| 24 | |