48 points · by ingve · 3y ago · 20 comments
One that I recently did was for iterating over possible errors of weight w in a quantum stabilizer code (for [1]). In a classical code this would just be the problem in the blog post, since there's only one type of error (bit flips). In a quantum code you have bit flips (X) but also phase flips (Z) and combination-bit-and-phase-flips (Y) all having weight 1. So the problem becomes: given ints (x, z) find the next (x2, z2) such that popcnt(x2 | z2) == popcnt(x | z).
The solution I came up with, which is likely very suboptimal, was to iterate over single-integer solutions for a given weight. Then, for each single-integer solution m, iterate over the pairs x,z where x|z == m. Here's python pseudocode for the second part:
def pair_sat_increment(x: int, z: int, m: int) -> Tuple[int, int]:
"""Returns the next (x, z) such that x | z == m."""
inc = x & z
up = ~inc
inc |= ~m
inc += 1
inc &= m
up &= inc
z &= inc | ~x
z ^= x & up
x ^= up
return x, z
[1]: https://github.com/quantumlib/Stim/issues/397 for(u64 x = -m & m; ;x = (x - m) & m) {
const u64 r = x ^ m;
for(u64 t = -x & x; ; t = (t - x) & x) {
const u64 z = r | t;
// Use (x,z)
if(t == 0) break;
}
if(x==0) break;
}That's pretty cool. How do you ask an SAT solver for "the nearest integer with the same population count", and how do you describe the available bitwise operations to it?
As for the actual boolean formula, I believe that he used his own creation, Haroldbot [1] which can evaluate bitwise expressions and enumerate all cases where the expression evaluates to true. Since simple formulas for previous and next integer with the same popcount, it should be doable. I've even constructed a concrete formula with the suggested formula:
let n = (let t = x + (x & -x) in let u = x & ~t in t ^ ((u >>s tzcnt(u)) >>s 1)) in
let p = (let v = x - (~x & (x + 1)) in let w = ~x & v in v ^ ((w >>s tzcnt(w)) >>s 1)) in
let c = (let y = -x & (x + 1) in x ^ y ^ (y >> 1)) in
(n == -1) || (p == 0) || (
(p < x) && (popcnt(p) == popcnt(x)) &&
(x < n) && (popcnt(x) == popcnt(n)) &&
((x - p > n - x) && (c == n)) || ((x - p <= n - x) && (c == p)))
)
...where `n` is for the next such integer (or all 1s), `p` is for the previous such integer (or all 0s), `c` is for the closest such integer, and assuming that both `p` and `n` exists, `c` should be the closest of either `p` or `n`. This expression can be mechanically translated to a single boolean formula, which Haroldbot internally does for complicated formulas [2].[2] It apparently uses binary decision diagrams for simpler cases: http://haroldbot.nl/how.html
I actually found the author's complementing trick for getting the previous integer more interesting (because more applications):
>However since we already have a version that walks to the next we can simply apply a linear transform to map/unmap the input/result. The ones complement (~x) reverses the notion of unsigned integer ordering so the follow function walks to the previous
Maybe I should suggest avoiding the same fate for Rust's unchecked_shl and unchecked_shr. (These aren't stabilized yet unlike the safe options and the rotates)
It's never too late to replace Undefined Behaviour with Unspecified Behaviour.
Why is it too late? Changing it in a later version of the standard wouldn't be a further constraint on any written code; any code which is currently correct will remain correct, and some code which could have done literally anything will now only be able to have one of a few possible behaviours.