#!/usr/bin/env python3 """Check free-walk conservation exactly and differentiate two uniform shear gates.""" from cmath import exp from fractions import Fraction from itertools import permutations, product from math import acos, cos, isclose, pi, prod, sin, sqrt class Cyclotomic32: """Rational polynomials in z = exp(2 pi i / 32), reduced by z**16 = -1.""" def __init__(self, coefficients): self.coefficients = tuple(coefficients) def __add__(self, other): return Cyclotomic32(a + b for a, b in zip(self.coefficients, other.coefficients)) def __neg__(self): return Cyclotomic32(-a for a in self.coefficients) def __sub__(self, other): return self + (-other) def __mul__(self, other): if not isinstance(other, Cyclotomic32): return Cyclotomic32(a * other for a in self.coefficients) out = [0] * 16 for i, a in enumerate(self.coefficients): if a: for j, b in enumerate(other.coefficients): if b: k = i + j out[k % 16] += (1 if k < 16 else -1) * a * b return Cyclotomic32(out) __rmul__ = __mul__ def conjugate(self): c = self.coefficients return Cyclotomic32([c[0], *(-x for x in reversed(c[1:]))]) def is_zero(self): return not any(self.coefficients) def phase(step): """Integer angle in units of 2 pi / 32.""" n = step % 32 coefficients = [0] * 16 coefficients[n % 16] = 1 if n < 16 else -1 return Cyclotomic32(coefficients) ONE = phase(0) ZERO = ONE - ONE I = phase(8) HALF = Fraction(1, 2) def cosine(n): return (phase(n) + phase(-n)) * HALF def sine(n): return (phase(n) - phase(-n)) * (-I) * HALF def half_angle(n): assert n % 2 == 0, "angle is outside this exact 32-phase test" return n // 2 def contraction(values, weights): return sum((x * w for x, w in zip(values, weights)), ZERO) def inner(bra, ket): return contraction([x.conjugate() for x in bra], ket) def pauli(axis, spinor): a, b = spinor return ((b, a), (-I * b, I * a), (a, -b))[axis] def rotate(axis, angle, spinor): c, s = cosine(angle), -I * sine(angle) return tuple(c * a + s * b for a, b in zip(spinor, pauli(axis, spinor))) def eigenstate(p, omega): c = [cosine(2 * x) for x in p] s = [sine(2 * x) for x in p] assert (prod(c, start=ONE) - cosine(omega)).is_zero() bx, by, bz = s[0] * c[1] * c[2], s[1] * c[2], s[2] spinor = (sine(omega) + bz, bx + I * by) if all(x.is_zero() for x in spinor): spinor = (bx - I * by, sine(omega) - bz) return spinor def probability_current(p, q, omega, omega_q): """Integrated directed link flux of all six conditional shifts.""" ket, bra = eigenstate(p, omega), eigenstate(q, omega_q) transfer = [a - b for a, b in zip(p, q)] time_phase = phase(half_angle(omega - omega_q)) current = [inner(bra, ket), ZERO, ZERO, ZERO] for axis in (0, 1, 2, 2, 1, 0): norm = inner(bra, ket) spin = inner(bra, pauli(axis, ket)) right, left = (norm + spin) * HALF, (norm - spin) * HALF h = half_angle(transfer[axis]) flux = phase(-h) * right - phase(h) * left current[axis + 1] = current[axis + 1] + time_phase * flux ket = rotate(axis, p[axis], ket) bra = rotate(axis, q[axis], bra) return current def secant_vector(p, q, omega, omega_q): """Symmetric telescoping of the difference of the two mass shells.""" c = [cosine(2 * x) for x in p] d = [cosine(2 * x) for x in q] result = [sine(half_angle(omega + omega_q))] for i in range(3): forward = prod(c[:i] + d[i + 1:], start=ONE) reverse = prod(d[:i] + c[i + 1:], start=ONE) result.append(cosine(half_angle(p[i] - q[i])) * sine(p[i] + q[i]) * (forward + reverse)) return result def check_pair(p, q, omega, omega_q): j = probability_current(p, q, omega, omega_q) v = secant_vector(p, q, omega, omega_q) spatial = [a - b for a, b in zip(p, q)] temporal = -2 * sine(half_angle(omega - omega_q)) incidence = [temporal, *(2 * sine(half_angle(t)) for t in spatial)] filters = [] gravity = [temporal] for i, t in enumerate(spatial): later = prod((cosine(2 * u) for u in spatial[i + 1:]), start=ONE) filters.append(cosine(half_angle(t)) * cosine(t) * later) gravity.append(sine(2 * t) * later * HALF) weights = [prod(filters, start=ONE)] weights.extend(prod(filters[:i] + filters[i + 1:], start=ONE) for i in range(3)) tensor = [[(j[a] * v[b] + j[b] * v[a]) * HALF for b in range(4)] for a in range(4)] filtered = [[tensor[a][b] * weights[a] * weights[b] for b in range(4)] for a in range(4)] residuals = [contraction(j, incidence), contraction(v, incidence)] residuals.extend(contraction(row, incidence) for row in tensor) residuals.extend(contraction(row, gravity) for row in filtered) assert all(r.is_zero() for r in residuals) # A wrong derivative on the original current is a nontrivial control. assert not contraction(j, gravity).is_zero() assert sum(not t.is_zero() for row in filtered for t in row) >= 12 return len(residuals) def matrix_product(a, b): return tuple(tuple(sum(a[i][k] * b[k][j] for k in range(2)) for j in range(2)) for i in range(2)) def spin_rotation(axis, theta): x, y, z = axis c, s = cos(theta), -1j * sin(theta) return ((c + s * z, s * (x - 1j * y)), (s * (x + 1j * y), c - s * z)) def walk(k, tilt): x, y, z = k rx = spin_rotation((cos(tilt), sin(tilt), 0), x / 2) ry = spin_rotation((0, 1, 0), y / 2) rz = spin_rotation((0, 0, 1), z) w = rx for gate in (ry, rz, ry, rx): w = matrix_product(w, gate) return w def shear_walk(k, angle): """Append the four link-rotation pairs to the untilted walk.""" w = walk(k, 0) for dx, dz in product((-1, 1), repeat=2): theta = dx * k[0] + k[1] + dz * k[2] s = ((exp(-0.5j * theta), 0), (0, exp(0.5j * theta))) adjoint = ((s[1][1], 0), (0, s[0][0])) for g in (adjoint, spin_rotation((0, 1, 0), angle / 8), s, s, spin_rotation((0, 1, 0), -angle / 8), adjoint): w = matrix_product(w, g) return w def phase_slope(builder, k, step=1e-5): def omega(angle): w = builder(k, angle) return acos(max(-1, min(1, (w[0][0] + w[1][1]).real / 2))) return (omega(step) - omega(-step)) / (2 * step) def main(): checks = 0 for perm in permutations(range(3)): p = tuple((4, 2, -2)[i] for i in perm) q = tuple((2, 0, 0)[i] for i in perm) for a, b in product((-1, 1), repeat=2): checks += check_pair(p, q, a * 8, b * 4) print(f"{checks} exact Ward checks passed across 24 band-pair cases") for x, native, target in [(pi / 2, 1.0, 0.0), (pi / 4, 1 / sqrt(3), 1 / (2 * sqrt(3)))]: k = (x, x, 0.0) observed = phase_slope(walk, k), phase_slope(shear_walk, k) assert all(isclose(a, b, abs_tol=1e-8) for a, b in zip(observed, (native, target))) print(f"x=y={x:.8f} native slope={observed[0]:.10f} " f"constructed slope={observed[1]:.10f}") if __name__ == "__main__": main()