Skip to content

Commit 2d61fdf

Browse files
committed
upate shor code
1 parent b4755c5 commit 2d61fdf

File tree

2 files changed

+42
-90
lines changed

2 files changed

+42
-90
lines changed
Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,40 @@
1-
import numpy as np
2-
from qutip import Qobj, tensor, basis, sigmax, sigmay, sigmaz
3-
from qutip_qip.operations import Gate
41
from qutip_qip.circuit import QubitCircuit
5-
6-
__all__ = ["ShorCode"]
7-
2+
from qutip_qip.algorithms import BitFlipCode
3+
from qutip_qip.algorithms import PhaseFlipCode
84

95
class ShorCode:
106
"""
11-
Implementation of the 9-qubit Shor code.
12-
13-
The Shor code protects against arbitrary single-qubit errors by combining
14-
the 3-qubit phase-flip code with the 3-qubit bit-flip code.
15-
16-
The logical states are encoded as:
17-
|0⟩ → (|000⟩ + |111⟩)(|000⟩ + |111⟩)(|000⟩ + |111⟩) / 2√2
18-
|1⟩ → (|000⟩ - |111⟩)(|000⟩ - |111⟩)(|000⟩ - |111⟩) / 2√2
19-
20-
This encoding allows correction of any single-qubit error (bit-flip, phase-flip,
21-
or both) on any of the 9 physical qubits.
7+
Constructs the 9-qubit Shor code encoding circuit using BitFlipCode and PhaseFlipCode.
228
"""
239

24-
@staticmethod
25-
def encode_circuit():
10+
def __init__(self):
11+
# Logical qubit -> Phase protection: [0, 3, 6] (1 qubit to 3)
12+
# Each → BitFlipCode: [0,1,2], [3,4,5], [6,7,8]
13+
self.data_qubits = list(range(9))
14+
self.phase_code = PhaseFlipCode(data_qubits=[0, 3, 6], syndrome_qubits=[]) # No ancillas for encoding
15+
self.bit_blocks = [
16+
BitFlipCode(data_qubits=[0, 1, 2], syndrome_qubits=[]),
17+
BitFlipCode(data_qubits=[3, 4, 5], syndrome_qubits=[]),
18+
BitFlipCode(data_qubits=[6, 7, 8], syndrome_qubits=[])
19+
]
20+
self.n_qubits = 9 # Encoding only requires 9 qubits
21+
22+
def encode_circuit(self):
2623
"""
27-
Create a circuit for encoding a single qubit into the 9-qubit Shor code.
28-
29-
The encoding process:
30-
1. Apply phase-flip encoding to the input (creating |+++⟩ or |---⟩)
31-
2. Apply bit-flip encoding to each of the three qubits
24+
Construct the 9-qubit Shor code encoding circuit.
3225
3326
Returns
3427
-------
35-
qc : instance of QubitCircuit
36-
Encoding circuit for the Shor code.
28+
QubitCircuit
29+
Circuit that encodes one logical qubit into the Shor code.
3730
"""
38-
qc = QubitCircuit(9)
39-
40-
# Step 1: Phase-flip encoding on the first qubit
41-
# Apply Hadamard to the input qubit
42-
qc.add_gate("SNOT", targets=0)
43-
44-
# Create the GHZ-like state by using CNOTs
45-
qc.add_gate("CNOT", controls=0, targets=3)
46-
qc.add_gate("CNOT", controls=0, targets=6)
47-
48-
# Apply Hadamard to all three qubits
49-
qc.add_gate("SNOT", targets=0)
50-
qc.add_gate("SNOT", targets=3)
51-
qc.add_gate("SNOT", targets=6)
52-
53-
# Step 2: Bit-flip encoding for each of the three blocks
54-
# First block: qubits 0,1,2
55-
qc.add_gate("CNOT", controls=0, targets=1)
56-
qc.add_gate("CNOT", controls=0, targets=2)
31+
qc = QubitCircuit(self.n_qubits)
5732

58-
# Second block: qubits 3,4,5
59-
qc.add_gate("CNOT", controls=3, targets=4)
60-
qc.add_gate("CNOT", controls=3, targets=5)
33+
phase_encode = self.phase_code.encode_circuit()
34+
qc.gates.extend(phase_encode.gates)
6135

62-
# Third block: qubits 6,7,8
63-
qc.add_gate("CNOT", controls=6, targets=7)
64-
qc.add_gate("CNOT", controls=6, targets=8)
36+
for bit_code in self.bit_blocks:
37+
bit_encode = bit_code.encode_circuit()
38+
qc.gates.extend(bit_encode.gates)
6539

6640
return qc

tests/test_shor_code.py

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,23 @@
11
import pytest
2-
from qutip_qip.algorithms import ShorCode
2+
from qutip_qip.algorithms import ShorCode # Adjust the import as per your file/module name
33

4+
def test_shor_encode_structure():
5+
shor = ShorCode()
6+
circuit = shor.encode_circuit()
47

5-
def test_encode_circuit():
6-
"""Test the Shor code encoding circuit structure."""
7-
qc = ShorCode.encode_circuit()
8+
# Gate count: 3 Hadamards (phase flip) + 6 CNOTs (bit flip blocks)
9+
assert len(circuit.gates) == 9, "Shor code should have 3 Hadamards and 6 CNOTs in encode circuit."
810

9-
# Check correct number of qubits
10-
assert qc.N == 9
11+
# Check Hadamard gates applied to qubits 0, 3, 6
12+
snot_targets = [gate.targets[0] for gate in circuit.gates if gate.name == "SNOT"]
13+
assert set(snot_targets) == {0, 3, 6}, "Hadamards should be on qubits 0, 3, and 6."
1114

12-
# Check total number of gates (1H + 2CNOT + 3H + 6CNOT = 12 gates)
13-
assert len(qc.gates) == 12
15+
# Check correct number of CNOTs for 3 blocks
16+
cnot_gates = [gate for gate in circuit.gates if gate.name == "CNOT"]
17+
assert len(cnot_gates) == 6, "Shor code encoding must include 6 CNOTs."
1418

15-
# Check first Hadamard gate (phase-flip encoding starts)
16-
assert qc.gates[0].name == "SNOT" and qc.gates[0].targets[0] == 0
17-
18-
# Check first level CNOTs (create GHZ-like state across blocks)
19-
assert qc.gates[1].name == "CNOT"
20-
assert qc.gates[1].controls == [0] and qc.gates[1].targets[0] == 3
21-
assert qc.gates[2].name == "CNOT"
22-
assert qc.gates[2].controls == [0] and qc.gates[2].targets[0] == 6
23-
24-
# Check three Hadamard gates (complete phase-flip encoding)
25-
assert qc.gates[3].name == "SNOT" and qc.gates[3].targets[0] == 0
26-
assert qc.gates[4].name == "SNOT" and qc.gates[4].targets[0] == 3
27-
assert qc.gates[5].name == "SNOT" and qc.gates[5].targets[0] == 6
28-
29-
# Check bit-flip encoding CNOTs for first block
30-
assert qc.gates[6].name == "CNOT"
31-
assert qc.gates[6].controls == [0] and qc.gates[6].targets[0] == 1
32-
assert qc.gates[7].name == "CNOT"
33-
assert qc.gates[7].controls == [0] and qc.gates[7].targets[0] == 2
34-
35-
# Check bit-flip encoding CNOTs for second block
36-
assert qc.gates[8].name == "CNOT"
37-
assert qc.gates[8].controls == [3] and qc.gates[8].targets[0] == 4
38-
assert qc.gates[9].name == "CNOT"
39-
assert qc.gates[9].controls == [3] and qc.gates[9].targets[0] == 5
40-
41-
# Check bit-flip encoding CNOTs for third block
42-
assert qc.gates[10].name == "CNOT"
43-
assert qc.gates[10].controls == [6] and qc.gates[10].targets[0] == 7
44-
assert qc.gates[11].name == "CNOT"
45-
assert qc.gates[11].controls == [6] and qc.gates[11].targets[0] == 8
19+
# Check CNOTs in each block
20+
expected_blocks = [(0, 1), (0, 2), (3, 4), (3, 5), (6, 7), (6, 8)]
21+
actual_blocks = [(g.controls[0], g.targets[0]) for g in cnot_gates]
22+
for pair in expected_blocks:
23+
assert pair in actual_blocks, f"CNOT {pair} not found in circuit."

0 commit comments

Comments
 (0)