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

feat: Move the compiled eccs to a separate package #517

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/change-filters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ rust:
python:
- *rust-core
- "tket2-py/**"
- "tket2-eccs/**"
- "pyproject.toml"
- "poetry.lock"
60 changes: 60 additions & 0 deletions .github/workflows/python-pure-wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build and publish pure python wheels
# Builds and publishes the pure wheels on pypi.
#
# This does not include the main `tket2-py` package, which is built using maturin.
# See `python-wheels.yml` for that workflow.
#
# When running on a release event or as a workflow dispatch for a tag,
# and if the tag matches `{package}-v*`,
# this workflow will publish the wheels to pypi.
# If the version is already published, pypi just ignores it.

on:
workflow_dispatch:
push:
branches:
- main
release:
types:
- published

jobs:
build-publish:
name: Build and publish wheels
runs-on: ubuntu-latest
strategy:
matrix:
package:
- 'tket2-eccs'

steps:
- uses: actions/checkout@v4
- name: Run sccache-cache
uses: mozilla-actions/[email protected]
- name: Install poetry
run: pipx install poetry
- name: Set up Python '3.10'
uses: actions/setup-python@v5
with:
python-version: '3.10'
cache: "poetry"

- name: Build sdist and wheels
run: |
cd ${{ matrix.package }}
poetry build -o ../dist

- name: Upload the built packages as artifacts
uses: actions/upload-artifact@v4
with:
name: build-${{ matrix.package }}-sdist
path: |
dist/*.tar.gz
dist/*.whl

- name: Publish to PyPI
if: ${{ (github.event_name == 'release' && github.ref_type == 'tag' && startsWith(github.ref, format('refs/tags/{0}-v', matrix.package)) ) || (github.event_name == 'workflow_dispatch' && github.ref_type == 'tag' && startsWith(github.ref, format('refs/tags/{0}-v', matrix.package)) ) }}
run: |
cd ${{ matrix.package }}
poetry config pypi-token.pypi ${{ secrets.PYPI_PUBLISH }}
poetry publish --dist-dir ../dist --skip-existing
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ repos:
- id: py-test
name: pytest
description: Run python tests
entry: poetry run -- sh -c "maturin develop && pytest --cov=./ --cov-report=html"
# Ensure that we are using the local version of `tket2-eccs` and not the one from PyPI
entry: poetry run -- sh -c "poetry install -C tket2-eccs && maturin develop && pytest --cov=./ --cov-report=html"
language: system
files: \.py$
pass_filenames: false
3 changes: 2 additions & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"tket2-py": "0.1.0a4"
"tket2-py": "0.1.0a4",
"tket2-eccs": "0.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is correct because it matches the initial version in release-please-config.json. The robot will do the right thing here.

}
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ urlencoding = "2.1.2"
webbrowser = "1.0.0"
cool_asserts = "2.0.3"
zstd = "0.13.2"

[profile.release.package.tket2-py]
# Some configurations to reduce the size of tket2 wheels
strip = true
8 changes: 6 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ check:

# Compile the wheels for the python package.
build:
#!/usr/bin/env bash
set -euo pipefail
# Ensure that we are using the local version of `pytket-eccs`
poetry install -C tket2-eccs
poetry run -- maturin build --release

# Run all the tests.
test language="[rust|python]" : (_run_lang language \
"poetry run cargo test --all-features --workspace" \
"poetry run maturin develop && poetry run pytest"
"poetry install -C tket2-eccs && poetry run maturin develop && poetry run pytest"
)

# Auto-fix all clippy warnings.
Expand All @@ -37,7 +41,7 @@ format language="[rust|python]": (_run_lang language \
# Generate a test coverage report.
coverage language="[rust|python]": (_run_lang language \
"poetry run -- cargo llvm-cov --lcov > lcov.info" \
"poetry run -- maturin develop && poetry run pytest --cov=./ --cov-report=html"
"poetry install -C tket2-eccs && poetry run -- maturin develop && poetry run pytest --cov=./ --cov-report=html"
)

# Load a shell with all the dependencies installed
Expand Down
15 changes: 14 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "tket2-py"
version = "0.1.0a4"
description = "pytket extension for the tket 2 compiler"
description = "Quantinuum's TKET2 Quantum Compiler"
classifiers = [
"Environment :: Console",
"Programming Language :: Python :: 3.10",
Expand All @@ -25,10 +25,12 @@ packages = [{ include = "tket2-py" }]

[tool.poetry.dependencies]
python = "^3.10"

# Note: Be sure to update the dependency versions in [project.dependencies] as well
#
# Poetry does not currently follow PEP 621, it will be supported on poetry 2
# https://github.com/python-poetry/poetry/issues/3332
tket2_eccs = { path = "tket2-eccs", develop = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks strange. I think the [project] section below is what gets published to pypi, and this is what is used when you are pointing to the git repo

Copy link
Collaborator Author

@aborgna-q aborgna-q Jul 31, 2024

Choose a reason for hiding this comment

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

Yes, this is abusing the inconsistent dependency definitions (see PR linked in the comment) to work around not being able to define local path + pypi dependencies.

poetry uses tool.poetry.dependencies to define the dependency, so we tell it to use the local project (develop = true makes it so changes in tket2_eccs get reflected in the poetry env)

When the wheels get built however their metadata is computed using project.dependencies, so we reference the (currently unpublished) package there.

pytket = "1.30.0"
hugr = "^0.5.0"

Expand Down Expand Up @@ -68,12 +70,16 @@ maintainers = [
{ name = "TKET development team", email = "[email protected]" },
]
version = "0.1.0a4"
description = "pytket extension for the tket 2 compiler"
description = "Quantinuum's TKET2 Quantum Compiler"
requires-python = ">=3.10"
license = { file = "LICENCE" }

# Note: Be sure to update the dependency versions in [tool.poetry.dependencies] as well
dependencies = ['pytket >= 1.29.2, < 2', 'hugr >= 0.5.0, < 0.6']
dependencies = [
'pytket >= 1.29.2, < 2',
'hugr >= 0.5.0, < 0.6',
'tket2_eccs >= 0.1.0, < 0.2',
]

[project.urls]
homepage = "https://github.com/CQCL/tket2"
Expand Down
9 changes: 9 additions & 0 deletions release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
"draft": false,
"prerelease": false,
"draft-pull-request": true
},
"tket2-eccs": {
"release-type": "python",
"component": "tket2-eccs",
"package-name": "tket2_eccs",
"include-component-in-tag": true,
"draft": false,
"prerelease": false,
"draft-pull-request": true
}
},
"changelog-sections": [
Expand Down
2 changes: 1 addition & 1 deletion scripts/compile-test-eccs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ done
# Additional hard-coded step:
# The python package contains a copy of the nam_6_3 ecc,
# which must be manually copied.
PY_ECCS_DIR="$DIR/../tket2-py/tket2/data"
PY_ECCS_DIR="$DIR/../tket2-eccs/src/tket2_eccs/data"
nam_6_3="$ECCS_DIR/nam_6_3.rwr"
PY_NAM_6_3="$PY_ECCS_DIR/nam_6_3.rwr"

Expand Down
Binary file added tket2-eccs/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions tket2-eccs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Python wheel artifacts
dist
1 change: 1 addition & 0 deletions tket2-eccs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
36 changes: 36 additions & 0 deletions tket2-eccs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# tket2-eccs

[![pypi][]](https://pypi.org/project/tket2/)
[![codecov][]](https://codecov.io/gh/CQCL/tket2)
[![py-version][]](https://pypi.org/project/tket2/)

[codecov]: https://img.shields.io/codecov/c/gh/CQCL/tket2?logo=codecov
[py-version]: https://img.shields.io/pypi/pyversions/tket2
[pypi]: https://img.shields.io/pypi/v/tket2

This is an auxiliary Python package containing compiler rewrite sets used for `tket2`'s optimisation passes.

This package is intended to be used as an internal dependency for `tket2`.
See https://pypi.org/project/tket2/ for the main package.


## Install

TKET2-eccs can be installed via `pip`.

```sh
pip install tket2_eccs
```

## Development

See [DEVELOPMENT.md] information on how to develop and contribute to this package.

[DEVELOPMENT.md]: https://github.com/CQCL/tket2/blob/main/DEVELOPMENT.md


## License

This project is licensed under Apache License, Version 2.0 ([LICENCE][] or http://www.apache.org/licenses/LICENSE-2.0).

[LICENCE]: https://github.com/CQCL/tket2/blob/main/LICENCE
7 changes: 7 additions & 0 deletions tket2-eccs/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions tket2-eccs/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[tool.poetry]
name = "tket2-eccs"
version = "0.1.0"
description = "Precompiled rewrite sets for the tket 2 compiler"
classifiers = [
"Environment :: Console",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: Apache Software License",
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX :: Linux",
"Operating System :: Microsoft :: Windows",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering",
]
authors = ["TKET development team <[email protected]>"]
maintainers = ["TKET development team <[email protected]>"]
include = ["pyproject.toml"]
license = "Apache-2.0"
readme = "README.md"

packages = [{ include = "tket2_eccs", from = "src" }]

[tool.poetry.dependencies]
python = "^3.10"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[project]
name = "tket2_eccs"
classifiers = [
"Environment :: Console",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: Apache Software License",
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX :: Linux",
"Operating System :: Microsoft :: Windows",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering",
]
authors = [
{ name = "TKET development team", email = "[email protected]" },
]
maintainers = [
{ name = "TKET development team", email = "[email protected]" },
]
version = "0.1.0"
description = "Precompiled rewrite sets for the tket 2 compiler"
requires-python = ">=3.10"
license = { file = "LICENCE" }

dependencies = []

[project.urls]
homepage = "https://github.com/CQCL/tket2"
repository = "https://github.com/CQCL/tket2"
27 changes: 27 additions & 0 deletions tket2-eccs/src/tket2_eccs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""`tket2-eccs` is a Python package containing compiler rewrite sets used for `tket2`'s optimisation passes.

This package is intended to be used as an internal dependency for `tket2`.
"""

# This is updated by our release-please workflow, triggered by this
# annotation: x-release-please-version
__version__ = "0.1.0"

from importlib import resources
from pathlib import Path


def nam_6_3() -> Path:
"""Get the path to the included `nam_6_3.rwr` rewriter.

This is a rewrite set obtained from the equivalence classes of circuits with
up to 3 qubits and 6 gates on the Nam gateset, as
generated by TASO.

See https://dl.acm.org/doi/10.1145/3341301.3359630
"""
with resources.as_file(
resources.files("tket2_eccs").joinpath("data/nam_6_3.rwr")
) as r:
rewriter = Path(r)
return rewriter
Empty file.
4 changes: 2 additions & 2 deletions tket2-py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ See the [Getting Started][getting-started] guide and the other [examples].

## Development

This package uses [pyo3](https://pyo3.rs/v0.16.4/) and
This package uses [pyo3](https://pyo3.rs/latest/) and
[maturin](https://github.com/PyO3/maturin) to bind TKET2 functionality to
python as the `tket2` package.

Expand All @@ -56,4 +56,4 @@ See [DEVELOPMENT.md] for more information.

This project is licensed under Apache License, Version 2.0 ([LICENCE][] or http://www.apache.org/licenses/LICENSE-2.0).

[LICENCE]: ./LICENCE
[LICENCE]: https://github.com/CQCL/tket2/blob/main/LICENCE
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer the other way, if LICENSE ever changed this would be wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I forgot to mention this. We need an absolute path. The LICENCE link on pypi's README is currently broken due to this.
https://pypi.org/project/tket2/

13 changes: 8 additions & 5 deletions tket2-py/tket2/passes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pathlib import Path
from typing import Optional
from importlib import resources

from pytket import Circuit
from pytket.passes import CustomPass, BasePass
Expand Down Expand Up @@ -49,10 +48,14 @@
`log_dir` and `rebase` are optional and will be passed on to the Badger
optimiser if provided."""
if rewriter is None:
with resources.as_file(
resources.files("tket2").joinpath("data/nam_6_3.rwr")
) as r:
rewriter = Path(r)
try:
import tket2_eccs
except ImportError:
raise ValueError(

Check warning on line 54 in tket2-py/tket2/passes.py

View check run for this annotation

Codecov / codecov/patch

tket2-py/tket2/passes.py#L53-L54

Added lines #L53 - L54 were not covered by tests
"The default rewriter is not available. Please specify a path to a rewriter or install tket2-eccs."
)

rewriter = tket2_eccs.nam_6_3()
opt = optimiser.BadgerOptimiser.load_precompiled(rewriter)

def apply(circuit: Circuit) -> Circuit:
Expand Down
Loading
Loading