-
Notifications
You must be signed in to change notification settings - Fork 3k
C API: Burn Optimize1qGatesDecomposition.
#14710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
67a495a
Initial: Introduce C-API Access to `Optimize1qGatesDecomposition`.
raynelfss 3f053cb
Fix: Add target identity test.
raynelfss a77ce5e
Chore: Fix docstring and free some pointers.
raynelfss 0fe11b8
Add: Support for optional target for `Optimize1qGatesDecomposition` C…
raynelfss 84c6bf3
Chore: Add release note.
raynelfss 6edc33c
Chore: Fix docstring.
raynelfss cfc03cb
Chore: Fix bug in release note
raynelfss 7ee5c4a
Merge remote-tracking branch 'upstream/main' into burn-optimize-1q-de…
raynelfss cf16fa9
Chore: Address review comments
raynelfss 619ec0d
Update crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs
raynelfss 7e8a147
Chore: Fix docstring
raynelfss b4ba388
Chore: Fix release note
raynelfss 4ab2e26
Update test/c/test_optimize_1q_decomposition.c
raynelfss 19f55a6
Chore: Address review comments.
raynelfss 25cd6cf
Merge remote-tracking branch 'upstream/main' into burn-optimize-1q-de…
raynelfss 923a8bb
Fix: Review comments
raynelfss 0a75d7c
Fix: Address some memory leakage.
raynelfss 01d280e
Fix: Incorrect function name call
raynelfss 9315826
Chore: Address different review items
raynelfss b4bd4ba
Fix: Replace `QkOpCounts` failures until it is fixed.
raynelfss 82ce69d
Merge remote-tracking branch 'upstream/main' into burn-optimize-1q-de…
raynelfss 9224253
Fix: Add final review comments.
raynelfss b40298b
Fix: Cformatting issues.
raynelfss 85317c9
Fix: More c formatting issues.
raynelfss 1b86842
Merge remote-tracking branch 'upstream/main' into burn-optimize-1q-de…
raynelfss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| // This code is part of Qiskit. | ||
| // | ||
| // (C) Copyright IBM 2025 | ||
| // | ||
| // This code is licensed under the Apache License, Version 2.0. You may | ||
| // obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| // of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| // | ||
| // Any modifications or derivative works of this code must retain this | ||
| // copyright notice, and modified files need to carry a notice indicating | ||
| // that they have been altered from the originals. | ||
|
|
||
| pub mod elide_permutations; | ||
| pub mod optimize_1q_decomposition; | ||
| pub mod remove_identity_equiv; | ||
| pub mod vf2; |
103 changes: 103 additions & 0 deletions
103
crates/cext/src/transpiler/passes/optimize_1q_decomposition.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // This code is part of Qiskit. | ||
| // | ||
| // (C) Copyright IBM 2025 | ||
| // | ||
| // This code is licensed under the Apache License, Version 2.0. You may | ||
| // obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| // of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| // | ||
| // Any modifications or derivative works of this code must retain this | ||
| // copyright notice, and modified files need to carry a notice indicating | ||
| // that they have been altered from the originals. | ||
|
|
||
| use crate::pointers::{const_ptr_as_ref, mut_ptr_as_ref}; | ||
| use qiskit_circuit::{ | ||
| circuit_data::CircuitData, converters::dag_to_circuit, dag_circuit::DAGCircuit, | ||
| }; | ||
| use qiskit_transpiler::{passes::run_optimize_1q_gates_decomposition, target::Target}; | ||
|
|
||
| /// @ingroup QkTranspilerPasses | ||
| /// Runs the Optimize1qGatesDecomposition pass in standalone mode on a circuit. | ||
| /// | ||
| /// Optimize1qGatesDecomposition, as its name implies, optimizes chains of single-qubit | ||
| /// gates by combining them into a single gate. | ||
| /// | ||
| /// The decision of whether to replace the original chain depends on: | ||
| /// - If the original chain was out of basis. | ||
| /// - If the original chain was in basis but the replacement has lower error rates. | ||
| /// - If the original chain is an identity (chain gets removed). | ||
| /// | ||
| /// The error is the combined multiplication of the errors of individual gates on the | ||
| /// qubit it operates on. | ||
| /// | ||
| /// @param circuit A pointer to the ``QkCircuit`` object to transform. | ||
| /// @param target A pointer to the ``QkTarget`` object or a null pointer. | ||
| /// In the case a null pointer is provided the pass calculates the error | ||
| /// of each instruction based on each one-qubit sequence's length, and | ||
| /// will support all basis gates on its Euler basis set. | ||
|
raynelfss marked this conversation as resolved.
Outdated
|
||
| /// | ||
| /// @return The circuit after applying the optimizations. | ||
|
raynelfss marked this conversation as resolved.
Outdated
|
||
| /// | ||
| /// # Example | ||
| /// | ||
| /// QkTarget *target = qk_target_new(1); | ||
| /// double u_errors[3] = {0., 1e-4, 1e-4}; | ||
| /// for (int idx = 0; idx < 3; idx++) { | ||
| /// QkTargetEntry *u_entry = qk_target_entry_new(QkGate_U); | ||
| /// uint32_t qargs[1] = { | ||
| /// 0, | ||
| /// }; | ||
| /// qk_target_entry_add_property(u_entry, qargs, 1, NAN, u_errors[idx]); | ||
| /// qk_target_add_instruction(target, u_entry); | ||
| /// } | ||
| /// | ||
| /// // Build circuit | ||
| /// QkCircuit *circuit = qk_circuit_new(1, 0); | ||
| /// uint32_t qubits[1] = {0}; | ||
| /// for (int iter = 0; iter < 3; iter++) { | ||
| /// qk_circuit_gate(circuit, QkGate_H, qubits, NULL); | ||
| /// } | ||
| /// | ||
| /// // Run transpiler pass | ||
| /// qk_transpiler_standalone_optimize_1q_gates_decomposition(circuit, target); | ||
| /// | ||
| /// // Clean up | ||
| /// qk_target_free(target); | ||
| /// qk_circuit_free(circuit); | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Behavior is undefined if ``circuit`` is not a valid, non-null pointer to a ``QkCircuit`` and | ||
| /// if ``target`` is not a valid pointer to a ``QkTarget``. | ||
| #[no_mangle] | ||
| #[cfg(feature = "cbinding")] | ||
| pub unsafe extern "C" fn qk_transpiler_standalone_optimize_1q_gates_decomposition( | ||
|
raynelfss marked this conversation as resolved.
Outdated
|
||
| circuit: *mut CircuitData, | ||
| target: *const Target, | ||
| ) { | ||
| // SAFETY: Per documentation, the pointer is non-null and aligned. | ||
| let target = unsafe { | ||
| if target.is_null() { | ||
| None | ||
| } else { | ||
| Some(const_ptr_as_ref(target)) | ||
| } | ||
| }; | ||
| // SAFETY: Per documentation, the pointer is non-null and aligned. | ||
| let circuit = unsafe { mut_ptr_as_ref(circuit) }; | ||
|
|
||
| // Convert the circuit to a DAG. | ||
| let mut circuit_as_dag = DAGCircuit::from_circuit_data(circuit, false, None, None, None, None) | ||
| .expect("Error while converting the circuit to a dag."); | ||
|
Comment on lines
+90
to
+
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if we would have a set of |
||
|
|
||
| // Run the pass | ||
| run_optimize_1q_gates_decomposition(&mut circuit_as_dag, target, None, None) | ||
| .expect("Error while running the pass."); | ||
|
|
||
| // Convert the DAGCircuit back to an instance of CircuitData | ||
| let dag_to_circuit = dag_to_circuit(&circuit_as_dag, false) | ||
| .expect("Error while converting the dag to a circuit."); | ||
|
|
||
| // Convert to pointer. | ||
| *circuit = dag_to_circuit; | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
releasenotes/notes/c-api-optimize-1q-decomp-88954adfe952e91f.yaml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| features_c: | ||
| - | | ||
| Add support for the :class:`.Optimize1qGatesDecomposition` transpiler pass through | ||
| the C API, via the :cpp:func:`qk_transpiler_standalone_optimize_1q_gates_decomposition` | ||
| method. | ||
|
|
||
| Here's an example: | ||
|
|
||
| .. code-block:: C | ||
|
|
||
| #include <qiskit.h> | ||
|
|
||
| // Build a circuit | ||
| QkCircuit *circuit = qk_circuit_new(1, 0); | ||
| uint32_t qubits[1] = {0}; | ||
| double params_pos[1] = {3.14 / 7.}; | ||
| double params_neg[1] = {-3.14 / 7.}; | ||
| qk_circuit_gate(circuit, QkGate_RY, qubits, params_pos); | ||
| qk_circuit_gate(circuit, QkGate_RY, qubits, params_neg); | ||
|
|
||
| // Run the transpiler pass with no Target | ||
|
raynelfss marked this conversation as resolved.
Outdated
|
||
| qk_transpiler_standalone_optimize_1q_gates_decomposition(circuit, NULL); | ||
|
|
||
| // Free both circuits | ||
| qk_circuit_free(circuit); | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.