tket2 icon indicating copy to clipboard operation
tket2 copied to clipboard

How can 3rd party pyo3 projects do Circuit manipulation?

Open aborgna-q opened this issue 7 months ago • 3 comments

Scenario:

Someone writes a pass in rust code:

pub fn my_pass(circ: Circuit) -> Circuit;

and wants to make it available as a python method using pyo3.

How are they supposed to go about it?

In the tket2-py we define a Tk2Circuit wrapper marked with pyo3's #[pyclass], and some utilities to read it from python inputs:

#[pyfunction]
pub fn my_python_pass<'py>(circ: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
    tket2_py::circuit::convert::update_circ(circ, |circ, _| my_pass(circ))
}

These conversion utilities are part of the private tket2-py, so they are not available for external crates. How can we expose these?

aborgna-q avatar Apr 09 '25 15:04 aborgna-q

One option is to move the pyo3 dependency into tket2 (under a pyo3 feature), and expose convert there.

aborgna-q avatar Apr 09 '25 15:04 aborgna-q

Thanks for making an issue :) Currently have to export the python circuit to json... load it in... modify in rust and then export the json back out again. Can construct a circuit from this output json though.

CalMacCQ avatar Apr 10 '25 10:04 CalMacCQ

Related: https://github.com/CQCL/hugr/discussions/2616

aborgna-q avatar Oct 09 '25 15:10 aborgna-q