-
Notifications
You must be signed in to change notification settings - Fork 2.9k
PBC transformations for C #15756
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
PBC transformations for C #15756
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f0f6c14
Expose PBC operations to C
Cryoris 7a41044
Fix docs & swap ZX order to be consistent with Pauli
Cryoris c089533
Expose LitinskiTrafo to C
Cryoris 1c59a0e
Add ConvertToPauliRotations to C
Cryoris 6be045d
Extra sausage for win math
Cryoris 0dba1d4
Fix compat with lates main
Cryoris 4640a42
Shelly's comments
Cryoris c2e32dd
Merge branch 'main' into pbc-trafo-for-c
Cryoris 12b7fac
review comments
Cryoris 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
45 changes: 45 additions & 0 deletions
45
crates/cext/src/transpiler/passes/convert_to_pauli_rotations.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,45 @@ | ||
| // This code is part of Qiskit. | ||
| // | ||
| // (C) Copyright IBM 2026 | ||
| // | ||
| // 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 https://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 qiskit_circuit::{circuit_data::CircuitData, dag_circuit::DAGCircuit}; | ||
| use qiskit_transpiler::passes::py_convert_to_pauli_rotations; | ||
|
|
||
| use crate::pointers::mut_ptr_as_ref; | ||
|
|
||
| /// @ingroup QkTranspilerPassesStandalone | ||
| /// Run the ``ConvertToPauliRotations`` pass in-place on a circuit. | ||
| /// | ||
| /// This pass converts all standard gates (with less than 4 qubits) in the circuit into a sequence | ||
| /// of ``QkPauliProductRotation`` gates and measurements into ``QkPauliProductMeasurement`` | ||
| /// instructions. Note that this pass panics if the circuit contains non-standard gates. | ||
| /// The suggested workflow is to first transpile into a standard basis, keeping rotation gates | ||
| /// (such as ``QkGate_RXX`` and others) intact where possible, and then call this pass. | ||
| /// | ||
| /// @param circuit A pointer to the circuit on which to run the pass. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Behavior is undefined if ``circuit`` is not valid, non-null pointers to a ``QkCircuit``. | ||
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn qk_transpiler_pass_standalone_convert_to_pauli_rotations( | ||
| circuit: *mut CircuitData, | ||
| ) { | ||
| // SAFETY: The user guarantees the pointer is safe to read. | ||
| let circuit = unsafe { mut_ptr_as_ref(circuit) }; | ||
| let dag = match DAGCircuit::from_circuit_data(circuit, false, None, None, None, None) { | ||
| Ok(dag) => dag, | ||
| Err(_) => panic!("Internal Circuit -> DAG conversion failed"), | ||
| }; | ||
| let out = py_convert_to_pauli_rotations(&dag).expect("Failed running PBC conversion."); | ||
| // If a DAG is returned, the circuit has been modified. Else just leave it as is. | ||
| *circuit = CircuitData::from_dag_ref(&out).expect("Internal DAG -> Circuit conversion failed"); | ||
|
alexanderivrii marked this conversation as resolved.
|
||
| } | ||
51 changes: 51 additions & 0 deletions
51
crates/cext/src/transpiler/passes/litinski_transformation.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,51 @@ | ||
| // This code is part of Qiskit. | ||
| // | ||
| // (C) Copyright IBM 2026 | ||
| // | ||
| // 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 https://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 qiskit_circuit::{circuit_data::CircuitData, dag_circuit::DAGCircuit}; | ||
| use qiskit_transpiler::passes::run_litinski_transformation; | ||
|
|
||
| use crate::pointers::mut_ptr_as_ref; | ||
|
|
||
| /// @ingroup QkTranspilerPassesStandalone | ||
| /// Run the ``LitinskiTransformation`` pass in-place on a circuit. | ||
| /// | ||
| /// This pass commutes all Clifford gates to the end of the circuit, converting Pauli rotation | ||
| /// gates into ``QkPauliProductRotation`` gates and measurements into ``QkPauliProductMeasurement`` | ||
| /// instructions. Note that this pass currently only supports circuits that have ``QkGate_T``, | ||
| /// ``QkGate_Tdg`` or ``QkGate_RZ`` gates as non-Cliffords and panics otherwise. The suggested | ||
| /// workflow is to first transpile into a Clifford+RZ basis and then call this pass. | ||
| /// | ||
| /// @param circuit A pointer to the circuit on which to run the pass. | ||
| /// @param fix_clifford If ``true``, leave the Clifford gates at the end of the circuit. If | ||
| /// ``false`` they are omitted. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Behavior is undefined if ``circuit`` is not valid, non-null pointers to a ``QkCircuit``. | ||
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn qk_transpiler_pass_standalone_litinski_transformation( | ||
| circuit: *mut CircuitData, | ||
| fix_clifford: bool, | ||
| ) { | ||
|
alexanderivrii marked this conversation as resolved.
|
||
| // SAFETY: The user guarantees the pointer is safe to read. | ||
| let circuit = unsafe { mut_ptr_as_ref(circuit) }; | ||
| let dag = DAGCircuit::from_circuit_data(circuit, false, None, None, None, None) | ||
| .expect("Internal Circuit -> DAG conversion failed"); | ||
|
|
||
| let maybe_out = run_litinski_transformation(&dag, fix_clifford, false, true) | ||
| .expect("Failed running Litinski transformation"); | ||
| // If a DAG is returned, the circuit has been modified. Else just leave it as is. | ||
| if let Some(out) = maybe_out { | ||
| *circuit = | ||
| CircuitData::from_dag_ref(&out).expect("Internal DAG -> Circuit conversion failed") | ||
| }; | ||
| } | ||
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
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
9 changes: 9 additions & 0 deletions
9
releasenotes/notes/pbc-transformations-c-7dc8230831d133c3.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,9 @@ | ||
| --- | ||
| features_c: | ||
| - Added :c:func:`qk_transpiler_pass_standalone_litinski_transformation` to apply a Litinski | ||
| transformation to a ``QkCircuit`` in the C API. This is the equivalent of Python's | ||
| :class:`.LitinskiTransformation` pass. | ||
|
ShellyGarion marked this conversation as resolved.
|
||
| - Added :c:func:`qk_transpiler_pass_standalone_convert_to_pauli_rotations` to convert a | ||
| ``QkCircuit`` made up of standard gates and measurements into Pauli-based computation format | ||
| using ``QkPauliProductRotation`` and ``QkPauliProductMeasurement`` instructions. | ||
| This is the equivalent of Python's :class:`.ConvertToPauliRotations` pass. | ||
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.