quantrs2-ml 0.1.3

Quantum Machine Learning module for QuantRS2
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
#![allow(
    clippy::pedantic,
    clippy::unnecessary_wraps,
    clippy::needless_range_loop,
    clippy::useless_vec,
    clippy::needless_collect,
    clippy::too_many_arguments
)]
//! Quantum Neural Architecture Search Example
//!
//! This example demonstrates various quantum neural architecture search algorithms
//! including evolutionary search, reinforcement learning, random search,
//! Bayesian optimization, and DARTS.

use quantrs2_ml::prelude::*;
use quantrs2_ml::qnn::QNNLayerType;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::prelude::*;

fn main() -> Result<()> {
    println!("=== Quantum Neural Architecture Search Demo ===\n");

    // Step 1: Evolutionary algorithm search
    println!("1. Evolutionary Algorithm Search...");
    evolutionary_search_demo()?;

    // Step 2: Random search baseline
    println!("\n2. Random Search Baseline...");
    random_search_demo()?;

    // Step 3: Reinforcement learning search
    println!("\n3. Reinforcement Learning Search...");
    rl_search_demo()?;

    // Step 4: Bayesian optimization
    println!("\n4. Bayesian Optimization Search...");
    bayesian_search_demo()?;

    // Step 5: DARTS (Differentiable Architecture Search)
    println!("\n5. DARTS (Differentiable Architecture Search)...");
    darts_demo()?;

    // Step 6: Multi-objective optimization
    println!("\n6. Multi-Objective Optimization...");
    multi_objective_demo()?;

    // Step 7: Architecture analysis
    println!("\n7. Architecture Analysis...");
    architecture_analysis_demo()?;

    println!("\n=== Quantum NAS Demo Complete ===");

    Ok(())
}

/// Evolutionary algorithm search demonstration
fn evolutionary_search_demo() -> Result<()> {
    // Create search space
    let search_space = create_default_search_space();

    // Configure evolutionary strategy
    let strategy = SearchStrategy::Evolutionary {
        population_size: 20,
        mutation_rate: 0.2,
        crossover_rate: 0.7,
        elitism_ratio: 0.1,
    };

    let mut nas = QuantumNAS::new(strategy, search_space);

    println!("   Created evolutionary NAS:");
    println!("   - Population size: 20");
    println!("   - Mutation rate: 0.2");
    println!("   - Crossover rate: 0.7");
    println!("   - Elitism ratio: 0.1");

    // Set evaluation data (synthetic for demo)
    let eval_data = Array2::from_shape_fn((100, 4), |(i, j)| (i as f64 + j as f64) / 50.0);
    let eval_labels = Array1::from_shape_fn(100, |i| i % 2);
    nas.set_evaluation_data(eval_data, eval_labels);

    // Run search
    println!("\n   Running evolutionary search for 10 generations...");
    let best_architectures = nas.search(10)?;

    println!("   Search complete!");
    println!(
        "   - Best architectures found: {}",
        best_architectures.len()
    );

    if let Some(best) = best_architectures.first() {
        println!("   - Best architecture: {best}");
        println!("   - Circuit depth: {}", best.metrics.circuit_depth);
        println!("   - Parameter count: {}", best.metrics.parameter_count);

        if let Some(expressivity) = best.properties.expressivity {
            println!("   - Expressivity: {expressivity:.3}");
        }
    }

    // Show search summary
    let summary = nas.get_search_summary();
    println!(
        "   - Total architectures evaluated: {}",
        summary.total_architectures_evaluated
    );
    println!("   - Pareto front size: {}", summary.pareto_front_size);

    Ok(())
}

/// Random search baseline demonstration
fn random_search_demo() -> Result<()> {
    let search_space = create_default_search_space();
    let strategy = SearchStrategy::Random { num_samples: 50 };

    let mut nas = QuantumNAS::new(strategy, search_space);

    println!("   Created random search NAS:");
    println!("   - Number of samples: 50");

    // Generate synthetic evaluation data
    let eval_data = Array2::from_shape_fn((80, 4), |(i, j)| {
        0.5f64.mul_add((i as f64).sin(), 0.3 * (j as f64).cos())
    });
    let eval_labels = Array1::from_shape_fn(80, |i| usize::from(i % 3 != 0));
    nas.set_evaluation_data(eval_data, eval_labels);

    println!("\n   Running random search...");
    let best_architectures = nas.search(50)?;

    println!("   Random search complete!");
    if let Some(best) = best_architectures.first() {
        println!("   - Best random architecture: {best}");
        if let Some(accuracy) = best.metrics.accuracy {
            println!("   - Accuracy: {accuracy:.3}");
        }
    }

    Ok(())
}

/// Reinforcement learning search demonstration
fn rl_search_demo() -> Result<()> {
    let search_space = create_custom_search_space();

    let strategy = SearchStrategy::ReinforcementLearning {
        agent_type: RLAgentType::PolicyGradient,
        exploration_rate: 0.3,
        learning_rate: 0.01,
    };

    let mut nas = QuantumNAS::new(strategy, search_space);

    println!("   Created RL-based NAS:");
    println!("   - Agent type: Policy Gradient");
    println!("   - Exploration rate: 0.3");
    println!("   - Learning rate: 0.01");

    println!("\n   Running RL search for 100 episodes...");
    let best_architectures = nas.search(100)?;

    println!("   RL search complete!");
    println!("   - Architectures found: {}", best_architectures.len());

    if let Some(best) = best_architectures.first() {
        println!("   - Best RL architecture: {best}");
        if let Some(entanglement) = best.properties.entanglement_capability {
            println!("   - Entanglement capability: {entanglement:.3}");
        }
    }

    Ok(())
}

/// Bayesian optimization search demonstration
fn bayesian_search_demo() -> Result<()> {
    let search_space = create_default_search_space();

    let strategy = SearchStrategy::BayesianOptimization {
        acquisition_function: AcquisitionFunction::ExpectedImprovement,
        num_initial_points: 10,
    };

    let mut nas = QuantumNAS::new(strategy, search_space);

    println!("   Created Bayesian optimization NAS:");
    println!("   - Acquisition function: Expected Improvement");
    println!("   - Initial random points: 10");

    // Set up evaluation data
    let eval_data = generate_quantum_data(60, 4);
    let eval_labels = Array1::from_shape_fn(60, |i| i % 3);
    nas.set_evaluation_data(eval_data, eval_labels);

    println!("\n   Running Bayesian optimization for 30 iterations...");
    let best_architectures = nas.search(30)?;

    println!("   Bayesian optimization complete!");
    if let Some(best) = best_architectures.first() {
        println!("   - Best Bayesian architecture: {best}");
        if let Some(hardware_eff) = best.metrics.hardware_efficiency {
            println!("   - Hardware efficiency: {hardware_eff:.3}");
        }
    }

    Ok(())
}

/// DARTS demonstration
fn darts_demo() -> Result<()> {
    let search_space = create_darts_search_space();

    let strategy = SearchStrategy::DARTS {
        learning_rate: 0.01,
        weight_decay: 1e-4,
    };

    let mut nas = QuantumNAS::new(strategy, search_space);

    println!("   Created DARTS NAS:");
    println!("   - Learning rate: 0.01");
    println!("   - Weight decay: 1e-4");
    println!("   - Differentiable architecture search");

    println!("\n   Running DARTS for 200 epochs...");
    let best_architectures = nas.search(200)?;

    println!("   DARTS search complete!");
    if let Some(best) = best_architectures.first() {
        println!("   - DARTS architecture: {best}");
        println!("   - Learned through gradient-based optimization");

        if let Some(gradient_var) = best.properties.gradient_variance {
            println!("   - Gradient variance: {gradient_var:.3}");
        }
    }

    Ok(())
}

/// Multi-objective optimization demonstration
fn multi_objective_demo() -> Result<()> {
    let search_space = create_default_search_space();

    let strategy = SearchStrategy::Evolutionary {
        population_size: 30,
        mutation_rate: 0.15,
        crossover_rate: 0.8,
        elitism_ratio: 0.2,
    };

    let mut nas = QuantumNAS::new(strategy, search_space);

    println!("   Multi-objective optimization:");
    println!("   - Optimizing accuracy vs. complexity");
    println!("   - Finding Pareto-optimal architectures");

    // Run search
    nas.search(15)?;

    // Analyze Pareto front
    let pareto_front = nas.get_pareto_front();
    println!("   Pareto front analysis:");
    println!("   - Pareto-optimal architectures: {}", pareto_front.len());

    for (i, arch) in pareto_front.iter().take(3).enumerate() {
        println!(
            "   Architecture {}: {} params, {:.3} accuracy",
            i + 1,
            arch.metrics.parameter_count,
            arch.metrics.accuracy.unwrap_or(0.0)
        );
    }

    Ok(())
}

/// Architecture analysis demonstration
fn architecture_analysis_demo() -> Result<()> {
    println!("   Analyzing quantum circuit architectures...");

    // Create sample architectures with different properties
    let architectures = create_sample_architectures();

    println!("\n   Architecture comparison:");
    for (i, arch) in architectures.iter().enumerate() {
        println!("   Architecture {}:", i + 1);
        println!("     - Layers: {}", arch.layers.len());
        println!("     - Qubits: {}", arch.num_qubits);
        println!("     - Circuit depth: {}", arch.metrics.circuit_depth);

        if let Some(expressivity) = arch.properties.expressivity {
            println!("     - Expressivity: {expressivity:.3}");
        }

        if let Some(entanglement) = arch.properties.entanglement_capability {
            println!("     - Entanglement: {entanglement:.3}");
        }

        if let Some(barren_plateau) = arch.properties.barren_plateau_score {
            println!("     - Barren plateau risk: {barren_plateau:.3}");
        }

        println!();
    }

    // Performance trade-offs analysis
    println!("   Performance trade-offs:");
    println!("   - Deeper circuits: higher expressivity, more barren plateaus");
    println!("   - More entanglement: better feature mixing, higher noise sensitivity");
    println!("   - More parameters: greater capacity, overfitting risk");

    Ok(())
}

/// Generate quantum-inspired synthetic data
fn generate_quantum_data(samples: usize, features: usize) -> Array2<f64> {
    Array2::from_shape_fn((samples, features), |(i, j)| {
        let phase = (i as f64).mul_add(0.1, j as f64 * 0.2).sin();
        let amplitude = (i as f64 / samples as f64).exp() * 0.5;
        amplitude.mul_add(phase, 0.1 * fastrand::f64())
    })
}

/// Create custom search space for RL demo
fn create_custom_search_space() -> SearchSpace {
    SearchSpace {
        layer_types: vec![
            QNNLayerType::VariationalLayer { num_params: 4 },
            QNNLayerType::VariationalLayer { num_params: 8 },
            QNNLayerType::EntanglementLayer {
                connectivity: "circular".to_string(),
            },
            QNNLayerType::EntanglementLayer {
                connectivity: "linear".to_string(),
            },
        ],
        depth_range: (1, 5),
        qubit_constraints: QubitConstraints {
            min_qubits: 3,
            max_qubits: 6,
            topology: Some(QuantumTopology::Ring),
        },
        param_ranges: vec![("variational_params".to_string(), (3, 12))]
            .into_iter()
            .collect(),
        connectivity_patterns: vec!["linear".to_string(), "circular".to_string()],
        measurement_bases: vec!["computational".to_string(), "Pauli-Z".to_string()],
    }
}

/// Create search space optimized for DARTS
fn create_darts_search_space() -> SearchSpace {
    SearchSpace {
        layer_types: vec![
            QNNLayerType::VariationalLayer { num_params: 6 },
            QNNLayerType::VariationalLayer { num_params: 9 },
            QNNLayerType::EntanglementLayer {
                connectivity: "full".to_string(),
            },
        ],
        depth_range: (3, 6),
        qubit_constraints: QubitConstraints {
            min_qubits: 4,
            max_qubits: 4, // Fixed for DARTS
            topology: Some(QuantumTopology::Complete),
        },
        param_ranges: vec![("variational_params".to_string(), (6, 9))]
            .into_iter()
            .collect(),
        connectivity_patterns: vec!["full".to_string()],
        measurement_bases: vec!["computational".to_string()],
    }
}

/// Create sample architectures for analysis
fn create_sample_architectures() -> Vec<ArchitectureCandidate> {
    vec![
        // Simple architecture
        ArchitectureCandidate {
            id: "simple".to_string(),
            layers: vec![
                QNNLayerType::EncodingLayer { num_features: 4 },
                QNNLayerType::VariationalLayer { num_params: 6 },
                QNNLayerType::MeasurementLayer {
                    measurement_basis: "computational".to_string(),
                },
            ],
            num_qubits: 3,
            metrics: ArchitectureMetrics {
                accuracy: Some(0.65),
                loss: Some(0.4),
                circuit_depth: 3,
                parameter_count: 6,
                training_time: Some(10.0),
                memory_usage: Some(512),
                hardware_efficiency: Some(0.8),
            },
            properties: ArchitectureProperties {
                expressivity: Some(0.3),
                entanglement_capability: Some(0.2),
                gradient_variance: Some(0.1),
                barren_plateau_score: Some(0.2),
                noise_resilience: Some(0.7),
            },
        },
        // Complex architecture
        ArchitectureCandidate {
            id: "complex".to_string(),
            layers: vec![
                QNNLayerType::EncodingLayer { num_features: 6 },
                QNNLayerType::VariationalLayer { num_params: 12 },
                QNNLayerType::EntanglementLayer {
                    connectivity: "full".to_string(),
                },
                QNNLayerType::VariationalLayer { num_params: 12 },
                QNNLayerType::EntanglementLayer {
                    connectivity: "circular".to_string(),
                },
                QNNLayerType::MeasurementLayer {
                    measurement_basis: "Pauli-Z".to_string(),
                },
            ],
            num_qubits: 6,
            metrics: ArchitectureMetrics {
                accuracy: Some(0.85),
                loss: Some(0.2),
                circuit_depth: 8,
                parameter_count: 24,
                training_time: Some(45.0),
                memory_usage: Some(2048),
                hardware_efficiency: Some(0.4),
            },
            properties: ArchitectureProperties {
                expressivity: Some(0.8),
                entanglement_capability: Some(0.9),
                gradient_variance: Some(0.3),
                barren_plateau_score: Some(0.7),
                noise_resilience: Some(0.3),
            },
        },
        // Balanced architecture
        ArchitectureCandidate {
            id: "balanced".to_string(),
            layers: vec![
                QNNLayerType::EncodingLayer { num_features: 4 },
                QNNLayerType::VariationalLayer { num_params: 8 },
                QNNLayerType::EntanglementLayer {
                    connectivity: "circular".to_string(),
                },
                QNNLayerType::VariationalLayer { num_params: 8 },
                QNNLayerType::MeasurementLayer {
                    measurement_basis: "computational".to_string(),
                },
            ],
            num_qubits: 4,
            metrics: ArchitectureMetrics {
                accuracy: Some(0.78),
                loss: Some(0.28),
                circuit_depth: 5,
                parameter_count: 16,
                training_time: Some(25.0),
                memory_usage: Some(1024),
                hardware_efficiency: Some(0.65),
            },
            properties: ArchitectureProperties {
                expressivity: Some(0.6),
                entanglement_capability: Some(0.5),
                gradient_variance: Some(0.15),
                barren_plateau_score: Some(0.4),
                noise_resilience: Some(0.6),
            },
        },
    ]
}