-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make native py modules behave like python's (#212)
Cleans up the `tket2-py` structure and makes the modules behave like normal python ones. With this we can do ```python from tket2.circuit import T2Circuit ``` Minor changes: - Moves some code from `tket2-py/src/lib.rs` to a new `pattern.rs`. - Deletes some commented-out code. Closes #209
- Loading branch information
Showing
14 changed files
with
197 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! | ||
use crate::circuit::{to_hugr, T2Circuit}; | ||
|
||
use hugr::Hugr; | ||
use pyo3::prelude::*; | ||
use tket2::portmatching::pyo3::PyPatternMatch; | ||
use tket2::portmatching::{CircuitPattern, PatternMatcher}; | ||
use tket2::rewrite::CircuitRewrite; | ||
|
||
/// The module definition | ||
pub fn module(py: Python) -> PyResult<&PyModule> { | ||
let m = PyModule::new(py, "_pattern")?; | ||
m.add_class::<tket2::portmatching::CircuitPattern>()?; | ||
m.add_class::<tket2::portmatching::PatternMatcher>()?; | ||
m.add_class::<CircuitRewrite>()?; | ||
m.add_class::<Rule>()?; | ||
m.add_class::<RuleMatcher>()?; | ||
|
||
m.add( | ||
"InvalidPatternError", | ||
py.get_type::<tket2::portmatching::pattern::PyInvalidPatternError>(), | ||
)?; | ||
m.add( | ||
"InvalidReplacementError", | ||
py.get_type::<hugr::hugr::views::sibling_subgraph::PyInvalidReplacementError>(), | ||
)?; | ||
|
||
Ok(m) | ||
} | ||
|
||
#[derive(Clone)] | ||
#[pyclass] | ||
/// A rewrite rule defined by a left hand side and right hand side of an equation. | ||
pub struct Rule(pub [Hugr; 2]); | ||
|
||
#[pymethods] | ||
impl Rule { | ||
#[new] | ||
fn new_rule(l: PyObject, r: PyObject) -> PyResult<Rule> { | ||
let l = to_hugr(l)?; | ||
let r = to_hugr(r)?; | ||
|
||
Ok(Rule([l, r])) | ||
} | ||
} | ||
#[pyclass] | ||
struct RuleMatcher { | ||
matcher: PatternMatcher, | ||
rights: Vec<Hugr>, | ||
} | ||
|
||
#[pymethods] | ||
impl RuleMatcher { | ||
#[new] | ||
pub fn from_rules(rules: Vec<Rule>) -> PyResult<Self> { | ||
let (lefts, rights): (Vec<_>, Vec<_>) = | ||
rules.into_iter().map(|Rule([l, r])| (l, r)).unzip(); | ||
let patterns: Result<Vec<CircuitPattern>, _> = | ||
lefts.iter().map(CircuitPattern::try_from_circuit).collect(); | ||
let matcher = PatternMatcher::from_patterns(patterns?); | ||
|
||
Ok(Self { matcher, rights }) | ||
} | ||
|
||
pub fn find_match(&self, target: &T2Circuit) -> PyResult<Option<CircuitRewrite>> { | ||
let h = &target.0; | ||
let p_match = self.matcher.find_matches_iter(h).next(); | ||
if let Some(m) = p_match { | ||
let py_match = PyPatternMatch::try_from_rust(m, h, &self.matcher)?; | ||
let r = self.rights.get(py_match.pattern_id).unwrap().clone(); | ||
let rw = py_match.to_rewrite(h, r)?; | ||
Ok(Some(rw)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} |
Oops, something went wrong.