quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing 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
//! Batch optimization for parameterized quantum circuits using SciRS2

use super::execution::{BatchCircuit, BatchCircuitExecutor};
use super::BatchStateVector;
use crate::error::{QuantRS2Error, QuantRS2Result};
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::Complex64;
// use scirs2_core::parallel_ops::*;
use crate::optimization_stubs::{minimize, Method, OptimizeResult, Options};
use crate::parallel_ops_stubs::*;
// use scirs2_core::optimization::{minimize, Method, OptimizeResult, Options};
use std::sync::Arc;

// Import SciRS2 optimization
// extern crate scirs2_optimize;
// use scirs2_optimize::unconstrained::{minimize, Method, OptimizeResult, Options};

/// Batch optimizer for parameterized quantum circuits
pub struct BatchParameterOptimizer {
    /// Circuit executor
    executor: BatchCircuitExecutor,
    /// Optimization configuration
    config: OptimizationConfig,
    /// Cache for gradient computations
    gradient_cache: Option<GradientCache>,
}

/// Optimization configuration
#[derive(Debug, Clone)]
pub struct OptimizationConfig {
    /// Maximum iterations
    pub max_iterations: usize,
    /// Convergence tolerance
    pub tolerance: f64,
    /// Learning rate
    pub learning_rate: f64,
    /// Use parallel gradient computation
    pub parallel_gradients: bool,
    /// Optimization method
    pub method: Method,
    /// Enable gradient caching
    pub enable_cache: bool,
}

impl Default for OptimizationConfig {
    fn default() -> Self {
        Self {
            max_iterations: 100,
            tolerance: 1e-6,
            learning_rate: 0.1,
            parallel_gradients: true,
            method: Method::BFGS,
            enable_cache: true,
        }
    }
}

/// Gradient cache for repeated computations
#[derive(Debug, Clone)]
struct GradientCache {
    /// Cached gradients
    gradients: Vec<Array1<f64>>,
    /// Parameter values for cached gradients
    parameters: Vec<Vec<f64>>,
    /// Maximum cache size
    max_size: usize,
}

impl BatchParameterOptimizer {
    /// Create a new batch parameter optimizer
    pub const fn new(executor: BatchCircuitExecutor, config: OptimizationConfig) -> Self {
        let gradient_cache = if config.enable_cache {
            Some(GradientCache {
                gradients: Vec::new(),
                parameters: Vec::new(),
                max_size: 100,
            })
        } else {
            None
        };

        Self {
            executor,
            config,
            gradient_cache,
        }
    }

    /// Optimize parameters for a batch of circuits
    pub fn optimize_batch(
        &mut self,
        circuit_fn: impl Fn(&[f64]) -> QuantRS2Result<BatchCircuit> + Sync + Send + Clone + 'static,
        initial_params: &[f64],
        cost_fn: impl Fn(&BatchStateVector) -> f64 + Sync + Send + Clone + 'static,
        initial_states: &BatchStateVector,
    ) -> QuantRS2Result<OptimizeResult<f64>> {
        let _num_params = initial_params.len();

        // Define objective function
        let executor = Arc::new(self.executor.clone());
        let states = Arc::new(initial_states.clone());
        let circuit_fn = Arc::new(circuit_fn);
        let cost_fn = Arc::new(cost_fn);

        let objective = {
            let executor = executor;
            let states = states;
            let circuit_fn = circuit_fn;
            let cost_fn = cost_fn;

            move |params: &scirs2_core::ndarray::ArrayView1<f64>| -> f64 {
                let params_slice = match params.as_slice() {
                    Some(slice) => slice,
                    None => return f64::INFINITY,
                };
                let circuit = match (*circuit_fn)(params_slice) {
                    Ok(c) => c,
                    Err(_) => return f64::INFINITY,
                };

                let mut batch_copy = (*states).clone();
                match executor.execute_batch(&circuit, &mut batch_copy) {
                    Ok(_) => (*cost_fn)(&batch_copy),
                    Err(_) => f64::INFINITY,
                }
            }
        };

        // Create options
        let options = Options {
            max_iter: self.config.max_iterations,
            ftol: self.config.tolerance,
            gtol: self.config.tolerance,
            ..Default::default()
        };

        // Run optimization using SciRS2
        let initial_array = Array1::from_vec(initial_params.to_vec());
        let result = minimize(objective, &initial_array, self.config.method, Some(options));

        match result {
            Ok(opt_result) => Ok(opt_result),
            Err(e) => Err(QuantRS2Error::InvalidInput(format!(
                "Optimization failed: {e:?}"
            ))),
        }
    }

    /// Compute gradients using parameter shift rule
    pub fn compute_gradients_batch(
        &mut self,
        circuit_fn: impl Fn(&[f64]) -> QuantRS2Result<BatchCircuit> + Sync + Send,
        params: &[f64],
        cost_fn: impl Fn(&BatchStateVector) -> f64 + Sync + Send,
        initial_states: &BatchStateVector,
        shift: f64,
    ) -> QuantRS2Result<Vec<f64>> {
        // Check cache
        if let Some(cache) = &self.gradient_cache {
            for (i, cached_params) in cache.parameters.iter().enumerate() {
                if params
                    .iter()
                    .zip(cached_params)
                    .all(|(a, b)| (a - b).abs() < 1e-10)
                {
                    return Ok(cache.gradients[i].to_vec());
                }
            }
        }

        let num_params = params.len();

        if self.config.parallel_gradients {
            // Compute gradients in parallel
            // Clone executor for parallel use
            let executor = self.executor.clone();
            let gradients: Vec<f64> = (0..num_params)
                .into_par_iter()
                .map(|i| {
                    compute_single_gradient_static(
                        &executor,
                        &circuit_fn,
                        params,
                        i,
                        &cost_fn,
                        initial_states,
                        shift,
                    )
                    .unwrap_or(0.0)
                })
                .collect();

            // Update cache
            if let Some(cache) = &mut self.gradient_cache {
                if cache.gradients.len() >= cache.max_size {
                    cache.gradients.remove(0);
                    cache.parameters.remove(0);
                }
                cache.gradients.push(Array1::from_vec(gradients.clone()));
                cache.parameters.push(params.to_vec());
            }

            Ok(gradients)
        } else {
            // Sequential gradient computation
            let mut gradients = vec![0.0; num_params];

            for i in 0..num_params {
                gradients[i] = self.compute_single_gradient(
                    &circuit_fn,
                    params,
                    i,
                    &cost_fn,
                    initial_states,
                    shift,
                )?;
            }

            Ok(gradients)
        }
    }

    /// Compute gradient for a single parameter
    fn compute_single_gradient(
        &self,
        circuit_fn: impl Fn(&[f64]) -> QuantRS2Result<BatchCircuit>,
        params: &[f64],
        param_idx: usize,
        cost_fn: impl Fn(&BatchStateVector) -> f64,
        initial_states: &BatchStateVector,
        shift: f64,
    ) -> QuantRS2Result<f64> {
        compute_single_gradient_static(
            &self.executor,
            &circuit_fn,
            params,
            param_idx,
            &cost_fn,
            initial_states,
            shift,
        )
    }

    /// Optimize multiple parameter sets in parallel
    pub fn optimize_parallel_batch(
        &mut self,
        circuit_fn: impl Fn(&[f64]) -> QuantRS2Result<BatchCircuit> + Sync + Send + Clone + 'static,
        initial_param_sets: &[Vec<f64>],
        cost_fn: impl Fn(&BatchStateVector) -> f64 + Sync + Send + Clone + 'static,
        initial_states: &BatchStateVector,
    ) -> QuantRS2Result<Vec<OptimizeResult<f64>>> {
        let results: Vec<_> = initial_param_sets
            .par_iter()
            .map(|params| {
                let mut optimizer = self.clone();
                optimizer.optimize_batch(
                    circuit_fn.clone(),
                    params,
                    cost_fn.clone(),
                    initial_states,
                )
            })
            .collect::<QuantRS2Result<Vec<_>>>()?;

        Ok(results)
    }
}

impl Clone for BatchParameterOptimizer {
    fn clone(&self) -> Self {
        Self {
            executor: self.executor.clone(),
            config: self.config.clone(),
            gradient_cache: self.gradient_cache.clone(),
        }
    }
}

impl Clone for BatchCircuitExecutor {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            gpu_backend: self.gpu_backend.clone(),
            // thread_pool: None, // Don't clone thread pool - removed per CORE_USAGE_POLICY
        }
    }
}

/// Static function to compute gradient for a single parameter
fn compute_single_gradient_static(
    executor: &BatchCircuitExecutor,
    circuit_fn: &impl Fn(&[f64]) -> QuantRS2Result<BatchCircuit>,
    params: &[f64],
    param_idx: usize,
    cost_fn: &impl Fn(&BatchStateVector) -> f64,
    initial_states: &BatchStateVector,
    shift: f64,
) -> QuantRS2Result<f64> {
    // Parameter shift rule: df/dθ = (f(θ+π/2) - f(θ-π/2)) / 2
    let mut params_plus = params.to_vec();
    let mut params_minus = params.to_vec();

    params_plus[param_idx] += shift;
    params_minus[param_idx] -= shift;

    // Evaluate at shifted parameters
    let circuit_plus = circuit_fn(&params_plus)?;
    let circuit_minus = circuit_fn(&params_minus)?;

    let mut states_plus = initial_states.clone();
    let mut states_minus = initial_states.clone();

    let result_plus = executor.execute_batch(&circuit_plus, &mut states_plus);
    let result_minus = executor.execute_batch(&circuit_minus, &mut states_minus);

    result_plus?;
    result_minus?;

    let cost_plus = cost_fn(&states_plus);
    let cost_minus = cost_fn(&states_minus);

    Ok((cost_plus - cost_minus) / (2.0 * shift))
}

/// Batch VQE (Variational Quantum Eigensolver) optimization
pub struct BatchVQE {
    /// Parameter optimizer
    optimizer: BatchParameterOptimizer,
    /// Hamiltonian to minimize
    hamiltonian: Array2<Complex64>,
}

impl BatchVQE {
    /// Create a new batch VQE optimizer
    pub const fn new(
        executor: BatchCircuitExecutor,
        hamiltonian: Array2<Complex64>,
        config: OptimizationConfig,
    ) -> Self {
        Self {
            optimizer: BatchParameterOptimizer::new(executor, config),
            hamiltonian,
        }
    }

    /// Run VQE optimization
    pub fn optimize(
        &mut self,
        ansatz_fn: impl Fn(&[f64]) -> QuantRS2Result<BatchCircuit> + Sync + Send + Clone + 'static,
        initial_params: &[f64],
        num_samples: usize,
        n_qubits: usize,
    ) -> QuantRS2Result<VQEResult> {
        // Create batch of initial states
        let batch = BatchStateVector::new(num_samples, n_qubits, Default::default())?;

        // Define cost function (energy expectation)
        let hamiltonian = self.hamiltonian.clone();
        let cost_fn = move |states: &BatchStateVector| -> f64 {
            let mut total_energy = 0.0;

            for i in 0..states.batch_size() {
                if let Ok(state) = states.get_state(i) {
                    let energy = compute_energy(&state, &hamiltonian);
                    total_energy += energy;
                }
            }

            total_energy / states.batch_size() as f64
        };

        // Run optimization
        let result = self
            .optimizer
            .optimize_batch(ansatz_fn, initial_params, cost_fn, &batch)?;

        Ok(VQEResult {
            optimal_params: result.x.to_vec(),
            ground_state_energy: result.fun,
            iterations: result.iterations,
            converged: result.success,
        })
    }
}

/// VQE optimization result
#[derive(Debug, Clone)]
pub struct VQEResult {
    /// Optimal parameters
    pub optimal_params: Vec<f64>,
    /// Ground state energy
    pub ground_state_energy: f64,
    /// Number of iterations
    pub iterations: usize,
    /// Whether optimization converged
    pub converged: bool,
}

/// Compute energy expectation value
fn compute_energy(state: &Array1<Complex64>, hamiltonian: &Array2<Complex64>) -> f64 {
    let temp = hamiltonian.dot(state);
    let energy = state
        .iter()
        .zip(temp.iter())
        .map(|(a, b)| a.conj() * b)
        .sum::<Complex64>();

    energy.re
}

/// Batch QAOA (Quantum Approximate Optimization Algorithm)
pub struct BatchQAOA {
    /// Parameter optimizer
    optimizer: BatchParameterOptimizer,
    /// Problem Hamiltonian
    cost_hamiltonian: Array2<Complex64>,
    /// Mixer Hamiltonian
    mixer_hamiltonian: Array2<Complex64>,
    /// Number of QAOA layers
    p: usize,
}

impl BatchQAOA {
    /// Create a new batch QAOA optimizer
    pub const fn new(
        executor: BatchCircuitExecutor,
        cost_hamiltonian: Array2<Complex64>,
        mixer_hamiltonian: Array2<Complex64>,
        p: usize,
        config: OptimizationConfig,
    ) -> Self {
        Self {
            optimizer: BatchParameterOptimizer::new(executor, config),
            cost_hamiltonian,
            mixer_hamiltonian,
            p,
        }
    }

    /// Run QAOA optimization
    pub fn optimize(
        &mut self,
        initial_params: &[f64],
        num_samples: usize,
        n_qubits: usize,
    ) -> QuantRS2Result<QAOAResult> {
        if initial_params.len() != 2 * self.p {
            return Err(QuantRS2Error::InvalidInput(format!(
                "Expected {} parameters, got {}",
                2 * self.p,
                initial_params.len()
            )));
        }

        // Create QAOA circuit constructor
        // let _p = self.p;
        let _cost_ham = self.cost_hamiltonian.clone();
        let _mixer_ham = self.mixer_hamiltonian.clone();

        let qaoa_circuit = move |_params: &[f64]| -> QuantRS2Result<BatchCircuit> {
            // This is a placeholder - actual QAOA circuit construction would go here
            let circuit = BatchCircuit::new(n_qubits);
            // Add QAOA layers based on params, cost_ham, mixer_ham
            Ok(circuit)
        };

        // Create batch of superposition initial states
        let batch = BatchStateVector::new(num_samples, n_qubits, Default::default())?;
        // Initialize to uniform superposition (would apply Hadamards)

        // Define cost function
        let cost_hamiltonian = self.cost_hamiltonian.clone();
        let cost_fn = move |states: &BatchStateVector| -> f64 {
            let mut total_cost = 0.0;

            for i in 0..states.batch_size() {
                if let Ok(state) = states.get_state(i) {
                    let cost = compute_energy(&state, &cost_hamiltonian);
                    total_cost += cost;
                }
            }

            total_cost / states.batch_size() as f64
        };

        // Run optimization
        let result =
            self.optimizer
                .optimize_batch(qaoa_circuit, initial_params, cost_fn, &batch)?;

        Ok(QAOAResult {
            optimal_params: result.x.to_vec(),
            optimal_cost: result.fun,
            iterations: result.iterations,
            converged: result.success,
        })
    }
}

/// QAOA optimization result
#[derive(Debug, Clone)]
pub struct QAOAResult {
    /// Optimal parameters (beta and gamma values)
    pub optimal_params: Vec<f64>,
    /// Optimal cost value
    pub optimal_cost: f64,
    /// Number of iterations
    pub iterations: usize,
    /// Whether optimization converged
    pub converged: bool,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::gate::single::Hadamard;
    use crate::qubit::QubitId;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_gradient_computation() {
        let config = Default::default();
        let executor = BatchCircuitExecutor::new(config)
            .expect("BatchCircuitExecutor creation should succeed");
        let mut optimizer = BatchParameterOptimizer::new(executor, Default::default());

        // Simple circuit function
        let circuit_fn = |_params: &[f64]| -> QuantRS2Result<BatchCircuit> {
            let mut circuit = BatchCircuit::new(1);
            // Add parameterized rotation based on params[0]
            circuit.add_gate(Box::new(Hadamard { target: QubitId(0) }))?;
            Ok(circuit)
        };

        // Simple cost function
        let cost_fn = |_states: &BatchStateVector| -> f64 { 1.0 };

        let batch = BatchStateVector::new(1, 1, Default::default())
            .expect("BatchStateVector creation should succeed");
        let params = vec![0.5];

        let gradients = optimizer
            .compute_gradients_batch(circuit_fn, &params, cost_fn, &batch, 0.01)
            .expect("Gradient computation should succeed");

        assert_eq!(gradients.len(), 1);
    }

    #[test]
    fn test_vqe_setup() {
        let executor = BatchCircuitExecutor::new(Default::default())
            .expect("BatchCircuitExecutor creation should succeed");

        // Simple 2x2 Hamiltonian
        let hamiltonian = array![
            [Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
            [Complex64::new(0.0, 0.0), Complex64::new(-1.0, 0.0)]
        ];

        let vqe = BatchVQE::new(executor, hamiltonian, Default::default());

        // Just test creation
        assert_eq!(vqe.hamiltonian.shape(), &[2, 2]);
    }
}