From 53d5b151a054a357f841f1733811b3711cdfa1a8 Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Fri, 10 Nov 2023 15:30:27 +0000 Subject: [PATCH] Only require tket2/hugr pyo3 features for error bindings. --- tket2-py/Cargo.toml | 2 +- tket2-py/src/circuit.rs | 4 ++-- tket2-py/src/circuit/convert.rs | 5 +++-- tket2-py/src/pattern.rs | 2 +- tket2-py/src/pattern/portmatching.rs | 7 +++---- tket2/Cargo.toml | 7 +------ 6 files changed, 11 insertions(+), 16 deletions(-) diff --git a/tket2-py/Cargo.toml b/tket2-py/Cargo.toml index 0dc9b67d..120e0372 100644 --- a/tket2-py/Cargo.toml +++ b/tket2-py/Cargo.toml @@ -16,7 +16,7 @@ tket2 = { workspace = true, features = ["pyo3", "portmatching"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" tket-json-rs = { workspace = true, features = ["pyo3"] } -quantinuum-hugr = { workspace = true } +quantinuum-hugr = { workspace = true, features = ["pyo3"] } portgraph = { workspace = true, features = ["pyo3", "serde"] } pyo3 = { workspace = true, features = ["extension-module"] } num_cpus = "1.16.0" diff --git a/tket2-py/src/circuit.rs b/tket2-py/src/circuit.rs index 7d2a039b..af848e0d 100644 --- a/tket2-py/src/circuit.rs +++ b/tket2-py/src/circuit.rs @@ -59,8 +59,8 @@ pub fn to_hugr_dot(c: Py) -> PyResult { /// Downcast a python object to a [`Hugr`]. #[pyfunction] -pub fn to_hugr(c: Py) -> PyResult { - with_hugr(c, |hugr| hugr) +pub fn to_hugr(c: Py) -> PyResult { + with_hugr(c, |hugr| hugr.into()) } /// A [`hugr::Node`] wrapper for Python. diff --git a/tket2-py/src/circuit/convert.rs b/tket2-py/src/circuit/convert.rs index ac43d794..33d183ff 100644 --- a/tket2-py/src/circuit/convert.rs +++ b/tket2-py/src/circuit/convert.rs @@ -2,6 +2,7 @@ use pyo3::{prelude::*, PyTypeInfo}; +use derive_more::From; use hugr::{Hugr, HugrView}; use tket2::extension::REGISTRY; use tket2::json::TKETDecode; @@ -12,7 +13,7 @@ use crate::pattern::rewrite::PyCircuitRewrite; /// A manager for tket 2 operations on a tket 1 Circuit. #[pyclass] -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, From)] pub struct T2Circuit { /// Rust representation of the circuit. pub hugr: Hugr, @@ -23,7 +24,7 @@ impl T2Circuit { #[new] fn from_circuit(circ: PyObject) -> PyResult { Ok(Self { - hugr: super::to_hugr(circ)?, + hugr: with_hugr(circ, |hugr| hugr)?, }) } diff --git a/tket2-py/src/pattern.rs b/tket2-py/src/pattern.rs index 071ceaa5..ed3899e1 100644 --- a/tket2-py/src/pattern.rs +++ b/tket2-py/src/pattern.rs @@ -49,7 +49,7 @@ impl Rule { let l = to_hugr(l)?; let r = to_hugr(r)?; - Ok(Rule([l, r])) + Ok(Rule([l.hugr, r.hugr])) } } #[pyclass] diff --git a/tket2-py/src/pattern/portmatching.rs b/tket2-py/src/pattern/portmatching.rs index 75344ed3..c73e0548 100644 --- a/tket2-py/src/pattern/portmatching.rs +++ b/tket2-py/src/pattern/portmatching.rs @@ -3,14 +3,13 @@ use std::fmt; use derive_more::{From, Into}; -use hugr::Node; use itertools::Itertools; use portmatching::PatternID; use pyo3::{prelude::*, types::PyIterator}; use tket2::portmatching::{CircuitPattern, PatternMatch, PatternMatcher}; -use crate::circuit::{try_with_hugr, with_hugr}; +use crate::circuit::{try_with_hugr, with_hugr, PyNode}; /// A pattern that match a circuit exactly /// @@ -111,8 +110,8 @@ impl PyPatternMatch { } /// Returns the root of the pattern in the circuit. - pub fn root(&self) -> Node { - self.pmatch.root() + pub fn root(&self) -> PyNode { + self.pmatch.root().into() } /// A string representation of the pattern. diff --git a/tket2/Cargo.toml b/tket2/Cargo.toml index 712655c1..25edaad7 100644 --- a/tket2/Cargo.toml +++ b/tket2/Cargo.toml @@ -12,12 +12,7 @@ name = "tket2" path = "src/lib.rs" [features] -pyo3 = [ - "dep:pyo3", - "tket-json-rs/pyo3", - "portgraph/pyo3", - "quantinuum-hugr/pyo3", -] +pyo3 = ["dep:pyo3"] portmatching = ["dep:portmatching", "dep:rmp-serde"] [dependencies]