cqlib-core 0.1.0-beta.5

The high-performance core foundation for the CQLib quantum computing ecosystem.
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// 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.

//! # Feature Map Module
//!
//! This module provides feature encoding circuits for quantum machine learning.
//! Feature maps encode classical data into quantum states, enabling quantum
//! computers to process classical information.
//!
//! ## Available Encodings
//!
//! - [`BasisEncoding`]: Computational basis state preparation from a bitstring
//! - [`AngleEncoding`]: Simple tensor product encoding using rotation gates
//! - [`ZFeatureMap`]: First-order Pauli-Z feature map
//! - [`IQPFeatureMap`]: IQP-style diagonal feature map
//! - [`ZZFeatureMap`]: Entangling feature map with ZZ interactions
//! - [`PauliFeatureMap`]: General-purpose feature map with arbitrary Pauli strings

use super::traits::Ansatz;
use super::two_local::EntanglementTopology;
use crate::circuit::Instruction;
use crate::circuit::Parameter;
use crate::circuit::ParameterValue;
use crate::circuit::bit::Qubit;
use crate::circuit::circuit_impl::Circuit;
use crate::circuit::error::CircuitError;
use crate::circuit::gate::StandardGate;
use crate::qis::evolution::PauliEvolution;
use crate::qis::pauli::{Pauli, PauliString};

/// Angle Encoding (also known as Tensor Product Encoding).
///
/// Encodes $N$ classical features into $N$ qubits by applying parameterized
/// single-qubit rotations. A common choice is applying $R_X(x_i)$ or $R_Y(x_i)$
/// to each qubit $i$.
///
/// This is the simplest feature map, mapping features independently.
#[derive(Debug, Clone)]
pub struct AngleEncoding {
    num_qubits: usize,
    rotation_gate: StandardGate,
}

impl AngleEncoding {
    /// Creates a new AngleEncoding using the specified rotation gate.
    /// Typically, `StandardGate::RX` or `StandardGate::RY` is used.
    pub fn new(num_qubits: usize, rotation_gate: StandardGate) -> Self {
        Self {
            num_qubits,
            rotation_gate,
        }
    }
}

impl Ansatz for AngleEncoding {
    fn validate(&self) -> Result<(), CircuitError> {
        if self.num_qubits == 0 {
            return Err(CircuitError::InvalidOperation(
                "AngleEncoding requires at least 1 qubit".to_string(),
            ));
        }
        // Only single-parameter single-qubit rotation gates are valid
        if !matches!(
            self.rotation_gate,
            StandardGate::RX | StandardGate::RY | StandardGate::RZ | StandardGate::Phase
        ) {
            return Err(CircuitError::InvalidOperation(format!(
                "AngleEncoding rotation_gate must be a single-parameter single-qubit gate \
                 (RX, RY, RZ, or Phase), got {:?}",
                self.rotation_gate
            )));
        }
        Ok(())
    }

    fn build_circuit(&self, prefix: &str) -> Result<Circuit, CircuitError> {
        self.validate()?;

        let mut circuit = Circuit::new(self.num_qubits);

        for q in 0..self.num_qubits {
            let param_name = format!("{}_{}", prefix, q);
            let param = Parameter::try_from(param_name.as_str())
                .map_err(|_| CircuitError::InvalidParameterValue(q, f64::NAN))?;

            circuit.append(
                Instruction::Standard(self.rotation_gate),
                vec![Qubit::new(q as u32)],
                vec![ParameterValue::Param(param)],
                None,
            )?;
        }

        Ok(circuit)
    }

    fn num_parameters(&self) -> usize {
        self.num_qubits
    }

    fn num_qubits(&self) -> usize {
        self.num_qubits
    }
}

/// Basis encoding into a computational basis state.
///
/// A boolean bitstring is mapped to a basis state by applying `X` to every qubit
/// whose bit value is `true`. For example, `[true, false, true]` prepares `|101>`.
/// This encoding has no trainable or symbolic parameters.
#[derive(Debug, Clone)]
pub struct BasisEncoding {
    bits: Vec<bool>,
}

impl BasisEncoding {
    /// Creates a new basis encoder from a boolean bitstring.
    pub fn new(bits: Vec<bool>) -> Self {
        Self { bits }
    }
}

impl Ansatz for BasisEncoding {
    fn validate(&self) -> Result<(), CircuitError> {
        if self.bits.is_empty() {
            return Err(CircuitError::InvalidOperation(
                "BasisEncoding requires at least 1 bit".to_string(),
            ));
        }
        Ok(())
    }

    fn build_circuit(&self, _prefix: &str) -> Result<Circuit, CircuitError> {
        self.validate()?;

        let mut circuit = Circuit::new(self.bits.len());
        for (idx, bit) in self.bits.iter().enumerate() {
            if *bit {
                circuit.x(Qubit::new(idx as u32))?;
            }
        }
        Ok(circuit)
    }

    fn num_parameters(&self) -> usize {
        0
    }

    fn num_qubits(&self) -> usize {
        self.bits.len()
    }
}

/// First-order Z feature map.
///
/// Each repetition applies a Hadamard layer followed by independent
/// `RZ(2 * x_i)` rotations. This is the non-entangling base case of
/// [`ZZFeatureMap`].
#[derive(Debug, Clone)]
pub struct ZFeatureMap {
    num_qubits: usize,
    reps: usize,
}

impl ZFeatureMap {
    /// Creates a new ZFeatureMap with `reps = 2`.
    pub fn new(num_qubits: usize) -> Self {
        Self {
            num_qubits,
            reps: 2,
        }
    }

    /// Sets the number of repetition layers.
    pub fn reps(mut self, reps: usize) -> Self {
        self.reps = reps;
        self
    }
}

impl Ansatz for ZFeatureMap {
    fn validate(&self) -> Result<(), CircuitError> {
        if self.num_qubits == 0 {
            return Err(CircuitError::InvalidOperation(
                "ZFeatureMap requires at least 1 qubit".to_string(),
            ));
        }
        Ok(())
    }

    fn build_circuit(&self, prefix: &str) -> Result<Circuit, CircuitError> {
        self.validate()?;

        let mut circuit = Circuit::new(self.num_qubits);
        let mut x_params = Vec::with_capacity(self.num_qubits);
        for i in 0..self.num_qubits {
            let param_name = format!("{}_{}", prefix, i);
            let param = Parameter::try_from(param_name.as_str())
                .map_err(|_| CircuitError::InvalidParameterValue(i, f64::NAN))?;
            x_params.push(param);
        }

        for _layer in 0..self.reps {
            for q in 0..self.num_qubits {
                circuit.h(Qubit::new(q as u32))?;
            }
            for (i, param) in x_params.iter().enumerate() {
                circuit.rz(
                    Qubit::new(i as u32),
                    ParameterValue::Param(param.clone() * 2.0),
                )?;
            }
        }

        Ok(circuit)
    }

    fn num_parameters(&self) -> usize {
        if self.reps == 0 {
            return 0;
        }
        self.num_qubits
    }

    fn num_qubits(&self) -> usize {
        self.num_qubits
    }
}

/// IQP-style diagonal feature map.
///
/// This is an explicit named wrapper over the same diagonal ZZ feature-map
/// structure used by [`ZZFeatureMap`]: each repetition applies a Hadamard
/// layer, first-order Z rotations, and second-order ZZ interactions according
/// to the selected entanglement topology.
#[derive(Debug, Clone)]
pub struct IQPFeatureMap {
    inner: ZZFeatureMap,
}

impl IQPFeatureMap {
    /// Creates a new IQPFeatureMap with `reps = 2` and full entanglement.
    pub fn new(num_qubits: usize) -> Self {
        Self {
            inner: ZZFeatureMap::new(num_qubits),
        }
    }

    /// Sets the number of repetition layers.
    pub fn reps(mut self, reps: usize) -> Self {
        self.inner = self.inner.reps(reps);
        self
    }

    /// Sets the entanglement topology for two-qubit diagonal interactions.
    pub fn entanglement(mut self, topology: EntanglementTopology) -> Self {
        self.inner = self.inner.entanglement(topology);
        self
    }
}

impl Ansatz for IQPFeatureMap {
    fn validate(&self) -> Result<(), CircuitError> {
        self.inner.validate()
    }

    fn build_circuit(&self, prefix: &str) -> Result<Circuit, CircuitError> {
        self.inner.build_circuit(prefix)
    }

    fn num_parameters(&self) -> usize {
        self.inner.num_parameters()
    }

    fn num_qubits(&self) -> usize {
        self.inner.num_qubits()
    }
}

/// ZZ Feature Map (also known as the IQP Encoding).
///
/// A second-order Pauli-Z evolution circuit widely used in Quantum Kernel Methods.
/// It encodes classical data using single-qubit Z-rotations and two-qubit ZZ-entanglement,
/// making the feature space highly non-linear and difficult to simulate classically.
///
/// For data vector $x$, a single layer applies:
/// 1. Hadamard gates on all qubits.
/// 2. $R_Z(2 \cdot x_i)$ on each qubit $i$.
/// 3. $e^{-i (2 \cdot (\pi - x_i)(\pi - x_j)) Z_i Z_j}$ for entangled pairs $(i,j)$.
///
/// Note: The specific encoding function $\phi(x)$ can vary. We use a common variation:
/// $\phi_i(x) = x_i$ for single qubits, and $\phi_{ij}(x) = (\pi - x_i)(\pi - x_j)$ for pairs.
#[derive(Debug, Clone)]
pub struct ZZFeatureMap {
    num_qubits: usize,
    reps: usize,
    entanglement: EntanglementTopology,
}

impl ZZFeatureMap {
    /// Creates a new ZZFeatureMap.
    ///
    /// By default, it uses `reps = 2` and `EntanglementTopology::Full`.
    pub fn new(num_qubits: usize) -> Self {
        Self {
            num_qubits,
            reps: 2,
            entanglement: EntanglementTopology::Full,
        }
    }

    /// Sets the number of repetition layers.
    pub fn reps(mut self, reps: usize) -> Self {
        self.reps = reps;
        self
    }

    /// Sets the entanglement topology.
    pub fn entanglement(mut self, topology: EntanglementTopology) -> Self {
        self.entanglement = topology;
        self
    }
}

impl Ansatz for ZZFeatureMap {
    fn validate(&self) -> Result<(), CircuitError> {
        if self.num_qubits == 0 {
            return Err(CircuitError::InvalidOperation(
                "ZZFeatureMap requires at least 1 qubit".to_string(),
            ));
        }
        if let EntanglementTopology::Custom(pairs) = &self.entanglement {
            use std::collections::HashSet;
            let mut seen: HashSet<(usize, usize)> = HashSet::new();
            for (i, j) in pairs {
                if *i >= self.num_qubits || *j >= self.num_qubits {
                    return Err(CircuitError::InvalidOperation(format!(
                        "Custom entanglement topology contains out-of-bounds index: \
                         ({}, {}) for {} qubits",
                        i, j, self.num_qubits
                    )));
                }
                if i == j {
                    return Err(CircuitError::InvalidOperation(format!(
                        "Custom entanglement topology contains self-loop ({}, {})",
                        i, j
                    )));
                }
                // Undirected duplicate check
                let edge = if i < j { (*i, *j) } else { (*j, *i) };
                if !seen.insert(edge) {
                    return Err(CircuitError::InvalidOperation(format!(
                        "Custom entanglement topology contains duplicate edge ({}, {})",
                        i, j
                    )));
                }
            }
        }
        Ok(())
    }

    fn build_circuit(&self, prefix: &str) -> Result<Circuit, CircuitError> {
        self.validate()?;

        let mut circuit = Circuit::new(self.num_qubits);
        let qubits = circuit.qubits();

        // Prepare the base parameters [x_0, x_1, ... x_{n-1}]
        let mut x_params = Vec::with_capacity(self.num_qubits);
        for i in 0..self.num_qubits {
            let param_name = format!("{}_{}", prefix, i);
            let param = Parameter::try_from(param_name.as_str())
                .map_err(|_| CircuitError::InvalidParameterValue(i, f64::NAN))?;
            x_params.push(param);
        }

        // Generate the pairs based on the chosen topology
        let pairs = self.entanglement.generate_pairs(self.num_qubits);

        for _layer in 0..self.reps {
            // 1. Initial Hadamard layer
            for q in 0..self.num_qubits {
                circuit.h(Qubit::new(q as u32))?;
            }

            // 2. Single qubit phase encoding: RZ(2 * x_i)
            // U = e^{-i * x_i * Z} = RZ(2 * x_i)
            for (i, item) in x_params.iter().enumerate().take(self.num_qubits) {
                let angle = item.clone() * 2.0;
                circuit.rz(Qubit::new(i as u32), ParameterValue::Param(angle))?;
            }

            // 3. Two qubit ZZ entanglement: e^{-i * 2 * (\pi - x_i)(\pi - x_j) * Z_i Z_j}
            if !pairs.is_empty() {
                for &(i, j) in &pairs {
                    if i < self.num_qubits && j < self.num_qubits {
                        // Let phi_{ij} = (\pi - x_i) * (\pi - x_j)
                        // Evolution: R_ZZ(\theta) = e^{-i (\theta/2) Z_i Z_j}
                        // So we want \theta/2 = 2 * phi_{ij} => \theta = 4 * phi_{ij}

                        let pi_param = Parameter::pi();
                        let xi = x_params[i].clone();
                        let xj = x_params[j].clone();

                        // construct (\pi - x_i) * (\pi - x_j)
                        let phi_ij = (pi_param.clone() - xi) * (pi_param - xj);
                        let angle = phi_ij * 4.0;

                        // Using pauli evolution for ZZ
                        let mut pauli_str = PauliString::new(self.num_qubits);
                        pauli_str.set_pauli(i, Pauli::Z);
                        pauli_str.set_pauli(j, Pauli::Z);

                        circuit.pauli_evolution(
                            &pauli_str,
                            ParameterValue::Param(angle),
                            &qubits,
                        )?;
                    }
                }
            }
        }

        Ok(circuit)
    }

    fn num_parameters(&self) -> usize {
        if self.reps == 0 {
            return 0;
        }
        self.num_qubits
    }

    fn num_qubits(&self) -> usize {
        self.num_qubits
    }
}

/// Pauli Feature Map: A general-purpose data encoding circuit using Pauli evolution.
///
/// The PauliFeatureMap encodes classical data into quantum states through parameterized
/// Pauli evolution gates. It supports arbitrary Pauli strings (e.g., "Z", "ZZ", "ZZZ")
/// and flexible entanglement topologies.
///
/// # Mathematical Foundation
///
/// For a k-local Pauli template P and feature vector x, the encoding applies:
///
/// | Locality | Evolution | Angle θ (passed to `pauli_evolution`) |
/// |----------|-----------|----------------------------------------|
/// | k = 1    | $e^{-i x_i P}$ | $2 x_i$ |
/// | k ≥ 2    | $e^{-i 2 \prod_{j \in S}(\pi - x_j) P}$ | $4 \prod_{j \in S}(\pi - x_j)$ |
///
/// where `pauli_evolution(θ)` implements $e^{-i \frac{\theta}{2} P}$.
///
/// The non-linear k-local mapping $\prod_{j \in S}(\pi - x_j)$ creates a rich
/// feature space through the kernel trick, making the kernel function hard to
/// evaluate classically for large circuits.
///
/// # Architecture
///
/// The circuit structure for each repetition layer:
///
/// 1. **Hadamard Layer**: Apply $H$ gates to all qubits to create superposition.
/// 2. **k-local Pauli Evolution**: For each Pauli template P and each k-tuple
///    of qubit indices $(q_0, \ldots, q_{k-1})$ from the entanglement topology,
///    apply $e^{-i \phi(x_{q_0}, \ldots, x_{q_{k-1}}) P}$.
///
/// # Example
///
/// ```rust
/// use cqlib_core::circuit::ansatz::{Ansatz, PauliFeatureMap, EntanglementTopology};
/// use cqlib_core::qis::pauli::PauliString;
///
/// // 3-qubit feature map with Z (1-local) + ZZ (2-local) + ZZZ (3-local)
/// let feature_map = PauliFeatureMap::new(3)
///     .reps(1)
///     .paulis(vec![
///         ("Z".parse::<PauliString>().unwrap(),   "Z".to_string()),
///         ("ZZ".parse::<PauliString>().unwrap(),  "ZZ".to_string()),
///         ("ZZZ".parse::<PauliString>().unwrap(), "ZZZ".to_string()),
///     ])
///     .entanglement(EntanglementTopology::Full);
///
/// let circuit = feature_map.build_circuit("x").unwrap();
/// assert_eq!(feature_map.num_parameters(), 3);
/// ```
#[derive(Debug, Clone)]
pub struct PauliFeatureMap {
    /// Number of qubits (features) in the feature map.
    num_qubits: usize,
    /// Number of repetition layers for the encoding.
    reps: usize,
    /// List of Pauli strings to use for evolution, with their labels.
    paulis: Vec<(PauliString, String)>,
    /// Entanglement topology for 2-local interactions.
    entanglement: EntanglementTopology,
    /// Prefix for parameter names (default: "x").
    parameter_prefix: String,
}

impl PauliFeatureMap {
    /// Creates a new PauliFeatureMap with default configuration.
    ///
    /// # Default Configuration
    ///
    /// - `reps`: 2 layers
    /// - `paulis`: ["Z", "ZZ"] (single-qubit Z and two-qubit ZZ interactions)
    /// - `entanglement`: [`Full`](EntanglementTopology::Full) all-to-all connectivity
    /// - `parameter_prefix`: "x"
    ///
    /// # Arguments
    ///
    /// * `num_qubits` - The number of qubits (features) in the feature map.
    ///
    /// # Example
    ///
    /// ```rust
    /// use cqlib_core::circuit::ansatz::{Ansatz, PauliFeatureMap};
    ///
    /// let fm = PauliFeatureMap::new(4);
    /// assert_eq!(fm.num_qubits(), 4);
    /// ```
    pub fn new(num_qubits: usize) -> Self {
        Self {
            num_qubits,
            reps: 2,
            paulis: vec![
                ("Z".parse::<PauliString>().unwrap(), "Z".to_string()),
                ("ZZ".parse::<PauliString>().unwrap(), "ZZ".to_string()),
            ],
            entanglement: EntanglementTopology::Full,
            parameter_prefix: "x".to_string(),
        }
    }

    /// Sets the number of repetition layers.
    ///
    /// Each repetition applies the full encoding sequence (Hadamard + Pauli evolutions).
    /// More repetitions increase the expressiveness of the feature map but also
    /// increase circuit depth.
    ///
    /// # Arguments
    ///
    /// * `reps` - The number of repetition layers.
    pub fn reps(mut self, reps: usize) -> Self {
        self.reps = reps;
        self
    }

    /// Sets the Pauli strings to use for evolution.
    ///
    /// Each Pauli string specifies a type of interaction:
    /// - Single-character strings (e.g., "Z", "X", "Y") create 1-local interactions.
    /// - Multi-character strings (e.g., "ZZ", "XY") create multi-qubit interactions.
    ///
    /// # Arguments
    ///
    /// * `paulis` - A vector of `(PauliString, label)` tuples.
    pub fn paulis(mut self, paulis: Vec<(PauliString, String)>) -> Self {
        self.paulis = paulis;
        self
    }

    /// Sets the entanglement topology for 2-local interactions.
    ///
    /// The topology determines which qubit pairs are connected by 2-local
    /// Pauli evolution gates.
    ///
    /// # Arguments
    ///
    /// * `topology` - The entanglement topology.
    pub fn entanglement(mut self, topology: EntanglementTopology) -> Self {
        self.entanglement = topology;
        self
    }

    /// Sets the parameter name prefix.
    ///
    /// Parameter names are generated as `{prefix}_{index}` (e.g., "x_0", "x_1").
    ///
    /// # Arguments
    ///
    /// * `prefix` - The parameter name prefix.
    pub fn parameter_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.parameter_prefix = prefix.into();
        self
    }
}

impl Ansatz for PauliFeatureMap {
    fn validate(&self) -> Result<(), CircuitError> {
        // Check that num_qubits > 0
        if self.num_qubits == 0 {
            return Err(CircuitError::InvalidOperation(
                "PauliFeatureMap requires at least 1 qubit".to_string(),
            ));
        }

        // Check that pauli strings have length <= num_qubits
        for (pauli_str, _) in &self.paulis {
            if pauli_str.num_qubits > self.num_qubits {
                return Err(CircuitError::InvalidOperation(format!(
                    "Pauli string '{}' has length {} which exceeds num_qubits {}",
                    pauli_str, pauli_str.num_qubits, self.num_qubits
                )));
            }
        }

        // If Custom topology, validate all indices and reject ambiguous edges.
        if let EntanglementTopology::Custom(pairs) = &self.entanglement {
            use std::collections::HashSet;
            let mut seen: HashSet<(usize, usize)> = HashSet::new();
            for (i, j) in pairs {
                if *i >= self.num_qubits || *j >= self.num_qubits {
                    return Err(CircuitError::InvalidOperation(format!(
                        "Custom entanglement topology contains out-of-bounds index: ({}, {}) for {} qubits",
                        i, j, self.num_qubits
                    )));
                }
                if i == j {
                    return Err(CircuitError::InvalidOperation(format!(
                        "Custom entanglement topology contains self-loop ({}, {})",
                        i, j
                    )));
                }
                let edge = if i < j { (*i, *j) } else { (*j, *i) };
                if !seen.insert(edge) {
                    return Err(CircuitError::InvalidOperation(format!(
                        "Custom entanglement topology contains duplicate edge ({}, {})",
                        i, j
                    )));
                }
            }
        }

        Ok(())
    }

    fn build_circuit(&self, prefix: &str) -> Result<Circuit, CircuitError> {
        // Validate configuration first
        self.validate()?;

        let mut circuit = Circuit::new(self.num_qubits);
        let qubits = circuit.qubits();

        // Use provided prefix if non-empty, otherwise use self.parameter_prefix
        let effective_prefix = if prefix.is_empty() {
            &self.parameter_prefix
        } else {
            prefix
        };

        // Prepare the base parameters [x_0, x_1, ..., x_{n-1}]
        let mut x_params = Vec::with_capacity(self.num_qubits);
        for i in 0..self.num_qubits {
            let param_name = format!("{}_{}", effective_prefix, i);
            let param = Parameter::try_from(param_name.as_str())
                .map_err(|_| CircuitError::InvalidParameterValue(i, f64::NAN))?;
            x_params.push(param);
        }

        // Apply the encoding for each repetition layer
        for _layer in 0..self.reps {
            // Step 1: Initial Hadamard layer to create superposition
            for q in 0..self.num_qubits {
                circuit.h(Qubit::new(q as u32))?;
            }

            // Step 2: Apply Pauli evolution for each Pauli string
            for (pauli_str, _) in &self.paulis {
                // Determine the support (non-identity positions) of the template Pauli string.
                let support = pauli_str.support();
                let k = support.len();

                // An all-identity Pauli string contributes only a global phase; skip it.
                if k == 0 {
                    continue;
                }

                // Extract the Pauli operator type at each support position.
                let template_ops: Vec<Pauli> = support
                    .iter()
                    .map(|&idx| pauli_str.get_pauli(idx))
                    .collect();

                // Generate all k-tuples of circuit qubit indices for this k-local interaction.
                // For k=1: each single qubit independently.
                // For k≥2: k-tuples from the chosen entanglement topology.
                let tuples = self.entanglement.generate_k_tuples(k, self.num_qubits);

                for tuple in &tuples {
                    // Build a full-length PauliString that places each template operator
                    // at the corresponding circuit qubit position in the tuple.
                    let mut full_pauli = PauliString::new(self.num_qubits);
                    for (pos, &qubit_idx) in tuple.iter().enumerate() {
                        full_pauli.set_pauli(qubit_idx, template_ops[pos]);
                    }

                    // Compute the encoding angle θ passed to `pauli_evolution`,
                    // which implements e^{-i θ/2 P}:
                    //
                    //   k=1: θ = 2·x_i        →  e^{-i x_i P}
                    //   k≥2: θ = 4·∏(π−x_j)  →  e^{-i 2·∏(π−x_j) P}
                    let angle = if k == 1 {
                        x_params[tuple[0]].clone() * 2.0
                    } else {
                        let pi = Parameter::pi();
                        let mut product = pi.clone() - x_params[tuple[0]].clone();
                        for &qi in &tuple[1..] {
                            product = product * (pi.clone() - x_params[qi].clone());
                        }
                        product * 4.0
                    };

                    circuit.pauli_evolution(&full_pauli, ParameterValue::Param(angle), &qubits)?;
                }
            }
        }

        Ok(circuit)
    }

    fn num_parameters(&self) -> usize {
        // The number of parameters equals the number of features (qubits)
        // Each qubit has one input feature parameter x_i
        if self.reps == 0 {
            return 0;
        }
        self.num_qubits
    }

    fn num_qubits(&self) -> usize {
        self.num_qubits
    }
}

#[cfg(test)]
#[path = "feature_map_test.rs"]
mod feature_map_test;