Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Restructure the python code #211

Merged
merged 16 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@ jobs:
with:
components: rustfmt, clippy
- uses: mozilla-actions/[email protected]
- name: Check formatting
- name: Check rust formatting
run: cargo fmt -- --check
- name: Check python formatting
uses: chartboost/ruff-action@v1
with:
args: format --check
- name: Run clippy
run: cargo clippy --all-targets --all-features --workspace -- -D warnings
- name: Build docs
run: cargo doc --no-deps --all-features
env:
RUSTDOCFLAGS: "-Dwarnings"
- name: Python lints
uses: chartboost/ruff-action@v1
with:
args: check

benches:
# Not required, we can ignore it for the merge queue check.
Expand Down Expand Up @@ -101,14 +109,11 @@ jobs:
cache: 'pip'
- name: Build pyo3 bindings
run: |
pip install -r requirements.txt
cd pyrs
pip install -r dev-requirements.txt
maturin build
pip install ../target/wheels/*.whl
pip install target/wheels/*.whl
- name: Test pyo3 bindings
run: |
cd pyrs
pytest
run: pytest

coverage:
if: github.event_name != 'merge_group'
Expand Down Expand Up @@ -149,15 +154,12 @@ jobs:
cache: 'pip'
- name: Build pyo3 bindings
run: |
pip install -r requirements.txt
pip install -r dev-requirements.txt
pip install pytest-cov
cd pyrs
maturin build
pip install ../target/wheels/*.whl
pip install target/wheels/*.whl
- name: Run python tests with coverage instrumentation
run: |
cd pyrs
pytest --cov=./ --cov-report=xml
run: pytest --cov=./ --cov-report=xml
- name: Upload python coverage to codecov.io
uses: codecov/codecov-action@v3
with:
Expand Down
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Build artifacts
/target
tket2-py/target/

# Lock files
Cargo.lock
pyrs/target/
poetry.lock

# Python caches
__pycache__
.ipynb_checkpoints

# Devenv
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

81 changes: 14 additions & 67 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,81 +1,28 @@
[package]
name = "tket2"
version = "0.1.0"
edition = "2021"

[lib]
bench = false
name = "tket2"
path = "src/lib.rs"

[profile.release]
lto = "thin"

[dependencies]
lazy_static = "1.4.0"
cgmath = "0.18.0"
num-rational = "0.4"
num-complex = { version = "0.4", optional = true }
tket-json-rs = { workspace = true }
rayon = "1.5"
thiserror = "1.0.28"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
downcast-rs = "1.2.0"
priority-queue = "1.3.0"
smol_str = "0.2.0"
typetag = "0.2.8"
itertools = { workspace = true }
petgraph = { version = "0.6.3", default-features = false }
serde_yaml = "0.9.22"
# portmatching = { version = "0.2.0", optional = true, features = ["serde"]}
portmatching = { optional = true, git = "https://github.com/lmondada/portmatching", rev = "738c91c", features = [
"serde",
] }
derive_more = "0.99.17"
quantinuum-hugr = { workspace = true }
portgraph = { workspace = true, features = ["serde"] }
pyo3 = { workspace = true, optional = true, features = ["multiple-pymethods"] }
strum_macros = "0.25.2"
strum = "0.25.0"
fxhash = "0.2.1"
rmp-serde = { version = "1.1.2", optional = true }
delegate = "0.10.0"
csv = { version = "1.2.2" }
chrono = { version = "0.4.30" }
bytemuck = "1.14.0"
stringreader = "0.1.1"
crossbeam-channel = "0.5.8"
tracing = { workspace = true }

[features]
pyo3 = [
"dep:pyo3",
"tket-json-rs/pyo3",
"portgraph/pyo3",
"quantinuum-hugr/pyo3",
]
portmatching = ["dep:portmatching", "dep:rmp-serde"]

[dev-dependencies]
rstest = "0.18.1"
criterion = { version = "0.5.1", features = ["html_reports"] }
webbrowser = "0.8.10"
urlencoding = "2.1.2"

[[bench]]
name = "bench_main"
harness = false

[workspace]
resolver = "2"
members = ["tket2", "tket2-py", "compile-rewriter", "taso-optimiser"]
default-members = ["tket2"]

members = ["pyrs", "compile-rewriter", "taso-optimiser"]
[workspace.package]
version = "0.1.0"
rust-version = "1.70"
edition = "2021"
homepage = "https://github.com/CQCL/tket2"
license-file = "LICENCE"
# authors

[workspace.dependencies]

tket2 = { path = "./tket2" }
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "0beb165" }
portgraph = { version = "0.10" }
pyo3 = { version = "0.20" }
itertools = { version = "0.11.0" }
tket-json-rs = "0.2.0"
tracing = "0.1.37"
portmatching = { git = "https://github.com/lmondada/portmatching", rev = "738c91c", features = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems unrelated?

"serde",
] }
44 changes: 18 additions & 26 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,24 @@ devenv shell
All the required dependencies should be available. You can automate loading the
shell by setting up [direnv](https://devenv.sh/automatic-shell-activation/).

### Manual setup
### Poetry setup

To setup the environment manually you will need:

- Rust: https://www.rust-lang.org/tools/install
- Rust 1.70+: https://www.rust-lang.org/tools/install

- Python 3.10+: https://www.python.org/downloads/
- Poetry: https://python-poetry.org/

It is advisable to use a virtual environment for keeping the python
environment isolated, see
[`venv`](https://docs.python.org/3/tutorial/venv.html) for more details.

Install the python development dependencies with:

```bash
pip install -r pyrs/dev-requirements.txt
```


You can use the git hook in [`.github/pre-commit`](.github/pre-commit) to automatically run the test and check formatting before committing.
To install it, run:

```bash
ln -s .github/pre-commit .git/hooks/pre-commit
# Or, to check before pushing instead
ln -s .github/pre-commit .git/hooks/pre-push
```
Simply run `poetry shell` to activate an environment with all the required dependencies.

## 🏃 Running the tests

To compile and test the rust code, run:
The repository root contains a Justfile with the most common development tasks.
Run `just` to see a list.

To manually compile and test the rust code, run:

```bash
cargo build
cargo test
```

Expand All @@ -78,11 +62,19 @@ cargo +nightly miri test
To run the python tests, run:

```bash
cd pyrs
maturin develop
pytest
```

You can use the script in [`.github/pre-commit`](.github/pre-commit) to run the test and formatting required by our CI.
To automatically check that before each commit, install it as a hook with:

```bash
ln -s .github/pre-commit .git/hooks/pre-commit
# Or, to check before pushing instead
ln -s .github/pre-commit .git/hooks/pre-push
```

## 💅 Coding Style

The rustfmt tool is used to enforce a consistent rust coding style. The CI will fail if the code is not formatted correctly. Python code is formatted with black.
Expand All @@ -93,7 +85,7 @@ To format your code, run:
# Format rust code
cargo fmt
# Format python code
black .
ruff format .
```

We also check for clippy warnings, which are a set of linting rules for rust. To run clippy, run:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Version 2 of the TKET compiler.
## Features

- `pyo3`
This optional feature enables some python bindings via pyo3. See the `pyrs` folder for more.
This optional feature enables some python bindings via pyo3. See the `tket2-py` folder for more.

- `portmatching`
This enables pattern matching using the `portmatching` crate.
Expand Down
9 changes: 6 additions & 3 deletions compile-rewriter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[package]
name = "compile-matcher"
version = "0.1.0"
edition = "2021"
edition = { workspace = true }
version = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
license-file = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.2", features = ["derive"] }
tket2 = { path = "../", features = ["portmatching"] }
tket2 = { workspace = true, features = ["portmatching"] }
quantinuum-hugr = { workspace = true }
itertools = { workspace = true }
5 changes: 3 additions & 2 deletions pyrs/dev-requirements.txt → dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Use common dependencies
# Runtime requirements
-r requirements.txt

# Development requirements
maturin # Build wheels
black # Code formatting
pytest # Unit testing
ruff # Code formatting
pytket # TKET1
jupyterlab # For running the examples
graphviz # Visualisation of Hugrs in the notebooks
2 changes: 1 addition & 1 deletion devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
enable = true;

venv.enable = true;
venv.requirements = "-r ${config.env.DEVENV_ROOT}/pyrs/dev-requirements.txt";
venv.requirements = "-r ${config.env.DEVENV_ROOT}/tket2-py/dev-requirements.txt";
};

# https://devenv.sh/pre-commit-hooks/
Expand Down
1 change: 0 additions & 1 deletion ext/symengine.rs
Submodule symengine.rs deleted from 05ae9c
32 changes: 28 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
# List the available commands
help:
@just --list --justfile {{justfile()}}

# Run all the rust tests
test:
cargo test
cargo test --all-features

# Auto-fix all clippy warnings
fix:
cargo clippy --fix --allow-staged
cargo clippy --all-targets --all-features --workspace --fix --allow-staged

# Build the python package wheels
pybuild:
maturin build --release

# Build the python package for local development
pydevelop:
maturin develop

# Run the python tests
pytest: pydevelop
pytest

# Run the pre-commit checks
check:
./.github/pre-commit

ptest:
(cd pyrs && maturin develop && pytest)
# Format the code
format:
cargo fmt
ruff format .
46 changes: 46 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[tool.poetry]
name = "tket2-py"
version = "0.1.0"
description = "pytket extension for the tket 2 compiler"
classifiers = [] # TODO
authors = [] # TODO
maintainers = [] # TODO
include = ["pyproject.toml"]
license = "Apache-2.0"
readme = "README.md"

packages = [{ include = "tket2-py" }]

[tool.poetry.dependencies]
python = "^3.10,<3.12"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not 3.12?


[tool.poetry.dev-dependencies]
maturin = "^1.3.0"
pytket = "*"
pytest = "^7.1.2"
ruff = "^0.1.3"

[build-system]
requires = ["maturin~=1.3"]
build-backend = "maturin"

[project]
name = "tket2"
version = "0.1.0"
description = "pytket extension for the tket 2 compiler"
authors = [] # TODO
classifiers = [] # TODO
requires-python = ">=3.10"
license = "Apache-2.0"

[project.urls]
homepage = "https://github.com/CQCL/tket2"
repository = "https://github.com/CQCL/tket2"

[tool.maturin]
manifest-path = "tket2-py/Cargo.toml"
python-source = "tket2-py"

[tool.pytest.ini_options]
# Lark throws deprecation warnings for `src_parse` and `src_constants`.
filterwarnings = "ignore::DeprecationWarning:lark.*"
Loading