Skip to content

Conversation

velocityraptor7085
Copy link
Contributor

Summary

Details and comments

The approach used includes the following changes:

  1. Adds a file called version.h in the directory crates/cext having macros for the version information.
  2. Includes the version.h macros in the qiskit.h file generated for the C API calls (added through the cbindgen.toml file within crates/cext).
  3. The version information is populated into the version.h file by reading the VERSION.txt file (within the qiskit subdirectory) by the build.rs code via regex matching and replacement at compile time (for this an additional build dependency regex = "1" was added to the Cargo.toml file in the cext crate). The removes the need to update the header for every new release.
  • All code was formatted via cargo fmt and passed the tox -elint test.
  • The modified branch was tested and validated via the test_version_info.c file in the test/c directory.

@velocityraptor7085 velocityraptor7085 requested a review from a team as a code owner May 21, 2025 13:37
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label May 21, 2025
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

  • @Qiskit/terra-core

@CLAassistant
Copy link

CLAassistant commented May 21, 2025

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ velocityraptor7085
❌ Archit Chadalawada


Archit Chadalawada seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Thanks for doing this, I think the approach you're using here is a bit more complicated than it needs to be. Couldn't we just define a couple of pub constants in the cext rust code and then tell cbindgen to include them in the header? If so then we can just use the cargo env variables to populate the version numbers appropriately.

@mtreinish
Copy link
Member

Oh also you appear to have authored commits with a different email address not associated with your github account. You'll either need to associate that email account to your github account, or edit the commits on your branch to reset the author metadata to use the correct email address and force push that updated history to your PR's branch on your fork. We won't be able to merge a PR if the CLA bot isn't all green.

@velocityraptor7085
Copy link
Contributor Author

Okay, I am working on it. 👍

@velocityraptor7085 velocityraptor7085 force-pushed the c-version-support branch 2 times, most recently from 83273cd to bec775e Compare May 22, 2025 04:17
@velocityraptor7085 velocityraptor7085 requested a review from a team as a code owner May 22, 2025 04:17
@velocityraptor7085
Copy link
Contributor Author

I have updated the code as well as force pushed the correct author information.

Summary

Details and comments

The approach used includes the following changes:

  1. Added macros for numerical Qiskit versions in the cbindgen.toml file (within crates/cext).
  2. Updated the build.rs file in the crates/cext directory to fetch version information at compile-time via the CARGO_PKG_VERSION_... environment variables.

This enables the cbindgen.toml file to directly include the version-related macros in the generated qiskit.h header file, thereby making it directly accessible via the Qiskit C API.

Please let me know if this approach is okay.
Thank you.

@Cryoris
Copy link
Contributor

Cryoris commented May 23, 2025

I'm not super sure about this approach, since we effectively end up with some version number committed in cbindgen.toml and dynamically overwrite them. So when we update Qiskit, we either (1) end up with an out-of-date version in cbindgen.toml or (2) manually update it, but then we wouldn't need the dynamic overwrite in the first place.

I was hoping we could have a solution where we could dynamically add the version numbers to the header, but after scouting the cbindgen docs I couldn't find anything suitable that allows to extend e.g. the after_includes (only to replace it completely).

I'm wondering whether at this point it wouldn't just be easier to hardcode the version number? Or a compromise, if we want to stick with the cargo-generated version, we could maybe have a placeholder comment in the after_includes of cbindgen.toml that doesn't hardcode version numbers. (@velocityraptor7085 nothing you have to change yet, this is to discuss with @mtreinish first 🙂)

@velocityraptor7085
Copy link
Contributor Author

velocityraptor7085 commented May 23, 2025

Sure @Cryoris . Actually, the cbindgen.toml was initially having a placeholder (not hardcoded values) which were defined. The idea was that whenever the make c command was run by the user, the up-to-date Qiskit version (as part of the environment variables) will overwrite whatever comes after the #define ... in the after_includes section of the cbindgen.toml (The logic for dynamically writing this is in the build.rs file).

So, If you try testing this by removing the version numbers from the cbindgen.toml and re-running make c, the version numbers should automatically be populated. I can update the cbindgen.toml with some placeholder like @ or something else suitable to avoid any ambiguity.

Please let me know in case I am missing something.
Thank you.

@Cryoris
Copy link
Contributor

Cryoris commented May 27, 2025

We discussed offline with @mtreinish and came to the conclusion that it's probably simpler to just hardcode the version number, without any additional cbindgen logic. That is partially motivated by #14430, which we'll likely change to sooner than later, which drops cbindgen anyways. Could you update the code accordingly? 🙂

@velocityraptor7085
Copy link
Contributor Author

Okay @Cryoris. I have updated the code to use the hard-coded version information directly without any additional logic related to cbindgen.toml. Let me know if this works. Thank you.

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.

Thanks for the updates, this generally looks good to me now. I left some small comments below.

@velocityraptor7085
Copy link
Contributor Author

velocityraptor7085 commented May 28, 2025

Thank you so much! 🙂 I have made the suggested changes @Cryoris.
BTW, I'm just curious as to why it's a convention to leave an empty blank line at the end of the cbindgen.toml as I am not very experienced with this.

@Cryoris
Copy link
Contributor

Cryoris commented May 29, 2025

@velocityraptor7085 the history of this PR includes some commits that don't belong here (e.g. beeb97c). Would you mind rebasing to remove these or just clean the history?

BTW, I'm just curious as to why it's a convention to leave an empty blank line at the end of the cbindgen.toml as I am not very experienced with this.

It's often considered good practice because some tools expect the last line to be a linebreak (see e.g. https://stackoverflow.com/questions/2287967/why-is-it-recommended-to-have-empty-line-in-the-end-of-a-source-file)

Another note: you don't have to keep updating your branch with the main version, that's not required to merge your PR and it keeps triggering CI to run 🙂

@Cryoris
Copy link
Contributor

Cryoris commented May 29, 2025

Also, let us know if you need help cleaning up the history.

@mtreinish mtreinish added this to the 2.1.0 milestone May 29, 2025
@mtreinish mtreinish added the Intern PR PR submitted by IBM Quantum interns label May 29, 2025
@mtreinish mtreinish added Changelog: New Feature Include in the "Added" section of the changelog and removed Community PR PRs from contributors that are not 'members' of the Qiskit repo labels May 29, 2025
@velocityraptor7085
Copy link
Contributor Author

Thanks a lot. I just made the changes. Let me know if the changes are good to go.

@coveralls
Copy link

coveralls commented May 29, 2025

Pull Request Test Coverage Report for Build 15350808054

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 533 unchanged lines in 14 files lost coverage.
  • Overall coverage increased (+0.02%) to 87.861%

Files with Coverage Reduction New Missed Lines %
crates/transpiler/src/passes/unitary_synthesis.rs 1 94.57%
qiskit/circuit/library/pauli_evolution.py 2 96.49%
qiskit/result/sampled_expval.py 2 93.55%
crates/qasm2/src/lex.rs 3 93.23%
qiskit/synthesis/evolution/suzuki_trotter.py 4 90.38%
qiskit/synthesis/evolution/qdrift.py 5 86.49%
qiskit/synthesis/evolution/product_formula.py 7 89.9%
qiskit/transpiler/instruction_durations.py 8 85.59%
qiskit/circuit/quantumcircuitdata.py 10 80.82%
crates/quantum_info/src/sparse_observable/mod.rs 15 94.16%
Totals Coverage Status
Change from base Build 15312889782: 0.02%
Covered Lines: 80415
Relevant Lines: 91525

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Thanks for doing this, I think it mostly looks good. Just one small inline question. Besides that can you add a release note to document the addition to the C API. The documentation around this is here: https://github.com/Qiskit/qiskit/blob/main/CONTRIBUTING.md#release-notes

@velocityraptor7085
Copy link
Contributor Author

Okay I'll have a look and add a release note 👍

@velocityraptor7085
Copy link
Contributor Author

@mtreinish Please let me know if the changes are appropriate. Thank you.

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Just some comments on the release notes, thanks for adding them.

velocityraptor7085 and others added 2 commits May 30, 2025 19:33
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@velocityraptor7085
Copy link
Contributor Author

Thanks a lot for your help @mtreinish !

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

This LGTM now thanks for the updates. I left a small comment inline about a missing copyright header. I'll apply that and then approve this for merge.

@mtreinish mtreinish enabled auto-merge May 30, 2025 16:04
@mtreinish mtreinish added this pull request to the merge queue May 30, 2025
Merged via the queue into Qiskit:main with commit 0e0c026 May 30, 2025
26 checks passed
rahaman-quantum pushed a commit to rahaman-quantum/qiskit that referenced this pull request Jun 20, 2025
…iskit#14423)

* Added support for querying version information in the Qiskit C API.

* Added support for querying version information in the Qiskit C API.

* Added support for querying version information in the Qiskit C API.

* Added support for querying version information in the Qiskit C API.

* Added support for querying version information in the Qiskit C API.

* Added support for querying version information in the Qiskit C API.

* Added support for querying version information in the Qiskit C API.

* Updated cbindgen.toml with version numbers for the Qiskit C API

* Update crates/cext/cbindgen.toml

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

* Updated cbindgen.toml with version numbers for the Qiskit C API

* updated Makefile

* updated Makefile

* Removed printf statement in the test file

* Added release note for C API version support

* Update releasenotes/notes/c-api-version-support-55b018bf9552db1f.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Update releasenotes/notes/c-api-version-support-55b018bf9552db1f.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Add missing license header to the test file

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
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 Intern PR PR submitted by IBM Quantum interns
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants