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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 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.
//! Convenient constructor functions for common ansatz patterns.
//!
//! This module provides high-level factory functions that create pre-configured
//! ansatze for common use cases in variational quantum algorithms.
//!
//! # Available Ansatze
//!
//! - [`real_amplitudes`]: Ansatz with real-valued amplitudes, suitable for chemistry
//! and optimization problems where the wavefunction can be constrained to real numbers.
//!
//! - [`efficient_su2`]: Hardware-efficient ansatz spanning SU(2), widely used in
//! VQE and Quantum Machine Learning.
//!
//! - [`zz_feature_map`]: Second-order Pauli-Z feature map for quantum kernel methods.
//!
//! - [`pauli_feature_map`]: General-purpose Pauli feature map with arbitrary Pauli strings.
//!
//! # Comparison
//!
//! | Ansatz | Rotation Gates | Entanglement | Use Case |
//! |--------|---------------|--------------|----------|
//! | RealAmplitudes | RY | CX | Chemistry, QAOA with real amplitudes |
//! | EfficientSU2 | RY, RZ | CX | General VQE, QML |
//! | ZZFeatureMap | Z + ZZ | (topology) | Quantum kernel methods |
//! | PauliFeatureMap | arbitrary Paulis | (topology) | QML feature encoding |
//!
//! # Example
//!
//! ```
//! use cqlib_core::circuit::ansatz::{real_amplitudes, efficient_su2, zz_feature_map, EntanglementTopology, Ansatz};
//!
//! // Create a RealAmplitudes ansatz for 4 qubits
//! let ra = real_amplitudes(4, 3, EntanglementTopology::Linear);
//! assert_eq!(ra.num_parameters(), 16); // 4 qubits * 4 layers
//!
//! // Create an EfficientSU2 ansatz
//! let su2 = efficient_su2(3, 2, EntanglementTopology::Full);
//! assert_eq!(su2.num_parameters(), 18); // 3 qubits * 2 gates * 3 layers
//!
//! // Create a ZZFeatureMap for 3 qubits
//! let fm = zz_feature_map(3, 2, EntanglementTopology::Full);
//! assert_eq!(fm.num_parameters(), 3);
//! ```
use ;
use ;
use crateStandardGate;
use cratePauliString;
/// Creates a RealAmplitudes ansatz.
///
/// This is a heuristic trial wave function used as an ansatz in chemistry applications or
/// QAOA where the wave function is constrained to have real amplitudes (i.e., no imaginary parts).
///
/// The ansatz consists of alternating layers of $R_Y$ rotations and $CX$ entanglements.
/// This structure ensures that the resulting statevector has only real components when
/// starting from $|0\rangle^{\otimes n}$.
///
/// # Mathematical Structure
///
/// For $n$ qubits and $r$ repetitions:
///
/// $$|\psi(\theta)\rangle = \left( \prod_{i=0}^{n-1} R_Y(\theta_{r,i}) \right) \prod_{l=0}^{r-1} \left[ \left( \prod_{(i,j) \in E} CX_{i,j} \right) \left( \prod_{i=0}^{n-1} R_Y(\theta_{l,i}) \right) \right] |0\rangle^{\otimes n}$$
///
/// where $E$ is the set of edges defined by the entanglement topology.
///
/// # Arguments
///
/// * `num_qubits` - The number of qubits in the ansatz. Must be at least 1.
/// * `reps` - The number of repetition layers. Each repetition adds a rotation
/// layer and an entanglement layer. The final rotation layer is always included.
/// * `entanglement` - The topology of the entanglement (e.g., Linear, Full).
/// See [`EntanglementTopology`] for options.
///
/// # Returns
///
/// A [`TwoLocal`] instance configured for Real Amplitudes with:
/// - Rotation gates: `[RY]`
/// - Entanglement gate: `CX`
/// - Total parameters: `(reps + 1) × num_qubits`
///
/// # Example
///
/// ```
/// use cqlib_core::circuit::ansatz::{real_amplitudes, EntanglementTopology, Ansatz};
///
/// // 3 qubits, 2 reps, linear entanglement
/// let ansatz = real_amplitudes(3, 2, EntanglementTopology::Linear);
///
/// assert_eq!(ansatz.num_qubits(), 3);
/// assert_eq!(ansatz.num_parameters(), 9); // (2+1) * 3 = 9
///
/// let circuit = ansatz.build_circuit("theta").unwrap();
/// ```
/// Creates an EfficientSU2 ansatz.
///
/// A hardware-efficient, heuristic ansatz that consists of layers of single-qubit
/// operations spanning SU(2) and $CX$ entanglements. It is widely used in VQE and
/// Quantum Machine Learning models due to its expressibility and relatively low
/// circuit depth.
///
/// The ansatz consists of alternating layers of $[R_Y, R_Z]$ rotations and $CX$ entanglements.
/// Using both $R_Y$ and $R_Z$ allows the ansatz to span the full SU(2) space for each qubit.
///
/// # Mathematical Structure
///
/// For $n$ qubits and $r$ repetitions:
///
/// $$|\psi(\theta, \phi)\rangle = \left( \prod_{i=0}^{n-1} R_Z(\phi_{r,i}) R_Y(\theta_{r,i}) \right) \prod_{l=0}^{r-1} \left[ \left( \prod_{(i,j) \in E} CX_{i,j} \right) \left( \prod_{i=0}^{n-1} R_Z(\phi_{l,i}) R_Y(\theta_{l,i}) \right) \right] |0\rangle^{\otimes n}$$
///
/// where $E$ is the set of edges defined by the entanglement topology.
///
/// # Arguments
///
/// * `num_qubits` - The number of qubits in the ansatz. Must be at least 1.
/// * `reps` - The number of repetition layers. Each repetition adds a rotation
/// layer and an entanglement layer. The final rotation layer is always included.
/// * `entanglement` - The topology of the entanglement (e.g., Linear, Full).
/// See [`EntanglementTopology`] for options.
///
/// # Returns
///
/// A [`TwoLocal`] instance configured for Efficient SU2 with:
/// - Rotation gates: `[RY, RZ]`
/// - Entanglement gate: `CX`
/// - Total parameters: `(reps + 1) × num_qubits × 2`
///
/// # Example
///
/// ```
/// use cqlib_core::circuit::ansatz::{efficient_su2, EntanglementTopology, Ansatz};
///
/// // 2 qubits, 1 rep, full entanglement
/// let ansatz = efficient_su2(2, 1, EntanglementTopology::Full);
///
/// assert_eq!(ansatz.num_qubits(), 2);
/// assert_eq!(ansatz.num_parameters(), 8); // (1+1) * 2 * 2 = 8
///
/// let circuit = ansatz.build_circuit("p").unwrap();
/// ```
/// Creates a ZZFeatureMap.
///
/// A second-order Pauli-Z feature map widely used in quantum kernel methods.
/// Encodes classical data using single-qubit Z-rotations and two-qubit ZZ-entanglement,
/// making the feature kernel hard to evaluate classically for large circuits.
///
/// The circuit structure for each repetition layer:
/// 1. Hadamard layer on all qubits.
/// 2. `RZ(2 · x_i)` on each qubit i.
/// 3. `e^{-i · 2 · (π − x_i)(π − x_j) · Z_i Z_j}` for each entangled pair `(i, j)`.
///
/// # Arguments
///
/// * `num_qubits` - The number of qubits (= number of input features). Must be ≥ 1.
/// * `reps` - The number of repetition layers. More reps → richer feature space.
/// * `entanglement` - Connectivity pattern for ZZ interactions.
///
/// # Returns
///
/// A [`ZZFeatureMap`] instance. Always has `num_qubits` parameters (one per feature).
///
/// # Example
///
/// ```
/// use cqlib_core::circuit::ansatz::{zz_feature_map, EntanglementTopology, Ansatz};
///
/// let fm = zz_feature_map(3, 2, EntanglementTopology::Full);
/// assert_eq!(fm.num_qubits(), 3);
/// assert_eq!(fm.num_parameters(), 3); // one parameter x_i per qubit
///
/// let circuit = fm.build_circuit("x").unwrap();
/// ```
/// Creates a PauliFeatureMap.
///
/// A general-purpose data encoding circuit using Pauli evolution gates.
/// Supports arbitrary Pauli strings (e.g., "Z", "ZZ", "XY", "ZZZ") and flexible
/// entanglement topologies.
///
/// For each repetition layer:
/// 1. Hadamard layer on all qubits.
/// 2. For each Pauli template P and each k-tuple of qubit indices from the topology:
/// - If k=1: apply `e^{-i x_i P}` with angle `2 · x_i`.
/// - If k≥2: apply `e^{-i 2 · ∏(π − x_j) P}` with angle `4 · ∏(π − x_j)`.
///
/// # Arguments
///
/// * `num_qubits` - The number of qubits (= number of input features). Must be ≥ 1.
/// * `reps` - The number of repetition layers.
/// * `paulis` - Pauli string templates with labels. Use the [`std::str::FromStr`]
/// implementation for construction.
/// * `entanglement` - Connectivity pattern for multi-qubit interactions.
///
/// # Returns
///
/// A [`PauliFeatureMap`] instance. Always has `num_qubits` parameters (one per feature).
///
/// # Example
///
/// ```
/// use cqlib_core::circuit::ansatz::{pauli_feature_map, EntanglementTopology, Ansatz};
/// use cqlib_core::qis::pauli::PauliString;
///
/// // ZZFeatureMap-equivalent: Z + ZZ paulis
/// let fm = pauli_feature_map(
/// 3, 2,
/// vec![
/// ("Z".parse::<PauliString>().unwrap(), "Z".to_string()),
/// ("ZZ".parse::<PauliString>().unwrap(), "ZZ".to_string()),
/// ],
/// EntanglementTopology::Full,
/// );
/// assert_eq!(fm.num_qubits(), 3);
/// assert_eq!(fm.num_parameters(), 3);
///
/// let circuit = fm.build_circuit("x").unwrap();
/// ```