-
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.
feat: python errors and some pyrs helpers (#114)
- Loading branch information
Showing
12 changed files
with
184 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! Circuit-related functionality and utilities. | ||
#![allow(unused)] | ||
|
||
use pyo3::prelude::*; | ||
|
||
use hugr::{Hugr, HugrView}; | ||
use tket2::extension::REGISTRY; | ||
use tket2::json::TKETDecode; | ||
use tket_json_rs::circuit_json::SerialCircuit; | ||
|
||
/// Apply a fallible function expecting a hugr on a pytket circuit. | ||
pub fn try_with_hugr<T, E, F>(circ: Py<PyAny>, f: F) -> PyResult<T> | ||
where | ||
E: Into<PyErr>, | ||
F: FnOnce(Hugr) -> Result<T, E>, | ||
{ | ||
let hugr = SerialCircuit::_from_tket1(circ).decode()?; | ||
(f)(hugr).map_err(|e| e.into()) | ||
} | ||
|
||
/// Apply a function expecting a hugr on a pytket circuit. | ||
pub fn with_hugr<T, F: FnOnce(Hugr) -> T>(circ: Py<PyAny>, f: F) -> PyResult<T> { | ||
try_with_hugr(circ, |hugr| Ok::<T, PyErr>((f)(hugr))) | ||
} | ||
|
||
/// Apply a hugr-to-hugr function on a pytket circuit, and return the modified circuit. | ||
pub fn try_update_hugr<E: Into<PyErr>, F: FnOnce(Hugr) -> Result<Hugr, E>>( | ||
circ: Py<PyAny>, | ||
f: F, | ||
) -> PyResult<Py<PyAny>> { | ||
let hugr = try_with_hugr(circ, f)?; | ||
SerialCircuit::encode(&hugr)?.to_tket1() | ||
} | ||
|
||
/// Apply a hugr-to-hugr function on a pytket circuit, and return the modified circuit. | ||
pub fn update_hugr<F: FnOnce(Hugr) -> Hugr>(circ: Py<PyAny>, f: F) -> PyResult<Py<PyAny>> { | ||
let hugr = with_hugr(circ, f)?; | ||
SerialCircuit::encode(&hugr)?.to_tket1() | ||
} | ||
|
||
#[pyfunction] | ||
pub fn validate_hugr(c: Py<PyAny>) -> PyResult<()> { | ||
try_with_hugr(c, |hugr| hugr.validate(®ISTRY)) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn to_hugr_dot(c: Py<PyAny>) -> PyResult<String> { | ||
with_hugr(c, |hugr| hugr.dot_string()) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn to_hugr(c: Py<PyAny>) -> PyResult<Hugr> { | ||
with_hugr(c, |hugr| hugr) | ||
} |
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 |
---|---|---|
@@ -1,17 +1,56 @@ | ||
//! Python bindings for TKET2. | ||
#![warn(missing_docs)] | ||
|
||
mod circuit; | ||
|
||
use pyo3::prelude::*; | ||
use tket2::portmatching::{CircuitPattern, PatternMatcher}; | ||
|
||
/// The Python bindings to TKET2. | ||
#[pymodule] | ||
fn pyrs(py: Python, m: &PyModule) -> PyResult<()> { | ||
add_patterns_module(py, m)?; | ||
add_circuit_module(py, m)?; | ||
add_pattern_module(py, m)?; | ||
Ok(()) | ||
} | ||
|
||
fn add_patterns_module(py: Python, parent: &PyModule) -> PyResult<()> { | ||
let m = PyModule::new(py, "patterns")?; | ||
m.add_class::<CircuitPattern>()?; | ||
m.add_class::<PatternMatcher>()?; | ||
parent.add_submodule(m)?; | ||
Ok(()) | ||
/// circuit module | ||
fn add_circuit_module(py: Python, parent: &PyModule) -> PyResult<()> { | ||
let m = PyModule::new(py, "circuit")?; | ||
m.add_class::<tket2::T2Op>()?; | ||
m.add_class::<tket2::Pauli>()?; | ||
|
||
m.add("HugrError", py.get_type::<hugr::hugr::PyHugrError>())?; | ||
m.add("BuildError", py.get_type::<hugr::builder::PyBuildError>())?; | ||
m.add( | ||
"ValidationError", | ||
py.get_type::<hugr::hugr::validate::PyValidationError>(), | ||
)?; | ||
m.add( | ||
"HUGRSerializationError", | ||
py.get_type::<hugr::hugr::serialize::PyHUGRSerializationError>(), | ||
)?; | ||
m.add( | ||
"OpConvertError", | ||
py.get_type::<tket2::json::PyOpConvertError>(), | ||
)?; | ||
|
||
parent.add_submodule(m) | ||
} | ||
|
||
/// portmatching module | ||
fn add_pattern_module(py: Python, parent: &PyModule) -> PyResult<()> { | ||
let m = PyModule::new(py, "pattern")?; | ||
m.add_class::<tket2::portmatching::CircuitPattern>()?; | ||
m.add_class::<tket2::portmatching::PatternMatcher>()?; | ||
|
||
m.add( | ||
"InvalidPatternError", | ||
py.get_type::<tket2::portmatching::pattern::PyInvalidPatternError>(), | ||
)?; | ||
m.add( | ||
"InvalidReplacementError", | ||
py.get_type::<hugr::hugr::views::sibling_subgraph::PyInvalidReplacementError>(), | ||
)?; | ||
|
||
parent.add_submodule(m) | ||
} |
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
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
Oops, something went wrong.