qec 0.3.2__py3-none-any.whl → 0.3.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qec/__init__.py +2 -0
- qec/code_constructions/__init__.py +17 -0
- qec/code_constructions/css_code.py +959 -0
- qec/code_constructions/hgp_code.py +344 -0
- qec/code_constructions/periodic_surface_xzzx.py +100 -0
- qec/code_constructions/rotated_xzzx.py +122 -0
- qec/code_constructions/stabilizer_code.py +654 -0
- qec/code_constructions/surface_code.py +66 -0
- qec/code_constructions/toric_code.py +56 -0
- qec/code_instances/__init__.py +5 -0
- qec/code_instances/five_qubit_code.py +67 -0
- qec/codetables_de/__init__.py +3 -0
- qec/codetables_de/codetables_de.py +97 -0
- qec/utils/__init__.py +6 -0
- qec/utils/binary_pauli_utils.py +404 -0
- qec/utils/codetables_de_utils.py +272 -0
- qec/utils/load_code_util.py +142 -0
- qec/utils/sparse_binary_utils.py +80 -0
- {qec-0.3.2.dist-info → qec-0.3.3.dist-info}/METADATA +1 -1
- qec-0.3.3.dist-info/RECORD +27 -0
- qec-0.3.2.dist-info/RECORD +0 -10
- {qec-0.3.2.dist-info → qec-0.3.3.dist-info}/WHEEL +0 -0
- {qec-0.3.2.dist-info → qec-0.3.3.dist-info}/licenses/LICENSE +0 -0
- {qec-0.3.2.dist-info → qec-0.3.3.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,66 @@
|
|
1
|
+
import ldpc.codes.rep_code
|
2
|
+
|
3
|
+
import ldpc.codes
|
4
|
+
from qec.code_constructions import HypergraphProductCode
|
5
|
+
|
6
|
+
|
7
|
+
class SurfaceCode(HypergraphProductCode):
|
8
|
+
"""
|
9
|
+
Represents a Surface Code, which is a type of quantum error correction code.
|
10
|
+
|
11
|
+
The Surface Code is constructed using two repetition codes, one for the X stabilizers
|
12
|
+
and one for the Z stabilizers. The distances dx and dz specify the code distances
|
13
|
+
for the X and Z stabilizers, respectively, corresponding to the height and width of the lattice.
|
14
|
+
|
15
|
+
The Surface Code is a specific instance of a Hypergraph Product Code. The hypergraph product
|
16
|
+
construction allows for the creation of quantum error correction codes with desirable properties
|
17
|
+
such as high distance and low-weight stabilizers. In this construction, two classical codes are
|
18
|
+
used to generate the quantum code. For the Surface Code, these classical codes are simple repetition
|
19
|
+
codes.
|
20
|
+
|
21
|
+
Parameters
|
22
|
+
----------
|
23
|
+
dx : int, optional
|
24
|
+
The code distance for the X stabilizers (width of the lattice). If not specified, it will be set to the value of dz.
|
25
|
+
dz : int, optional
|
26
|
+
The code distance for the Z stabilizers (height of the lattice). If not specified, it will be set to the value of dx.
|
27
|
+
|
28
|
+
Raises
|
29
|
+
------
|
30
|
+
ValueError
|
31
|
+
If both dx and dz are not specified.
|
32
|
+
|
33
|
+
Attributes
|
34
|
+
----------
|
35
|
+
x_code_distance : int
|
36
|
+
The code distance for X errors.
|
37
|
+
z_code_distance : int
|
38
|
+
The code distance for Z errors.
|
39
|
+
code_distance : int
|
40
|
+
The minimum of x_code_distance and z_code_distance.
|
41
|
+
|
42
|
+
Notes
|
43
|
+
-----
|
44
|
+
The Surface Code is constructed using the hypergraph product of two repetition codes. The repetition
|
45
|
+
code is a simple classical code where each bit is repeated multiple times to provide redundancy. By
|
46
|
+
taking the hypergraph product of two such codes, we obtain a quantum code with stabilizers that are
|
47
|
+
low-weight and a code distance that is determined by the distances of the original repetition codes.
|
48
|
+
"""
|
49
|
+
|
50
|
+
def __init__(self, dx: int = None, dz: int = None):
|
51
|
+
if dx is None and dz is None:
|
52
|
+
raise ValueError("Please specify dx or dz")
|
53
|
+
if dx is None:
|
54
|
+
dx = dz
|
55
|
+
if dz is None:
|
56
|
+
dz = dx
|
57
|
+
|
58
|
+
h1 = ldpc.codes.rep_code(dz)
|
59
|
+
h2 = ldpc.codes.rep_code(dx)
|
60
|
+
|
61
|
+
super().__init__(h1, h2, name=f"({dx}x{dz})-Surface Code")
|
62
|
+
|
63
|
+
self.x_code_distance = dx
|
64
|
+
self.z_code_distance = dz
|
65
|
+
|
66
|
+
self.code_distance = min(self.x_code_distance, self.z_code_distance)
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import ldpc.codes
|
2
|
+
from qec.code_constructions import HypergraphProductCode
|
3
|
+
|
4
|
+
|
5
|
+
class ToricCode(HypergraphProductCode):
|
6
|
+
"""
|
7
|
+
Represents a Toric Code, which is a type of quantum error correction code.
|
8
|
+
|
9
|
+
The Toric Code is constructed using two ring codes, one for the X stabilizers
|
10
|
+
and one for the Z stabilizers. The distances dx and dz specify the code distances
|
11
|
+
for the X and Z stabilizers, respectively, corresponding to the height and width of the lattice.
|
12
|
+
|
13
|
+
The Toric Code is defined on a torus and is the generalization of the Surface Code with periodic
|
14
|
+
boundary conditions. The Toric Code has a logical qubit count of 2.
|
15
|
+
|
16
|
+
Parameters
|
17
|
+
----------
|
18
|
+
dx : int, optional
|
19
|
+
The code distance for the X stabilizers (width of the lattice). If not specified, it will be set to the value of dz.
|
20
|
+
dz : int, optional
|
21
|
+
The code distance for the Z stabilizers (height of the lattice). If not specified, it will be set to the value of dx.
|
22
|
+
|
23
|
+
Raises
|
24
|
+
------
|
25
|
+
ValueError
|
26
|
+
If both dx and dz are not specified.
|
27
|
+
|
28
|
+
Attributes
|
29
|
+
----------
|
30
|
+
x_code_distance : int
|
31
|
+
The code distance the X errors.
|
32
|
+
z_code_distance : int
|
33
|
+
The code distance for Z errors.
|
34
|
+
code_distance : int
|
35
|
+
The minimum of x_code_distance and z_code_distance.
|
36
|
+
logical_qubit_count : int
|
37
|
+
The number of logical qubits in the code, which is 2 for the Toric Code.
|
38
|
+
"""
|
39
|
+
|
40
|
+
def __init__(self, dx: int = None, dz: int = None):
|
41
|
+
if dx is None and dz is None:
|
42
|
+
raise ValueError("Please specify dx or dz")
|
43
|
+
if dx is None:
|
44
|
+
dx = dz
|
45
|
+
if dz is None:
|
46
|
+
dz = dx
|
47
|
+
|
48
|
+
h1 = ldpc.codes.ring_code(dz)
|
49
|
+
h2 = ldpc.codes.ring_code(dx)
|
50
|
+
|
51
|
+
super().__init__(h1, h2, name=f"({dx}x{dz})-Toric Code")
|
52
|
+
|
53
|
+
self.x_code_distance = dx
|
54
|
+
self.z_code_distance = dz
|
55
|
+
|
56
|
+
self.code_distance = min(self.x_code_distance, self.z_code_distance)
|
@@ -0,0 +1,67 @@
|
|
1
|
+
from qec.code_constructions import StabilizerCode
|
2
|
+
|
3
|
+
|
4
|
+
class FiveQubitCode(StabilizerCode):
|
5
|
+
"""
|
6
|
+
Five-Qubit Quantum Error-Correcting Code.
|
7
|
+
|
8
|
+
The `FiveQubitCode` class implements the [[5, 1, 3]] quantum stabilizer code,
|
9
|
+
which is the smallest possible quantum error-correcting code capable of
|
10
|
+
correcting an arbitrary single-qubit error. This code encodes one logical
|
11
|
+
qubit into five physical qubits and has a distance of three, allowing it
|
12
|
+
to detect up to two errors and correct one.
|
13
|
+
|
14
|
+
Parameters
|
15
|
+
----------
|
16
|
+
None
|
17
|
+
|
18
|
+
Attributes
|
19
|
+
----------
|
20
|
+
code_distance : int
|
21
|
+
The distance of the quantum code. For the five-qubit code, this is set to 3.
|
22
|
+
|
23
|
+
Inherits
|
24
|
+
--------
|
25
|
+
StabilizerCode
|
26
|
+
The base class providing functionalities for stabilizer-based quantum
|
27
|
+
error-correcting codes, including initialization, distance computation,
|
28
|
+
and parameter retrieval.
|
29
|
+
|
30
|
+
Examples
|
31
|
+
--------
|
32
|
+
>>> five_qubit = FiveQubitCode()
|
33
|
+
>>> five_qubit.phyiscal_qubit_count
|
34
|
+
5
|
35
|
+
>>> five_qubit.logical_qubit_count
|
36
|
+
1
|
37
|
+
>>> five_qubit.code_distance
|
38
|
+
3
|
39
|
+
"""
|
40
|
+
|
41
|
+
def __init__(self):
|
42
|
+
"""
|
43
|
+
Initialize the Five-Qubit Code with predefined Pauli stabilizers.
|
44
|
+
|
45
|
+
The constructor sets up the stabilizer generators for the [[5, 1, 3]]
|
46
|
+
quantum code using their corresponding Pauli strings. It then calls the
|
47
|
+
superclass initializer to establish the stabilizer matrix and other
|
48
|
+
essential parameters.
|
49
|
+
|
50
|
+
Parameters
|
51
|
+
----------
|
52
|
+
None
|
53
|
+
|
54
|
+
Raises
|
55
|
+
------
|
56
|
+
ValueError
|
57
|
+
If the provided stabilizer generators do not satisfy the necessary
|
58
|
+
commutation relations required for a valid stabilizer code.
|
59
|
+
"""
|
60
|
+
# Define the Pauli stabilizer generators for the five-qubit code
|
61
|
+
pauli_stabilizers = [["XZZXI"], ["IXZZX"], ["XIXZZ"], ["ZXIXZ"]]
|
62
|
+
|
63
|
+
# Initialize the StabilizerCode with the defined stabilizers and a custom name
|
64
|
+
super().__init__(stabilizers=pauli_stabilizers, name="5-Qubit Code")
|
65
|
+
|
66
|
+
# Set the distance attribute specific to the five-qubit code
|
67
|
+
self.code_distance = 3
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import numpy as np
|
2
|
+
from qec.code_constructions import StabilizerCode
|
3
|
+
from qec.utils.codetables_de_utils import get_codetables_de_matrix, pcm_to_csr_matrix
|
4
|
+
|
5
|
+
|
6
|
+
class CodeTablesDE(StabilizerCode):
|
7
|
+
"""
|
8
|
+
A code object built from data obtained from Markus Grassl's codetables.de website (with `q=4`).
|
9
|
+
|
10
|
+
This class inherits from `StabilizerCode` and initialises the code
|
11
|
+
by querying the codetables.de website for the specified parameters `(n, k)`,
|
12
|
+
constructing the stabilizer (PCM) matrix, and passing it up to the
|
13
|
+
`StabilizerCode` parent class.
|
14
|
+
|
15
|
+
Parameters
|
16
|
+
----------
|
17
|
+
physical_qubit_count : int
|
18
|
+
Length of the code (number of physical qubits).
|
19
|
+
logical_qubit_count : int
|
20
|
+
Dimension of the code (number of logical qubits).
|
21
|
+
|
22
|
+
Attributes
|
23
|
+
----------
|
24
|
+
name : str
|
25
|
+
Name assigned to this code instance. Defaults to "CodeTablesDE".
|
26
|
+
url : str
|
27
|
+
The URL from which this code's data was retrieved.
|
28
|
+
code_distance : int
|
29
|
+
The code's minimum distance. This is updated if the reported upper bound
|
30
|
+
from the codetables.de website is smaller than the base class default.
|
31
|
+
|
32
|
+
See Also
|
33
|
+
--------
|
34
|
+
StabilizerCode : Parent class providing stabilizer code functionality.
|
35
|
+
get_codetables_de_matrix : Function that queries codetables.de to retrieve code data.
|
36
|
+
pcm_to_csr_matrix : Function that converts a PCM-like list of column indices into a CSR matrix.
|
37
|
+
|
38
|
+
Notes
|
39
|
+
-----
|
40
|
+
- The data is retrieved from:
|
41
|
+
https://codetables.de
|
42
|
+
maintained by Markus Grassl.
|
43
|
+
"""
|
44
|
+
|
45
|
+
def __init__(self, physical_qubit_count: int, logical_qubit_count: int):
|
46
|
+
"""
|
47
|
+
Initialise a code from Markus Grassl's codetables.de website with `q=4`, `n`, and `k`.
|
48
|
+
|
49
|
+
This method queries the codetables.de database for a stabilizer matrix
|
50
|
+
describing a code with parameters (q=4, n, k). The matrix is then
|
51
|
+
converted to a CSR (Compressed Sparse Row) format and passed to
|
52
|
+
the parent `StabilizerCode` class.
|
53
|
+
|
54
|
+
Parameters
|
55
|
+
----------
|
56
|
+
n : int
|
57
|
+
Length of the code (number of physical qubits).
|
58
|
+
k : int
|
59
|
+
Dimension of the code (number of logical qubits).
|
60
|
+
|
61
|
+
Notes
|
62
|
+
-----
|
63
|
+
- `d_upper` from the query result is used to potentially update `self.code_distance`
|
64
|
+
if it is smaller than the default distance assigned by `StabilizerCode`.
|
65
|
+
- Since this code is defined over GF(4), `q` is hardcoded as 4.
|
66
|
+
- Data is retrieved from Markus Grassl's website (https://codetables.de).
|
67
|
+
|
68
|
+
Raises
|
69
|
+
------
|
70
|
+
ValueError
|
71
|
+
If no valid matrix data can be retrieved from codetables.de, or
|
72
|
+
if the site indicates that such a code does not exist.
|
73
|
+
"""
|
74
|
+
# Retrieve code data from codetables.de
|
75
|
+
ct_dict = get_codetables_de_matrix(
|
76
|
+
q=4, n=physical_qubit_count, k=logical_qubit_count
|
77
|
+
)
|
78
|
+
|
79
|
+
# Construct the stabilizer matrix in CSR format
|
80
|
+
# The matrix is 2*n columns wide, as is typical for GF(4) stabilizers.
|
81
|
+
stabilizer_matrix = pcm_to_csr_matrix(
|
82
|
+
ct_dict["pcm"], num_cols=2 * int(ct_dict["n"])
|
83
|
+
)
|
84
|
+
|
85
|
+
# Initialise the parent class with this stabilizer matrix
|
86
|
+
super().__init__(stabilizers=stabilizer_matrix)
|
87
|
+
|
88
|
+
# Name of this code and the URL from which we retrieved it
|
89
|
+
self.name = "CodeTablesDE"
|
90
|
+
self.url = ct_dict["url"]
|
91
|
+
|
92
|
+
# Update distance if the reported upper bound is smaller than the default
|
93
|
+
if self.code_distance is None:
|
94
|
+
self.code_distance = np.inf
|
95
|
+
|
96
|
+
if int(ct_dict["d_upper"]) < self.code_distance:
|
97
|
+
self.code_distance = int(ct_dict["d_upper"])
|