-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathconsolidate_blocks.rs
More file actions
70 lines (65 loc) · 2.56 KB
/
Copy pathconsolidate_blocks.rs
File metadata and controls
70 lines (65 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 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 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_consolidate_blocks, target::Target};
use crate::pointers::{const_ptr_as_ref, mut_ptr_as_ref};
/// @ingroup QkTranspilerPassesStandalone
/// Run the ConsolidateBlocks pass on a circuit.
///
/// ConsolidateBlocks is a transpiler pass that consolidates consecutive blocks of
/// gates operating on the same qubits into a Unitary gate, to later on be
/// resynthesized, which leads to a more optimal subcircuit.
///
/// @param circuit A pointer to the circuit to run ConsolidateBlocks on.
/// @param target A pointer to the target to run ConsolidateBlocks on.
/// @param approximation_degree A float between `[0.0, 1.0]` or a `NaN` which
/// defaults to `1.0`. Lower approximates more.
/// @param force_consolidate: Force block consolidation.
///
/// # 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``.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn qk_transpiler_pass_standalone_consolidate_blocks(
circuit: *mut CircuitData,
target: *const Target,
approximation_degree: f64,
force_consolidate: bool,
) {
let circuit = unsafe { mut_ptr_as_ref(circuit) };
let target = unsafe {
if target.is_null() {
None
} else {
Some(const_ptr_as_ref(target))
}
};
let approximation_degree = if approximation_degree.is_nan() {
1.0
} else {
approximation_degree
};
let mut circ_as_dag = DAGCircuit::from_circuit_data(circuit, true, None, None)
.expect("Error while converting from CircuitData to DAGCircuit.");
// Call the pass
run_consolidate_blocks(
&mut circ_as_dag,
force_consolidate,
Some(approximation_degree),
target,
)
.expect("Error running the consolidate blocks pass.");
let result_circuit = CircuitData::from_dag_ref(&circ_as_dag)
.expect("Error while converting from DAGCircuit to CircuitData.");
*circuit = result_circuit;
}