Skip to content

Commit

Permalink
chore: Use pre-commit, update justfile and fix lints (#329)
Browse files Browse the repository at this point in the history
Changes are a bit messy since enabling pre-commit required fixing some
lints.

The main changes are:
- `justfile`: Brought in the simpler recipes from hugr
- `.pre-commit-config.yaml`: Configured all the checks (Except for mypy,
since we are missing typing info on the bindings).

Closes #318 (although I'll also update the ci checks workflow at some
point)
  • Loading branch information
aborgna-q authored May 2, 2024
1 parent 15904f9 commit c002846
Show file tree
Hide file tree
Showing 27 changed files with 673 additions and 362 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source_url "https://raw.githubusercontent.com/cachix/devenv/d1f7b48e35e6dee421cfd0f51481d17f77586997/direnvrc" "sha256-YBzqskFZxmNb3kYVoKD9ZixoPXJh1C9ZvTLGFRkauZ0="

use devenv
use devenv
46 changes: 0 additions & 46 deletions .github/pre-commit

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Continuous integration
on:
push:
branches:
- main
- main
pull_request:
branches:
- main
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/drop-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ jobs:
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Cleanup
run: |
gh extension install actions/gh-actions-cache
REPO=${{ github.repository }}
BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"
echo "Fetching list of cache key"
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 )
## Setting this to not fail the workflow while deleting cache keys.
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ devenv.local.nix

# direnv
.direnv
.pre-commit-config.yaml

# Coverage reports
.coverage
coverage.xml
lcov.info

# python hypothesis testing
.hypothesis
.hypothesis
94 changes: 94 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 # Use the ref you want to point at
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: check-toml
- id: check-vcs-permalinks
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
exclude: |
(?x)^(
test_files/.*|
.*.snap|
.*.snap.new|
.release-please-manifest.json
)$
- id: trailing-whitespace
exclude: |
(?x)^(
test_files/.*|
.*.snap|
.*.snap.new
)$
- id: fix-byte-order-marker
- id: mixed-line-ending
# Python-specific
- id: check-ast
- id: check-docstring-first
- id: debug-statements

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format

# The bindings for `tket2-py` do not define their types, so we need to ignore mypy for them.
# This should be re-enabled once we add .pyi files.
#
#- repo: https://github.com/pre-commit/mirrors-mypy
# rev: v1.9.0
# hooks:
# - id: mypy
# additional_dependencies: [pydantic]

- repo: local
hooks:
- id: cargo-fmt
name: cargo format
description: Format rust code with `cargo fmt`.
entry: poetry run -- cargo fmt --all -- --check
language: system
files: \.rs$
pass_filenames: false
- id: cargo-check
name: cargo check
description: Check rust code with `cargo check`.
entry: poetry run -- cargo check --all --all-features --workspace
language: system
files: \.rs$
pass_filenames: false
- id: cargo-test
name: cargo test
description: Run tests with `cargo test`.
entry: poetry run -- cargo test --all-features --workspace
language: system
files: \.rs$
pass_filenames: false
- id: cargo-clippy
name: cargo clippy
description: Run clippy lints with `cargo clippy`.
entry: poetry run -- cargo clippy --all-features --all-targets --workspace -- -D warnings
language: system
files: \.rs$
pass_filenames: false
- id: cargo-doc
name: cargo doc
description: Generate documentation with `cargo doc`.
entry: poetry run -- cargo doc --no-deps --all-features --workspace
language: system
files: \.rs$
pass_filenames: false
- id: py-test
name: pytest
description: Run python tests
entry: poetry run -- sh -c "maturin develop && pytest --cov=./ --cov-report=html"
language: system
files: \.py$
pass_filenames: false
82 changes: 37 additions & 45 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,71 +28,68 @@ shell by setting up [direnv](https://devenv.sh/automatic-shell-activation/).

To setup the environment manually you will need:

- Rust 1.75+: https://www.rust-lang.org/tools/install
- Just: https://just.systems/
- Rust `>=1.75`: https://www.rust-lang.org/tools/install
- Poetry `>=1.8`: https://python-poetry.org/

- Poetry: https://python-poetry.org/


Simply run `poetry shell` to activate an environment with all the required dependencies.

## 🏃 Running the tests

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:
Once you have these installed, install the required python dependencies and setup pre-commit hooks with:

```bash
cargo test
just setup
```

Run the benchmarks with:
## 🚀 Local development using the tket2 python library

```bash
cargo bench
```

Finally, if you have rust nightly installed, you can run `miri` to detect
undefined behaviour in the code. Note that the _devenv_ shell only has rust
stable available.
If you want to use the `tket2` python library directly from the repository, you can build it with:

```bash
cargo +nightly miri test
just build
```

To run the python tests, run:
This will build the python wheels and make them available in the `target/wheels` folder.

Alternatively, you can build the package directly into a virtual environment as an editable package.
That way, you can make changes to the python code and see the changes reflected in your environment.
For this you must have `maturin` installed (you can install it with `pip install maturin`) and run:

```bash
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:
## 🏃 Running the tests

To compile and test the code, run:

```bash
ln -s .github/pre-commit $PWD/.git/hooks/pre-commit
# Or, to check before pushing instead
ln -s .github/pre-commit $PWD/.git/hooks/pre-push
just test
# or, to test only the rust code or the python code
just test rust
just test python
```

Run `just` to see all available commands.

## 💅 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.
We use `rustfmt` and `ruff` to enforce a consistent coding style. The CI will fail if the code is not formatted correctly.

To format your code, run:

```bash
# Format rust code
cargo fmt
# Format python code
ruff format .
just format
```

We also check for linting errors with `clippy`, `ruff` and `mypy`.
To check all linting and formatting, run:

```bash
just check
```

We also check for clippy warnings, which are a set of linting rules for rust. To run clippy, run:
some errors can be fixed automatically with

```bash
cargo clippy --all-targets
just fix
```

## 📈 Code Coverage
Expand All @@ -101,18 +98,13 @@ We run coverage checks on the CI. Once you submit a PR, you can review the
line-by-line coverage report on
[codecov](https://app.codecov.io/gh/CQCL/tket2/commits?branch=All%20branches).

To run the rust coverage checks locally, install `cargo-llvm-cov`, generate the report with:
To run the coverage checks locally, install `cargo-llvm-cov` by running `cargo install cargo-llvm-cov` and then run:
```bash
cargo llvm-cov --lcov > lcov.info
just coverage
```
and open it with your favourite coverage viewer. In VSCode, you can use
This will generate a coverage file that can be opened with your favourite coverage viewer. In VSCode, you can use
[`coverage-gutters`](https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters).

Similarly, to run the python coverage checks locally, install `pytest-cov` and run:
```bash
pytest --cov=./ --cov-report=xml
```

## 🌐 Contributing to tket2

We welcome contributions to tket2! Please open [an issue](https://github.com/CQCL/tket2/issues/new) or [pull request](https://github.com/CQCL/tket2/compare) if you have any questions or suggestions.
Expand All @@ -138,4 +130,4 @@ We accept the following contribution types:
- test: Adding missing tests, refactoring tests; no production code change.
- ci: CI related changes. These changes are not published in the changelog.
- chore: Updating build tasks, package manager configs, etc. These changes are not published in the changelog.
- revert: Reverting previous commits.
- revert: Reverting previous commits.
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ quantum software developers to take advantage of its state of the art
compilation for many different quantum architectures.

Circuits are represented using the HUGR IR defined in the
[quantinuum-hugr] crate. TKET2 augments Hugr with
* The [`Circuit`] trait, providing a high-level interface for working with HUGRs representing quantum circuits
* a HUGR extension with quantum operations
* A composable pass system for optimising circuits
[hugr] crate. TKET2 augments Hugr with
* The [`Circuit`] trait, providing a high-level interface for working with HUGRs representing quantum circuits
* a HUGR extension with quantum operations
* A composable pass system for optimising circuits
* A number of built-in rewrite utilities and passes for common optimisations

This crate is interoperable with [`tket1`] circuits via its
serial encoding.

[quantinuum-hugr]: https://lib.rs/crates/quantinuum-hugr
[hugr]: https://lib.rs/crates/hugr
[`Circuit`]: https://docs.rs/tket2/latest/tket2/trait.Circuit.html
[`tket1`]: https://github.com/CQCL/tket

# Using TKET2

Defining a circuit in TKET2 is currently done by using the low-level [hugr Builder] API, or by loading tket1 circuits from JSON files.

[hugr Builder]: https://docs.rs/quantinuum-hugr/latest/hugr/builder/index.html
[hugr Builder]: https://docs.rs/hugr/latest/hugr/builder/index.html

```rust
use tket2::{Circuit, Hugr};
Expand Down
4 changes: 2 additions & 2 deletions compile-rewriter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ CircuitMatchers can be generated either from ECC files generated by Quartz, or f
patterns in TK1 JSON format.

### Example use
To generate the circuit matcher that corresponds to the ECC stored in
To generate the circuit matcher that corresponds to the ECC stored in
`../test_files/T_Tdg_H_X_CX_complete_ECC_set.json`, run the following command
from this folder:
```
cargo run -- -i test_files/T_Tdg_H_X_CX_complete_ECC_set.json
```
This will generate a file called `matcher.bin` in this folder. See `cargo run -- -h` for more information.
This will generate a file called `matcher.bin` in this folder. See `cargo run -- -h` for more information.
10 changes: 0 additions & 10 deletions dev-requirements.txt

This file was deleted.

Loading

0 comments on commit c002846

Please sign in to comment.