Datasets:
Formats:
csv
Languages:
English
Size:
10K - 100K
Tags:
recursive-self-improvement
symmetry-reduction
finite-groups
exact-compilation
dynamic-programming
reproducible-research
Download reference_core.py from PureOne/Neyaru-Reciprocal-Stabilizer-Compilation: direct link, hf CLI and curl.
- Browser
- Download file 6.02 kB
-
https://huggingface.co/datasets/PureOne/Neyaru-Reciprocal-Stabilizer-Compilation/resolve/main/reference_core.py
- Command line
-
hf download hf://datasets/PureOne/Neyaru-Reciprocal-Stabilizer-Compilation/reference_core.py
-
curl -L -o reference_core.py https://huggingface.co/datasets/PureOne/Neyaru-Reciprocal-Stabilizer-Compilation/resolve/main/reference_core.py
6.02 kB
| """Neyaru minimal exact core. Python 3.10+, standard library only. | |
| Run: python3 reference_core.py | |
| The benchmark implementation is separate and reports full overhead. | |
| """ | |
| from dataclasses import dataclass | |
| from math import inf | |
| class Dihedral: | |
| """Elements r**a s**b encoded a + m*b; group order is 2*m.""" | |
| def __init__(self, m): | |
| self.m, self.n = m, 2*m | |
| self.mul = [[((a % m) + (1 if a < m else -1)*(b % m)) % m | |
| + m*((a // m) ^ (b // m)) | |
| for b in range(2*m)] for a in range(2*m)] | |
| self.inv = [next(b for b in range(self.n) | |
| if self.mul[a][b] == 0) for a in range(self.n)] | |
| def closure(self, generators): | |
| found, queue = {0}, [0] | |
| for a in queue: | |
| for b in generators: | |
| c = self.mul[a][b] | |
| if c not in found: | |
| found.add(c) | |
| queue.append(c) | |
| return found | |
| def stabilizer(G, values, left): | |
| """Rare-value filter, exact tests, and certified subgroup closure.""" | |
| buckets = {} | |
| for x, v in enumerate(values): | |
| buckets.setdefault(v, []).append(x) | |
| if len(buckets) == 1: | |
| return frozenset(range(G.n)) | |
| bucket = min(buckets.values(), key=len) | |
| anchor = bucket[0] | |
| candidates = [(G.mul[y][G.inv[anchor]] if left else | |
| G.mul[G.inv[anchor]][y]) for y in bucket] | |
| known, generators = {0}, [] | |
| for a in sorted(candidates): | |
| if a in known: | |
| continue | |
| if all(values[G.mul[a][x] if left else G.mul[x][a]] == values[x] | |
| for x in range(G.n)): | |
| generators.append(a) | |
| known = G.closure(generators) | |
| return frozenset(known) | |
| class Kernel: | |
| values: tuple | |
| L: frozenset | |
| R: frozenset | |
| leaf: str = "" | |
| children: object = None | |
| choices: object = None | |
| def witness(self, boundary): | |
| if self.values[boundary] == inf: | |
| raise ValueError("infeasible boundary") | |
| if self.children is None: | |
| return [(self.leaf, boundary, self.values[boundary])] | |
| a, b = self.choices[boundary] | |
| u, v = self.children | |
| return u.witness(a) + v.witness(b) | |
| def leaf(G, name, values): | |
| values = tuple(values) | |
| return Kernel(values, stabilizer(G, values, True), | |
| stabilizer(G, values, False), leaf=name) | |
| def compose(G, f, g, quotient=True, learn=True): | |
| """All role occurrences remain in the witness DAG. | |
| pairs counts only objective candidates, not total runtime. | |
| Complete symmetry and orbit costs are charged in the benchmark. | |
| """ | |
| if quotient: | |
| H = f.R & g.L | |
| inner, covered = [], set() | |
| for a in range(G.n): | |
| if a not in covered: | |
| inner.append(a) | |
| covered.update(G.mul[a][h] for h in H) | |
| orbits, covered = [], set() | |
| for z in range(G.n): | |
| if z in covered: | |
| continue | |
| orbit = {} | |
| for l in sorted(f.L): | |
| for r in sorted(g.R): | |
| x = G.mul[G.mul[l][z]][r] | |
| orbit.setdefault(x, (l, r)) | |
| covered.update(orbit) | |
| orbits.append((z, orbit)) | |
| else: | |
| inner = list(range(G.n)) | |
| orbits = [(z, {z: (0, 0)}) for z in range(G.n)] | |
| values, choices, pairs = [inf]*G.n, [None]*G.n, 0 | |
| for z, orbit in orbits: | |
| best, arg = inf, None | |
| for a in inner: | |
| b = G.mul[G.inv[a]][z] | |
| score = f.values[a] + g.values[b] | |
| pairs += 1 | |
| if score < best: | |
| best, arg = score, (a, b) | |
| for x, (l, r) in orbit.items(): | |
| values[x] = best | |
| if arg is not None: | |
| a, b = arg | |
| # Retrieve child witnesses at NEW boundaries. Never edit | |
| # a primitive choice merely because aggregate costs agree. | |
| choices[x] = (G.mul[l][a], G.mul[b][r]) | |
| values = tuple(values) | |
| L = stabilizer(G, values, True) if learn else f.L | |
| R = stabilizer(G, values, False) if learn else g.R | |
| out = Kernel(values, L, R, children=(f, g), choices=choices) | |
| for x, pair in enumerate(choices): | |
| if pair is not None: | |
| a, b = pair | |
| assert G.mul[a][b] == x | |
| assert f.values[a] + g.values[b] == values[x] | |
| return out, pairs | |
| def verify_witness(G, kernel, boundary): | |
| # Enumerated positions distinguish repeated occurrences of a leaf type. | |
| product, cost = 0, 0 | |
| for position, (leaf_type, action, action_cost) in enumerate( | |
| kernel.witness(boundary)): | |
| assert position >= 0 and leaf_type | |
| product = G.mul[product][action] | |
| cost += action_cost | |
| assert product == boundary and cost == kernel.values[boundary] | |
| def demo(): | |
| G = Dihedral(16) | |
| c, M = 3, 19 | |
| def seed(j): | |
| step, size = G.m // (2**j), 2**j | |
| values = [M]*G.n | |
| for a in range(size//2 + 1): | |
| values[(a*step) % G.m] = 0 | |
| values[G.m] = c | |
| return leaf(G, "f" + str(j), values) | |
| h, _ = compose(G, seed(1), seed(1)) | |
| print("level |L| |R| quotient_pairs dense_pairs") | |
| for j in range(2, 5): | |
| f = seed(j) | |
| nxt, reduced_pairs = compose(G, h, f) | |
| dense, dense_pairs = compose(G, h, f, quotient=False, learn=False) | |
| assert nxt.values == dense.values and nxt.values != h.values | |
| expected_K = {a*(G.m//(2**j)) for a in range(2**j)} | |
| assert set(nxt.L) == expected_K == set(nxt.R) | |
| for boundary in range(G.n): | |
| verify_witness(G, nxt, boundary) | |
| print(j, len(nxt.L), len(nxt.R), reduced_pairs, dense_pairs) | |
| # Newly learned metadata changes the NEXT composition procedure. | |
| h = nxt | |
| assert h.values == tuple([0]*G.m + [c]*G.m) | |
| print("Exact table equality, new-kernel growth, and witnesses passed.") | |
| print("Pair counts exclude discovery/orbit overhead; see benchmark.") | |
| if __name__ == "__main__": | |
| demo() | |