qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Add Public Python FFI to the C API

Open mtreinish opened this issue 2 months ago • 3 comments

What should we add?

In https://github.com/Qiskit/qiskit/pull/14958 a Python FFI to the C API was added to Qiskit's python test suite. This lets us interoperate with the C API from Python. However this arguably doesn't need to be limited to our testing and can provide value to all our python users. Especially those that want to write functionality in C for performance but primarily use Python.

This is similar to #14202 but for Python not Rust. The reason I think for Python this makes more sense in Qiskit as opposed to a separate library is we already bundle the C API in our _accelerate library so unlike rust our Python users already have the C API installed and setup.

The goal of this issue is to take the FFI added for testing in #14958 and move it in the qiskit namespace as part of the public api (although probably marked as experimental at first until the C API is stabilized). This means cleaning up the interface for external consumption with documentation, etc.

mtreinish avatar Oct 09 '25 13:10 mtreinish

Hi @mtreinish

I would like to pick this up, if it is still open. Based on my understanding, in the PR we have implemented python ffi for C API but we don't want to limit it to just C API. We need to create something like qiskit.ffi, once the PR #14958 has been merged?

I have pulled the commits in #14958 to my local and have started little POC on the qiskit.ffi, would like to discuss further after more implementation (if the issue is still open). I am really excited to work on it.

RahilJain1366 avatar Nov 18 '25 01:11 RahilJain1366

Hi Rahil - thanks for the interest here. Since we opened this issue, we actually realised there's a place where we might end up having to autogenerate a bunch of C prototypes for a different purpose (embedding the C API in a PyCapsule to make it available in C extensions) in a way that will naturally extend to generating the Python ctypes FFI as well, so it may be the case that we end up autogenerating this.

I need to look at the details of that tomorrow just ahead of the 2.3 feature freeze, and can let you know.

jakelishman avatar Nov 18 '25 10:11 jakelishman

Hi @jakelishman

Thanks for the update! I would be glad to support the autogeneration work if it moves forward or the moving to qiskit namespace work. Feel free to update me after you review everything for the 2.3 feature freeze. Looking forward to hearing from you!

RahilJain1366 avatar Nov 18 '25 23:11 RahilJain1366