Skip to content

Commit

Permalink
Move CircuitRewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Nov 10, 2023
1 parent 83c54ac commit 657adf3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
7 changes: 4 additions & 3 deletions tket2-py/src/circuit/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use hugr::{Hugr, HugrView};
use tket2::extension::REGISTRY;
use tket2::json::TKETDecode;
use tket2::passes::CircuitChunks;
use tket2::rewrite::CircuitRewrite;
use tket_json_rs::circuit_json::SerialCircuit;

use crate::pattern::rewrite::PyCircuitRewrite;

/// A manager for tket 2 operations on a tket 1 Circuit.
#[pyclass]
#[derive(Clone, Debug, PartialEq)]
Expand All @@ -30,8 +31,8 @@ impl T2Circuit {
SerialCircuit::encode(&self.hugr)?.to_tket1_with_gil()
}

fn apply_match(&mut self, rw: CircuitRewrite) {
rw.apply(&mut self.hugr).expect("Apply error.");
fn apply_match(&mut self, rw: PyCircuitRewrite) {
rw.rewrite.apply(&mut self.hugr).expect("Apply error.");
}
}
impl T2Circuit {
Expand Down
10 changes: 6 additions & 4 deletions tket2-py/src/pattern.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
//! Pattern matching on circuits.
pub mod portmatching;
pub mod rewrite;

use crate::circuit::{to_hugr, T2Circuit};

use hugr::Hugr;
use pyo3::prelude::*;
use tket2::portmatching::{CircuitPattern, PatternMatcher};
use tket2::rewrite::CircuitRewrite;

use self::rewrite::PyCircuitRewrite;

/// The module definition
pub fn module(py: Python) -> PyResult<&PyModule> {
let m = PyModule::new(py, "_pattern")?;
m.add_class::<CircuitRewrite>()?;
m.add_class::<self::rewrite::PyCircuitRewrite>()?;
m.add_class::<Rule>()?;
m.add_class::<RuleMatcher>()?;
m.add_class::<self::portmatching::PyCircuitPattern>()?;
Expand Down Expand Up @@ -69,12 +71,12 @@ impl RuleMatcher {
Ok(Self { matcher, rights })
}

pub fn find_match(&self, target: &T2Circuit) -> PyResult<Option<CircuitRewrite>> {
pub fn find_match(&self, target: &T2Circuit) -> PyResult<Option<PyCircuitRewrite>> {
let h = &target.hugr;
if let Some(p_match) = self.matcher.find_matches_iter(h).next() {
let r = self.rights.get(p_match.pattern_id().0).unwrap().clone();
let rw = p_match.to_rewrite(h, r)?;
Ok(Some(rw))
Ok(Some(rw.into()))
} else {
Ok(None)
}
Expand Down
19 changes: 19 additions & 0 deletions tket2-py/src/pattern/rewrite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Bindings for circuit rewrites.
use derive_more::From;
use pyo3::prelude::*;
use tket2::rewrite::CircuitRewrite;

/// A rewrite rule for circuits.
///
/// Python equivalent of [`CircuitRewrite`].
///
/// [`CircuitRewrite`]: tket2::rewrite::CircuitRewrite
#[pyclass]
#[pyo3(name = "CircuitRewrite")]
#[derive(Debug, Clone, From)]
#[repr(transparent)]
pub struct PyCircuitRewrite {
/// Rust representation of the circuit chunks.
pub rewrite: CircuitRewrite,
}
4 changes: 0 additions & 4 deletions tket2/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ use hugr::{
Hugr, SimpleReplacement,
};

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

use crate::circuit::Circuit;

/// A subcircuit of a circuit.
Expand Down Expand Up @@ -64,7 +61,6 @@ impl Subcircuit {
}

/// A rewrite rule for circuits.
#[cfg_attr(feature = "pyo3", pyclass)]
#[derive(Debug, Clone, From, Into)]
pub struct CircuitRewrite(SimpleReplacement);

Expand Down

0 comments on commit 657adf3

Please sign in to comment.