Module pyqec.pyqec

A toolbox for classical (and soon quantum) error correction.

Classes

class BinarySymmetricChannel (...)

An implementation of a binary symmetric channel.

A binary symmetric channel flips the value of each bits according to a given error probability.

Methods

def error_probability(self)
def sample_error_of_length(self, length)
class FlipDecoder (...)

Methods

def decode(...)
class LinearCode

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 have the property that H G^T = 0.

Example

This example shows 2 way to define the Hamming code.

From a parity check matrix

code_from_checks = LinearCode.from_checks(
    7,
    [[0, 1, 2, 4], [0, 1, 3, 5], [0, 2, 3, 6]]
)

From a generator matrix

code_from_generators = LinearCode.from_generators(
    7,
    [[0, 4, 5, 6], [1, 4, 5], [2, 4, 6], [3, 5, 6]]
)

Comparison

Use the == if you want to know if 2 codes have exactly the same parity check matrix and generator matrix. However, since there is freedom in the choice of parity check matrix and generator matrix for the same code, use has_same_codespace_as method if you want to know if 2 codes define the same codespace even if they may have different parity check matrix or generator matrix.

>>> code_from_checks == code_from_generators
False
>>> code_from_checks.has_same_codespace_as(code_from_generators)
True

Static methods

def from_checks(block_size, checks)

Constructs a LinearCode from a parity check matrix. A parity check matrix H has the property that Hx = 0 for all codeword x.

Args

block_size : int
The number of bits in the code.
checks : list of list of int
A list of checks where each check is represented by the list of positions where this check has value 1.

Returns

LinearCode
The linear code with the given checks.

Raises

ValueError
If a check has a position greater or equal to the block size.

Example

A 3 bits repetition code.

code = LinearCode.from_checks(3, [[0, 1], [1, 2]])
def from_generators(block_size, generators)

Constructs a LinearCode from a generator matrix. A generator matrix G has the property that for any codeword x we have x = G^T y where y is the unencoded bitstring.

Args

block_size : int
The number of bits in the code.
generators : list of list of int
A list of codeword generators where each generator is represented by the list of positions where this generator has value 1.

Returns

LinearCode
The linear code with the given codeword generators.

Raises

ValueError
If a generator has a position greater or equal to the block size.

Example

A 3 bits repetition code.

code = LinearCode.from_generators(3, [[0, 1, 2]])
def random_regular_code(block_size, number_of_checks, bit_degree, check_degree, random_seed)

Samples a random regular codes.

Parameters

block_size : int, default = 4
The number of bits in the code.
number_of_checks : int, default = 3
The number of checks in the code.
bit_degree : int, default = 3
The number of checks connected to each bit.
check_degree : int, default = 4
The number of bits connected to each check.
random_seed : int, optional
A seed to feed the random number generator. By default, the rng is initialize from entropy.

Returns

LinearCode
A random linear code with the given parameters.

Raises

ValueError
If block_size * bit_degree != number_of_checks * check_degree.

Methods

def block_size(self)

The number of bits in the code.

def dimension(self)

The number of encoded qubits.

def generator_matrix(self)

The generator matrix of the code.

def has_codeword(self, message)

Checks if the given message is a codeword of the code.

Parameters

message : list of int
The positions with value 1 in the message.

Returns

bool
True if the message has a zero syndrome and False otherwise.

Raises

ValueError
If a position in the message is greater or equal to the block size of the code.
def has_same_codespace_as(self, other)

Checks if the other code defined the same codespace as this code.

Parameters

other : LinearCode
The code to compare.

Returns

bool
True if other codewords are exactly the same as this code codewords.
def minimal_distance(self)

The weight of the small non trivial codeword.

Returns

The minimal distance of the code if
the dimension is at least 1 or -1
if the dimension is 0.

Notes

This function execution time scale exponentially
with the dimension of the code.
Use at your own risk!
def number_of_checks(self)

The number of checks in the code.

def number_of_generators(self)

The number of codeword generators in the code.

def parity_check_matrix(self)

The parity check matrix of the code.

def syndrome_of(self, message)

The syndrome of a given message.

Parameters

message : list of int
The positions with value 1 in the message.

Returns

list of int
The positions where H y is 1 where H is the parity check matrix of the code and y the input message.

Raises

ValueError
If a position in the message is greater or equal to the block size of the code.