From 6bffc6cb5f93f8c26fe88df168837415767e64c2 Mon Sep 17 00:00:00 2001 From: cah Date: Tue, 24 Feb 2026 16:49:55 -0700 Subject: [PATCH] feat: add Z=128 support for matrix construction and validation Make validate_matrix() and run_full_pipeline() accept z parameter instead of using hardcoded Z=32. Thread cn_mode/alpha to validation. Add --z/--cn-mode/--alpha CLI options to full pipeline. Co-Authored-By: Claude Opus 4.6 --- model/density_evolution.py | 24 ++++++++++++++---------- model/test_density_evolution.py | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/model/density_evolution.py b/model/density_evolution.py index 1ed7d22..bd7553f 100644 --- a/model/density_evolution.py +++ b/model/density_evolution.py @@ -757,7 +757,8 @@ def verify_matrix(H_base, z=32): # FER Validation # ============================================================================= -def validate_matrix(H_base, lam_s_points, n_frames=200, lam_b=0.1, max_iter=30): +def validate_matrix(H_base, lam_s_points, n_frames=200, lam_b=0.1, max_iter=30, + z=32, cn_mode='offset', alpha=0.75): """ Run full Monte Carlo FER simulation for a base matrix. @@ -765,7 +766,6 @@ def validate_matrix(H_base, lam_s_points, n_frames=200, lam_b=0.1, max_iter=30): """ from ldpc_analysis import generic_decode, peg_encode - z = Z k = z H_full = _build_full_h_from_base(H_base, z) @@ -790,7 +790,8 @@ def validate_matrix(H_base, lam_s_points, n_frames=200, lam_b=0.1, max_iter=30): llr_q = quantize_llr(llr_float) decoded, converged, iters, sw = generic_decode( - llr_q, H_base, z=z, max_iter=max_iter, q_bits=Q_BITS + llr_q, H_base, z=z, max_iter=max_iter, q_bits=Q_BITS, + cn_mode=cn_mode, alpha=alpha ) total_iter += iters @@ -813,7 +814,7 @@ def validate_matrix(H_base, lam_s_points, n_frames=200, lam_b=0.1, max_iter=30): return results -def run_full_pipeline(lam_b=0.1, seed=42): +def run_full_pipeline(lam_b=0.1, seed=42, z=32, cn_mode='offset', alpha=0.75): """ Full pipeline: optimize -> construct -> validate. """ @@ -854,8 +855,8 @@ def run_full_pipeline(lam_b=0.1, seed=42): # Step 3: Construct matrix print("\n--- Step 3: Matrix construction ---") - H_opt, girth = construct_base_matrix(best_degrees, z=32, n_trials=2000) - checks = verify_matrix(H_opt, z=32) + H_opt, girth = construct_base_matrix(best_degrees, z=z, n_trials=2000) + checks = verify_matrix(H_opt, z=z) print(f" Constructed matrix: girth={girth}, rank={checks['actual_rank']}/{checks['expected_rank']}") print(f" Encodable: {checks['encodable']}, Full rank: {checks['full_rank']}") print(f" Base matrix:\n{H_opt}") @@ -867,10 +868,13 @@ def run_full_pipeline(lam_b=0.1, seed=42): print(f"\n{'lam_s':>8s} {'Optimized':>12s} {'Original':>12s} {'PEG ring':>12s}") print("-" * 50) - fer_opt = validate_matrix(H_opt, lam_s_points, n_frames=200, lam_b=lam_b) - fer_orig = validate_matrix(H_BASE, lam_s_points, n_frames=200, lam_b=lam_b) + fer_opt = validate_matrix(H_opt, lam_s_points, n_frames=200, lam_b=lam_b, + z=z, cn_mode=cn_mode, alpha=alpha) + fer_orig = validate_matrix(H_BASE, lam_s_points, n_frames=200, lam_b=lam_b, + z=z, cn_mode=cn_mode, alpha=alpha) fer_peg = validate_matrix(ref_matrices['PEG ring [7,3,3,3,2,2,2,2]'], - lam_s_points, n_frames=200, lam_b=lam_b) + lam_s_points, n_frames=200, lam_b=lam_b, + z=z, cn_mode=cn_mode, alpha=alpha) for lam_s in lam_s_points: f_opt = fer_opt[lam_s]['fer'] @@ -1016,7 +1020,7 @@ def main(): print(f" lam_s={lam_s:.1f}: FER={r['fer']:.3f}, BER={r['ber']:.6f}") elif args.command == 'full': - run_full_pipeline(seed=args.seed) + run_full_pipeline(seed=args.seed, z=args.z, cn_mode=args.cn_mode, alpha=args.alpha) else: parser.print_help() diff --git a/model/test_density_evolution.py b/model/test_density_evolution.py index 4abfd66..29045af 100644 --- a/model/test_density_evolution.py +++ b/model/test_density_evolution.py @@ -283,6 +283,31 @@ class TestNormalizedMinSumDE: ) +class TestZ128Support: + """Tests for Z=128 matrix construction and optimization.""" + + def test_construct_z128_valid(self): + """Construct matrix for [7,4,4,4,4,3,3,3] with z=128. Verify full rank, girth >= 6, encodable.""" + from density_evolution import construct_base_matrix, verify_matrix + np.random.seed(42) + target = [7, 4, 4, 4, 4, 3, 3, 3] + H_base, girth = construct_base_matrix(target, z=128, n_trials=500) + checks = verify_matrix(H_base, z=128) + assert checks['full_rank'], f"Full rank failed: {checks['actual_rank']}/{checks['expected_rank']}" + assert girth >= 6, f"Girth {girth} should be >= 6" + assert checks['encodable'], "Should be encodable" + + def test_validate_z128_runs(self): + """Run validate_matrix with z=128, n_frames=5 at lam_s=5. Verify returns dict with FER.""" + from density_evolution import construct_base_matrix, validate_matrix + np.random.seed(42) + target = [7, 4, 4, 4, 4, 3, 3, 3] + H_base, girth = construct_base_matrix(target, z=128, n_trials=200) + results = validate_matrix(H_base, lam_s_points=[5.0], n_frames=5, lam_b=0.1, z=128) + assert 5.0 in results, f"Expected key 5.0, got {list(results.keys())}" + assert 'fer' in results[5.0] + + class TestAlphaOptimization: """Tests for alpha sweep optimization."""