-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: python errors and some pyrs helpers #114
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cbce6a9
Add some pyrs helpers
aborgna-q e2d2c05
Some pyrs circuit utility functions
aborgna-q 8a03e95
Merge remote-tracking branch 'origin/main' into feat/py-bindings
aborgna-q ce12993
Export the new hugr python errors
aborgna-q 14be6d6
fix docs
aborgna-q 7b24712
Update module name in tests
aborgna-q 54a8069
Mention python tests in DEVELOPMENT.md
aborgna-q File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice