diff --git a/python_test/test_simplex_code.py b/python_test/test_simplex_code.py new file mode 100644 index 0000000..dbdbf5f --- /dev/null +++ b/python_test/test_simplex_code.py @@ -0,0 +1,44 @@ +import pytest +import numpy as np +from ldpc.codes.hamming_code import hamming_code +from ldpc.codes.simplex_code import simplex_code + +def test_simplex_code_m3(): + m = 3 + H_simplex_actual = simplex_code(m) + expected_H_simplex_m3 = np.array([ + [1, 1, 1, 0, 0, 0, 0], + [0, 1, 1, 1, 1, 0, 0], + [0, 1, 0, 1, 0, 1, 0], + [0, 0, 1, 1, 0, 0, 1] + ], dtype=np.uint8) # Use uint8 for binary matrices + + # Check if matrices are equal + assert np.array_equal(H_simplex_actual.toarray(), expected_H_simplex_m3) + # Check dimensions + assert H_simplex_actual.shape == (4, 7) # Based on the example given + +def test_simplex_code_duality(): + """Tests the duality property: H_hamming @ H_simplex.T = 0""" + for m_val in [3, 4, 5]: # Test for a few small values of m + H_hamming = hamming_code(m_val) + H_simplex = simplex_code(m_val) + + # Perform matrix multiplication over GF(2) + # scipy.sparse matrices support matrix multiplication + result = (H_hamming @ H_simplex.transpose()).toarray() % 2 + + # All elements should be zero + assert np.all(result == 0), f"Duality check failed for m={m_val}" + +def test_simplex_code_dimensions(): + """Tests if the output matrix has the correct dimensions.""" + for m_val in [2, 3, 4, 5]: + H_simplex = simplex_code(m_val) + n = 2**m_val - 1 # Length of the code + #The dimension of the parity check matrix of the simplex code + # should be ( (2^m - 1) - m ) x (2^m - 1). + expected_rows = (2**m_val - 1) - m_val + expected_cols = 2**m_val - 1 + assert H_simplex.shape == (expected_rows, expected_cols), \ + f"Dimension mismatch for m={m_val}: Expected ({expected_rows}, {expected_cols}), got {H_simplex.shape}" diff --git a/src_python/ldpc/codes/__init__.py b/src_python/ldpc/codes/__init__.py index 7bba6a8..bb87ee3 100644 --- a/src_python/ldpc/codes/__init__.py +++ b/src_python/ldpc/codes/__init__.py @@ -1,3 +1,4 @@ from ldpc.codes.rep_code import rep_code, ring_code from ldpc.codes.hamming_code import hamming_code from ldpc.codes.random_binary_code import random_binary_code +from ldpc.codes.simplex_code import simplex_code diff --git a/src_python/ldpc/codes/simplex_code.py b/src_python/ldpc/codes/simplex_code.py new file mode 100644 index 0000000..003591c --- /dev/null +++ b/src_python/ldpc/codes/simplex_code.py @@ -0,0 +1,36 @@ +import numpy as np +from ldpc.codes import hamming_code +from ldpc.mod2 import kernel +import scipy.sparse + +def simplex_code(m: int) -> scipy.sparse.csr_matrix: + """ + Outputs the parity check matrix of a binary simplex code. + + Simplex codes are a family of binary linear codes that are duals of Hamming codes. + For a given integer m , the binary simplex code has parameters [ 2^m - 1 , m , 2^m - 1 ] . + These codes are notable for their large minimum distance and large autormorphism groups that are useful for compiling logic in quantum stabiliser codes. + + Parameters + ---------- + m (int): Dimension of the simplex code. + + Returns + ---------- + H_simplex (np.ndarray): Parity check matrix of shape (2^m - 1 - m, 2^m - 1). + + Raises + ------ + TypeError + If the input variable 'dimension' is not of type 'int'. + """ + + # Implement function here + if not isinstance(m, int): + raise TypeError("The input variable 'rank' must be of type 'int'.") + H_hamming = hamming_code(m) + + H_simplex = kernel(H_hamming) + + + return H_simplex \ No newline at end of file