Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pehamTom committed Sep 5, 2024
1 parent 444be7b commit 607ab62
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/mqt/qecc/codes/stabilizer_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ def __init__(
Lx: The logical X-operators.
"""
self._check_stabilizer_generators(generators)

self.n = get_n_qubits_from_pauli(generators[0])

Check warning

Code scanning / CodeQL

Overwriting attribute in super-class or sub-class

Assignment overwrites attribute n, which was previously defined in subclass [CSSCode](1). Assignment overwrites attribute n, which was previously defined in subclass [CSSCode](2).
self.generators = paulis_to_binary(generators)
self.n = get_n_qubits_from_pauli(self.generators[0])
self.symplectic_matrix = self.generators[:, :-1] # discard the phase
self.phases = self.generators[:, -1]
self.k = self.n - mod2.rank(self.generators)
Expand Down Expand Up @@ -172,6 +171,7 @@ def pauli_to_binary(p: Pauli) -> npt.NDArray:
phase = 0
if p[0] in {"+", "-"}:
phase = 0 if p[0] == "+" else 1
p = p[1:]
x_part = np.array([int(p == "X") for p in p])
z_part = np.array([int(p == "Z") for p in p])
y_part = np.array([int(p == "Y") for p in p])
Expand Down Expand Up @@ -204,6 +204,8 @@ def get_n_qubits_from_pauli(p: Pauli) -> int:
"""Get the number of qubits from a Pauli string."""
if isinstance(p, np.ndarray):
return int(p.shape[0] // 2)
if p[0] in {"+", "-"}:
return len(p) - 1
return len(p)


Expand Down
30 changes: 24 additions & 6 deletions test/python/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ def test_five_qubit_code(five_qubit_code_stabs: list[str]) -> None:
assert strings == five_qubit_code_stabs


def test_stabilizer_sign() -> None:
"""Test that (negative) signs are correctly handled in stabilizer codes."""
s = ["-ZZZZ", "-XXXX"]
code = StabilizerCode(s)
assert code.n == 4
assert code.k == 2

error = "XIII"
syndrome = code.get_syndrome(error)
assert np.array_equal(syndrome, np.array([1, 0]))


def test_no_stabilizers() -> None:
"""Test that an error is raised if no stabilizers are provided."""
with pytest.raises(InvalidStabilizerCodeError):
Expand Down Expand Up @@ -212,34 +224,40 @@ def test_invalid_pauli_strings() -> None:
def test_no_x_logical() -> None:
"""Test that an error is raised if no X logical is provided when a Z logical is provided."""
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lz=["XXII"])
StabilizerCode(["ZZZZ", "XXXX"], Lx=["XXII"])


def test_no_z_logical() -> None:
"""Test that an error is raised if no Z logical is provided when an X logical is provided."""
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lx=["ZZII"])
StabilizerCode(["ZZZZ", "XXXX"], Lz=["ZZII"])


def test_logicals_wrong_length() -> None:
"""Test that an error is raised if the logicals have the wrong length."""
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lx=["XX"], Lz=["ZZ"])
StabilizerCode(["ZZZZ", "XXXX"], Lx=["XX"], Lz=["IZZI"])
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lx=["IXXI"], Lz=["ZZ"])


def test_commuting_logicals() -> None:
"""Test that an error is raised if the logicals commute."""
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lx=["ZZII"], Lz=["XXII"])
StabilizerCode(["ZZZZ", "XXXX"], Lz=["ZZII"], Lx=["XXII"])


def test_anticommuting_logicals() -> None:
"""Test that an error is raised if the logicals anticommute with the stabilizer generators."""
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lx=["ZI II"], Lz=["XIII"])
StabilizerCode(["ZZZZ", "XXXX"], Lz=["ZIII"], Lx=["IXXI"])
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lz=["IZZI"], Lx=["XIII"])


def test_too_many_logicals() -> None:
"""Test that an error is raised if too many logicals are provided."""
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lx=["ZZII", "ZZII", "ZZII"], Lz=["XXII", "IIXX", "IIXX"])
StabilizerCode(["ZZZZ", "XXXX"], Lz=["ZZII", "ZZII", "ZZII"], Lx=["IXXI"])
with pytest.raises(InvalidStabilizerCodeError):
StabilizerCode(["ZZZZ", "XXXX"], Lz=["IZZI"], Lx=["XXII", "XXII", "XXII"])

0 comments on commit 607ab62

Please sign in to comment.