Skip to content

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented Mar 12, 2025

Summary

Building off the infrastructure for the sparse observable added in
PR #13445 this commit adds a C FFI for building Quantum Circuits. Right
now there is a function to create an empty circuit with n qubits and m
clbits and then a function to add standard gates to a circuit and then
also get the op counts out of a circuit. This is a start of the
functionality for a C API around interacting with circuits, later PRs
will expand this so we can have a more fully featured C API in the
future.

Details and comments

Part of #13276

This PR is based on top of #13986 and will need to rebased after that merges. To see the contents of just this PR you can look at: mtreinish/qiskit-core@no-py-token-constructor-path...mtreinish:qiskit-core:c-api-circuit-rebased

TODO:

  • Add release notes

@mtreinish mtreinish added Changelog: New Feature Include in the "Added" section of the changelog Rust This PR or issue is related to Rust code in the repository mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library C API Related to the C API labels Mar 12, 2025
@mtreinish mtreinish added this to the 2.1.0 milestone Mar 12, 2025
@mtreinish mtreinish requested a review from a team as a code owner March 12, 2025 16:05
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @levbishop

@mtreinish mtreinish force-pushed the c-api-circuit-rebased branch from d3d226a to 590e7a8 Compare March 12, 2025 16:49
Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

This looks really cool! I left some comments below 🙂

@mtreinish mtreinish force-pushed the c-api-circuit-rebased branch from 2e02dc1 to bbde5e5 Compare March 12, 2025 19:50
@coveralls
Copy link

coveralls commented Mar 12, 2025

Pull Request Test Coverage Report for Build 14519935511

Details

  • 14 of 267 (5.24%) changed or added relevant lines in 5 files are covered.
  • 8 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.2%) to 87.988%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/cext/src/pointers.rs 0 19 0.0%
crates/cext/src/circuit.rs 0 234 0.0%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 2 92.23%
crates/qasm2/src/parse.rs 6 97.15%
Totals Coverage Status
Change from base Build 14499882025: -0.2%
Covered Lines: 73735
Relevant Lines: 83801

💛 - Coveralls

mtreinish and others added 6 commits March 14, 2025 17:25
Building off the infrastructure for the sparse observable added in
PR Qiskit#13445 this commit adds a C FFI for building Quantum Circuits. Right
now there is a function to create an empty circuit with n qubits and m
clbits and then a function to add standard gates to a circuit and then
also get the op counts out of a circuit. This is a start of the
functionality for a C API around interacting with circuits, later PRs
will expand this so we can have a more fully featured C API in the
future.

Part of Qiskit#13276
Co-authored-by: Julien Gacon <gaconju@gmail.com>
This also updates the test logic to make sure we always free even in
case of a failure.
@mtreinish mtreinish force-pushed the c-api-circuit-rebased branch from 3e62ec6 to 669695a Compare March 15, 2025 18:22
@mtreinish mtreinish changed the title [WIP] Add initial C API for circuit construction Add initial C API for circuit construction Mar 15, 2025
@mtreinish mtreinish requested a review from raynelfss April 16, 2025 20:59
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

I didn't actually review the implementations, I just joined in with the bikeshedding.

Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

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

LGTM! (except for the minor dates detail)

QkCircuit *qc = qk_circuit_new(1000, 1000);
uint32_t num_qubits = qk_circuit_num_qubits(qc);
uint32_t num_clbits = qk_circuit_num_clbits(qc);
size_t num_instructions = qk_circuit_num_instructions(qc);
qk_circuit_free(qc);

return (num_qubits != 1000 || num_clbits != 1000 || num_instructions != 0) ? EqualityError : Ok;
if (num_qubits != 1000) {
printf("The number of qubits %d is not 1000", num_qubits);
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to standardize this in the future. I'm wondering if this is printed somehow in order with CTest's output or if it is shown at another point and the origin is hard to pin down.

Copy link
Member Author

Choose a reason for hiding this comment

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

IIRC, I used print debugging for developing the tests in the first place and ctest put the output somewhere I could find. But if we need to make improvements here we can definitely do it in a follow up.

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

Two small comments (that can be skipped) -- LGTM!

@mtreinish mtreinish added this pull request to the merge queue Apr 23, 2025
Merged via the queue into Qiskit:main with commit d2db278 Apr 23, 2025
24 checks passed
@mtreinish mtreinish deleted the c-api-circuit-rebased branch April 23, 2025 13:13
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Apr 23, 2025
The C api's build.rs was generating a config file that pointed to
where the Python that pyo3 links against was located. This was
previously used by the c test's cmake file to add to the search path on
windows to enable the windows ctest runner to find the python lib it
needs to load to run the tests. However, in practice this wasn't
pointing to the correct location to point windows at libpython and
PR Qiskit#14006 stopped using this file when it updated the path set to
be found using cmake's FindPython module. This commit is just a follow
up to remove this part of the build.rs as it's not longer actively used
for anything.
github-merge-queue bot pushed a commit that referenced this pull request Apr 24, 2025
The C api's build.rs was generating a config file that pointed to
where the Python that pyo3 links against was located. This was
previously used by the c test's cmake file to add to the search path on
windows to enable the windows ctest runner to find the python lib it
needs to load to run the tests. However, in practice this wasn't
pointing to the correct location to point windows at libpython and
PR #14006 stopped using this file when it updated the path set to
be found using cmake's FindPython module. This commit is just a follow
up to remove this part of the build.rs as it's not longer actively used
for anything.
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Apr 28, 2025
This commit adds some more details to the C API docs for the new
QkCircuit struct which was added in Qiskit#14006.
@mtreinish mtreinish mentioned this pull request Apr 28, 2025
2 tasks
github-merge-queue bot pushed a commit that referenced this pull request May 12, 2025
* Improve QkCircuit C API docs

This commit adds some more details to the C API docs for the new
QkCircuit struct which was added in #14006.

* Apply suggestions from code review

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update docs/cdoc/qk-circuit.rst

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Add members option to doxygen group

* Split out data structs in docs

* Expand on current limitations of API in docs

* Other docstring fixes

* C docs don't have cross refs

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
rahaman-quantum pushed a commit to rahaman-quantum/qiskit that referenced this pull request Jun 20, 2025
* Improve QkCircuit C API docs

This commit adds some more details to the C API docs for the new
QkCircuit struct which was added in Qiskit#14006.

* Apply suggestions from code review

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update docs/cdoc/qk-circuit.rst

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Add members option to doxygen group

* Split out data structs in docs

* Expand on current limitations of API in docs

* Other docstring fixes

* C docs don't have cross refs

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
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: New Feature Include in the "Added" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library priority: high Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants