quantrs2-sim 0.1.3

Quantum circuit simulators for the QuantRS2 framework
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
//! Holographic encoding methods for quantum error correction.
//!
//! This module contains methods for creating various holographic encoding matrices
//! used in the holographic quantum error correction framework.

use scirs2_core::ndarray::Array2;
use scirs2_core::Complex64;
use std::f64::consts::PI;

use crate::error::Result;

use super::config::HolographicCodeType;
use super::simulator::HolographicQECSimulator;

impl HolographicQECSimulator {
    /// Create holographic encoding matrix using tensor network structure
    pub fn create_holographic_encoding_matrix(
        &self,
        boundary_dim: usize,
        bulk_dim: usize,
    ) -> Result<Array2<Complex64>> {
        let mut encoding_matrix = Array2::zeros((bulk_dim, boundary_dim));

        match self.config.error_correction_code {
            HolographicCodeType::AdSRindler => {
                self.create_ads_rindler_encoding(&mut encoding_matrix)?;
            }
            HolographicCodeType::HolographicStabilizer => {
                self.create_holographic_stabilizer_encoding(&mut encoding_matrix)?;
            }
            HolographicCodeType::BulkGeometry => {
                self.create_bulk_geometry_encoding(&mut encoding_matrix)?;
            }
            HolographicCodeType::TensorNetwork => {
                self.create_tensor_network_encoding(&mut encoding_matrix)?;
            }
            HolographicCodeType::HolographicSurface => {
                self.create_holographic_surface_encoding(&mut encoding_matrix)?;
            }
            HolographicCodeType::PerfectTensor => {
                self.create_perfect_tensor_encoding(&mut encoding_matrix)?;
            }
            HolographicCodeType::EntanglementEntropy => {
                self.create_entanglement_entropy_encoding(&mut encoding_matrix)?;
            }
            HolographicCodeType::AdSCFTCode => {
                self.create_ads_cft_encoding(&mut encoding_matrix)?;
            }
        }

        Ok(encoding_matrix)
    }

    /// Create AdS-Rindler holographic encoding
    pub fn create_ads_rindler_encoding(
        &self,
        encoding_matrix: &mut Array2<Complex64>,
    ) -> Result<()> {
        let (bulk_dim, boundary_dim) = encoding_matrix.dim();

        // AdS-Rindler encoding based on Rindler coordinates
        for i in 0..bulk_dim {
            for j in 0..boundary_dim {
                let rindler_factor = self.calculate_rindler_factor(i, j);
                let entanglement_factor = self.calculate_entanglement_factor(i, j);

                encoding_matrix[[i, j]] = Complex64::new(rindler_factor * entanglement_factor, 0.0);
            }
        }

        // Normalize the encoding matrix
        Self::normalize_encoding_matrix(encoding_matrix)?;
        Ok(())
    }

    /// Calculate Rindler factor for AdS-Rindler encoding
    #[must_use]
    pub fn calculate_rindler_factor(&self, bulk_index: usize, boundary_index: usize) -> f64 {
        let rindler_horizon = self.config.ads_radius;
        let bulk_position = (bulk_index as f64) / f64::from(1 << self.config.bulk_qubits);
        let boundary_position =
            (boundary_index as f64) / f64::from(1 << self.config.boundary_qubits);

        // Rindler transformation factor with phase shift to avoid zeros
        let factor = (rindler_horizon * bulk_position).cosh()
            * (2.0 * PI).mul_add(boundary_position, PI / 4.0).cos();

        factor.abs().max(1e-10)
    }

    /// Calculate entanglement factor for holographic encoding
    #[must_use]
    pub fn calculate_entanglement_factor(&self, bulk_index: usize, boundary_index: usize) -> f64 {
        let mutual_information = self.calculate_mutual_information(bulk_index, boundary_index);
        let entanglement_entropy = self.calculate_entanglement_entropy(bulk_index, boundary_index);

        (mutual_information * entanglement_entropy).sqrt()
    }

    /// Calculate mutual information between bulk and boundary regions
    pub(crate) fn calculate_mutual_information(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> f64 {
        let bulk_entropy = self.calculate_region_entropy(bulk_index, true);
        let boundary_entropy = self.calculate_region_entropy(boundary_index, false);
        let joint_entropy = self.calculate_joint_entropy(bulk_index, boundary_index);

        bulk_entropy + boundary_entropy - joint_entropy
    }

    /// Calculate entanglement entropy for region
    pub(crate) fn calculate_entanglement_entropy(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> f64 {
        // Use Ryu-Takayanagi prescription: S = Area/(4G)
        let area = self.calculate_rt_surface_area(bulk_index, boundary_index);
        let gravitational_constant = 1.0; // Natural units

        area / (4.0 * gravitational_constant)
    }

    /// Calculate Ryu-Takayanagi surface area
    pub(crate) fn calculate_rt_surface_area(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> f64 {
        let bulk_position = (bulk_index as f64) / f64::from(1 << self.config.bulk_qubits);
        let boundary_position =
            (boundary_index as f64) / f64::from(1 << self.config.boundary_qubits);

        // Minimal surface area calculation
        let radial_distance = (bulk_position - boundary_position).abs();
        let ads_factor = self.config.ads_radius / radial_distance.mul_add(radial_distance, 1.0);

        ads_factor * self.config.central_charge
    }

    /// Calculate region entropy
    pub(crate) fn calculate_region_entropy(&self, region_index: usize, is_bulk: bool) -> f64 {
        let max_index = if is_bulk {
            Self::safe_dimension(self.config.bulk_qubits).unwrap_or(8)
        } else {
            Self::safe_dimension(self.config.boundary_qubits).unwrap_or(4)
        };

        // Ensure we have at least a reasonable minimum for computation
        let max_index = max_index.max(2);
        let region_size = ((region_index % max_index) as f64 + 0.1) / (max_index as f64 + 0.2);

        // Von Neumann entropy approximation with improved bounds
        if region_size > 0.01 && region_size < 0.99 {
            (-region_size).mul_add(
                region_size.ln(),
                -((1.0 - region_size) * (1.0 - region_size).ln()),
            )
        } else {
            // Return a small positive entropy instead of zero
            0.1
        }
    }

    /// Calculate joint entropy
    fn calculate_joint_entropy(&self, bulk_index: usize, boundary_index: usize) -> f64 {
        let combined_entropy = self.calculate_region_entropy(bulk_index, true)
            + self.calculate_region_entropy(boundary_index, false);

        // Add quantum correlations
        let correlation_factor = self.calculate_correlation_factor(bulk_index, boundary_index);
        combined_entropy * (1.0 - correlation_factor)
    }

    /// Calculate correlation factor between bulk and boundary
    pub(crate) fn calculate_correlation_factor(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> f64 {
        let bulk_position = (bulk_index as f64) / f64::from(1 << self.config.bulk_qubits);
        let boundary_position =
            (boundary_index as f64) / f64::from(1 << self.config.boundary_qubits);

        // Correlation based on holographic correspondence
        let distance = (bulk_position - boundary_position).abs();
        (-distance / self.config.ads_radius).exp()
    }

    /// Create holographic stabilizer encoding
    pub(crate) fn create_holographic_stabilizer_encoding(
        &self,
        encoding_matrix: &mut Array2<Complex64>,
    ) -> Result<()> {
        let (bulk_dim, boundary_dim) = encoding_matrix.dim();

        // Create stabilizer-based holographic encoding
        for i in 0..bulk_dim {
            for j in 0..boundary_dim {
                let stabilizer_factor = Self::calculate_stabilizer_factor(i, j);
                let holographic_factor = self.calculate_holographic_factor(i, j);

                encoding_matrix[[i, j]] =
                    Complex64::new(stabilizer_factor * holographic_factor, 0.0);
            }
        }

        Self::normalize_encoding_matrix(encoding_matrix)?;
        Ok(())
    }

    /// Calculate stabilizer factor for encoding
    fn calculate_stabilizer_factor(bulk_index: usize, boundary_index: usize) -> f64 {
        let bulk_parity = f64::from(bulk_index.count_ones() % 2);
        let boundary_parity = f64::from(boundary_index.count_ones() % 2);

        // Stabilizer correlation
        if bulk_parity == boundary_parity {
            1.0 / (1.0 + bulk_index as f64).sqrt()
        } else {
            -1.0 / (1.0 + bulk_index as f64).sqrt()
        }
    }

    /// Calculate holographic factor for encoding
    pub(crate) fn calculate_holographic_factor(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> f64 {
        let bulk_weight = f64::from(bulk_index.count_ones());
        let boundary_weight = f64::from(boundary_index.count_ones());

        // Holographic weight correlation
        let weight_correlation = (bulk_weight - boundary_weight).abs();
        (-weight_correlation / self.config.central_charge).exp()
    }

    /// Create bulk geometry encoding
    pub(crate) fn create_bulk_geometry_encoding(
        &self,
        encoding_matrix: &mut Array2<Complex64>,
    ) -> Result<()> {
        let (bulk_dim, boundary_dim) = encoding_matrix.dim();

        // Encoding based on bulk geometry and geodesics
        for i in 0..bulk_dim {
            for j in 0..boundary_dim {
                let geodesic_length = self.calculate_geodesic_length(i, j);
                let geometric_factor = self.calculate_geometric_factor(i, j);

                encoding_matrix[[i, j]] = Complex64::new(
                    (-geodesic_length / self.config.ads_radius).exp() * geometric_factor,
                    0.0,
                );
            }
        }

        Self::normalize_encoding_matrix(encoding_matrix)?;
        Ok(())
    }

    /// Calculate geodesic length in `AdS` space
    pub(crate) fn calculate_geodesic_length(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> f64 {
        let bulk_position = (bulk_index as f64) / f64::from(1 << self.config.bulk_qubits);
        let boundary_position =
            (boundary_index as f64) / f64::from(1 << self.config.boundary_qubits);

        // AdS geodesic length calculation
        let radial_bulk = 1.0 / (1.0 - bulk_position);
        let radial_boundary = 1.0 / (1.0 - boundary_position);

        self.config.ads_radius * (radial_bulk / radial_boundary).ln().abs()
    }

    /// Calculate geometric factor
    pub(crate) fn calculate_geometric_factor(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> f64 {
        let bulk_curvature = self.calculate_bulk_curvature(bulk_index);
        let boundary_curvature = self.calculate_boundary_curvature(boundary_index);

        (bulk_curvature.abs() / boundary_curvature).sqrt()
    }

    /// Calculate bulk curvature
    fn calculate_bulk_curvature(&self, bulk_index: usize) -> f64 {
        let position = (bulk_index as f64) / f64::from(1 << self.config.bulk_qubits);
        let ads_curvature = -1.0 / (self.config.ads_radius * self.config.ads_radius);

        ads_curvature * (1.0 - position).powi(2)
    }

    /// Calculate boundary curvature
    fn calculate_boundary_curvature(&self, boundary_index: usize) -> f64 {
        let position = (boundary_index as f64) / f64::from(1 << self.config.boundary_qubits);

        // Boundary is typically flat, but can have induced curvature
        // Ensure positive curvature to avoid division by zero
        0.1f64
            .mul_add((2.0 * PI * position).sin(), 1.0)
            .abs()
            .max(0.1)
    }

    /// Create tensor network encoding
    pub(crate) fn create_tensor_network_encoding(
        &self,
        encoding_matrix: &mut Array2<Complex64>,
    ) -> Result<()> {
        let (bulk_dim, boundary_dim) = encoding_matrix.dim();

        // Tensor network based holographic encoding
        for i in 0..bulk_dim {
            for j in 0..boundary_dim {
                let tensor_element = self.calculate_tensor_network_element(i, j);
                encoding_matrix[[i, j]] = tensor_element;
            }
        }

        Self::normalize_encoding_matrix(encoding_matrix)?;
        Ok(())
    }

    /// Calculate tensor network element
    pub(crate) fn calculate_tensor_network_element(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> Complex64 {
        let bulk_legs = Self::get_tensor_legs(bulk_index, true);
        let boundary_legs = Self::get_tensor_legs(boundary_index, false);

        // Contract tensor legs between bulk and boundary
        let contraction_value = Self::contract_tensor_legs(&bulk_legs, &boundary_legs);

        Complex64::new(contraction_value, 0.0)
    }

    /// Get tensor legs for given index
    fn get_tensor_legs(index: usize, is_bulk: bool) -> Vec<f64> {
        let mut legs = Vec::new();
        let num_legs = if is_bulk { 4 } else { 2 }; // Bulk tensors have more legs

        for i in 0..num_legs {
            let leg_value = (((index >> i) & 1) as f64).mul_add(2.0, -1.0); // Convert to {-1, 1}
            legs.push(leg_value);
        }

        legs
    }

    /// Contract tensor legs
    fn contract_tensor_legs(bulk_legs: &[f64], boundary_legs: &[f64]) -> f64 {
        let mut contraction = 1.0;

        // Contract matching legs
        let min_legs = bulk_legs.len().min(boundary_legs.len());
        for i in 0..min_legs {
            contraction *= bulk_legs[i] * boundary_legs[i];
        }

        // Add remaining bulk leg contributions
        for leg in &bulk_legs[min_legs..] {
            contraction *= leg;
        }

        contraction / (bulk_legs.len() as f64).sqrt()
    }

    /// Create holographic surface encoding
    pub(crate) fn create_holographic_surface_encoding(
        &self,
        encoding_matrix: &mut Array2<Complex64>,
    ) -> Result<()> {
        let (bulk_dim, boundary_dim) = encoding_matrix.dim();

        // Surface code based holographic encoding
        for i in 0..bulk_dim {
            for j in 0..boundary_dim {
                let surface_element = self.calculate_surface_code_element(i, j);
                encoding_matrix[[i, j]] = surface_element;
            }
        }

        Self::normalize_encoding_matrix(encoding_matrix)?;
        Ok(())
    }

    /// Calculate surface code element
    fn calculate_surface_code_element(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> Complex64 {
        let bulk_x = bulk_index % (1 << (self.config.bulk_qubits / 2));
        let bulk_y = bulk_index / (1 << (self.config.bulk_qubits / 2));
        let boundary_x = boundary_index % (1 << (self.config.boundary_qubits / 2));
        let boundary_y = boundary_index / (1 << (self.config.boundary_qubits / 2));

        // Surface code connectivity
        let x_parity = (bulk_x ^ boundary_x).count_ones() % 2;
        let y_parity = (bulk_y ^ boundary_y).count_ones() % 2;

        let amplitude = if x_parity == y_parity {
            1.0 / (1.0 + (bulk_x + bulk_y) as f64).sqrt()
        } else {
            // Use suppressed but non-zero value for off-parity connections
            1e-8 / (2.0 + (bulk_x + bulk_y) as f64).sqrt()
        };

        Complex64::new(amplitude, 0.0)
    }

    /// Create perfect tensor encoding
    pub(crate) fn create_perfect_tensor_encoding(
        &self,
        encoding_matrix: &mut Array2<Complex64>,
    ) -> Result<()> {
        let (bulk_dim, boundary_dim) = encoding_matrix.dim();

        // Perfect tensor network encoding
        for i in 0..bulk_dim {
            for j in 0..boundary_dim {
                let perfect_element = self.calculate_perfect_tensor_element(i, j);
                encoding_matrix[[i, j]] = perfect_element;
            }
        }

        Self::normalize_encoding_matrix(encoding_matrix)?;
        Ok(())
    }

    /// Calculate perfect tensor element
    fn calculate_perfect_tensor_element(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> Complex64 {
        let bulk_state = Self::index_to_state_vector(bulk_index, self.config.bulk_qubits);
        let boundary_state =
            Self::index_to_state_vector(boundary_index, self.config.boundary_qubits);

        // Perfect tensor conditions
        let overlap = Self::calculate_state_overlap(&bulk_state, &boundary_state);
        let perfect_factor = Self::calculate_perfect_tensor_factor(bulk_index, boundary_index);

        Complex64::new(overlap * perfect_factor, 0.0)
    }

    /// Convert index to state vector
    fn index_to_state_vector(index: usize, num_qubits: usize) -> Vec<f64> {
        let mut state = vec![0.0; num_qubits];
        for (i, elem) in state.iter_mut().enumerate() {
            *elem = if (index >> i) & 1 == 1 { 1.0 } else { 0.0 };
        }
        state
    }

    /// Calculate state overlap
    fn calculate_state_overlap(state1: &[f64], state2: &[f64]) -> f64 {
        let min_len = state1.len().min(state2.len());
        let mut overlap = 0.0;

        for i in 0..min_len {
            overlap += state1[i] * state2[i];
        }

        overlap / (min_len as f64).sqrt()
    }

    /// Calculate perfect tensor factor
    fn calculate_perfect_tensor_factor(bulk_index: usize, boundary_index: usize) -> f64 {
        let bulk_weight = f64::from(bulk_index.count_ones());
        let boundary_weight = f64::from(boundary_index.count_ones());

        // Perfect tensor satisfies specific weight conditions
        if (bulk_weight - boundary_weight).abs() <= 1.0 {
            1.0 / (1.0 + bulk_weight).sqrt()
        } else {
            // Use exponentially suppressed but non-zero value
            1e-6 / (1.0 + (bulk_weight - boundary_weight).abs()).sqrt()
        }
    }

    /// Create entanglement entropy encoding
    pub(crate) fn create_entanglement_entropy_encoding(
        &self,
        encoding_matrix: &mut Array2<Complex64>,
    ) -> Result<()> {
        let (bulk_dim, boundary_dim) = encoding_matrix.dim();

        // Encoding based on entanglement entropy structure
        for i in 0..bulk_dim {
            for j in 0..boundary_dim {
                let entropy_element = self.calculate_entanglement_entropy_element(i, j);
                encoding_matrix[[i, j]] = entropy_element;
            }
        }

        Self::normalize_encoding_matrix(encoding_matrix)?;
        Ok(())
    }

    /// Calculate entanglement entropy element
    fn calculate_entanglement_entropy_element(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> Complex64 {
        let bulk_entropy = self.calculate_region_entropy(bulk_index, true);
        let boundary_entropy = self.calculate_region_entropy(boundary_index, false);
        let mutual_information = self.calculate_mutual_information(bulk_index, boundary_index);

        // Entanglement entropy based amplitude
        let amplitude = (mutual_information / (bulk_entropy + boundary_entropy + 1e-10)).sqrt();

        Complex64::new(amplitude, 0.0)
    }

    /// Create AdS/CFT encoding
    pub(crate) fn create_ads_cft_encoding(
        &self,
        encoding_matrix: &mut Array2<Complex64>,
    ) -> Result<()> {
        let (bulk_dim, boundary_dim) = encoding_matrix.dim();

        // AdS/CFT correspondence encoding
        for i in 0..bulk_dim {
            for j in 0..boundary_dim {
                let ads_cft_element = self.calculate_ads_cft_element(i, j);
                encoding_matrix[[i, j]] = ads_cft_element;
            }
        }

        Self::normalize_encoding_matrix(encoding_matrix)?;
        Ok(())
    }

    /// Calculate AdS/CFT element
    pub(crate) fn calculate_ads_cft_element(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> Complex64 {
        let bulk_field = self.calculate_bulk_field_value(bulk_index);
        let boundary_field = self.calculate_boundary_field_value(boundary_index);
        let correlation_function = self.calculate_correlation_function(bulk_index, boundary_index);

        // AdS/CFT dictionary
        let amplitude = bulk_field * boundary_field * correlation_function;

        Complex64::new(amplitude, 0.0)
    }

    /// Calculate bulk field value
    pub(crate) fn calculate_bulk_field_value(&self, bulk_index: usize) -> f64 {
        let position = (bulk_index as f64) / f64::from(1 << self.config.bulk_qubits);
        let radial_coordinate = 1.0 / (1.0 - position);

        // Bulk field in AdS space
        (radial_coordinate / self.config.ads_radius).powf(self.calculate_conformal_dimension())
    }

    /// Calculate boundary field value
    pub(crate) fn calculate_boundary_field_value(&self, boundary_index: usize) -> f64 {
        let position = (boundary_index as f64) / f64::from(1 << self.config.boundary_qubits);

        // Boundary CFT field
        (2.0 * PI * position).sin() / (1.0 + position).sqrt()
    }

    /// Calculate conformal dimension
    pub(crate) fn calculate_conformal_dimension(&self) -> f64 {
        // Conformal dimension based on central charge
        (self.config.central_charge / 12.0).sqrt()
    }

    /// Calculate correlation function
    pub(crate) fn calculate_correlation_function(
        &self,
        bulk_index: usize,
        boundary_index: usize,
    ) -> f64 {
        let bulk_position = (bulk_index as f64) / f64::from(1 << self.config.bulk_qubits);
        let boundary_position =
            (boundary_index as f64) / f64::from(1 << self.config.boundary_qubits);

        // Two-point correlation function
        let distance = (bulk_position - boundary_position).abs();
        let conformal_dimension = self.calculate_conformal_dimension();

        1.0 / (1.0 + distance).powf(2.0 * conformal_dimension)
    }

    /// Normalize encoding matrix
    pub(crate) fn normalize_encoding_matrix(encoding_matrix: &mut Array2<Complex64>) -> Result<()> {
        let (rows, cols) = encoding_matrix.dim();

        // Normalize each column
        for j in 0..cols {
            let mut column_norm = 0.0;
            for i in 0..rows {
                column_norm += encoding_matrix[[i, j]].norm_sqr();
            }

            if column_norm > 1e-10 {
                let norm_factor = 1.0 / column_norm.sqrt();
                for i in 0..rows {
                    encoding_matrix[[i, j]] *= norm_factor;
                }
            } else {
                // If column is all zeros, add small diagonal elements
                if j < rows {
                    encoding_matrix[[j, j]] = Complex64::new(1e-6, 0.0);
                } else {
                    encoding_matrix[[0, j]] = Complex64::new(1e-6, 0.0);
                }
            }
        }

        Ok(())
    }
}