Skip to content

Commit

Permalink
Portmatching in TKET2 (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmondada authored Jul 31, 2023
1 parent e4bddbe commit 1454f8e
Show file tree
Hide file tree
Showing 15 changed files with 726 additions and 895 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "--cfg=ci_run"
MIRIFLAGS: '-Zmiri-permissive-provenance' # Required due to warnings in bitvec 1.0.1
FEATURES: "pyo3" # Features to test, ignoring the ones that require c++ bindings
FEATURES: "pyo3, portmatching" # Features to test, ignoring the ones that require c++ bindings

jobs:
check:
Expand All @@ -31,9 +31,9 @@ jobs:
- name: Check formatting
run: cargo fmt -- --check
- name: Run clippy
run: cargo clippy --all-targets -- -D warnings
run: cargo clippy --all-targets --features="$FEATURES" -- -D warnings
- name: Build docs
run: cargo doc --no-deps --features=$FEATURES
run: cargo doc --no-deps --features="$FEATURES"
env:
RUSTDOCFLAGS: "-Dwarnings"

Expand All @@ -49,7 +49,7 @@ jobs:
- name: Build benchmarks with no features
run: cargo bench --verbose --no-run --no-default-features
- name: Build benchmarks with all (non c++) features
run: cargo bench --verbose --no-run --features=$FEATURES
run: cargo bench --verbose --no-run --features="$FEATURES"

tests:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -79,8 +79,15 @@ jobs:
- name: Build with no features
run: cargo build --verbose --no-default-features
- name: Build with all (non c++) features
run: cargo build --verbose --features=$FEATURES
run: cargo build --verbose --features="$FEATURES"
- name: Tests with no features
run: cargo test --verbose --no-default-features
- name: Tests with all (non c++) features
run: cargo test --verbose --features=$FEATURES
run: cargo test --verbose --features="$FEATURES"
- name: Test pyo3 bindings
run: |
pip install -r requirements.txt
cd pyrs
maturin build
pip install ../target/wheels/*.whl
pytest
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ thiserror = "1.0.28"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
downcast-rs = "1.2.0"
portgraph = "0.7.1"
portgraph = "0.7.2"
priority-queue = "1.3.0"
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", tag = "v0.0.0-alpha.5" }
smol_str = "0.2.0"
typetag = "0.2.8"
itertools = "0.11.0"
petgraph = { version = "0.6.3", default-features = false }
serde_yaml = "0.9.22"
portmatching = { version = "0.2.0", optional = true, features = ["serde"]}
derive_more = "0.99.17"

[features]
pyo3 = ["dep:pyo3", "tket-json-rs/pyo3", "tket-json-rs/tket2ops", "portgraph/pyo3", "quantinuum-hugr/pyo3"]
tkcxx = ["dep:tket-rs", "dep:num-complex"]
portmatching = ["dep:portmatching"]

[dev-dependencies]
rstest = "0.18.1"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ This optional feature enables some python bindings via pyo3. See the `pyrs` fold
- `tkcxx`
This enables binding to TKET-1 code using [cxx](https://cxx.rs/). For this you will to set up an environment with conan. See the [tket-rs README](https://github.com/CQCL-DEV/tket-rs#readme) for more details.

- `portmatching`
This enables pattern matching using the `portmatching` crate.

## Developing TKET2

See [DEVELOPMENT.md](DEVELOPMENT.md) for instructions on setting up the development environment.
Expand Down
2 changes: 1 addition & 1 deletion pyrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.19", features = ["extension-module"] }
tket2 = { path = "../", features = ["pyo3"] }
tket2 = { path = "../", features = ["pyo3", "portmatching"] }
portgraph = { version = "0.7.1", features = ["pyo3"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion pyrs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## pyrs

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

Recommended:
Expand Down
31 changes: 11 additions & 20 deletions pyrs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
use hugr::{Hugr, HugrView};
use pyo3::create_exception;
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
use tket2::json::TKETDecode;
use tket_json_rs::circuit_json::SerialCircuit;
use tket2::portmatching::{CircuitMatcher, CircuitPattern};

create_exception!(pyrs, PyValidateError, PyException);

#[pyfunction]
fn check_soundness(c: Py<PyAny>) -> PyResult<()> {
let ser_c = SerialCircuit::_from_tket1(c);
let hugr: Hugr = ser_c.decode().unwrap();
println!("{}", hugr.dot_string());
hugr.validate()
.map_err(|e| PyValidateError::new_err(e.to_string()))
}

/// A Python module implemented in Rust.
/// The Python bindings to TKET2.
#[pymodule]
fn pyrs(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(check_soundness, m)?)?;
fn pyrs(py: Python, m: &PyModule) -> PyResult<()> {
add_patterns_module(py, m)?;
Ok(())
}

m.add("ValidateError", _py.get_type::<PyValidateError>())?;
fn add_patterns_module(py: Python, parent: &PyModule) -> PyResult<()> {
let m = PyModule::new(py, "patterns")?;
m.add_class::<CircuitPattern>()?;
m.add_class::<CircuitMatcher>()?;
parent.add_submodule(m)?;
Ok(())
}
Loading

0 comments on commit 1454f8e

Please sign in to comment.