Add C API testing framework to Python tests#14958
Merged
Merged
Conversation
Collaborator
|
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 19481750170Warning: 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
💛 - Coveralls |
14ed71b to
27f0db8
Compare
Right now the Qiskit C API (and by extension the internal rust code) is lacking some of the helper utilities necessary to do deeper validation of the output circuits. Mainly `Operator.from_circuit` to do unitary equivalence checks accounting for a layout introduced by the transpiler. But also things like just circuit equality checks are easier in Python because the tooling doesn't exist in pure-Rust/C yet. As we're building the C API to be the universal interface to Qiskit from any programming language it is very easy to leverage the C API from Python. This commit adds a new testing framework to the Python tests that builds a C FFI using ctypes to call into the loaded compiled extension (which already bundles the C API). It then wraps that ffi in a couple of helper functions to go from a Python circuit to a C circuit, build a c target from basis gates and a coupling map, and then a function to transpile a c circuit using the C api which returns a Python circuit. This enables us to write tests in Python and validate the output circuits are equivalent accounting for layout and permutations the transpiler applies. It also lets us use any other tooling we have in Python for testing. This framework does not eliminate the need for testing the C API directly from C, it is only intended to do deeper testing while the functionality exposed to C is more limited specifically for the transpiler. The tests added in this PR are just a starting point and basically are copies of Python space full path various transpilation tests. This is not expected to be the final set of tests, but more just serve as a starting point.
27f0db8 to
3ccbe40
Compare
Collaborator
|
One or more of the following people are relevant to this code:
|
Cryoris
reviewed
Sep 4, 2025
Cryoris
left a comment
Contributor
There was a problem hiding this comment.
Very nice to enable this testing! Once the qk_obs_apply_layout is merged we should add observable tests as a follow up.
Co-authored-by: Julien Gacon <gaconju@gmail.com>
Cryoris
approved these changes
Nov 19, 2025
Shobhit21287
pushed a commit
to Shobhit21287/qiskit
that referenced
this pull request
Nov 20, 2025
* Add C API testing framework to Python tests Right now the Qiskit C API (and by extension the internal rust code) is lacking some of the helper utilities necessary to do deeper validation of the output circuits. Mainly `Operator.from_circuit` to do unitary equivalence checks accounting for a layout introduced by the transpiler. But also things like just circuit equality checks are easier in Python because the tooling doesn't exist in pure-Rust/C yet. As we're building the C API to be the universal interface to Qiskit from any programming language it is very easy to leverage the C API from Python. This commit adds a new testing framework to the Python tests that builds a C FFI using ctypes to call into the loaded compiled extension (which already bundles the C API). It then wraps that ffi in a couple of helper functions to go from a Python circuit to a C circuit, build a c target from basis gates and a coupling map, and then a function to transpile a c circuit using the C api which returns a Python circuit. This enables us to write tests in Python and validate the output circuits are equivalent accounting for layout and permutations the transpiler applies. It also lets us use any other tooling we have in Python for testing. This framework does not eliminate the need for testing the C API directly from C, it is only intended to do deeper testing while the functionality exposed to C is more limited specifically for the transpiler. The tests added in this PR are just a starting point and basically are copies of Python space full path various transpilation tests. This is not expected to be the final set of tests, but more just serve as a starting point. * Add safety comment * Add missing return types to ctypes interface * Fix ctypes interface def order * Add helper function for building c arrays * Add free on cleanup to tests * Update test/python/c_api/ffi.py Co-authored-by: Julien Gacon <gaconju@gmail.com> * Add missing layout free * Fix lint from code suggestion * Add docstring to build_homogenous_target() * Add more descriptive test name * Add useless docstring to methods --------- Co-authored-by: Julien Gacon <gaconju@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Right now the Qiskit C API (and by extension the internal rust code) is
lacking some of the helper utilities necessary to do deeper validation
of the output circuits. Mainly
Operator.from_circuitto do unitaryequivalence checks accounting for a layout introduced by the transpiler.
But also things like just circuit equality checks are easier in Python
because the tooling doesn't exist in pure-Rust/C yet. As we're building
the C API to be the universal interface to Qiskit from any programming
language it is very easy to leverage the C API from Python.
This commit adds a new testing framework to the Python tests that builds
a C FFI using ctypes to call into the loaded compiled extension (which
already bundles the C API). It then wraps that ffi in a couple of helper
functions to go from a Python circuit to a C circuit, build a c target
from basis gates and a coupling map, and then a function to transpile a
c circuit using the C api which returns a Python circuit. This enables
us to write tests in Python and validate the output circuits are
equivalent accounting for layout and permutations the transpiler
applies. It also lets us use any other tooling we have in Python for
testing.
This framework does not eliminate the need for
testing the C API directly from C, it is only intended to do deeper
testing while the functionality exposed to C is more limited
specifically for the transpiler.
Details and comments
TODO:
This PR depends on #14760 and will need to be rebased after #14760 merges. To see the contents of the just this PR you can look at the HEAD commit: 14ed71b