Skip to content

Commit 9e4dc2b

Browse files
committed
lint fixes
1 parent 78bdc11 commit 9e4dc2b

File tree

6 files changed

+122
-105
lines changed

6 files changed

+122
-105
lines changed

src/qutip_qip/algorithms/error_correction/bit_flip.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
__all__ = ["BitFlipCode"]
99

10+
1011
class BitFlipCode:
1112
"""
1213
Implementation of the 3-qubit bit-flip code.
13-
14+
1415
The bit-flip code protects against X (bit-flip) errors by encoding
1516
a single logical qubit across three physical qubits:
1617
|0⟩ → |000⟩
@@ -21,7 +22,7 @@ class BitFlipCode:
2122
def encode_circuit():
2223
"""
2324
Create a circuit for encoding a single qubit into the 3-qubit bit-flip code.
24-
25+
2526
Returns
2627
-------
2728
qc : instance of QubitCircuit
@@ -31,53 +32,53 @@ def encode_circuit():
3132
qc.add_gate("CNOT", controls=0, targets=1)
3233
qc.add_gate("CNOT", controls=0, targets=2)
3334
return qc
34-
35+
3536
@staticmethod
3637
def syndrome_measurement_circuit():
3738
"""
3839
Create a circuit for syndrome measurement of the 3-qubit bit-flip code.
39-
40+
4041
Returns
4142
-------
4243
qc : instance of QubitCircuit
4344
Syndrome measurement circuit that uses two ancilla qubits.
4445
"""
4546
qc = QubitCircuit(5) # 3 data qubits + 2 syndrome qubits
46-
47+
4748
# First syndrome measurement: Parity of qubits 0 and 1
4849
qc.add_gate("CNOT", controls=0, targets=3)
4950
qc.add_gate("CNOT", controls=1, targets=3)
50-
51+
5152
# Second syndrome measurement: Parity of qubits 1 and 2
5253
qc.add_gate("CNOT", controls=1, targets=4)
5354
qc.add_gate("CNOT", controls=2, targets=4)
54-
55+
5556
return qc
56-
57+
5758
@staticmethod
5859
def correction_circuit(syndrome):
5960
"""
6061
Create a circuit for error correction based on syndrome measurement.
61-
62+
6263
Parameters
6364
----------
6465
syndrome : tuple
6566
Two-bit syndrome measurement result (s1, s2).
66-
67+
6768
Returns
6869
-------
6970
qc : instance of QubitCircuit
7071
Correction circuit applying X gates as needed.
7172
"""
7273
qc = QubitCircuit(3)
7374
s1, s2 = syndrome
74-
75+
7576
# Syndrome interpretation:
7677
# s1=0, s2=0: No error
7778
# s1=1, s2=0: Error on qubit 0
7879
# s1=1, s2=1: Error on qubit 1
7980
# s1=0, s2=1: Error on qubit 2
80-
81+
8182
if s1 == 1 and s2 == 0:
8283
# Error on qubit 0
8384
qc.add_gate("X", targets=0)
@@ -87,14 +88,14 @@ def correction_circuit(syndrome):
8788
elif s1 == 0 and s2 == 1:
8889
# Error on qubit 2
8990
qc.add_gate("X", targets=2)
90-
91+
9192
return qc
92-
93+
9394
@staticmethod
9495
def decode_circuit():
9596
"""
9697
Create a circuit for decoding the 3-qubit bit-flip code.
97-
98+
9899
Returns
99100
-------
100101
qc : instance of QubitCircuit
@@ -103,10 +104,9 @@ def decode_circuit():
103104
qc = QubitCircuit(3)
104105
qc.add_gate("CNOT", controls=0, targets=2)
105106
qc.add_gate("CNOT", controls=0, targets=1)
106-
107+
107108
# Add a Toffoli gate to verify the parity
108109
# If all qubits have the same value, the result is stored in qubit 0
109110
qc.add_gate("TOFFOLI", controls=[1, 2], targets=0)
110-
111+
111112
return qc
112-

src/qutip_qip/algorithms/error_correction/phase_flip.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,126 +5,127 @@
55

66
__all__ = ["PhaseFlipCode"]
77

8+
89
class PhaseFlipCode:
910
"""
1011
Implementation of the 3-qubit phase-flip code.
1112
The phase-flip code protects against Z (phase-flip) errors by encoding
1213
a single logical qubit across three physical qubits:
1314
|0⟩ → |+++⟩
1415
|1⟩ → |---⟩
15-
16+
1617
This is accomplished by applying Hadamard gates to transform the bit-flip code
1718
into the phase-flip code, as phase errors in the Hadamard basis appear as bit-flips.
1819
"""
19-
20+
2021
@staticmethod
2122
def encode_circuit():
2223
"""
2324
Create a circuit for encoding a single qubit into the 3-qubit phase-flip code.
24-
25+
2526
Returns
2627
-------
2728
qc : instance of QubitCircuit
2829
Encoding circuit for the phase-flip code.
2930
"""
3031
qc = QubitCircuit(3)
31-
32+
3233
# First apply Hadamard to the input qubit
3334
qc.add_gate("SNOT", targets=0)
34-
35+
3536
# Then use the bit-flip encoding structure
3637
qc.add_gate("CNOT", controls=0, targets=1)
3738
qc.add_gate("CNOT", controls=0, targets=2)
38-
39+
3940
# Apply Hadamard gates to all qubits
4041
qc.add_gate("SNOT", targets=0)
4142
qc.add_gate("SNOT", targets=1)
4243
qc.add_gate("SNOT", targets=2)
43-
44+
4445
return qc
45-
46+
4647
@staticmethod
4748
def syndrome_measurement_circuit():
4849
"""
4950
Create a circuit for syndrome measurement of the 3-qubit phase-flip code.
50-
51+
5152
Returns
5253
-------
5354
qc : instance of QubitCircuit
5455
Syndrome measurement circuit that uses two ancilla qubits.
5556
"""
5657
qc = QubitCircuit(5) # 3 data qubits + 2 syndrome qubits
57-
58+
5859
qc.add_gate("SNOT", targets=0)
5960
qc.add_gate("SNOT", targets=1)
6061
qc.add_gate("SNOT", targets=2)
61-
62+
6263
qc.add_gate("CNOT", controls=0, targets=3)
6364
qc.add_gate("CNOT", controls=1, targets=3)
64-
65+
6566
qc.add_gate("CNOT", controls=1, targets=4)
6667
qc.add_gate("CNOT", controls=2, targets=4)
67-
68+
6869
qc.add_gate("SNOT", targets=0)
6970
qc.add_gate("SNOT", targets=1)
7071
qc.add_gate("SNOT", targets=2)
71-
72+
7273
return qc
73-
74+
7475
@staticmethod
7576
def correction_circuit(syndrome):
7677
"""
7778
Create a circuit for error correction based on syndrome measurement.
78-
79+
7980
Parameters
8081
----------
8182
syndrome : tuple
8283
Two-bit syndrome measurement result (s1, s2).
83-
84+
8485
Returns
8586
-------
8687
qc : instance of QubitCircuit
8788
Correction circuit applying Z gates as needed.
8889
"""
8990
qc = QubitCircuit(3)
9091
s1, s2 = syndrome
91-
92+
9293
# Syndrome interpretation:
9394
# s1=0, s2=0: No error
9495
# s1=1, s2=0: Error on qubit 0
9596
# s1=1, s2=1: Error on qubit 1
9697
# s1=0, s2=1: Error on qubit 2
97-
98+
9899
if s1 == 1 and s2 == 0:
99100
qc.add_gate("Z", targets=0)
100101
elif s1 == 1 and s2 == 1:
101102
qc.add_gate("Z", targets=1)
102103
elif s1 == 0 and s2 == 1:
103104
qc.add_gate("Z", targets=2)
104-
105+
105106
return qc
106-
107+
107108
@staticmethod
108109
def decode_circuit():
109110
"""
110111
Create a circuit for decoding the 3-qubit phase-flip code.
111-
112+
112113
Returns
113114
-------
114115
qc : instance of QubitCircuit
115116
Decoding circuit for the phase-flip code.
116117
"""
117118
qc = QubitCircuit(3)
118-
119+
119120
qc.add_gate("SNOT", targets=0)
120121
qc.add_gate("SNOT", targets=1)
121122
qc.add_gate("SNOT", targets=2)
122-
123+
123124
qc.add_gate("CNOT", controls=0, targets=2)
124125
qc.add_gate("CNOT", controls=0, targets=1)
125-
126+
126127
qc.add_gate("TOFFOLI", controls=[1, 2], targets=0)
127-
128+
128129
qc.add_gate("SNOT", targets=0)
129-
130-
return qc
130+
131+
return qc

src/qutip_qip/algorithms/error_correction/shor_code.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,62 @@
55

66
__all__ = ["ShorCode"]
77

8+
89
class ShorCode:
910
"""
1011
Implementation of the 9-qubit Shor code.
11-
12+
1213
The Shor code protects against arbitrary single-qubit errors by combining
1314
the 3-qubit phase-flip code with the 3-qubit bit-flip code.
14-
15+
1516
The logical states are encoded as:
1617
|0⟩ → (|000⟩ + |111⟩)(|000⟩ + |111⟩)(|000⟩ + |111⟩) / 2√2
1718
|1⟩ → (|000⟩ - |111⟩)(|000⟩ - |111⟩)(|000⟩ - |111⟩) / 2√2
18-
19-
This encoding allows correction of any single-qubit error (bit-flip, phase-flip,
19+
20+
This encoding allows correction of any single-qubit error (bit-flip, phase-flip,
2021
or both) on any of the 9 physical qubits.
2122
"""
22-
23+
2324
@staticmethod
2425
def encode_circuit():
2526
"""
2627
Create a circuit for encoding a single qubit into the 9-qubit Shor code.
27-
28+
2829
The encoding process:
2930
1. Apply phase-flip encoding to the input (creating |+++⟩ or |---⟩)
3031
2. Apply bit-flip encoding to each of the three qubits
31-
32+
3233
Returns
3334
-------
3435
qc : instance of QubitCircuit
3536
Encoding circuit for the Shor code.
3637
"""
3738
qc = QubitCircuit(9)
38-
39+
3940
# Step 1: Phase-flip encoding on the first qubit
4041
# Apply Hadamard to the input qubit
4142
qc.add_gate("SNOT", targets=0)
42-
43+
4344
# Create the GHZ-like state by using CNOTs
4445
qc.add_gate("CNOT", controls=0, targets=3)
4546
qc.add_gate("CNOT", controls=0, targets=6)
46-
47-
# Apply Hadamard to all three qubits
47+
48+
# Apply Hadamard to all three qubits
4849
qc.add_gate("SNOT", targets=0)
4950
qc.add_gate("SNOT", targets=3)
5051
qc.add_gate("SNOT", targets=6)
51-
52+
5253
# Step 2: Bit-flip encoding for each of the three blocks
5354
# First block: qubits 0,1,2
5455
qc.add_gate("CNOT", controls=0, targets=1)
5556
qc.add_gate("CNOT", controls=0, targets=2)
56-
57+
5758
# Second block: qubits 3,4,5
5859
qc.add_gate("CNOT", controls=3, targets=4)
5960
qc.add_gate("CNOT", controls=3, targets=5)
60-
61+
6162
# Third block: qubits 6,7,8
6263
qc.add_gate("CNOT", controls=6, targets=7)
6364
qc.add_gate("CNOT", controls=6, targets=8)
64-
65-
return qc
65+
66+
return qc

0 commit comments

Comments
 (0)