-
Notifications
You must be signed in to change notification settings - Fork 24
Implement balanced product codes #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryan-kersten
wants to merge
27
commits into
qLDPCOrg:main
Choose a base branch
from
ryan-kersten:balenced-product
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
7c676e8
Find permutations of matrix
ryan-kersten 87b6c40
Add tests for permutation finding
ryan-kersten 4a5ae49
Add dim option to Groupmember.to_matrix
ryan-kersten a6edd42
Core functionality of balanced product code
ryan-kersten 6c47e0e
Tests for balanced product code
ryan-kersten 756d992
Add balanced product code to init
ryan-kersten b830766
Fix mocking in gap test
ryan-kersten a95a95d
Fix coverage
ryan-kersten ee3bc47
Fixed formatting+mocking
ryan-kersten fff6fc3
Fix coverage
ryan-kersten 51e39c0
Split up finding perms and balanced code
ryan-kersten 04d8f9c
Moved balance code tests around
ryan-kersten 744eb74
Alternative balanced code creation
ryan-kersten 709c2dd
Add balanced code tests
ryan-kersten d6c95da
Add distance balancing
ryan-kersten 5ba3f44
Distance balancing tests
ryan-kersten 69c3af3
Formatting fixes + docs
ryan-kersten 4885a77
Coverage fix
ryan-kersten 9c3fc5d
Fix coverage
ryan-kersten 36b6ed9
Fix tests
ryan-kersten 43d1716
add more doc for permutation fnx
ryan-kersten df50480
Rename to BPCode
ryan-kersten bcdf09d
Rename binaryMatrix
ryan-kersten 8e60117
Fix doc strings
ryan-kersten ece3773
Restore original to_matrix
ryan-kersten bca4dfa
Parsing GAP generator out fnx
ryan-kersten 7d528ef
Basic Color codes
ryan-kersten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import scipy | ||
| import sympy | ||
|
|
||
| import qldpc.external.groups | ||
| from qldpc import abstract | ||
| from qldpc.abstract import DEFAULT_FIELD_ORDER | ||
| from qldpc.math import first_nonzero_cols | ||
|
|
@@ -1564,6 +1565,119 @@ def __init__( | |
| CSSCode.__init__(self, matrix_x, matrix_z, field) | ||
|
|
||
|
|
||
| class BPCode(CSSCode): | ||
| """Code created from the product of classical codes. Similar to hypergraph codes. | ||
|
|
||
| Binary matrix (M) must have 2 permutations, r and c. Where r*M = M*C^T and r^l = I and c^l for some l. | ||
|
|
||
| qldpc.external.groups.get_balanced_permutations_of_matrix can be used to find these permutations. | ||
|
|
||
| I.e. | ||
| matrix = np.array( | ||
| [ | ||
| [1, 1, 0, 0, 0, 0], | ||
| [0, 1, 1, 0, 0, 0], | ||
| [0, 0, 1, 1, 0, 0], | ||
| [0, 0, 0, 1, 1, 0], | ||
| [0, 0, 0, 0, 1, 1], | ||
| [1, 0, 0, 0, 0, 1], | ||
| ] | ||
| ) | ||
| R,C = ldpc.external.groups.get_balanced_permutations_of_matrix(matrix,3) | ||
| balanced_code = codes.BalancedProductCode(matrix,R,C) | ||
|
|
||
| References: | ||
| - https://errorcorrectionzoo.org/c/balanced_product | ||
| - https://arxiv.org/pdf/2505.13679 | ||
|
|
||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| seed_matrix: np.typing.NDArray[np.int_], | ||
| R: qldpc.abstract.GroupMember, | ||
| C: qldpc.abstract.GroupMember, | ||
| field: int | None = None, | ||
| ) -> None: | ||
| if not np.all((seed_matrix == 0) | seed_matrix == 1): | ||
| raise ValueError("Binary matrix must only have zeros or ones") | ||
|
|
||
| if seed_matrix.shape[0] != seed_matrix.shape[1]: | ||
| raise ValueError("Only square binary matrix is supported at this time") | ||
| if not np.array_equal( | ||
| R.to_matrix(seed_matrix.shape[0]) @ seed_matrix, | ||
| seed_matrix @ C.to_matrix(seed_matrix.shape[1]).T, | ||
| ): | ||
| raise ValueError("Invalid permutations provided") | ||
|
|
||
| self._r = R | ||
| self._c = C | ||
|
Comment on lines
+1613
to
+1614
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not save the |
||
| self._seed_matrix = seed_matrix | ||
|
|
||
| check_x = np.hstack( | ||
| (seed_matrix.T, np.eye(seed_matrix.shape[0], dtype=int) + self._c.to_matrix()) | ||
| ) | ||
| check_z = np.hstack( | ||
| (np.eye(seed_matrix.shape[1], dtype=int) + self._r.to_matrix(), seed_matrix) | ||
| ) | ||
| CSSCode.__init__(self, check_x, check_z, field, is_subsystem_code=False) | ||
|
|
||
| @classmethod | ||
| def from_codes( | ||
| cls, code_q: CSSCode, code_c: ClassicalCode, distance_balancing: bool = False | ||
| ) -> qldpc.codes.BPCode: | ||
| """ | ||
| Allows for the creation of balanced codes from a classical code and a CSS code. | ||
|
|
||
| https://arxiv.org/pdf/2505.13679v1 | ||
|
|
||
| If 'distance_balancing', function will determine if x and z errors have the same distance | ||
| and if not, will construct the code to maximize the final distance. This involve exact distance calculations | ||
| which will only work on relatively small codes | ||
|
|
||
| """ | ||
|
|
||
| # i.e. rows of matrix, number qubits of matrix | ||
| r_C, n_C = code_c.matrix.shape | ||
|
|
||
| high_distance = code_q.code_z | ||
| low_distance = code_q.code_x | ||
| if distance_balancing: | ||
| if code_q.code_x.get_distance_exact() > code_q.code_z.get_distance_exact(): | ||
| temp = high_distance | ||
| high_distance = low_distance | ||
| low_distance = temp | ||
|
|
||
| r_low, n_low = low_distance.matrix.shape | ||
|
|
||
| # Make new H_x | ||
| Hx_tensor = np.kron(low_distance.matrix, np.eye(n_C, dtype=int)) | ||
| classical_T_tensor = np.kron( | ||
| np.eye(r_low, dtype=int), code_c.matrix.T | ||
| ) # shape: (r_low * n_C, r_low * r_C) | ||
| H_new_X = np.hstack((Hx_tensor, classical_T_tensor)) | ||
|
|
||
| # Make new H_z | ||
| Hz_tensor = np.kron(high_distance.matrix, np.eye(n_C, dtype=int)) | ||
|
|
||
| bottom_left = np.kron(np.eye(n_low, dtype=int), code_c.matrix) | ||
| bottom_right = np.kron(low_distance.matrix.T, np.eye(r_C, dtype=int)) | ||
|
|
||
| top_right_zeros = np.zeros( | ||
| (Hz_tensor.shape[0], bottom_left.shape[1] + bottom_right.shape[1] - Hz_tensor.shape[1]), | ||
| dtype=int, | ||
| ) | ||
| top_row = np.hstack((Hz_tensor, top_right_zeros)) | ||
|
|
||
| bottom_row = np.hstack((bottom_left, bottom_right)) | ||
|
|
||
| H_new_Z = np.vstack((top_row, bottom_row)) | ||
|
|
||
| new_code = cls.__new__(cls) | ||
| CSSCode.__init__(new_code, H_new_X, H_new_Z) | ||
| return new_code | ||
|
|
||
|
|
||
| class BaconShorCode(SHPCode): | ||
| """Bacon-Shor code on a square grid, implemented as a subsystem hypergraph product code. | ||
|
|
||
|
|
@@ -1614,3 +1728,86 @@ def __init__( | |
| self._dimension = dim_x * dim_z | ||
| self._distance_x = code_z.get_distance() # X errors are witnessed by the Z code | ||
| self._distance_z = code_x.get_distance() # Z errors are witnessed by the X code | ||
|
|
||
| class CCode(CSSCode): | ||
| def __init__( | ||
| self, | ||
| colors: list[int], | ||
| plaquettes: list[list[list[int]]], | ||
| dimension : int | ||
| ) -> None: | ||
| if len(colors) != len(plaquettes): | ||
| raise ValueError("Every plaquette needs a color") | ||
| unique_colors = set() | ||
| for color in colors: | ||
| unique_colors.add(color) | ||
| if len(unique_colors) <= dimension: | ||
| raise ValueError("N-colorable, may only have n colors") | ||
| unique_qubits =set() # do i really need this | ||
| for index_f,f in enumerate(plaquettes): | ||
| for index_g,g in enumerate(plaquettes): | ||
| for edge_f,edge_g in itertools.product(f, g): | ||
| if edge_f == edge_g and colors[index_f] == colors[index_g] and index_f != index_g: | ||
| raise ValueError("Invalid coloring") | ||
| for qubit in edge_f + edge_g: | ||
| unique_qubits.add(qubit) | ||
| #each generator, loop through qubit add identity if in, otherwise not, find ordering of | ||
| ##add | ||
| code_x = [] | ||
| # code_z = [] | ||
| for plaquette in plaquettes: | ||
| qubits = set() | ||
| for edge in plaquette: | ||
| for qubit in edge: | ||
| qubits.add(qubit) | ||
| stabilizer = [] | ||
| for bit in range(0,len(unique_qubits)): | ||
| if bit in qubits: | ||
| stabilizer.append(1) | ||
| else: | ||
| stabilizer.append(0) | ||
| code_x.append(stabilizer) | ||
|
|
||
| CSSCode.__init__(self,code_x,code_x) | ||
|
|
||
| # | ||
| # ### | ||
| # r_C, n_C = code_c.matrix.shape | ||
| # | ||
| # high_distance = code_q.code_z | ||
| # low_distance = code_q.code_x | ||
| # if distance_balancing: | ||
| # if code_q.code_x.get_distance_exact() > code_q.code_z.get_distance_exact(): | ||
| # temp = high_distance | ||
| # high_distance = low_distance | ||
| # low_distance = temp | ||
| # | ||
| # r_low, n_low = low_distance.matrix.shape | ||
| # | ||
| # # Make new H_x | ||
| # Hx_tensor = np.kron(low_distance.matrix, np.eye(n_C, dtype=int)) | ||
| # classical_T_tensor = np.kron( | ||
| # np.eye(r_low, dtype=int), code_c.matrix.T | ||
| # ) # shape: (r_low * n_C, r_low * r_C) | ||
| # H_new_X = np.hstack((Hx_tensor, classical_T_tensor)) | ||
| # | ||
| # # Make new H_z | ||
| # Hz_tensor = np.kron(high_distance.matrix, np.eye(n_C, dtype=int)) | ||
| # | ||
| # bottom_left = np.kron(np.eye(n_low, dtype=int), code_c.matrix) | ||
| # bottom_right = np.kron(low_distance.matrix.T, np.eye(r_C, dtype=int)) | ||
| # | ||
| # top_right_zeros = np.zeros( | ||
| # (Hz_tensor.shape[0], bottom_left.shape[1] + bottom_right.shape[1] - Hz_tensor.shape[1]), | ||
| # dtype=int, | ||
| # ) | ||
| # top_row = np.hstack((Hz_tensor, top_right_zeros)) | ||
| # | ||
| # bottom_row = np.hstack((bottom_left, bottom_right)) | ||
| # | ||
| # H_new_Z = np.vstack((top_row, bottom_row)) | ||
| # | ||
| # new_code = cls.__new__(cls) | ||
| # CSSCode.__init__(new_code, H_new_X, H_new_Z) | ||
| # return new_code | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we name these something like
perm_randperm_cand acceptcomb.Permutation | npt.NDArray[np.int_]inputs (withimport sympy.combinatorics as comb)? I imagine many users will prefer to pass explicit permutation matrices. Initialization can convert these objects to matrices for validity checks as needed, e.g.