Skip to content

Add C API testing framework to Python tests#14958

Merged
Cryoris merged 15 commits into
Qiskit:mainfrom
mtreinish:add-tests-from-python
Nov 19, 2025
Merged

Add C API testing framework to Python tests#14958
Cryoris merged 15 commits into
Qiskit:mainfrom
mtreinish:add-tests-from-python

Conversation

@mtreinish

@mtreinish mtreinish commented Aug 27, 2025

Copy link
Copy Markdown
Member

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_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.

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

@mtreinish mtreinish requested a review from a team as a code owner August 27, 2025 21:24
@mtreinish mtreinish added type: qa Issues and PRs that relate to testing and code quality Changelog: None Do not include in the GitHub Release changelog. C API Related to the C API labels Aug 27, 2025
@qiskit-bot

Copy link
Copy Markdown
Collaborator

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

@mtreinish mtreinish marked this pull request as draft August 27, 2025 21:24
@coveralls

coveralls commented Aug 27, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 19481750170

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

  • 10 of 10 (100.0%) changed or added relevant lines in 1 file are covered.
  • 14 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.02%) to 88.216%

Files with Coverage Reduction New Missed Lines %
crates/circuit/src/parameter/parameter_expression.rs 1 81.99%
crates/transpiler/src/passes/gate_direction.rs 1 96.86%
crates/qasm2/src/lex.rs 3 92.54%
crates/circuit/src/parameter/symbol_expr.rs 9 72.94%
Totals Coverage Status
Change from base Build 19420218711: 0.02%
Covered Lines: 94267
Relevant Lines: 106859

💛 - Coveralls

@mtreinish mtreinish force-pushed the add-tests-from-python branch from 14ed71b to 27f0db8 Compare August 28, 2025 17:38
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.
@mtreinish mtreinish force-pushed the add-tests-from-python branch from 27f0db8 to 3ccbe40 Compare August 30, 2025 02:15
@mtreinish mtreinish marked this pull request as ready for review August 30, 2025 02:16
@qiskit-bot

Copy link
Copy Markdown
Collaborator

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

  • @Qiskit/terra-core

@Cryoris Cryoris left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very nice to enable this testing! Once the qk_obs_apply_layout is merged we should add observable tests as a follow up.

Comment thread crates/cext/src/transpiler/transpile_layout.rs
Comment thread test/python/c_api/ffi.py
Comment thread test/python/c_api/ffi.py Outdated
Comment thread test/python/c_api/ffi.py
Comment thread test/python/c_api/ffi.py
Comment thread test/python/c_api/ffi.py
Comment thread test/python/c_api/test_transpile.py
Comment thread test/python/c_api/test_transpile.py
Comment thread test/python/c_api/test_transpile.py Outdated
Comment thread test/python/c_api/test_transpile.py
@mtreinish mtreinish requested a review from Cryoris November 6, 2025 19:26
@Cryoris Cryoris added this pull request to the merge queue Nov 19, 2025
Merged via the queue into Qiskit:main with commit 536371c Nov 19, 2025
23 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to Done in Qiskit 2.3 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>
@mtreinish mtreinish deleted the add-tests-from-python branch February 17, 2026 12:23
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: None Do not include in the GitHub Release changelog. type: qa Issues and PRs that relate to testing and code quality

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants