Skip to content
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

Adding info on how to add a rust module to qiskit and make it available from within python #12852

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/accelerate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ place to put the code. This is _usually_ the right place to put new Rust/Python
The `qiskit-pyext` crate is what actually builds the C extension modules. Modules in here should define
themselves has being submodules of `qiskit._accelerate`, and then the `qiskit-pyext` crate should bind them
into its `fn _accelerate` when it's making the C extension.

sbrandhsn marked this conversation as resolved.
Show resolved Hide resolved
Specifically, adding a rust module `my_module` to Qiskit such that it can be called from within Python involves
the following steps:
1. Add `pub fn my_module(m: &Bound<PyModule>) -> PyResult<()>` to your `my_module.rs` file with `m.add_wrapped(wrap_pyfunction!(mymodulefunction))?;` for new functions and `m.add_class::<MyModuleClass>()?;` for new classes.
sbrandhsn marked this conversation as resolved.
Show resolved Hide resolved
2. Add `pub mod my_module` to `crates/accelerate/src/lib.rs`
sbrandhsn marked this conversation as resolved.
Show resolved Hide resolved
3. To `crates/pyext/src/lib.rs`
* Add my_module::my_module to `use qiskit_accelerate::{`
* Add `m.add_wrapped(wrap_pymodule!(my_module))?;`
4. To `qiskit/__init__.py` add `sys.modules["qiskit._accelerate.my_module”] = _accelerate.module`
5. Compile, and you should be done. Within Python you can now `import qiskit._accelerate.my_module`
Loading