Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions crates/cext/src/transpiler/passes/convert_to_pauli_rotations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// This code is part of Qiskit.
Comment thread
ShellyGarion marked this conversation as resolved.
//
// (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");
Comment thread
alexanderivrii marked this conversation as resolved.
}
51 changes: 51 additions & 0 deletions crates/cext/src/transpiler/passes/litinski_transformation.rs
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,
) {
Comment thread
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")
};
}
2 changes: 2 additions & 0 deletions crates/cext/src/transpiler/passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
pub mod basis_translator;
pub mod commutative_cancellation;
pub mod consolidate_blocks;
pub mod convert_to_pauli_rotations;
pub mod elide_permutations;
pub mod gate_direction;
pub mod inverse_cancellation;
pub mod litinski_transformation;
pub mod optimize_1q_sequences;
pub mod remove_diagonal_gates_before_measure;
pub mod remove_identity_equiv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ fn generate_pauli_product_rotation_gate(paulis: &[BitTerm], angle: Param) -> Pau
/// the pass.
#[pyfunction]
#[pyo3(name = "convert_to_pauli_rotations")]
pub fn py_convert_to_pauli_rotations(dag: &mut DAGCircuit) -> PyResult<DAGCircuit> {
pub fn py_convert_to_pauli_rotations(dag: &DAGCircuit) -> PyResult<DAGCircuit> {
let mut new_dag = dag.copy_empty_like(VarsMode::Alike, BlocksMode::Drop)?;

// Iterate over nodes in the DAG and collect nodes
Expand Down
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.
Comment thread
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.
Loading