-
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!: get pauli propagation + examples working with new hugr builder #465
Changes from 2 commits
601d905
db87d5e
5c1b08f
fb73843
5eded53
17ff505
6340ef1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
//! Rust-backed representation of circuits | ||
|
||
use std::borrow::Borrow; | ||
use std::borrow::{Borrow, Cow}; | ||
|
||
use hugr::builder::{CircuitBuilder, DFGBuilder, Dataflow, DataflowHugr}; | ||
use hugr::extension::prelude::QB_T; | ||
|
@@ -178,7 +178,7 @@ impl Tk2Circuit { | |
Ok(self.clone()) | ||
} | ||
|
||
fn node_op(&self, node: PyNode) -> PyResult<PyCustomOp> { | ||
fn node_op(&self, node: PyNode) -> PyResult<Cow<[u8]>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. temporary hack There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😬 |
||
let custom: CustomOp = self | ||
.circ | ||
.hugr() | ||
|
@@ -191,7 +191,7 @@ impl Tk2Circuit { | |
)) | ||
})?; | ||
|
||
Ok(custom.into()) | ||
Ok(serde_json::to_vec(&custom).unwrap().into()) | ||
} | ||
|
||
fn node_inputs(&self, node: PyNode) -> Vec<PyWire> { | ||
|
@@ -226,55 +226,3 @@ impl Tk2Circuit { | |
circ.extract::<Tk2Circuit>() | ||
} | ||
} | ||
|
||
#[pyclass] | ||
#[derive(Clone, Debug, PartialEq, From)] | ||
pub(super) struct Dfg { | ||
/// Rust representation of the circuit. | ||
builder: DFGBuilder<Hugr>, | ||
} | ||
#[pymethods] | ||
impl Dfg { | ||
#[new] | ||
fn new(input_types: Vec<PyHugrType>, output_types: Vec<PyHugrType>) -> PyResult<Self> { | ||
let builder = DFGBuilder::new(FunctionType::new( | ||
into_vec(input_types), | ||
into_vec(output_types), | ||
)) | ||
.convert_pyerrs()?; | ||
Ok(Self { builder }) | ||
} | ||
|
||
fn inputs(&self) -> Vec<PyWire> { | ||
self.builder.input_wires().map_into().collect() | ||
} | ||
|
||
fn add_op(&mut self, op: Bound<PyAny>, inputs: Vec<PyWire>) -> PyResult<PyNode> { | ||
// TODO: Once we wrap `Dfg` in a pure python class we can make the conversion there, | ||
// and have a concrete `op: PyCustomOp` argument here. | ||
let custom: PyCustomOp = op | ||
.call_method0("to_custom") | ||
.map_err(|_| { | ||
PyErr::new::<PyValueError, _>( | ||
"The operation must implement the `ToCustomOp` protocol.", | ||
) | ||
})? | ||
.extract()?; | ||
let custom: CustomOp = custom.into(); | ||
self.builder | ||
.add_dataflow_op(custom, inputs.into_iter().map_into()) | ||
.convert_pyerrs() | ||
.map(|d| d.node().into()) | ||
} | ||
|
||
fn finish(&mut self, outputs: Vec<PyWire>) -> PyResult<Tk2Circuit> { | ||
Ok(Tk2Circuit { | ||
circ: self | ||
.builder | ||
.clone() | ||
.finish_hugr_with_outputs(outputs.into_iter().map_into(), ®ISTRY) | ||
.convert_pyerrs()? | ||
.into(), | ||
}) | ||
} | ||
} |
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.
needed for hugr-py compatibility
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.
This will prevent us from releasing new tket2-py versions. Can we do a guppy release soon?