#!/usr/bin/env python3 """Verify the finite comparisons and tail constants for the phase argument. Run python3 verify_phase.py. All comparisons use rational endpoints. The D-products used in kappa are always evaluated at 2, including at q=3. """ from dataclasses import dataclass from fractions import Fraction as F from functools import cache from math import isqrt, prod def require(condition, message): if not condition: raise ArithmeticError(message) @dataclass(frozen=True) class Interval: lo: F hi: F @staticmethod def enclose(value): return value if isinstance(value, Interval) else Interval(F(value), F(value)) def __add__(self, other): other = self.enclose(other) return Interval(self.lo + other.lo, self.hi + other.hi) __radd__ = __add__ def __neg__(self): return Interval(-self.hi, -self.lo) def __sub__(self, other): return self + (-self.enclose(other)) def __rsub__(self, other): return self.enclose(other) + (-self) def __mul__(self, other): other = self.enclose(other) endpoints = [x * y for x in (self.lo, self.hi) for y in (other.lo, other.hi)] return Interval(min(endpoints), max(endpoints)) __rmul__ = __mul__ def __truediv__(self, other): other = self.enclose(other) require(other.lo > 0 or other.hi < 0, 'division across zero') return self * Interval(1 / other.hi, 1 / other.lo) def __rtruediv__(self, other): return self.enclose(other) / self def __abs__(self): require(self.lo > 0 or self.hi < 0, 'denominator has unknown sign') return self if self.lo > 0 else -self def positive_power(self, exponent): require(self.lo >= 0 and exponent >= 0, 'nonpositive base or negative power') return Interval(self.lo ** exponent, self.hi ** exponent) @cache def d_product(j): def h(k): return prod(1 - F(1, 2 ** a) for a in range(1, k + 1)) return h(j) * h(j - 1) @cache def kappa(i, j): i, j = sorted((i, j)) if i == j == 1: return F(1) gaussian = d_product(i + j) / (d_product(i) * d_product(j)) return min(1 / d_product(i), gaussian / (1 - F(1, 2 ** (i + j - 1)))) LAMBDA = F(5, 7) def composition_rows(g, field_size, maximum): q = F(field_size) @cache def mass(i, j): i, j = sorted((i, j)) ratio = prod((1 + LAMBDA * q ** (1 - s)) / (1 + LAMBDA * q ** (1 - j - s)) for s in range(1, i + 1)) return kappa(i, j) * q ** (-(g - 1) * i * j) * ratio ** (2 * g) rows = [{}] for size in range(1, maximum + 1): row = {size: F(1)} for last in range(1, size): prefix = size - last row[last] = mass(last, prefix) * sum( weight / (q ** (previous + last) - 1) for previous, weight in rows[prefix].items()) rows.append(row) return rows, mass def phase_error(g, field_size, n, rows, mass): q = F(field_size) scale = 10 ** 18 floor = isqrt(field_size * scale ** 2) root = Interval(F(floor, scale), F(floor + 1, scale)) require(root.lo ** 2 < q < root.hi ** 2, 'invalid square root enclosure') def power(twice_exponent): integer, odd = divmod(twice_exponent, 2) return q ** integer * (root if odd else Interval.enclose(1)) m = n - 1 r, a, A = power(-n), power(1 - n), power(n) b = q * r M = g * (1 + a) / (1 - a) + b / (1 + b) def delta(twice_exponent): return abs(1 - power(twice_exponent)) if twice_exponent else 1 / M normalization = 2 - sum(rows[m].values()) # 1 - E_m require(normalization > 0, f'normalization n={n}') interior = Interval.enclose(0) for cut in range(1, m): k = min(cut, m - cut) ell = m - k left = sum(weight * (1 - r) / (power(2 * j - 2 * k + n) - 1) for j, weight in rows[k].items()) right = Interval.enclose(0) for p, weight in rows[ell].items(): exponent = 2 * (p + k + 1) - n numerator = A + 1 if exponent == 0 else A - 1 right += weight * numerator / delta(exponent) radial = ((1 + q ** k * a) / (1 + a)).positive_power(2 * g) interior += (q ** (-(g - 1) * k) * mass(k, ell) * left * right * (1 - b) / (1 - q ** k * r) / delta(2 * (k + 1) - n) * radial) endpoint = 2 * sum(weight * r * (q ** n - q ** (j + 1)) / delta(2 * (j + 1) - n) for j, weight in rows[m].items() if j < m) return (interior + endpoint) / normalization def verify_tail(): q = F(2) numerator_product = prod(1 + LAMBDA * q ** (1 - s) for s in range(1, 5)) require(numerator_product / (1 - F(5, 56)) < F(10, 3), 'product bound') C = 13 * F(10, 3) ** 6 require(C / 3 * 9 * q ** -18 < F(1, 2), 'row induction at j=10') n = 20 e = 2 * C / 3 * (n - 2) * q ** (-2 * (n - 2)) require(e < F(1, 2), 'normalization in the tail') require((1 + F(1, 1024)) ** 2 * (1 + F(1, 512)) / (1 - e) < 2, 'prefactor') require(3 * (1 + F(1, 512)) / (1 - F(1, 512)) + F(1, 512) < 5, 'phase speed') interior = 625 * 4 * C * 64 * (n - 2) * q ** (3 - 2 * n) endpoint = F(40, 3) * C * (n - 2) * q ** (4 - 3 * n // 2) require(interior + endpoint < F(1, 2), 'tail bound') print(f'At rank 20 the sum of the tail bounds is {interior + endpoint} < 1/2.') def main(): groups = ((3, 2, [(n, F(9, 100)) for n in range(7, 20)]), (3, 3, [(5, F(91, 1000)), (6, F(35, 10000))]), (4, 2, [(5, F(1238, 1000)), (6, F(79, 1000))])) for g, q, comparisons in groups: rows, mass = composition_rows(g, q, max(n for n, _ in comparisons) - 1) if g == 3 and q == 2: for j in range(1, 10): require(sum(rows[j].values()) < F(3, 2), f'initial row {j}') for n, upper in comparisons: error = phase_error(g, q, n, rows, mass) require(error.hi < upper < (2 if n % 2 else 1), f'phase bound {(g,q,n)}') print(f'g={g}, q={q}, n={n}: phase error < {upper}.') verify_tail() if __name__ == '__main__': main()