Skip to main content

cqlib_core/compile/transform/canonicalize/
config.rs

1// This code is part of Cqlib.
2//
3// (C) Copyright China Telecom Quantum Group 2025-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//! User-facing configuration for canonicalization.
14
15/// Configuration for circuit canonicalization.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct CanonicalizeConfig {
18    round_limit: u8,
19    recurse_control_flow: bool,
20    fold_gphase: bool,
21    canonicalize_instruction_form: bool,
22    drop_noops: bool,
23    canonicalize_barriers: bool,
24}
25
26impl Default for CanonicalizeConfig {
27    fn default() -> Self {
28        Self::production()
29    }
30}
31
32impl CanonicalizeConfig {
33    /// Returns production defaults for the first canonicalization stage.
34    pub const fn production() -> Self {
35        Self {
36            round_limit: 8,
37            recurse_control_flow: true,
38            fold_gphase: true,
39            canonicalize_instruction_form: true,
40            drop_noops: true,
41            canonicalize_barriers: true,
42        }
43    }
44
45    /// Returns a new configuration using production defaults.
46    pub const fn new() -> Self {
47        Self::production()
48    }
49
50    /// Sets the maximum number of canonicalization rounds.
51    pub const fn with_round_limit(mut self, round_limit: u8) -> Self {
52        self.round_limit = round_limit;
53        self
54    }
55
56    /// Controls whether control-flow bodies are recursively canonicalized.
57    pub const fn recurse_control_flow(mut self, enabled: bool) -> Self {
58        self.recurse_control_flow = enabled;
59        self
60    }
61
62    /// Controls whether `GPhase` operations are folded into scope-local phase.
63    pub const fn fold_gphase(mut self, enabled: bool) -> Self {
64        self.fold_gphase = enabled;
65        self
66    }
67
68    /// Controls whether `McGate` forms are collapsed into existing standard gates.
69    pub const fn canonicalize_instruction_form(mut self, enabled: bool) -> Self {
70        self.canonicalize_instruction_form = enabled;
71        self
72    }
73
74    /// Controls strict no-op removal.
75    pub const fn drop_noops(mut self, enabled: bool) -> Self {
76        self.drop_noops = enabled;
77        self
78    }
79
80    /// Controls barrier scope canonicalization and adjacent barrier merging.
81    pub const fn canonicalize_barriers(mut self, enabled: bool) -> Self {
82        self.canonicalize_barriers = enabled;
83        self
84    }
85
86    /// Returns the configured round limit.
87    pub const fn round_limit(&self) -> u8 {
88        self.round_limit
89    }
90
91    /// Returns whether control-flow recursion is enabled.
92    pub const fn recurses_control_flow(&self) -> bool {
93        self.recurse_control_flow
94    }
95
96    /// Returns whether `GPhase` operations are folded into scope-local phase.
97    pub const fn folds_gphase(&self) -> bool {
98        self.fold_gphase
99    }
100
101    /// Returns whether instruction forms are canonicalized.
102    pub const fn canonicalizes_instruction_form(&self) -> bool {
103        self.canonicalize_instruction_form
104    }
105
106    /// Returns whether strict no-ops are removed.
107    pub const fn drops_noops(&self) -> bool {
108        self.drop_noops
109    }
110
111    /// Returns whether barrier scopes are canonicalized and merged.
112    pub const fn canonicalizes_barriers(&self) -> bool {
113        self.canonicalize_barriers
114    }
115}