Skip to content

Commit

Permalink
🚨 also lint code construction
Browse files Browse the repository at this point in the history
  • Loading branch information
burgholzer committed Dec 22, 2023
1 parent b95e4b1 commit 88c7c99
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ unsafe-fixes = true

[tool.ruff.lint]
exclude = [
"*/code_construction/*",
"scripts/*",
"*/cc_decoder/plots.py"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from __future__ import annotations

import json
import os
import subprocess
from pathlib import Path
from typing import TYPE_CHECKING, Any

import ldpc.code_util
Expand Down Expand Up @@ -39,7 +39,7 @@ def run_checks_scipy(
and is_all_zeros((sd_3 * sd_4).todense() % 2)
):
msg = "Error generating 4D code, boundary maps do not square to zero"
raise Exception(msg)
raise RuntimeError(msg)


def generate_4d_product_code(
Expand Down Expand Up @@ -99,18 +99,15 @@ def generate_3d_product_code(

if not (is_all_zeros(d_1 @ d_2 % 2) and is_all_zeros(d_2 @ d_3 % 2)):
msg = "Error generating 3D code, boundary maps do not square to zero"
raise Exception(msg)
raise RuntimeError(msg)

return d_1, d_2, d_3


def create_outpath(codename: str) -> str:
"""Create output path for code files."""
path = f"generated_codes/{codename}/"

if not os.path.exists(path):
os.makedirs(path)

Path(path).mkdir(parents=True, exist_ok=True)
return path


Expand Down Expand Up @@ -140,15 +137,15 @@ def save_code(
def run_compute_distances(codename: str) -> None:
"""Run compute distances bash script."""
path = "generated_codes/" + codename
subprocess.run(["bash", "compute_distances.sh", path], check=False)
subprocess.run(["bash", "compute_distances.sh", path], check=False) # noqa: S603, S607


def _compute_distances(hx: NDArray[np.int32], hz: NDArray[np.int32], codename: str) -> None:
run_compute_distances(codename)
code_dict: Any = {}
m, n = hx.shape
_m, n = hx.shape
code_k = n - mod2.rank(hx) - mod2.rank(hz)
with open(f"generated_codes/{codename}/info.txt") as f:
with Path(f"generated_codes/{codename}/info.txt").open() as f:
code_dict = dict(
line[: line.rfind("#")].split(" = ") for line in f if not line.startswith("#") and line.strip()
)
Expand All @@ -160,8 +157,7 @@ def _compute_distances(hx: NDArray[np.int32], hz: NDArray[np.int32], codename: s
code_dict["dMX"] = int(code_dict["dMX"])
code_dict["dMZ"] = int(code_dict["dMZ"])

print("Code properties:", code_dict)
with open(f"generated_codes/{codename}/code_params.txt", "w") as file:
with Path(f"generated_codes/{codename}/code_params.txt").open("w") as file:
file.write(json.dumps(code_dict))


Expand Down Expand Up @@ -214,7 +210,7 @@ def create_code(
# Perform checks
if np.any(hz_t @ mz_t % 2) or np.any(hx @ hz_t % 2) or np.any(mx @ hx % 2):
msg = "err"
raise Exception(msg)
raise RuntimeError(msg)
save_code(hx, hz, mx, mz, codename, lx=None, lz=None)

if compute_logicals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from __future__ import annotations

import json
import os
import subprocess
from pathlib import Path
from typing import TYPE_CHECKING, Any

import numpy as np
Expand Down Expand Up @@ -35,7 +35,7 @@ def run_checks_scipy(d_1: csr_matrix, d_2: csr_matrix, d_3: csr_matrix, d_4: csr
"""Run checks on the boundary maps."""
if not (sparse_all_zeros(d_1 @ d_2) and sparse_all_zeros(d_2 @ d_3) and sparse_all_zeros(d_3 @ d_4)):
msg = "Error generating 4D code, boundary maps do not square to zero"
raise Exception(msg)
raise RuntimeError(msg)


def generate_4d_product_code(
Expand Down Expand Up @@ -103,18 +103,15 @@ def generate_3d_product_code(

if not (sparse_all_zeros(d_1 @ d_2) and sparse_all_zeros(d_2 @ d_3)):
msg = "Error generating 3D code, boundary maps do not square to zero"
raise Exception(msg)
raise RuntimeError(msg)

return d_1, d_2, d_3 # mx, hx, hzT # hx, hzT, mzT


def create_outpath(codename: str) -> str:
"""Create output path for code."""
path = f"/codes/generated_codes/{codename}/"

if not os.path.exists(path):
os.makedirs(path)

Path(path).mkdir(parents=True, exist_ok=True)
return path


Expand Down Expand Up @@ -149,14 +146,14 @@ def save_code(
def run_compute_distances(codename: str) -> None:
"""Run compute distances bash script."""
path = "/codes/generated_codes/" + codename
subprocess.run(["bash", "compute_distances_3D.sh", path], check=False)
subprocess.run(["bash", "compute_distances_3D.sh", path], check=False) # noqa: S603, S607


def _compute_distances(hx: NDArray[np.int32], hz: NDArray[np.int32], codename: str) -> None:
run_compute_distances(codename)
_, n = hx.shape
code_k = n - mod2.rank(hx) - mod2.rank(hz)
with open(f"/codes/generated_codes/{codename}/info.txt") as f:
with Path(f"/codes/generated_codes/{codename}/info.txt").open() as f:
code_dict: dict[str, Any] = dict(
line[: line.rfind("#")].split(" = ") for line in f if not line.startswith("#") and line.strip()
)
Expand All @@ -166,20 +163,19 @@ def _compute_distances(hx: NDArray[np.int32], hz: NDArray[np.int32], codename: s
code_dict["dX"] = int(code_dict["dX"])
code_dict["dZ"] = int(code_dict["dZ"])

print("Code properties:", code_dict)
with open(f"/codes/generated_codes/{codename}/code_params.txt", "w") as file:
with Path(f"/codes/generated_codes/{codename}/code_params.txt").open("w") as file:
file.write(json.dumps(code_dict))


def _store_code_params(hx: csr_matrix, hz: csr_matrix, codename: str) -> None:
"""Store code parameters in file."""
code_dict = {}
hx, hz = hx.todense(), hz.todense()
m, n = hx.shape
_m, n = hx.shape
code_k = n - mod2.rank(hx) - mod2.rank(hz)
code_dict["n"] = n
code_dict["k"] = code_k
with open(f"/codes/generated_codes/{codename}/code_params.txt", "w") as file:
with Path(f"/codes/generated_codes/{codename}/code_params.txt").open("w") as file:
file.write(json.dumps(code_dict))


Expand Down Expand Up @@ -207,7 +203,7 @@ def create_code(
hz = hz_t.transpose()
mz = mz_t.transpose()
if compute_logicals:
lx, lz = code_constructor._compute_logicals(hx.todense(), hz.todense())
lx, lz = code_constructor._compute_logicals(hx.todense(), hz.todense()) # noqa: SLF001
save_code(hx=hx, hz=hz, mz=mz, codename=codename, lx=lx, lz=lz)

if compute_distance:
Expand Down

0 comments on commit 88c7c99

Please sign in to comment.