Skip to content

Commit

Permalink
Calculate the exact distance of binary codes with brute force in cyth…
Browse files Browse the repository at this point in the history
…on (#211)

* add fast distance calculation for CSS codes with <= 64 qubits

* remove exact distance from surface code

* add pyx file forgotten before, also add > 64-qubit distance calculation

* choose distance method based on qubit number

* more distance calculations

* use c++

* revert to using c and fix array bug

* better test

* only brute-force distance up to n=64

* add cython distance calculation

* minor cleanup of _distance.pyx

* add type hint

* clean up build.py

* nit rearrangement

* typing fix

* rename functions

* ignore cython build artifacts

* fix typo

* fix typo

* typing fix and remove test.py file

* fix coverage

* modify error message to accomodate classical codes

* rename build.py to build-cython.py

* formatting fix

* add cython-lint options to pyproject.toml

* nit variable rename

* add calculation of distance for QuditCodes on <= 32 qubits

* fix coverage

* fix check for number of qubits

* remove debugging artifact

* typo fix

* one more typo

* rename get_gistance_subcode_64 to get_distance_sector_xz_64

* ctzll for gray code counter

* remove leading underscore from functions not accessible to python

* make rows_to_uint64 return a 2D array

* typecast

* type fix

* add comments

* add distance calculation for classical codes with arbitrary block lengths

* fix bug in QuditCode distance calculation

* add distance calculation for quantum codes with arbitrary block length

* some code rearranging

* balanced surface/toric codes

* Revert "balanced surface/toric codes"

This reverts commit 8d14312.

* properly balance surface and toric codes

* split off distance calculations by block length

* fix QuditCode distance calculation

* clean up weight_func construction

* renaming and comments

* add comment

* bugfix in QuditCode distance

* rename function

* add cython-lint to pyproject.toml

* more cleanup and helpful commenting

* add 128-bit classical code distance

* word

* minor bugfix for balanced CSS codes and warnings about runtime

* workflow update

* add cython language_level

* move language_level:3 and add comment

* increase timeout limit for the installation check

* fix string matching
  • Loading branch information
perlinm authored Jan 30, 2025
1 parent 042b9e2 commit 746561e
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 44 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
push:
branches: [ main ]
pull_request:
types: [ opened, synchronize, reopened, ready_for_review ] # these are the defaults, plus ready_for_review
types: [ opened, synchronize, reopened, ready_for_review ] # defaults plus ready_for_review
branches: [ main ]
workflow_dispatch:
workflow_call:
Expand All @@ -17,7 +17,7 @@ jobs:
name: Installation check
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
timeout-minutes: 2
timeout-minutes: 3
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.11
Expand All @@ -27,7 +27,7 @@ jobs:
- uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}
key: ${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'build-cython.py') }}
- name: Install package and dependencies
run: |
python3 -m pip install --upgrade pip
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ __pycache__

# sphinx documentation
docs/build

# cython build artifacts
build
*.c
*.so
32 changes: 32 additions & 0 deletions build-cython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
import os
import shutil
from distutils.command.build_ext import build_ext
from distutils.core import Distribution, Extension

import numpy
from Cython.Build import cythonize


def build_cython() -> None:
extension = Extension(
"*",
["*/**/*.pyx"],
include_dirs=[numpy.get_include()],
extra_compile_args=["-O3", "-march=native", "-Wall"],
)
ext_modules = cythonize(extension, compiler_directives={"language_level": "3"})
distribution = Distribution(dict(ext_modules=ext_modules))

cmd = build_ext(distribution)
cmd.ensure_finalized()
cmd.run()

# copy *.so files to their respective *.pyx directories
for output in cmd.get_outputs():
relative_extension = os.path.relpath(output, cmd.build_lib)
shutil.copyfile(output, relative_extension)


if __name__ == "__main__":
build_cython()
9 changes: 8 additions & 1 deletion checks/pytest_.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

import checks_superstaq

EXCLUDE = ("checks/*.py", "experiments/*.py", "*/__init__.py", "docs/source/conf.py")
EXCLUDE = (
"*/__init__.py",
"checks/*.py",
"examples/*.py",
"experiments/*.py",
"build-cython.py",
"docs/source/conf.py",
)

if __name__ == "__main__":
exit(checks_superstaq.pytest_.run(*sys.argv[1:], exclude=EXCLUDE))
12 changes: 10 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[build-system]
requires = ["poetry-core>=1.0.0"]
requires = ["poetry-core>=1.0.0", "cython", "setuptools", "numpy"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.build]
generate-setup-file = false
script = "build-cython.py"

[tool.poetry]
name = "qLDPC"
version = "0.0.24"
Expand All @@ -23,7 +27,7 @@ classifiers = [
"Topic :: Scientific/Engineering :: Mathematics",
"Topic :: Scientific/Engineering :: Physics",
]
include = ["qldpc/py.typed"]
include = ["qldpc/py.typed", { path = "qldpc/**/*.so", format = "wheel" }]

[tool.poetry.dependencies]
python = "^3.10"
Expand All @@ -41,6 +45,7 @@ stim = ">=1.14.0"
sympy = ">=1.12"

checks-superstaq = { version = ">=0.5.34", optional = true }
cython-lint = { version = ">=0.16.6", optional = true }

[tool.poetry.extras]
dev = ["checks-superstaq"]
Expand Down Expand Up @@ -75,3 +80,6 @@ exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"]

[tool.coverage.run]
include = ["./*"]

[tool.cython-lint]
max-line-length = 100
Loading

0 comments on commit 746561e

Please sign in to comment.