Skip to main content

cqlib_core/compile/transform/resynthesis/
config.rs

1// This code is part of Cqlib.
2//
3// (C) Copyright China Telecom Quantum Group 2026
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//! Configuration for numeric two-qubit block resynthesis.
14
15use crate::compile::commutation::CommutationConfig;
16use crate::compile::transform::decompose::unitary::TwoQubitSynthesisTarget;
17
18/// Configuration for ordinary standard-gate two-qubit block resynthesis.
19///
20/// `normal` is intended for the default compilation pipeline. `enhanced`
21/// increases bounded search budgets and is used where compile-time budget is
22/// explicitly traded for better post-routing cleanup.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct TwoQubitBlockResynthesisConfig {
25    /// Target capability used by exact two-qubit numerical synthesis.
26    pub two_qubit_target: TwoQubitSynthesisTarget,
27    /// Maximum number of source operations allowed in one bounded
28    /// commutation-aware candidate. Maximal uninterrupted pair runs are not
29    /// limited by this supplemental search budget.
30    pub max_block_ops: usize,
31    /// Maximum number of non-block operations that may be crossed while
32    /// collecting a block. Crossed operations are preserved at their original
33    /// source positions and must commute with the synthesized replacement.
34    pub max_crossed_ops: usize,
35    /// Maximum bounded collection budget per side of a two-qubit anchor.
36    ///
37    /// This is the number of dependency-DAG frontier nodes visited per
38    /// direction.
39    pub max_scan_span: usize,
40    /// Treat labeled operations as hard boundaries.
41    pub skip_labeled_ops: bool,
42    /// Recursively run the pass inside structured classical-control bodies.
43    pub recurse_control_flow: bool,
44    /// Semantic commutation engine configuration used by the local collector.
45    pub commutation: CommutationConfig,
46}
47
48impl TwoQubitBlockResynthesisConfig {
49    /// Returns the default-budget configuration for the selected two-qubit
50    /// synthesis basis.
51    pub fn normal(two_qubit_target: TwoQubitSynthesisTarget) -> Self {
52        Self {
53            two_qubit_target,
54            max_block_ops: 16,
55            max_crossed_ops: 4,
56            max_scan_span: 32,
57            skip_labeled_ops: true,
58            recurse_control_flow: true,
59            commutation: CommutationConfig {
60                enable_rule_oracle: true,
61                enable_matrix_fallback: false,
62                max_matrix_qubits: 4,
63            },
64        }
65    }
66
67    /// Returns a higher-budget configuration for enhanced optimization modes.
68    pub fn enhanced(two_qubit_target: TwoQubitSynthesisTarget) -> Self {
69        Self {
70            max_block_ops: 32,
71            max_crossed_ops: 8,
72            max_scan_span: 64,
73            ..Self::normal(two_qubit_target)
74        }
75    }
76}
77
78impl Default for TwoQubitBlockResynthesisConfig {
79    fn default() -> Self {
80        Self::normal(TwoQubitSynthesisTarget::default())
81    }
82}
83
84#[cfg(test)]
85#[path = "config_test.rs"]
86mod config_test;