fn bitparity_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn bitparity_counts(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= 0.0 && value <= 9007199254740991.0 && value % 1.0 == 0.0) { return false; } i += 1; } true } fn bitparity_compute(counts: vector) -> vector { if len(counts) != 4 { return []; } let total = sum(counts); if total <= 0.0 || total > 9007199254740991.0 { return []; } let even = (at(counts, 0) + at(counts, 3)) / total; let odd = (at(counts, 1) + at(counts, 2)) / total; [even, odd, even - odd] } // Measure even/odd parity from a two-bit measurement histogram. // Bin order is 00,01,10,11. Returns [even frequency, odd frequency, parity expectation]. At least one shot is required. pub fn bitparity_measure(counts: vector) -> vector { if !bitparity_counts(counts) { return []; } let result = bitparity_compute(counts); if !bitparity_finite(result) { return []; } result }