feat: add PEG base matrix constructor with shift optimization
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -131,3 +131,47 @@ class TestDegreeDistributionOptimizer:
|
||||
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"
|
||||
|
||||
|
||||
class TestPEGBaseMatrixConstructor:
|
||||
"""Tests for the PEG base matrix constructor."""
|
||||
|
||||
def test_construct_matches_target_degrees(self):
|
||||
"""Constructed matrix should have the target column degrees."""
|
||||
from density_evolution import construct_base_matrix
|
||||
np.random.seed(42)
|
||||
target = [7, 3, 3, 3, 2, 2, 2, 2]
|
||||
H_base, girth = construct_base_matrix(target, z=32, n_trials=500)
|
||||
# Check column degrees
|
||||
for c in range(H_base.shape[1]):
|
||||
actual_deg = np.sum(H_base[:, c] >= 0)
|
||||
assert actual_deg == target[c], (
|
||||
f"Col {c}: expected degree {target[c]}, got {actual_deg}"
|
||||
)
|
||||
|
||||
def test_construct_has_valid_rank(self):
|
||||
"""Full H matrix should have full rank, parity submatrix should too."""
|
||||
from density_evolution import construct_base_matrix, verify_matrix
|
||||
np.random.seed(42)
|
||||
target = [7, 3, 3, 3, 2, 2, 2, 2]
|
||||
H_base, girth = construct_base_matrix(target, z=32, n_trials=500)
|
||||
checks = verify_matrix(H_base, z=32)
|
||||
assert checks['full_rank'], f"Full matrix rank {checks['actual_rank']} < expected {checks['expected_rank']}"
|
||||
assert checks['parity_rank'], f"Parity submatrix not full rank"
|
||||
|
||||
def test_construct_encodable(self):
|
||||
"""Encoding a random info word should produce zero syndrome."""
|
||||
from density_evolution import construct_base_matrix, verify_matrix
|
||||
np.random.seed(42)
|
||||
target = [7, 3, 3, 3, 2, 2, 2, 2]
|
||||
H_base, girth = construct_base_matrix(target, z=32, n_trials=500)
|
||||
checks = verify_matrix(H_base, z=32)
|
||||
assert checks['encodable'], "Should be able to encode and verify syndrome=0"
|
||||
|
||||
def test_construct_girth_at_least_4(self):
|
||||
"""Constructed matrix should have girth >= 4."""
|
||||
from density_evolution import construct_base_matrix
|
||||
np.random.seed(42)
|
||||
target = [7, 3, 3, 3, 2, 2, 2, 2]
|
||||
H_base, girth = construct_base_matrix(target, z=32, n_trials=500)
|
||||
assert girth >= 4, f"Girth {girth} should be >= 4"
|
||||
|
||||
Reference in New Issue
Block a user