Skip to content

Commit

Permalink
chore!: Update pyo3 to 0.21 (#33)
Browse files Browse the repository at this point in the history
`pyo3 0.21.0` reworked the GIL references to use a new `Bound` smart
pointer.

See the migration guide: https://pyo3.rs/v0.21.1/migration.html

BREAKING CHANGE: `pyo3` dependencies must always match, as they all must
link to the same native library `python`.
  • Loading branch information
aborgna-q authored Apr 4, 2024
1 parent 239855f commit 4473bbc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ name = "tket_json_rs"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
uuid = { version = "1.4", features = ["serde"] }
pyo3 = { version = "0.20.0", optional = true }
pythonize = { version = "0.20.0", optional = true }
pyo3 = { version = "0.21.1", optional = true }
pythonize = { version = "0.21.1", optional = true }


[features]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Serializable Rust definition for circuits and operations of the

## Features

- `pyo3`: Enable Python bindings via pyo3.
- `pyo3`: Enable Python bindings and `pytket.Circuit` conversion via pyo3.

## Recent Changes

Expand Down
12 changes: 6 additions & 6 deletions src/pytket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
use crate::circuit_json::SerialCircuit;
use pyo3::prelude::*;
use pythonize::{depythonize, pythonize};
use pythonize::{depythonize_bound, pythonize};

impl SerialCircuit {
/// Create a new `SerialCircuit` from a `pytket.Circuit`.
pub fn from_tket1(circ: &PyAny) -> PyResult<Self> {
let circ = depythonize(circ.call_method0("to_dict").unwrap())?;
pub fn from_tket1(circ: &Bound<PyAny>) -> PyResult<Self> {
let circ = depythonize_bound(circ.call_method0("to_dict").unwrap())?;
Ok(circ)
}

/// Create a new `SerialCircuit` from a `pytket.Circuit`.
///
/// Utility function that calls [`SerialCircuit::from_tket1`] after acquiring the GIL.
pub fn from_tket1_with_gil(circ: Py<PyAny>) -> PyResult<Self> {
Python::with_gil(|py| Self::from_tket1(circ.as_ref(py)))
Python::with_gil(|py| Self::from_tket1(circ.bind(py)))
}

/// Convert a `SerialCircuit` to a `pytket.Circuit`.
pub fn to_tket1<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
pub fn to_tket1<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let dict = pythonize(py, self).unwrap();
let circ_module = PyModule::import(py, "pytket.circuit")?;
let circ_module = PyModule::import_bound(py, "pytket.circuit")?;

circ_module
.getattr("Circuit")?
Expand Down

0 comments on commit 4473bbc

Please sign in to comment.