Skip to content

Rewrite Qiskit C API extension-module guide#4890

Open
jakelishman wants to merge 6 commits intomainfrom
jake/c-extension
Open

Rewrite Qiskit C API extension-module guide#4890
jakelishman wants to merge 6 commits intomainfrom
jake/c-extension

Conversation

@jakelishman
Copy link
Copy Markdown
Member

The previous "Extend Qiskit from C" guide was very out of date, but more importantly, suggested several code patterns that are highly non-standard and require significant care and understanding to use safely.

As of Qiskit 2.4, the C API is now safely packaged up in a way that allows writing Python extension modules correctly and safely. This new form of the guide walks through the construction of a complete extension module, using the minimal amount of non-standard tooling.

The previous "Extend Qiskit from C" guide was very out of date, but more
importantly, suggested several code patterns that are highly
non-standard and require significant care and understanding to use
safely.

As of Qiskit 2.4, the C API is now safely packaged up in a way that
allows writing Python extension modules _correctly_ and safely.  This
new form of the guide walks through the construction of a complete
extension module, using the minimal amount of non-standard tooling.
@jakelishman jakelishman requested a review from a team March 26, 2026 14:25
@qiskit-bot
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Collaborator

@beckykd beckykd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm done with the first half, but I keep getting pulled into other things. I'll pick this up again this afternoon.

Copy link
Copy Markdown
Collaborator

@beckykd beckykd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thanks!

`pyproject.toml` file. However, our module will need access to the Qiskit C API header files during
its build process. Starting from v2.4, these are included in the Python distributions of the
Qiskit SDK, and you can locate the directory containing them using `qiskit.capi.get_include()`.
This leads us to a `setup.py` file that looks like:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This leads us to a `setup.py` file that looks like:
This returns a `setup.py` file that looks like:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't apply this one just because "This returns a setup.py file" sounds off to me - the file is still something we are writing as part of the guide, not something that is the output of code. Would

Suggested change
This leads us to a `setup.py` file that looks like:
This results in a `setup.py` file that looks like:

be ok?

version of Python that includes its C API headers (this is standard).

There are various options to write a C extension for Python. For simplicity, this guide starts with an approach that uses Python's built-in [`ctypes`](https://docs.python.org/3/library/ctypes.html) module. In the next section, the [the Manual C extension](#manual-c-extension) section provides an example of building the C extension using Python's C API to reduce runtime overhead.
You should be familiar with, or prepared to look up, the individual functions and objects available
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part doesn't use the C API objects yet, right? If that only comes further below, where you're also explicitly referring to the C API docs I would suggest scratching that sentence (which also reads a little discouraging to me, but maybe that's just me 😛)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is the requirements section for the experience we expect somebody reading the guide to have. We do use C API objects below, and don't explain them line by line.

Comment on lines +43 to +44
You must have the standard C compiler toolchain available for your platform. You must also have a
version of Python that includes its C API headers (this is standard).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to link to the "install the C API guide" which has a section on C requirements: https://quantum.cloud.ibm.com/docs/en/guides/install-c-api#requirements ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because you don't need to install anything - it's all included in the Python package.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C compiler toolchain is included in the Python package? 🤔

I was referring to your sentence that said "You must have the standard C compiler toolchain available for your platform." 😄

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sure, I can expand this a little if you like. I don't want to link to the "Install the C API" page though because that has much more complex requirements.

(Also we need to update that page too in a separate PR - it's also out of date.)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add more work here, only if it was 0 effort to link I would've added it, but you don't need to add more docs

obs = cextension.build_observable()
print("SparseObservable instance?", isinstance(obs, qiskit.quantum_info.SparseObservable))
print(obs)
The result of this is:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe briefly to explain this matches what we're actually expecting:

Suggested change
The result of this is:
In the resulting circuit we can see that the spectator qubits are indeed measured:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was kind of deliberately omitting any real description of the pass itself for this guide - the only thing I really wanted to draw attention to was that the code does in fact run. I don't want to imply that you have to understand the spectator measure stuff to understand the guide.

@Cryoris
Copy link
Copy Markdown
Collaborator

Cryoris commented Mar 27, 2026

This is a very nice guide and much more stable than what was in place before 😂 I'm wondering if the C logic in the pass distracts a bit from the build process and if a simpler function would be better. But then again it might be easier for users to start from this and tone it down to what they want instead of starting too simple.

Co-authored-by: Rebecca Dimock <66339736+beckykd@users.noreply.github.com>
Co-authored-by: abbycross <across@us.ibm.com>
Co-authored-by: Jake Lishman <jake@binhbar.com>
@jakelishman
Copy link
Copy Markdown
Member Author

@beckykd: I've updated this mostly with your editing, though I wasn't sure about a couple of them, which I've left open in the conversations above. Can you let me know if they're right / just apply them yourself if you're happy?

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

5 participants