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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 2025-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
// http://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.
//! Public compiler entry point.
//!
//! The compiler entry point runs the configured [`CompilerWorkflow`]
//! and returns the optimized circuit plus step-level diagnostics. The workflow
//! starts from a logical circuit, applies canonicalization, definition
//! expansion, knowledge-based rewrite, unitary and multi-controlled-gate
//! decomposition, optional device layout/routing, and target lowering.
//!
//! [`CompileTarget`] makes the target contract explicit: compilation is either
//! logical, lowered to a requested basis, or routed and legalized for one
//! concrete device. Device compilation uses the device topology for capacity
//! checks and SABRE routing, then checks every emitted standard operation
//! against the device's ordered native capabilities before final validation.
//!
//! [`CompileMode::Normal`] selects conservative production defaults.
//! [`CompileMode::Enhanced`] keeps the same semantic contract but spends more
//! rewrite, resynthesis, native-optimization, and routing effort, runs additional
//! cleanup around routing and target lowering, and, for a strict device target,
//! selects among fully lowered and validated routes with a bounded SABRE Pareto
//! beam.
use CompilerWorkflow;
use crate;
use crateResourcePolicy;
use crateVirtualPermutation;
use crate;
use crate;
/// Optimization effort selected for the compiler workflow.
/// User-facing configuration for [`compile`].
///
/// The configuration describes logical optimization effort, optional target
/// constraints, and the ancillary-resource permissions available before layout.
/// Target contract selected for a compilation run.
///
/// Device targets intentionally stay inline so the public configuration API
/// does not require allocation solely to select a target. Compile
/// configurations are coarse workflow objects, not per-operation data.
/// Device-specific compilation inputs.
/// Physical-layout information produced by device compilation.
/// Result returned by [`compile`].
/// Runs the configured compiler workflow over `circuit`.
///
/// The returned result records the optimized circuit and step-level reports for
/// the retained output path in workflow order. A device target additionally
/// returns its initial and final layouts. Errors are reported when a configured
/// target, native realization, or candidate validation cannot be satisfied.
///
/// # Examples
///
/// ```rust
/// use cqlib_core::circuit::{Circuit, Qubit};
/// use cqlib_core::compile::{CompileConfig, CompileMode, CompileTarget, compile};
/// use cqlib_core::compile::resource::ResourcePolicy;
///
/// let mut circuit = Circuit::new(2);
/// circuit.h(Qubit::new(0)).unwrap();
/// circuit.cx(Qubit::new(0), Qubit::new(1)).unwrap();
///
/// let result = compile(
/// &circuit,
/// CompileConfig {
/// mode: CompileMode::Normal,
/// target: CompileTarget::Logical,
/// resource_policy: ResourcePolicy::default(),
/// },
/// )
/// .unwrap();
///
/// assert_eq!(result.mode, CompileMode::Normal);
/// assert!(!result.steps.is_empty());
/// assert_eq!(result.circuit.qubits().len(), 2);
/// ```
/// Runs the configured compiler workflow by consuming `circuit`.
///
/// This is equivalent to [`compile`] but avoids cloning the complete input
/// circuit when the caller no longer needs it.