Classical error correction¶
Linear codes¶
-
class
pyqec.classical.
LinearCode
(parity_check_matrix=None, generator_matrix=None, tag=None)¶ An implementation of linear codes optimized for LDPC codes.
A code can be defined from either a parity check matrix H or a generator matrix G. These matrices need to be orthogonal, that is H G^T = 0.
- Parameters
parity_check_matrix (Optional[pyqec.sparse.BinaryMatrix]) – The parity check matrix of the code. Most be orthogonal to the generator matrix. If omited, one is computed from the generator matrix.
generator_matrix (Optional[pyqec.sparse.BinaryMatrix]) – The generator matrix of the code. Most be orthogonal to the parity check matrix. If omited, one is computed from the parity check matrix.
tag (Optional[String]) – A label for the code used to save data and make automatic legend in plots. If omited, the empty string is used a default tag.
Example
This example shows 2 ways to define the Hamming code. They both required you import the following.
>>> from pyqec.sparse import BinaryMatrix >>> from pyqec.classical import LinearCode
You can build a linear code from a parity check matrix
>>> matrix = BinaryMatrix(7, [[0, 1, 2, 4], [0, 1, 3, 5], [0, 2, 3, 6]]) >>> code_pcm = LinearCode(parity_check_matrix=pcm)
or from a generator matrix
>>> matrix = BinaryMatrix(7, [[0, 4, 5, 6], [1, 4, 5], [2, 4, 6], [3, 5, 6]]) >>> code_gm = LinearCode(generator_matrix=matrix)
Note
Use the == if you want to know if 2 codes have exactly the same parity check matrix, generator matrix and tags. However, since there is freedom in the choice of the matrices and tag for the same code, use has_same_codespace method if you want to know if 2 codes define the same codespace even if they may have different parity check matrices or generator matrices.
>>> code_pcm == code_gm False >>> code_pcm.has_same_codespace(code_gm) True
-
dimension
()¶ The number of encoded qubits.
-
generator_matrix
()¶ The parity check matrix of the code.
-
has_codeword
(message)¶ Checks if the given message is a codeword of the code.
- Parameters
message (Seq[int]) – The positions with value 1 in the message.
- Returns
True if the message has the right length and a zero syndrome or False otherwise.
- Return type
bool
-
has_same_codespace
(other)¶ Checks if the other code defined the same codespace as this code.
- Parameters
other (LinearCode) – The code to compare.
- Returns
True if other codewords are exactly the same as this code codewords.
- Return type
bool
-
length
()¶ The number of bits in the code.
>>> len(code) == code.length() true
-
minimal_distance
()¶ The weight of the smallest non trivial codeword.
- Returns
The minimal distance of the code if
the dimension is at least 1 or -1
if the dimension is 0.
Caution
This function execution time scale exponentially with the dimension of the code. Use at your own risk!
-
num_checks
()¶ The number of checks in the code.
-
num_generators
()¶ The number of codeword generators in the code.
-
parity_check_matrix
()¶ The parity check matrix of the code.
-
syndrome_of
(message)¶ The syndrome of a given message.
- Parameters
message (Seq[int]) – The positions with value 1 in the message.
- Returns
The positions where H y is 1 where H is the parity check matrix of the code and y the input message.
- Return type
list[int]
- Raises
ValueError – If a position in the message is greater or equal to the length of the code.
-
tag
()¶ The tag of the code.
Standard codes constructors¶
-
pyqec.classical.
hamming_code
(tag=None)¶ Returns an instance of the Hamming code.
- Parameters
tag (Optional[String]) – A label for the code used to save data and make automatic legend in plots.
-
pyqec.classical.
random_regular_code
(num_bits=4, num_checks=3, bit_degree=3, check_degree=4, random_seed=None, tag=None)¶ Samples a random regular codes.
- Parameters
num_bits (int) – The number of bits in the code.
num_checks (int, default = 3) – The number of checks in the code.
bit_degree (int) – The number of checks connected to each bit.
check_degree (int) – The number of bits connected to each check.
random_seed (Optional[int]) – A seed to feed the random number generator. By default, the rng is initialize from entropy.
tag (Optional[string]) – An identifier for the code.
- Returns
A random linear code with the given parameters.
- Return type
- Raises
ValueError – If block_size * bit_degree != number_of_checks * check_degree.
-
pyqec.classical.
repetition_code
(length, tag=None)¶ Returns an instance of the repetition code.
- Parameters
length (Int) – The number of bits.
tag (Optional[String]) – A label for the code used to save data and make automatic legend in plots.