Skip to content

Commit

Permalink
Add missing py_ext module file.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhartman committed Oct 26, 2023
1 parent ecf73d6 commit b30d2c6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/accelerate/src/quantum_circuit/py_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This code is part of Qiskit.
//
// (C) Copyright IBM 2023
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.
use pyo3::ffi::Py_ssize_t;
use pyo3::prelude::*;
use pyo3::types::{PyList, PyTuple};
use pyo3::{ffi, AsPyPointer, PyNativeType};

pub fn tuple_new(py: Python<'_>, items: Vec<PyObject>) -> Py<PyTuple> {
unsafe {
let ptr = ffi::PyTuple_New(items.len() as Py_ssize_t);
let tup: Py<PyTuple> = Py::from_owned_ptr(py, ptr);
for (i, obj) in items.into_iter().enumerate() {
ffi::PyTuple_SetItem(ptr, i as Py_ssize_t, obj.into_ptr());
}
tup
}
}

pub fn tuple_new_empty(py: Python<'_>) -> Py<PyTuple> {
unsafe { Py::from_owned_ptr(py, ffi::PyTuple_New(0)) }
}

pub fn tuple_from_list(list: &PyList) -> Py<PyTuple> {
unsafe { Py::from_owned_ptr(list.py(), ffi::PyList_AsTuple(list.as_ptr())) }
}

0 comments on commit b30d2c6

Please sign in to comment.