microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/marking_oracles/majority/Solution.qs
26lines · modecode
| 1 | namespace Kata { |
| 2 | open Microsoft.Quantum.Math; |
| 3 | |
| 4 | operation Oracle_Majority(x : Qubit[], y : Qubit) : Unit is Adj + Ctl { |
| 5 | let N = Length(x); |
| 6 | let log = BitSizeI(N); |
| 7 | use inc = Qubit[log]; |
| 8 | within { |
| 9 | for q in x { |
| 10 | Controlled Increment([q], inc); |
| 11 | } |
| 12 | } apply { |
| 13 | CNOT(inc[log-1], y); |
| 14 | if N == 5 { |
| 15 | CCNOT(inc[0], inc[1], y); |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | operation Increment(register : Qubit[]) : Unit is Adj + Ctl { |
| 21 | if Length(register) > 1 { |
| 22 | Controlled Increment([register[0]], register[1 ...]); |
| 23 | } |
| 24 | X(register[0]); |
| 25 | } |
| 26 | } |
| 27 | |