feat: add degree distribution optimizer with exhaustive search

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
cah
2026-02-24 05:53:23 -07:00
parent f5b3e318c4
commit a09c5f20e1
2 changed files with 182 additions and 0 deletions

View File

@@ -92,3 +92,42 @@ class TestThresholdComputation:
# Every row should have at least 2 connections
for r, conns in enumerate(profile['connections']):
assert len(conns) >= 2, f"Row {r} has only {len(conns)} connections"
class TestDegreeDistributionOptimizer:
"""Tests for the exhaustive search optimizer."""
def test_enumerate_candidates(self):
"""Enumeration should produce 3^7 = 2187 candidates."""
from density_evolution import enumerate_vn_candidates
candidates = enumerate_vn_candidates(m_base=7)
assert len(candidates) == 3**7, f"Expected 2187, got {len(candidates)}"
# Each candidate should have 8 elements (info col + 7 parity)
for c in candidates:
assert len(c) == 8
assert c[0] == 7 # info column always degree 7
def test_filter_removes_invalid(self):
"""Filter should keep valid distributions and remove truly invalid ones."""
from density_evolution import filter_by_row_degree
# All-dv=2 parity: parity_edges=14, dc_avg=3 -> valid for [3,6]
all_2 = [7, 2, 2, 2, 2, 2, 2, 2]
assert filter_by_row_degree([all_2], m_base=7, dc_min=3, dc_max=6) == [all_2]
# All-dv=4 parity: parity_edges=28, dc_avg=5 -> valid for [3,6]
all_4 = [7, 4, 4, 4, 4, 4, 4, 4]
assert filter_by_row_degree([all_4], m_base=7, dc_min=3, dc_max=6) == [all_4]
# A hypothetical all-dv=1 parity: parity_edges=7, total=14, avg dc=2 < 3 -> invalid
all_1 = [7, 1, 1, 1, 1, 1, 1, 1]
assert filter_by_row_degree([all_1], m_base=7, dc_min=3, dc_max=6) == []
# With tighter constraints (dc_min=4), all-dv=2 should be removed
assert filter_by_row_degree([all_2], m_base=7, dc_min=4, dc_max=6) == []
def test_optimizer_finds_better_than_original(self):
"""Optimizer should find a distribution with threshold <= original staircase."""
from density_evolution import optimize_degree_distribution, compute_threshold_for_profile
np.random.seed(42)
results = optimize_degree_distribution(m_base=7, lam_b=0.1, top_k=5, z_pop_coarse=5000, z_pop_fine=10000, tol=0.5)
assert len(results) > 0, "Optimizer should return at least one result"
best_degrees, best_threshold = results[0]
# Original staircase threshold is ~3-5 photons
assert best_threshold < 6.0, f"Best threshold {best_threshold} should be < 6.0"