Skip to content

Add auto-generated ctypes bindings to C API#16067

Open
jakelishman wants to merge 2 commits intoQiskit:mainfrom
jakelishman:c/ctypes/1
Open

Add auto-generated ctypes bindings to C API#16067
jakelishman wants to merge 2 commits intoQiskit:mainfrom
jakelishman:c/ctypes/1

Conversation

@jakelishman
Copy link
Copy Markdown
Member

@jakelishman jakelishman commented Apr 20, 2026

This is similar to how Python exposes its own C API through ctypes as ctypes.pythonapi. It's unlikely to be immediately useful as a feature for downstream users, but it simplifies the process of us testing our own code from Python space.

I had originally hoped / thought that numba would support calling these ctypes functions, but while numba (as of current version 0.65) does have support for calling ctypes functions defined purely in terms of primitive non-pointer integral types, it doesn't yet support ctypes.py_object or arbitrary pointers.

Close #15144

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

This is minor code reorganisation to avoid the `cext` build script
getting out of hand; we're about to add an extra `ctypes`-like backend
to it, which coudl do with being in its own module.
@jakelishman jakelishman requested a review from a team as a code owner April 20, 2026 16:03
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

1 similar comment
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@jakelishman jakelishman added Changelog: Added Add an "Added" entry in the GitHub Release changelog. C API Related to the C API labels Apr 20, 2026
@jakelishman jakelishman added this to the 2.5.0 milestone Apr 20, 2026
@jakelishman jakelishman added the on hold Can not fix yet label Apr 20, 2026
This is similar to how Python exposes its own C API through `ctypes` as
`ctypes.pythonapi`.  It's unlikely to be immediately useful as a feature
for downstream users, but it simplifies the process of us testing our
own code from Python space.

I had originally hoped / thought that `numba` would support calling
these `ctypes` functions, but while `numba` (as of current version 0.65)
does have support for calling `ctypes` functions defined purely in terms
of primitive non-pointer integral types, it doesn't yet support
`ctypes.py_object` or arbitrary pointers.
@coveralls
Copy link
Copy Markdown

Coverage Report for CI Build 24678475550

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.1%) to 87.563%

Details

  • Coverage increased (+0.1%) from the base build.
  • Patch coverage: 52 uncovered changes across 3 files (386 of 438 lines covered, 88.13%).
  • 8 coverage regressions across 3 files.

Uncovered Changes

File Changed Covered %
crates/bindgen/src/render/py_ctypes.rs 315 265 84.13%
crates/bindgen/src/render/c.rs 62 61 98.39%
crates/pyext/build.rs 46 45 97.83%

Coverage Regressions

8 previously-covered lines in 3 files lost coverage.

File Lines Losing Coverage Coverage
crates/qasm2/src/lex.rs 5 92.29%
crates/circuit/src/parameter/symbol_expr.rs 2 73.88%
crates/circuit/src/parameter/parameter_expression.rs 1 90.53%

Coverage Stats

Coverage Status
Relevant Lines: 120780
Covered Lines: 105758
Line Coverage: 87.56%
Coverage Strength: 972943.0 hits per line

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C API Related to the C API Changelog: Added Add an "Added" entry in the GitHub Release changelog. on hold Can not fix yet

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

Add Public Python FFI to the C API

3 participants