|
| 1 | +/// Low-ancilla incrementer circuit. |
| 2 | + |
| 3 | + |
| 4 | +function Log2(x : Int) : Double { |
| 5 | + return Std.Math.Log(Std.Convert.IntAsDouble(x)) / Std.Math.LogOf2(); |
| 6 | +} |
| 7 | + |
| 8 | +/// Computes ans := CTO(x), where CTO(x) is number of trailing (least |
| 9 | +/// significant) "1" bits in register x before first zero bit. CTO(0)=0. |
| 10 | +/// ans must be prepared in zero state. |
| 11 | +operation CountTrailingOnes(x : Qubit[], ans : Qubit[]) : Unit is Ctl + Adj { |
| 12 | + body (...) { |
| 13 | + Controlled CountTrailingOnes([], (x, ans)); |
| 14 | + } |
| 15 | + controlled (ctrl, ...) { |
| 16 | + let x_len = Length(x); |
| 17 | + let ctrl_len = Length(ctrl); |
| 18 | + if ctrl_len >= 2 { |
| 19 | + use anc = Qubit(); |
| 20 | + within { |
| 21 | + AND(ctrl[0], ctrl[1], anc); |
| 22 | + } apply { |
| 23 | + Controlled CountTrailingOnes([anc] + ctrl[2..ctrl_len-1], (x, ans)); |
| 24 | + } |
| 25 | + } elif (x_len == 1 and ctrl_len == 0) { |
| 26 | + CNOT(x[0], ans[0]); |
| 27 | + } elif (x_len == 1 and ctrl_len == 1) { |
| 28 | + CCNOT(ctrl[0], x[0], ans[0]); |
| 29 | + } elif (x_len == 2 and ctrl_len == 0) { |
| 30 | + X(x[1]); |
| 31 | + CCNOT(x[0], x[1], ans[0]); |
| 32 | + X(x[1]); |
| 33 | + CCNOT(x[0], x[1], ans[1]); |
| 34 | + } elif (x_len == 2 and ctrl_len == 1) { |
| 35 | + use x0 = Qubit(); |
| 36 | + within { |
| 37 | + AND(ctrl[0], x[0], x0); |
| 38 | + } apply { |
| 39 | + X(x[1]); |
| 40 | + CCNOT(x0, x[1], ans[0]); |
| 41 | + X(x[1]); |
| 42 | + CCNOT(x0, x[1], ans[1]); |
| 43 | + } |
| 44 | + } else { |
| 45 | + let n : Int = Std.Math.Ceiling(Log2(x_len)); |
| 46 | + let x_low = x[0..(1 <<< (n - 1))-1]; |
| 47 | + let x_high = x[(1 <<< (n - 1))..x_len-1]; |
| 48 | + if (x_len == 1 <<< n) { |
| 49 | + Std.Diagnostics.Fact(Length(ans) >= n + 1, "ans too small"); |
| 50 | + Controlled CountTrailingOnes(ctrl, (x_low, ans[0..n-1])); |
| 51 | + Controlled CountTrailingOnes(ctrl + [ans[n-1]], (x_high, ans[0..n-2] + [ans[n]])); |
| 52 | + Controlled CNOT(ctrl, (ans[n], ans[n-1])); |
| 53 | + } else { |
| 54 | + Std.Diagnostics.Fact(Length(ans) >= n, "ans too small"); |
| 55 | + Controlled CountTrailingOnes(ctrl, (x_low, ans[0..n-1])); |
| 56 | + Controlled CountTrailingOnes(ctrl + [ans[n-1]], (x_high, ans[0..n-2])); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Flips first `ctr` bits in `target`. |
| 63 | +/// If `ctr==0`, does nothing. |
| 64 | +/// If `ctr==Length(target)`, flips all bits. |
| 65 | +/// If `ctr>Length(target)`, behavior is undefined. |
| 66 | +operation FlipFirst(target : Qubit[], ctr : Qubit[]) : Unit is Ctl + Adj { |
| 67 | + body (...) { |
| 68 | + Controlled FlipFirst([], (target, ctr)); |
| 69 | + } |
| 70 | + controlled (ctrl, ...) { |
| 71 | + let target_len = Length(target); |
| 72 | + let ctr_len = Length(ctr); |
| 73 | + let n = Std.Math.Floor(Log2(target_len)) + 1; |
| 74 | + let ctrl_len = Length(ctrl); |
| 75 | + if ctrl_len >= 2 { |
| 76 | + use anc = Qubit(); |
| 77 | + within { |
| 78 | + AND(ctrl[0], ctrl[1], anc); |
| 79 | + } apply { |
| 80 | + Controlled FlipFirst([anc] + ctrl[2..ctrl_len-1], (target, ctr)); |
| 81 | + } |
| 82 | + } elif (ctr_len > n) { |
| 83 | + // Counter too large, ignore highest qubits. |
| 84 | + Controlled FlipFirst(ctrl, (target, ctr[0..n-1])); |
| 85 | + } elif (ctr_len < n) { |
| 86 | + // Counter too small, can only affect prefix of target. |
| 87 | + Controlled FlipFirst(ctrl, (target[0..(1 <<< ctr_len)-2], ctr)); |
| 88 | + } elif (target_len == 1 and ctrl_len == 0) { |
| 89 | + CNOT(ctr[0], target[0]); |
| 90 | + } elif (target_len == 1 and ctrl_len == 1) { |
| 91 | + CCNOT(ctrl[0], ctr[0], target[0]); |
| 92 | + } elif (target_len == 2 and ctrl_len == 0) { |
| 93 | + CNOT(ctr[0], target[0]); |
| 94 | + CNOT(ctr[1], target[0]); |
| 95 | + CNOT(ctr[1], target[1]); |
| 96 | + } elif (target_len == 2 and ctrl_len == 1) { |
| 97 | + CCNOT(ctrl[0], ctr[0], target[0]); |
| 98 | + CCNOT(ctrl[0], ctr[1], target[0]); |
| 99 | + CCNOT(ctrl[0], ctr[1], target[1]); |
| 100 | + } else { |
| 101 | + Std.Diagnostics.Fact(ctr_len == n, ""); |
| 102 | + Std.Diagnostics.Fact(target_len >= (1 <<< (n - 1)), ""); |
| 103 | + let target_low : Qubit[] = target[0..(1 <<< (n - 1))-1]; |
| 104 | + let target_high : Qubit[] = target[(1 <<< (n - 1))..target_len-1]; |
| 105 | + |
| 106 | + Controlled ApplyToEachCA(ctrl + [ctr[n-1]], (X, target_low)); |
| 107 | + if (Length(target_high) > 0) { |
| 108 | + Controlled FlipFirst(ctrl + [ctr[n-1]], (target_high, ctr[0..n-2])); |
| 109 | + } |
| 110 | + if (Length(target_low) > 1) { |
| 111 | + Controlled X(ctrl, (ctr[n-1])); |
| 112 | + Controlled FlipFirst(ctrl + [ctr[n-1]], (target_low[0..Length(target_low)-2], ctr[0..n-2])); |
| 113 | + Controlled X(ctrl, (ctr[n-1])); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/// Flips target iff x==y. |
| 120 | +operation FlipIfEqual(x : Qubit[], y : BigInt, target : Qubit) : Unit is Ctl + Adj { |
| 121 | + let y_bits = Std.Convert.BigIntAsBoolArray(y, Length(x)); |
| 122 | + within { |
| 123 | + ApplyPauliFromBitString(PauliX, false, y_bits, x); |
| 124 | + } apply { |
| 125 | + Controlled X(x, (target)); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/// Computes x = (x+1)%(2^n), where n=Length(x). |
| 130 | +operation IncrementByFlip(x : Qubit[]) : Unit is Adj { |
| 131 | + body (...) { |
| 132 | + Controlled IncrementByFlip([], (x)); |
| 133 | + } |
| 134 | + controlled (ctrl, ...) { |
| 135 | + use ctr = Qubit[Std.Math.Floor(Log2(Length(x) + 1)) + 1]; |
| 136 | + use carry = Qubit(); |
| 137 | + CountTrailingOnes(x, ctr); |
| 138 | + QuantumArithmetic.ConstAdder.AddConstant(1L, ctr); |
| 139 | + Controlled FlipFirst(ctrl, (x + [carry], ctr)); |
| 140 | + QuantumArithmetic.ConstAdder.AddConstant(-1L, ctr); |
| 141 | + |
| 142 | + // Uncompute carry. |
| 143 | + // We know that carry=1 iff ctr=Length(x). |
| 144 | + let x_len = Std.Convert.IntAsBigInt(Length(x)); |
| 145 | + Controlled FlipIfEqual(ctrl, (ctr, x_len, carry)); |
| 146 | + |
| 147 | + // Uncompute ctr. |
| 148 | + within { |
| 149 | + Controlled ApplyToEachCA(ctrl, (X, x)); |
| 150 | + } apply { |
| 151 | + Adjoint CountTrailingOnes(x, ctr); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments