|
| 1 | +// This code is part of Qiskit. |
| 2 | +// |
| 3 | +// (C) Copyright IBM 2025 |
| 4 | +// |
| 5 | +// This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +// obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +// |
| 9 | +// Any modifications or derivative works of this code must retain this |
| 10 | +// copyright notice, and modified files need to carry a notice indicating |
| 11 | +// that they have been altered from the originals. |
| 12 | + |
| 13 | +use crate::pointers::{const_ptr_as_ref, mut_ptr_as_ref}; |
| 14 | +use qiskit_circuit::{ |
| 15 | + circuit_data::CircuitData, converters::dag_to_circuit, dag_circuit::DAGCircuit, |
| 16 | +}; |
| 17 | +use qiskit_transpiler::{passes::run_optimize_1q_gates_decomposition, target::Target}; |
| 18 | + |
| 19 | +/// @ingroup QkTranspilerPasses |
| 20 | +/// Runs the Optimize1qGatesDecomposition pass in standalone mode on a circuit. |
| 21 | +/// |
| 22 | +/// Optimize1qGatesDecomposition optimizes single-qubit gate sequences by re-synthesizing |
| 23 | +/// the unitary under the constraints of the target's basis gates and error rates. |
| 24 | +/// |
| 25 | +/// The decision of whether to replace the original chain depends on: |
| 26 | +/// - If the original chain was out of basis. |
| 27 | +/// - If the original chain was in basis but the replacement has lower error rates. |
| 28 | +/// - If the original chain is an identity (chain gets removed). |
| 29 | +/// |
| 30 | +/// The error is the combined multiplication of the errors of individual gates on the |
| 31 | +/// qubit it operates on. |
| 32 | +/// |
| 33 | +/// @param circuit A pointer to the ``QkCircuit`` object to transform. |
| 34 | +/// @param target A pointer to the ``QkTarget`` object or a null pointer. |
| 35 | +/// In the case a null pointer is provided and gate errors are unknown |
| 36 | +/// the pass will choose the sequence with the least amount of gates, |
| 37 | +/// and will support all basis gates on its Euler basis set. |
| 38 | +/// |
| 39 | +/// # Example |
| 40 | +/// |
| 41 | +/// QkTarget *target = qk_target_new(1); |
| 42 | +/// double u_errors[3] = {0., 1e-4, 1e-4}; |
| 43 | +/// for (int idx = 0; idx < 3; idx++) { |
| 44 | +/// QkTargetEntry *u_entry = qk_target_entry_new(QkGate_U); |
| 45 | +/// uint32_t qargs[1] = { |
| 46 | +/// 0, |
| 47 | +/// }; |
| 48 | +/// qk_target_entry_add_property(u_entry, qargs, 1, NAN, u_errors[idx]); |
| 49 | +/// qk_target_add_instruction(target, u_entry); |
| 50 | +/// } |
| 51 | +/// |
| 52 | +/// // Build circuit |
| 53 | +/// QkCircuit *circuit = qk_circuit_new(1, 0); |
| 54 | +/// uint32_t qubits[1] = {0}; |
| 55 | +/// for (int iter = 0; iter < 3; iter++) { |
| 56 | +/// qk_circuit_gate(circuit, QkGate_H, qubits, NULL); |
| 57 | +/// } |
| 58 | +/// |
| 59 | +/// // Run transpiler pass |
| 60 | +/// qk_transpiler_standalone_optimize_1q_sequences(circuit, target); |
| 61 | +/// |
| 62 | +/// // Clean up |
| 63 | +/// qk_target_free(target); |
| 64 | +/// qk_circuit_free(circuit); |
| 65 | +/// |
| 66 | +/// # Safety |
| 67 | +/// |
| 68 | +/// Behavior is undefined if ``circuit`` is not a valid, non-null pointer to a ``QkCircuit`` and |
| 69 | +/// if ``target`` is not a valid pointer to a ``QkTarget``. |
| 70 | +#[no_mangle] |
| 71 | +#[cfg(feature = "cbinding")] |
| 72 | +pub unsafe extern "C" fn qk_transpiler_standalone_optimize_1q_sequences( |
| 73 | + circuit: *mut CircuitData, |
| 74 | + target: *const Target, |
| 75 | +) { |
| 76 | + // SAFETY: Per documentation, the pointer is non-null and aligned. |
| 77 | + let target = unsafe { |
| 78 | + if target.is_null() { |
| 79 | + None |
| 80 | + } else { |
| 81 | + Some(const_ptr_as_ref(target)) |
| 82 | + } |
| 83 | + }; |
| 84 | + // SAFETY: Per documentation, the pointer is non-null and aligned. |
| 85 | + let circuit = unsafe { mut_ptr_as_ref(circuit) }; |
| 86 | + |
| 87 | + // Convert the circuit to a DAG. |
| 88 | + let mut circuit_as_dag = DAGCircuit::from_circuit_data(circuit, false, None, None, None, None) |
| 89 | + .expect("Error while converting the circuit to a dag."); |
| 90 | + |
| 91 | + // Run the pass |
| 92 | + run_optimize_1q_gates_decomposition(&mut circuit_as_dag, target, None, None) |
| 93 | + .expect("Error while running the pass."); |
| 94 | + |
| 95 | + // Convert the DAGCircuit back to an instance of CircuitData |
| 96 | + let dag_to_circuit = dag_to_circuit(&circuit_as_dag, false) |
| 97 | + .expect("Error while converting the dag to a circuit."); |
| 98 | + |
| 99 | + // Convert to pointer. |
| 100 | + *circuit = dag_to_circuit; |
| 101 | +} |
0 commit comments