Skip to content

C API: Burn Optimize1qGatesDecomposition.#14710

Merged
Cryoris merged 25 commits into
Qiskit:mainfrom
raynelfss:burn-optimize-1q-decomp
Aug 28, 2025
Merged

C API: Burn Optimize1qGatesDecomposition.#14710
Cryoris merged 25 commits into
Qiskit:mainfrom
raynelfss:burn-optimize-1q-decomp

Conversation

@raynelfss
Copy link
Copy Markdown
Contributor

@raynelfss raynelfss commented Jul 8, 2025

Summary

After oxidizing Optimize1QGatesDecomposition in #12959 it's time to make a combustion reaction that releases a carbon byproduct (our C API for it). The following commits aim to expose a standalone function that will give access to the rust counterpart.

This implementation adds the function qk_transpiler_standalone_optimize_1q_gates_decomposition which accepts a QkCircuit and an optional instance of QkTarget and calls the Rust counterpart.

Details and comments

Closes #14453. Built on top of #14668 (NEEDS REBASING AFTER IT MERGES). REBASED!

@raynelfss raynelfss added this to the 2.2.0 milestone Jul 8, 2025
@raynelfss raynelfss added Changelog: None Do not include in the GitHub Release changelog. mod: transpiler Issues and PRs related to Transpiler C API Related to the C API labels Jul 8, 2025
@raynelfss raynelfss changed the title [WIP] C API: Burn Optimize1qGateDecomposition. [WIP] C API: Burn Optimize1qGatesDecomposition. Jul 8, 2025
@coveralls
Copy link
Copy Markdown

coveralls commented Jul 8, 2025

Pull Request Test Coverage Report for Build 17244313498

Details

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

Files with Coverage Reduction New Missed Lines %
crates/circuit/src/parameter/parameter_expression.rs 1 82.8%
crates/qasm2/src/expr.rs 1 93.63%
crates/circuit/src/parameter/symbol_expr.rs 2 72.82%
crates/qasm2/src/lex.rs 3 91.24%
Totals Coverage Status
Change from base Build 17244219599: 0.02%
Covered Lines: 90135
Relevant Lines: 101974

💛 - Coveralls

@raynelfss raynelfss force-pushed the burn-optimize-1q-decomp branch 2 times, most recently from 89fdfc0 to f122074 Compare July 9, 2025 19:30
@raynelfss raynelfss marked this pull request as ready for review July 10, 2025 18:02
@raynelfss raynelfss requested a review from a team as a code owner July 10, 2025 18:02
@qiskit-bot
Copy link
Copy Markdown
Collaborator

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

@raynelfss raynelfss changed the title [WIP] C API: Burn Optimize1qGatesDecomposition. C API: Burn Optimize1qGatesDecomposition. Jul 10, 2025
@raynelfss raynelfss added Changelog: Added Add an "Added" entry in the GitHub Release changelog. and removed Changelog: None Do not include in the GitHub Release changelog. labels Jul 10, 2025
raynelfss and others added 7 commits July 15, 2025 11:18
- The following commit adds initial access to a standalone C foreign function interface that allows users to run the `Optimize1qGatesDecomposition` transpiler pass. The feature as it stands requires the user to build a target prior to submitting the circuit; This might change in the future.
- Added 1 initial testing suite for the pass which converts a sequence of H gates into a different sequences depending on the provided target. More tests will be added with time.
… API.

- Add ability of running the pass without passing a `Target`.
- Add proper formatting to unit tests with multiple tests inside.
- Add tests without target.
@raynelfss raynelfss force-pushed the burn-optimize-1q-decomp branch from 42b7c38 to cfc03cb Compare July 15, 2025 15:18
@raynelfss
Copy link
Copy Markdown
Contributor Author

This PR is currently blocked on #14777

Comment thread crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs Outdated
/// qubit it operates on.
///
/// @param circuit A pointer to the ``QkCircuit`` object to transform.
/// @param target A pointer to the ``QkTarget`` object.
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.

From the code it looks like this is allowed to be a null pointer. What happens in this case? We should document that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From snippet:

/// In the case a null pointer is provided and gate errors are unknown
/// the pass will choose the sequence with the least amount of gates,
/// and will support all basis gates on its Euler basis set.

Comment thread crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs Outdated
Comment thread crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs Outdated
Comment thread crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
raynelfss and others added 5 commits August 12, 2025 09:32
- Make tha function call run in-place and modify the pointer to the `QkCircuit` in use.
- Remove `h_p_target` from tests.
- Use parametric gates in the target with generic parameter expressions instead of fixed ones.
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Comment thread crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs Outdated
Copy link
Copy Markdown
Collaborator

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

Overall LGTM, some nits below and then I think there are 2 open comments left: one about renaming this pass (which I still think we should take the chance for) and one about the docs saying that this pass compresses sequences into a single gate (which it doesn't, right?)

Comment thread crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs Outdated
Comment on lines +90 to +91
let mut circuit_as_dag = DAGCircuit::from_circuit_data(circuit, false, None, None, None, None)
.expect("Error while converting the circuit to a dag.");
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.

It would be nice if we would have a set of TranspilerErrors which we could return here instead of just panicking... but maybe this would need to be done generally for all passes in a follow-up.

Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
@Cryoris
Copy link
Copy Markdown
Collaborator

Cryoris commented Aug 18, 2025

This LGTM up to the two comments above on the documentation (this pass does not merge gates into a single gate, the Python docs are also wrong 🙂) and I still think we should rename this pass 🙂

Copy link
Copy Markdown
Collaborator

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

some small comments about memory freeing otherwise I think we can merge 👍🏻

Comment thread test/c/test_optimize_1q_decomposition.c Outdated
Comment thread test/c/test_optimize_1q_decomposition.c
Comment thread test/c/test_optimize_1q_decomposition.c Outdated
- Rename files from `optimize_1q_decomposition` to `optiize_1q_sequences`.
- Fixed previous oversight that counted operations before the pass was ran.
Cryoris
Cryoris previously approved these changes Aug 25, 2025
Copy link
Copy Markdown
Collaborator

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

LGTM, I have two optional comments which we can also clean up in a follow up.

Comment thread test/c/test_optimize_1q_sequences.c Outdated
Comment thread releasenotes/notes/c-api-optimize-1q-decomp-88954adfe952e91f.yaml Outdated
- Update release note to include explanation as to what happens if a `Target` is not provided.
- Update helper function `compare_gate_counts` to take `counts` by reference.
@Cryoris Cryoris added this pull request to the merge queue Aug 28, 2025
Merged via the queue into Qiskit:main with commit e868803 Aug 28, 2025
26 checks passed
littlebullGit pushed a commit to littlebullGit/qiskit that referenced this pull request Sep 5, 2025
* Initial: Introduce C-API Access to `Optimize1qGatesDecomposition`.
- The following commit adds initial access to a standalone C foreign function interface that allows users to run the `Optimize1qGatesDecomposition` transpiler pass. The feature as it stands requires the user to build a target prior to submitting the circuit; This might change in the future.
- Added 1 initial testing suite for the pass which converts a sequence of H gates into a different sequences depending on the provided target. More tests will be added with time.

* Fix: Add target identity test.

* Chore: Fix docstring and free some pointers.

* Add: Support for optional target for `Optimize1qGatesDecomposition` C API.
- Add ability of running the pass without passing a `Target`.
- Add proper formatting to unit tests with multiple tests inside.
- Add tests without target.

* Chore: Add release note.

* Chore: Fix docstring.

* Chore: Fix bug in release note

* Chore: Address review comments
- Make tha function call run in-place and modify the pointer to the `QkCircuit` in use.
- Remove `h_p_target` from tests.
- Use parametric gates in the target with generic parameter expressions instead of fixed ones.

* Update crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs

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

* Chore: Fix docstring

* Chore: Fix release note

* Update test/c/test_optimize_1q_decomposition.c

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

* Chore: Address review comments.

* Fix: Review comments

* Fix: Address some memory leakage.

* Fix: Incorrect function name call

* Chore: Address different review items
- Rename files from `optimize_1q_decomposition` to `optiize_1q_sequences`.
- Fixed previous oversight that counted operations before the pass was ran.

* Fix: Replace `QkOpCounts` failures until it is fixed.

* Fix: Add final review comments.
- Update release note to include explanation as to what happens if a `Target` is not provided.
- Update helper function `compare_gate_counts` to take `counts` by reference.

* Fix: Cformatting issues.

* Fix: More c formatting issues.

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
aaryav-3 pushed a commit to aaryav-3/qiskit that referenced this pull request Oct 21, 2025
* Initial: Introduce C-API Access to `Optimize1qGatesDecomposition`.
- The following commit adds initial access to a standalone C foreign function interface that allows users to run the `Optimize1qGatesDecomposition` transpiler pass. The feature as it stands requires the user to build a target prior to submitting the circuit; This might change in the future.
- Added 1 initial testing suite for the pass which converts a sequence of H gates into a different sequences depending on the provided target. More tests will be added with time.

* Fix: Add target identity test.

* Chore: Fix docstring and free some pointers.

* Add: Support for optional target for `Optimize1qGatesDecomposition` C API.
- Add ability of running the pass without passing a `Target`.
- Add proper formatting to unit tests with multiple tests inside.
- Add tests without target.

* Chore: Add release note.

* Chore: Fix docstring.

* Chore: Fix bug in release note

* Chore: Address review comments
- Make tha function call run in-place and modify the pointer to the `QkCircuit` in use.
- Remove `h_p_target` from tests.
- Use parametric gates in the target with generic parameter expressions instead of fixed ones.

* Update crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs

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

* Chore: Fix docstring

* Chore: Fix release note

* Update test/c/test_optimize_1q_decomposition.c

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

* Chore: Address review comments.

* Fix: Review comments

* Fix: Address some memory leakage.

* Fix: Incorrect function name call

* Chore: Address different review items
- Rename files from `optimize_1q_decomposition` to `optiize_1q_sequences`.
- Fixed previous oversight that counted operations before the pass was ran.

* Fix: Replace `QkOpCounts` failures until it is fixed.

* Fix: Add final review comments.
- Update release note to include explanation as to what happens if a `Target` is not provided.
- Update helper function `compare_gate_counts` to take `counts` by reference.

* Fix: Cformatting issues.

* Fix: More c formatting issues.

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
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: Added Add an "Added" entry in the GitHub Release changelog. mod: transpiler Issues and PRs related to Transpiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Optimize1qGatesDecomposition to the C API

5 participants