Add generated qiskit-pyo3-ffi crate#16145
Draft
jakelishman wants to merge 2 commits intoQiskit:mainfrom
Draft
Conversation
While starting to write a Rust/PyO3 raw-FFI backend for the C API via the Python package, I realised that a lot of the logic was about to be duplicated from the `ctypes` output. Had I realised this sooner, I'd have folded this commit into ae91986[^1] to avoid an immediate refactor. I've not attempted to de-duplicate _all_ logic that ever might occur (for example, lifting a `Function` is going to look _mostly_ similar but not identical), because there's enough little tweaks between the languages to make the abstraction awkward, and we're unlikely to have enough languages to really warrant it. For example, the `ctypes` binding needs to know that any custom "path" name of an `Enum` can't actually be exported to that name, because we use Python-space true `Enum` classes in the `ctypes` bindings for input. This restriction isn't present in other languages. [^1]: ae91986: Add auto-generated `ctypes` bindings to C API (Qiskitgh-16067)
This adds logic to our `qiskit-bindgen` export library to generate raw Rust-space bindings that link against the Qiskit C API using the same `PyCapsule`-stored vtable system that the `qiskit.h` header file in `QISKIT_EXTENSION_MODULE` mode does. For the build process, this adds a new `make` command: ```bash make qiskit-pyo3-ffi ``` that populates the complete crate into `dist/rust`. The low-level worker of the `make` command is the new subcommand ```bash [cargo run -p] qiskit-bindgen-cli -- generate-pyo3 -c crates/cext -o dist/rust/qiskit-pyo3-ffi ``` I've done this using a partial "template" crate embedded in `crates/bindgen` (but not within its `src` tree), which is analogous to how we output the C header files; many of the infrastructure files are hand-written, and only part of the system is generated. I set up the template crate `crates/bindgen/pyo3-ffi` to be largely sensible with respect to using `rust-analyzer`-enabled IDEs or clippy commands to check the template code itself. The template crate is not part of the workspace because it's intended for separate distribution. This commit has all the logic to produce the distribution artefact, but doesn't set up any sort of CD pipeline, because we still have some packaging decisions to make about that.
Coverage Report for CI Build 25404074983Coverage decreased (-0.2%) to 87.369%Details
Uncovered Changes
Coverage Regressions13 previously-covered lines in 3 files lost coverage.
Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds logic to our
qiskit-bindgenexport library to generate raw Rust-space bindings that link against the Qiskit C API using the samePyCapsule-stored vtable system that theqiskit.hheader file inQISKIT_EXTENSION_MODULEmode does.For the build process, this adds a new
makecommand:that populates the complete crate into
dist/rust. The low-level worker of themakecommand is the new subcommand[cargo run -p] qiskit-bindgen-cli -- generate-pyo3 -c crates/cext -o dist/rust/qiskit-pyo3-ffiI've done this using a partial "template" crate embedded in
crates/bindgen(but not within itssrctree), which is analogous to how we output the C header files; many of the infrastructure files are hand-written, and only part of the system is generated. I set up the template cratecrates/bindgen/pyo3-ffito be largely sensible with respect to usingrust-analyzer-enabled IDEs or clippy commands to check the template code itself.The template crate is not part of the workspace because it's intended for separate distribution. This commit has all the logic to produce the distribution artefact, but doesn't set up any sort of CD pipeline, because we still have some packaging decisions to make about that.
Close #15985
AI/LLM disclosure
Based on #16144.