pub struct QuantumTransformer { /* private fields */ }Expand description
Main quantum transformer model
Implementations§
Source§impl QuantumTransformer
impl QuantumTransformer
Sourcepub fn new(config: QuantumTransformerConfig) -> Result<Self>
pub fn new(config: QuantumTransformerConfig) -> Result<Self>
Create new quantum transformer model
Examples found in repository?
examples/quantum_transformer.rs (line 97)
54fn config_demo() -> Result<()> {
55 println!(" Creating various transformer configurations...");
56
57 // Small efficient model
58 let small_config = QuantumTransformerConfig::small();
59 println!(
60 " Small model: {} params, {} heads, {} layers",
61 small_config.model_dim, small_config.num_heads, small_config.num_layers
62 );
63
64 // Standard model
65 let default_config = QuantumTransformerConfig::default();
66 println!(
67 " Default model: {} params, {} heads, {} layers",
68 default_config.model_dim, default_config.num_heads, default_config.num_layers
69 );
70
71 // Large model
72 let large_config = QuantumTransformerConfig::large();
73 println!(
74 " Large model: {} params, {} heads, {} layers",
75 large_config.model_dim, large_config.num_heads, large_config.num_layers
76 );
77
78 // Custom configuration
79 let custom_config = QuantumTransformerConfig {
80 model_dim: 384,
81 num_heads: 6,
82 ff_dim: 1536,
83 num_layers: 8,
84 max_seq_len: 1024,
85 num_qubits: 12,
86 dropout_rate: 0.15,
87 attention_type: QuantumAttentionType::QuantumEnhancedMultiHead,
88 position_encoding: PositionEncodingType::Rotary,
89 };
90
91 println!(
92 " Custom model: {} dim, {} qubits, {:?} attention",
93 custom_config.model_dim, custom_config.num_qubits, custom_config.attention_type
94 );
95
96 // Create transformer with custom config
97 let transformer = QuantumTransformer::new(custom_config)?;
98 println!(
99 " Created transformer with {} total parameters",
100 transformer.num_parameters()
101 );
102
103 Ok(())
104}
105
106/// Demonstrate different quantum attention mechanisms
107fn attention_mechanisms_demo() -> Result<()> {
108 println!(" Testing various quantum attention mechanisms...");
109
110 let attention_types = vec![
111 ("Full Quantum", QuantumAttentionType::FullQuantum),
112 (
113 "Hybrid Quantum-Classical",
114 QuantumAttentionType::HybridQuantumClassical,
115 ),
116 (
117 "Variational Quantum",
118 QuantumAttentionType::VariationalQuantum,
119 ),
120 (
121 "Quantum Enhanced Multi-Head",
122 QuantumAttentionType::QuantumEnhancedMultiHead,
123 ),
124 (
125 "Quantum Self-Attention",
126 QuantumAttentionType::QuantumSelfAttention,
127 ),
128 ];
129
130 for (name, attention_type) in attention_types {
131 println!("\n --- {name} Attention ---");
132
133 let attention = QuantumMultiHeadAttention::new(4, 256, attention_type, 8)?;
134 println!(
135 " Created attention module: {} heads, {} model dim",
136 4, 256
137 ); // Fixed values since fields are private
138
139 // Test forward pass
140 let batch_size = 2;
141 let seq_len = 10;
142 let model_dim = 256;
143
144 let query = Array3::from_shape_fn((batch_size, seq_len, model_dim), |(b, s, d)| {
145 0.1 * (d as f64).mul_add(0.01, (s as f64).mul_add(0.1, b as f64))
146 });
147 let key = query.clone();
148 let value = query.clone();
149
150 let attention_output = attention.forward(&query, &key, &value, None)?;
151
152 println!(
153 " Attention output shape: {:?}",
154 attention_output.output.dim()
155 );
156 println!(
157 " Attention weights shape: {:?}",
158 attention_output.attention_weights.dim()
159 );
160
161 // Analyze quantum attention properties
162 let quantum_info = &attention_output.quantum_info;
163 let avg_entanglement = quantum_info.entanglement_matrix.mean().unwrap_or(0.0);
164 let max_coherence = quantum_info
165 .coherence_scores
166 .iter()
167 .copied()
168 .fold(f64::NEG_INFINITY, f64::max);
169
170 println!(" Average entanglement: {avg_entanglement:.4}");
171 println!(" Maximum coherence: {max_coherence:.4}");
172
173 // Attention pattern analysis
174 let attention_weights = &attention_output.attention_weights;
175 let max_attention = attention_weights
176 .iter()
177 .copied()
178 .fold(f64::NEG_INFINITY, f64::max);
179 let avg_attention = attention_weights.mean().unwrap_or(0.0);
180
181 println!(" Max attention weight: {max_attention:.4}");
182 println!(" Average attention: {avg_attention:.4}");
183
184 // Check attention sparsity
185 let sparsity = attention_weights.iter().filter(|&&x| x < 0.01).count() as f64
186 / attention_weights.len() as f64;
187 println!(" Attention sparsity: {:.1}%", sparsity * 100.0);
188 }
189
190 Ok(())
191}
192
193/// Demonstrate different position encoding types
194fn position_encoding_demo() -> Result<()> {
195 println!(" Testing quantum position encoding variants...");
196
197 let encoding_types = vec![
198 ("Sinusoidal", PositionEncodingType::Sinusoidal),
199 ("Quantum Phase", PositionEncodingType::QuantumPhase),
200 ("Learnable Quantum", PositionEncodingType::LearnableQuantum),
201 ("Relative", PositionEncodingType::Relative),
202 ("Rotary (RoPE)", PositionEncodingType::Rotary),
203 ];
204
205 let model_dim = 128;
206 let max_seq_len = 64;
207 let num_qubits = 8;
208
209 for (name, encoding_type) in encoding_types {
210 println!("\n --- {name} Position Encoding ---");
211
212 let pos_enc =
213 QuantumPositionEncoding::new(encoding_type, model_dim, max_seq_len, num_qubits)?;
214
215 let batch_size = 3;
216 let seq_len = 32;
217
218 let encodings = pos_enc.forward(seq_len, batch_size)?;
219 println!(" Encoding shape: {:?}", encodings.dim());
220
221 // Analyze position encoding properties
222 let encoding_range = {
223 let min_val = encodings.iter().copied().fold(f64::INFINITY, f64::min);
224 let max_val = encodings.iter().copied().fold(f64::NEG_INFINITY, f64::max);
225 max_val - min_val
226 };
227
228 println!(" Value range: {encoding_range:.4}");
229
230 // Check position distinguishability
231 let pos1 = encodings
232 .slice(scirs2_core::ndarray::s![0, 0, ..])
233 .to_owned();
234 let pos2 = encodings
235 .slice(scirs2_core::ndarray::s![0, seq_len - 1, ..])
236 .to_owned();
237 let position_distance = (&pos1 - &pos2).mapv(|x| x * x).sum().sqrt();
238
239 println!(" Distance between first and last position: {position_distance:.4}");
240
241 // Analyze periodicity for sinusoidal encodings
242 if name == "Sinusoidal" {
243 let mut periodicities = Vec::new();
244 for d in (0..model_dim).step_by(10) {
245 let values: Vec<f64> = (0..seq_len).map(|s| encodings[[0, s, d]]).collect();
246
247 // Simple periodicity check
248 let period = find_period(&values);
249 if period > 0 {
250 periodicities.push(period);
251 }
252 }
253
254 if !periodicities.is_empty() {
255 let avg_period =
256 periodicities.iter().sum::<usize>() as f64 / periodicities.len() as f64;
257 println!(" Average period length: {avg_period:.1}");
258 }
259 }
260
261 // Check quantum phase encoding properties
262 if name == "Quantum Phase" {
263 let phase_variance = encodings.var(0.0);
264 println!(" Phase encoding variance: {phase_variance:.4}");
265 }
266 }
267
268 Ok(())
269}
270
271/// Demonstrate complete transformer forward pass
272fn transformer_forward_demo() -> Result<()> {
273 println!(" Testing complete quantum transformer forward pass...");
274
275 let config = QuantumTransformerConfig {
276 model_dim: 256,
277 num_heads: 8,
278 ff_dim: 1024,
279 num_layers: 4,
280 max_seq_len: 128,
281 num_qubits: 10,
282 dropout_rate: 0.1,
283 attention_type: QuantumAttentionType::HybridQuantumClassical,
284 position_encoding: PositionEncodingType::QuantumPhase,
285 };
286
287 let transformer = QuantumTransformer::new(config.clone())?;
288 println!(
289 " Created transformer: {} layers, {} parameters",
290 config.num_layers,
291 transformer.num_parameters()
292 );
293
294 // Test with different sequence lengths
295 let test_sequences = vec![
296 (2, 16, 128), // small batch, short sequence
297 (4, 32, 128), // medium batch, medium sequence
298 (1, 64, 128), // single sample, long sequence
299 ];
300
301 for (batch_size, seq_len, input_dim) in test_sequences {
302 println!("\n Testing: batch={batch_size}, seq_len={seq_len}, input_dim={input_dim}");
303
304 // Create test input
305 let input = Array3::from_shape_fn((batch_size, seq_len, input_dim), |(b, s, d)| {
306 let base = 0.1 * (b as f64 + 1.0);
307 let seq_component = 0.05 * (s as f64 * 0.1).sin();
308 let dim_component = 0.02 * (d as f64 * 0.01).cos();
309 base + seq_component + dim_component
310 });
311
312 // Create causal mask for autoregressive modeling
313 let causal_mask = create_causal_mask(batch_size, seq_len);
314
315 // Forward pass
316 let start_time = std::time::Instant::now();
317 let output = transformer.forward(&input, Some(&causal_mask))?;
318 let forward_time = start_time.elapsed();
319
320 println!(" Output shape: {:?}", output.dim());
321 println!(" Forward pass time: {forward_time:.2?}");
322
323 // Analyze output properties
324 let output_mean = output.mean().unwrap_or(0.0);
325 let output_std = output.var(0.0).sqrt();
326 let output_range = {
327 let min_val = output.iter().copied().fold(f64::INFINITY, f64::min);
328 let max_val = output.iter().copied().fold(f64::NEG_INFINITY, f64::max);
329 max_val - min_val
330 };
331
332 println!(
333 " Output statistics: mean={output_mean:.4}, std={output_std:.4}, range={output_range:.4}"
334 );
335
336 // Check causality (if using causal mask)
337 let causality_check = check_causality(&input, &output, &causal_mask);
338 if causality_check {
339 println!(" ✓ Causal dependencies respected");
340 } else {
341 println!(" ⚠ Potential causality violations detected");
342 }
343
344 // Memory efficiency analysis
345 let memory_per_token = (transformer.num_parameters() * 8 + output.len() * 8) as f64
346 / (batch_size * seq_len) as f64;
347 println!(" Memory per token: {memory_per_token:.1} bytes");
348 }
349
350 Ok(())
351}
352
353/// Demonstrate quantum language modeling
354fn language_modeling_demo() -> Result<()> {
355 println!(" Quantum language modeling with transformer...");
356
357 let config = QuantumTransformerConfig {
358 model_dim: 384,
359 num_heads: 6,
360 ff_dim: 1536,
361 num_layers: 6,
362 max_seq_len: 256,
363 num_qubits: 12,
364 dropout_rate: 0.1,
365 attention_type: QuantumAttentionType::QuantumSelfAttention,
366 position_encoding: PositionEncodingType::Rotary,
367 };
368
369 let transformer = QuantumTransformer::new(config.clone())?;
370
371 // Simulate language modeling task
372 let vocab_size = 1000;
373 let batch_size = 4;
374 let seq_len = 64;
375
376 // Create tokenized sequences (simulated)
377 let input_tokens =
378 Array3::from_shape_fn((batch_size, seq_len, config.model_dim), |(b, s, d)| {
379 // Simulate token embeddings
380 let token_id = (b * seq_len + s) % vocab_size;
381 let embedding_val = (token_id as f64 / vocab_size as f64).mul_add(2.0, -1.0);
382 embedding_val * 0.1f64.mul_add(d as f64 / config.model_dim as f64, 1.0)
383 });
384
385 println!(" Processing {batch_size} sequences of length {seq_len}");
386
387 // Create causal mask for language modeling
388 let causal_mask = create_causal_mask(batch_size, seq_len);
389
390 // Forward pass
391 let logits = transformer.forward(&input_tokens, Some(&causal_mask))?;
392
393 // Simulate next token prediction
394 let mut perplexities = Vec::new();
395
396 for batch_idx in 0..batch_size {
397 let mut log_likelihood = 0.0;
398 let mut valid_predictions = 0;
399
400 for pos in 0..seq_len - 1 {
401 let current_logits = logits.slice(scirs2_core::ndarray::s![batch_idx, pos, ..]);
402
403 // Convert to probabilities (simplified softmax)
404 let max_logit = current_logits
405 .iter()
406 .copied()
407 .fold(f64::NEG_INFINITY, f64::max);
408 let exp_logits: Array1<f64> = current_logits.mapv(|x| (x - max_logit).exp());
409 let sum_exp = exp_logits.sum();
410 let probs = exp_logits / sum_exp;
411
412 // Simulate target token (next position embedding)
413 let target_embedding =
414 input_tokens.slice(scirs2_core::ndarray::s![batch_idx, pos + 1, ..]);
415 let target_prob = compute_token_probability(&probs, &target_embedding.to_owned())?;
416
417 if target_prob > 1e-10 {
418 log_likelihood += target_prob.ln();
419 valid_predictions += 1;
420 }
421 }
422
423 if valid_predictions > 0 {
424 let avg_log_likelihood = log_likelihood / f64::from(valid_predictions);
425 let perplexity = (-avg_log_likelihood).exp();
426 perplexities.push(perplexity);
427 }
428 }
429
430 if !perplexities.is_empty() {
431 let avg_perplexity = perplexities.iter().sum::<f64>() / perplexities.len() as f64;
432 println!(" Average perplexity: {avg_perplexity:.2}");
433
434 // Analyze quantum language model properties
435 println!(" Quantum language model analysis:");
436
437 // Attention pattern analysis
438 println!(" - Uses quantum self-attention for context modeling");
439 println!(" - Rotary position encoding preserves relative positions");
440 println!(
441 " - {} layers provide hierarchical representation",
442 config.num_layers
443 );
444
445 // Information flow analysis
446 let first_layer_norm = logits
447 .slice(scirs2_core::ndarray::s![0, .., ..])
448 .var(0.0)
449 .sqrt();
450 println!(" - Output layer standard deviation: {first_layer_norm:.4}");
451
452 // Quantum coherence in language representation
453 let quantum_coherence = analyze_quantum_language_coherence(&logits)?;
454 println!(" - Quantum coherence in representations: {quantum_coherence:.4}");
455 }
456
457 Ok(())
458}
459
460/// Demonstrate sequence-to-sequence tasks
461fn seq2seq_demo() -> Result<()> {
462 println!(" Quantum sequence-to-sequence modeling...");
463
464 // Encoder configuration
465 let encoder_config = QuantumTransformerConfig {
466 model_dim: 256,
467 num_heads: 8,
468 ff_dim: 1024,
469 num_layers: 4,
470 max_seq_len: 128,
471 num_qubits: 10,
472 dropout_rate: 0.1,
473 attention_type: QuantumAttentionType::HybridQuantumClassical,
474 position_encoding: PositionEncodingType::Sinusoidal,
475 };
476
477 // Decoder configuration (with causal attention)
478 let decoder_config = QuantumTransformerConfig {
479 model_dim: 256,
480 num_heads: 8,
481 ff_dim: 1024,
482 num_layers: 4,
483 max_seq_len: 128,
484 num_qubits: 10,
485 dropout_rate: 0.1,
486 attention_type: QuantumAttentionType::QuantumEnhancedMultiHead,
487 position_encoding: PositionEncodingType::QuantumPhase,
488 };
489
490 let encoder = QuantumTransformer::new(encoder_config)?;
491 let decoder = QuantumTransformer::new(decoder_config)?;
492
493 println!(" Created encoder-decoder architecture");
494 println!(" Encoder: {} parameters", encoder.num_parameters());
495 println!(" Decoder: {} parameters", decoder.num_parameters());
496
497 // Simulate translation task
498 let batch_size = 3;
499 let src_len = 32;
500 let tgt_len = 28;
501 let model_dim = 256;
502
503 // Source sequence (e.g., English)
504 let source = Array3::from_shape_fn((batch_size, src_len, model_dim), |(b, s, d)| {
505 let src_pattern = 0.3 * ((s as f64).mul_add(0.2, b as f64).sin());
506 0.1f64.mul_add(d as f64 / model_dim as f64, src_pattern)
507 });
508
509 // Target sequence (e.g., French)
510 let target = Array3::from_shape_fn((batch_size, tgt_len, model_dim), |(b, s, d)| {
511 let tgt_pattern = 0.4 * ((s as f64).mul_add(0.15, b as f64 * 0.3).cos());
512 0.12f64.mul_add(d as f64 / model_dim as f64, tgt_pattern)
513 });
514
515 println!("\n Processing translation: {src_len} -> {tgt_len} tokens");
516
517 // Encode source sequence
518 let encoder_output = encoder.forward(&source, None)?;
519 println!(" Encoder output shape: {:?}", encoder_output.dim());
520
521 // Decode with causal mask
522 let causal_mask = create_causal_mask(batch_size, tgt_len);
523 let decoder_output = decoder.forward(&target, Some(&causal_mask))?;
524 println!(" Decoder output shape: {:?}", decoder_output.dim());
525
526 // Cross-attention simulation (simplified)
527 println!("\n Cross-attention analysis:");
528 let cross_attention_scores = compute_cross_attention(&encoder_output, &decoder_output)?;
529 println!(
530 " Cross-attention shape: {:?}",
531 cross_attention_scores.dim()
532 );
533
534 // Analyze attention alignment
535 let max_alignment = cross_attention_scores
536 .iter()
537 .copied()
538 .fold(f64::NEG_INFINITY, f64::max);
539 let avg_alignment = cross_attention_scores.mean().unwrap_or(0.0);
540
541 println!(" Max alignment score: {max_alignment:.4}");
542 println!(" Average alignment: {avg_alignment:.4}");
543
544 // Translation quality metrics (simplified)
545 let translation_score = evaluate_translation_quality(&source, &target, &decoder_output)?;
546 println!(" Translation quality score: {translation_score:.4}");
547
548 // Quantum entanglement in cross-lingual representations
549 let cross_lingual_entanglement =
550 analyze_cross_lingual_entanglement(&encoder_output, &decoder_output)?;
551 println!(" Cross-lingual quantum entanglement: {cross_lingual_entanglement:.4}");
552
553 Ok(())
554}
555
556/// Demonstrate quantum data processing
557fn quantum_data_demo() -> Result<()> {
558 println!(" Processing quantum measurement data with transformers...");
559
560 let config = QuantumTransformerConfig {
561 model_dim: 128,
562 num_heads: 4,
563 ff_dim: 512,
564 num_layers: 3,
565 max_seq_len: 64,
566 num_qubits: 8,
567 dropout_rate: 0.05,
568 attention_type: QuantumAttentionType::FullQuantum,
569 position_encoding: PositionEncodingType::QuantumPhase,
570 };
571
572 let transformer = QuantumTransformer::new(config)?;
573
574 // Simulate quantum measurement sequences
575 let batch_size = 5;
576 let seq_len = 32;
577 let model_dim = 128;
578
579 println!(" Generating quantum measurement sequences...");
580
581 // Create quantum state evolution data
582 let quantum_data = Array3::from_shape_fn((batch_size, seq_len, model_dim), |(b, t, d)| {
583 // Simulate quantum state evolution with decoherence
584 let decoherence_factor = (-0.1 * t as f64).exp();
585 let quantum_amplitude =
586 decoherence_factor * (2.0 * std::f64::consts::PI * t as f64 / 8.0 + b as f64).sin();
587
588 // Add measurement noise
589 let noise = 0.05 * (fastrand::f64() - 0.5);
590
591 // Encode as amplitude and phase information
592 if d % 2 == 0 {
593 quantum_amplitude + noise
594 } else {
595 (d as f64)
596 .mul_add(0.1, 2.0 * std::f64::consts::PI * t as f64 / 10.0)
597 .cos()
598 + noise
599 }
600 });
601
602 println!(" Processing {batch_size} quantum sequences of {seq_len} measurements each");
603
604 // Process quantum data
605 let output = transformer.forward(&quantum_data, None)?;
606
607 // Analyze quantum data processing
608 println!("\n Quantum data analysis:");
609
610 // Coherence preservation
611 let input_coherence = compute_coherence_measure(&quantum_data)?;
612 let output_coherence = compute_coherence_measure(&output)?;
613 let coherence_preservation = output_coherence / input_coherence;
614
615 println!(" Input coherence: {input_coherence:.4}");
616 println!(" Output coherence: {output_coherence:.4}");
617 println!(
618 " Coherence preservation: {:.1}%",
619 coherence_preservation * 100.0
620 );
621
622 // Quantum information extraction
623 let quantum_features = extract_quantum_features(&output)?;
624 println!(" Extracted quantum features:");
625 println!(
626 " - Entanglement signature: {:.4}",
627 quantum_features.entanglement
628 );
629 println!(
630 " - Phase coherence: {:.4}",
631 quantum_features.phase_coherence
632 );
633 println!(
634 " - Amplitude stability: {:.4}",
635 quantum_features.amplitude_stability
636 );
637
638 // Decoherence detection
639 let decoherence_pattern = detect_decoherence_pattern(&output)?;
640 println!(" Decoherence detection:");
641 println!(" - Pattern strength: {:.4}", decoherence_pattern.strength);
642 println!(
643 " - Time constant: {:.2} steps",
644 decoherence_pattern.time_constant
645 );
646
647 // Quantum state classification
648 let state_classifications = classify_quantum_states(&output)?;
649 println!(" Quantum state classification:");
650 for (i, classification) in state_classifications.iter().enumerate() {
651 println!(
652 " - Sequence {}: {:.1}% entangled, {:.1}% coherent",
653 i,
654 classification.entangled_prob * 100.0,
655 classification.coherent_prob * 100.0
656 );
657 }
658
659 Ok(())
660}
661
662/// Demonstrate multi-scale quantum transformers
663fn multiscale_demo() -> Result<()> {
664 println!(" Multi-scale quantum transformer architecture...");
665
666 // Create transformers at different scales
667 let scales = vec![
668 (
669 "Fine-scale",
670 QuantumTransformerConfig {
671 model_dim: 128,
672 num_heads: 4,
673 ff_dim: 512,
674 num_layers: 2,
675 max_seq_len: 64,
676 num_qubits: 6,
677 dropout_rate: 0.1,
678 attention_type: QuantumAttentionType::VariationalQuantum,
679 position_encoding: PositionEncodingType::Sinusoidal,
680 },
681 ),
682 (
683 "Medium-scale",
684 QuantumTransformerConfig {
685 model_dim: 256,
686 num_heads: 8,
687 ff_dim: 1024,
688 num_layers: 4,
689 max_seq_len: 128,
690 num_qubits: 10,
691 dropout_rate: 0.1,
692 attention_type: QuantumAttentionType::HybridQuantumClassical,
693 position_encoding: PositionEncodingType::QuantumPhase,
694 },
695 ),
696 (
697 "Coarse-scale",
698 QuantumTransformerConfig {
699 model_dim: 512,
700 num_heads: 16,
701 ff_dim: 2048,
702 num_layers: 6,
703 max_seq_len: 256,
704 num_qubits: 16,
705 dropout_rate: 0.1,
706 attention_type: QuantumAttentionType::FullQuantum,
707 position_encoding: PositionEncodingType::Rotary,
708 },
709 ),
710 ];
711
712 let mut transformers = Vec::new();
713
714 for (scale_name, config) in scales {
715 let transformer = QuantumTransformer::new(config)?;
716 let num_params = transformer.num_parameters();
717
718 println!(" {scale_name} transformer: {num_params} parameters");
719 transformers.push((scale_name, transformer));
720 }
721
722 // Test hierarchical processing
723 println!("\n Hierarchical processing demonstration:");
724
725 let batch_size = 2;
726 let base_seq_len = 64;
727 let input_dim = 128;
728
729 // Create input data
730 let input_data = Array3::from_shape_fn((batch_size, base_seq_len, input_dim), |(b, s, d)| {
731 // Multi-scale signal with different frequency components
732 let fine_component = 0.3 * (s as f64 * 0.5).sin();
733 let medium_component = 0.2 * (s as f64 * 0.1).sin();
734 let coarse_component = 0.1 * (s as f64 * 0.02).sin();
735
736 let base_signal = fine_component + medium_component + coarse_component;
737 0.05f64.mul_add((d as f64).mul_add(0.01, b as f64), base_signal)
738 });
739
740 // Process at each scale
741 let mut scale_outputs = Vec::new();
742
743 for (scale_name, transformer) in &transformers {
744 // Adapt input to transformer's expected dimensions
745 let adapted_input = adapt_input_for_scale(&input_data, transformer.config())?;
746
747 println!(" Processing at {scale_name} scale...");
748 println!(" Adapted input shape: {:?}", adapted_input.dim());
749
750 let output = transformer.forward(&adapted_input, None)?;
751
752 // Analyze scale-specific patterns
753 let pattern_analysis = analyze_scale_patterns(&output)?;
754
755 scale_outputs.push((*scale_name, output));
756 println!(" Pattern analysis:");
757 println!(
758 " - Local patterns: {:.4}",
759 pattern_analysis.local_strength
760 );
761 println!(
762 " - Global patterns: {:.4}",
763 pattern_analysis.global_strength
764 );
765 println!(
766 " - Cross-scale coherence: {:.4}",
767 pattern_analysis.coherence
768 );
769 }
770
771 // Multi-scale fusion
772 println!("\n Multi-scale fusion analysis:");
773 let scale_refs: Vec<(&str, Array3<f64>)> = scale_outputs
774 .iter()
775 .map(|(name, output)| (*name, output.clone()))
776 .collect();
777 let fusion_result = fuse_multiscale_outputs(&scale_refs)?;
778 println!(
779 " Fused representation dimensions: {} features",
780 fusion_result.len()
781 );
782
783 let fusion_quality = evaluate_fusion_quality(&fusion_result)?;
784 println!(" Fusion quality metrics:");
785 println!(
786 " - Information preservation: {:.1}%",
787 fusion_quality.info_preservation * 100.0
788 );
789 println!(
790 " - Scale consistency: {:.1}%",
791 fusion_quality.scale_consistency * 100.0
792 );
793 println!(
794 " - Quantum coherence: {:.4}",
795 fusion_quality.quantum_coherence
796 );
797
798 Ok(())
799}Sourcepub fn forward(
&self,
input: &Array3<f64>,
attention_mask: Option<&Array3<bool>>,
) -> Result<Array3<f64>>
pub fn forward( &self, input: &Array3<f64>, attention_mask: Option<&Array3<bool>>, ) -> Result<Array3<f64>>
Forward pass through quantum transformer
Examples found in repository?
examples/quantum_transformer.rs (line 317)
272fn transformer_forward_demo() -> Result<()> {
273 println!(" Testing complete quantum transformer forward pass...");
274
275 let config = QuantumTransformerConfig {
276 model_dim: 256,
277 num_heads: 8,
278 ff_dim: 1024,
279 num_layers: 4,
280 max_seq_len: 128,
281 num_qubits: 10,
282 dropout_rate: 0.1,
283 attention_type: QuantumAttentionType::HybridQuantumClassical,
284 position_encoding: PositionEncodingType::QuantumPhase,
285 };
286
287 let transformer = QuantumTransformer::new(config.clone())?;
288 println!(
289 " Created transformer: {} layers, {} parameters",
290 config.num_layers,
291 transformer.num_parameters()
292 );
293
294 // Test with different sequence lengths
295 let test_sequences = vec![
296 (2, 16, 128), // small batch, short sequence
297 (4, 32, 128), // medium batch, medium sequence
298 (1, 64, 128), // single sample, long sequence
299 ];
300
301 for (batch_size, seq_len, input_dim) in test_sequences {
302 println!("\n Testing: batch={batch_size}, seq_len={seq_len}, input_dim={input_dim}");
303
304 // Create test input
305 let input = Array3::from_shape_fn((batch_size, seq_len, input_dim), |(b, s, d)| {
306 let base = 0.1 * (b as f64 + 1.0);
307 let seq_component = 0.05 * (s as f64 * 0.1).sin();
308 let dim_component = 0.02 * (d as f64 * 0.01).cos();
309 base + seq_component + dim_component
310 });
311
312 // Create causal mask for autoregressive modeling
313 let causal_mask = create_causal_mask(batch_size, seq_len);
314
315 // Forward pass
316 let start_time = std::time::Instant::now();
317 let output = transformer.forward(&input, Some(&causal_mask))?;
318 let forward_time = start_time.elapsed();
319
320 println!(" Output shape: {:?}", output.dim());
321 println!(" Forward pass time: {forward_time:.2?}");
322
323 // Analyze output properties
324 let output_mean = output.mean().unwrap_or(0.0);
325 let output_std = output.var(0.0).sqrt();
326 let output_range = {
327 let min_val = output.iter().copied().fold(f64::INFINITY, f64::min);
328 let max_val = output.iter().copied().fold(f64::NEG_INFINITY, f64::max);
329 max_val - min_val
330 };
331
332 println!(
333 " Output statistics: mean={output_mean:.4}, std={output_std:.4}, range={output_range:.4}"
334 );
335
336 // Check causality (if using causal mask)
337 let causality_check = check_causality(&input, &output, &causal_mask);
338 if causality_check {
339 println!(" ✓ Causal dependencies respected");
340 } else {
341 println!(" ⚠ Potential causality violations detected");
342 }
343
344 // Memory efficiency analysis
345 let memory_per_token = (transformer.num_parameters() * 8 + output.len() * 8) as f64
346 / (batch_size * seq_len) as f64;
347 println!(" Memory per token: {memory_per_token:.1} bytes");
348 }
349
350 Ok(())
351}
352
353/// Demonstrate quantum language modeling
354fn language_modeling_demo() -> Result<()> {
355 println!(" Quantum language modeling with transformer...");
356
357 let config = QuantumTransformerConfig {
358 model_dim: 384,
359 num_heads: 6,
360 ff_dim: 1536,
361 num_layers: 6,
362 max_seq_len: 256,
363 num_qubits: 12,
364 dropout_rate: 0.1,
365 attention_type: QuantumAttentionType::QuantumSelfAttention,
366 position_encoding: PositionEncodingType::Rotary,
367 };
368
369 let transformer = QuantumTransformer::new(config.clone())?;
370
371 // Simulate language modeling task
372 let vocab_size = 1000;
373 let batch_size = 4;
374 let seq_len = 64;
375
376 // Create tokenized sequences (simulated)
377 let input_tokens =
378 Array3::from_shape_fn((batch_size, seq_len, config.model_dim), |(b, s, d)| {
379 // Simulate token embeddings
380 let token_id = (b * seq_len + s) % vocab_size;
381 let embedding_val = (token_id as f64 / vocab_size as f64).mul_add(2.0, -1.0);
382 embedding_val * 0.1f64.mul_add(d as f64 / config.model_dim as f64, 1.0)
383 });
384
385 println!(" Processing {batch_size} sequences of length {seq_len}");
386
387 // Create causal mask for language modeling
388 let causal_mask = create_causal_mask(batch_size, seq_len);
389
390 // Forward pass
391 let logits = transformer.forward(&input_tokens, Some(&causal_mask))?;
392
393 // Simulate next token prediction
394 let mut perplexities = Vec::new();
395
396 for batch_idx in 0..batch_size {
397 let mut log_likelihood = 0.0;
398 let mut valid_predictions = 0;
399
400 for pos in 0..seq_len - 1 {
401 let current_logits = logits.slice(scirs2_core::ndarray::s![batch_idx, pos, ..]);
402
403 // Convert to probabilities (simplified softmax)
404 let max_logit = current_logits
405 .iter()
406 .copied()
407 .fold(f64::NEG_INFINITY, f64::max);
408 let exp_logits: Array1<f64> = current_logits.mapv(|x| (x - max_logit).exp());
409 let sum_exp = exp_logits.sum();
410 let probs = exp_logits / sum_exp;
411
412 // Simulate target token (next position embedding)
413 let target_embedding =
414 input_tokens.slice(scirs2_core::ndarray::s![batch_idx, pos + 1, ..]);
415 let target_prob = compute_token_probability(&probs, &target_embedding.to_owned())?;
416
417 if target_prob > 1e-10 {
418 log_likelihood += target_prob.ln();
419 valid_predictions += 1;
420 }
421 }
422
423 if valid_predictions > 0 {
424 let avg_log_likelihood = log_likelihood / f64::from(valid_predictions);
425 let perplexity = (-avg_log_likelihood).exp();
426 perplexities.push(perplexity);
427 }
428 }
429
430 if !perplexities.is_empty() {
431 let avg_perplexity = perplexities.iter().sum::<f64>() / perplexities.len() as f64;
432 println!(" Average perplexity: {avg_perplexity:.2}");
433
434 // Analyze quantum language model properties
435 println!(" Quantum language model analysis:");
436
437 // Attention pattern analysis
438 println!(" - Uses quantum self-attention for context modeling");
439 println!(" - Rotary position encoding preserves relative positions");
440 println!(
441 " - {} layers provide hierarchical representation",
442 config.num_layers
443 );
444
445 // Information flow analysis
446 let first_layer_norm = logits
447 .slice(scirs2_core::ndarray::s![0, .., ..])
448 .var(0.0)
449 .sqrt();
450 println!(" - Output layer standard deviation: {first_layer_norm:.4}");
451
452 // Quantum coherence in language representation
453 let quantum_coherence = analyze_quantum_language_coherence(&logits)?;
454 println!(" - Quantum coherence in representations: {quantum_coherence:.4}");
455 }
456
457 Ok(())
458}
459
460/// Demonstrate sequence-to-sequence tasks
461fn seq2seq_demo() -> Result<()> {
462 println!(" Quantum sequence-to-sequence modeling...");
463
464 // Encoder configuration
465 let encoder_config = QuantumTransformerConfig {
466 model_dim: 256,
467 num_heads: 8,
468 ff_dim: 1024,
469 num_layers: 4,
470 max_seq_len: 128,
471 num_qubits: 10,
472 dropout_rate: 0.1,
473 attention_type: QuantumAttentionType::HybridQuantumClassical,
474 position_encoding: PositionEncodingType::Sinusoidal,
475 };
476
477 // Decoder configuration (with causal attention)
478 let decoder_config = QuantumTransformerConfig {
479 model_dim: 256,
480 num_heads: 8,
481 ff_dim: 1024,
482 num_layers: 4,
483 max_seq_len: 128,
484 num_qubits: 10,
485 dropout_rate: 0.1,
486 attention_type: QuantumAttentionType::QuantumEnhancedMultiHead,
487 position_encoding: PositionEncodingType::QuantumPhase,
488 };
489
490 let encoder = QuantumTransformer::new(encoder_config)?;
491 let decoder = QuantumTransformer::new(decoder_config)?;
492
493 println!(" Created encoder-decoder architecture");
494 println!(" Encoder: {} parameters", encoder.num_parameters());
495 println!(" Decoder: {} parameters", decoder.num_parameters());
496
497 // Simulate translation task
498 let batch_size = 3;
499 let src_len = 32;
500 let tgt_len = 28;
501 let model_dim = 256;
502
503 // Source sequence (e.g., English)
504 let source = Array3::from_shape_fn((batch_size, src_len, model_dim), |(b, s, d)| {
505 let src_pattern = 0.3 * ((s as f64).mul_add(0.2, b as f64).sin());
506 0.1f64.mul_add(d as f64 / model_dim as f64, src_pattern)
507 });
508
509 // Target sequence (e.g., French)
510 let target = Array3::from_shape_fn((batch_size, tgt_len, model_dim), |(b, s, d)| {
511 let tgt_pattern = 0.4 * ((s as f64).mul_add(0.15, b as f64 * 0.3).cos());
512 0.12f64.mul_add(d as f64 / model_dim as f64, tgt_pattern)
513 });
514
515 println!("\n Processing translation: {src_len} -> {tgt_len} tokens");
516
517 // Encode source sequence
518 let encoder_output = encoder.forward(&source, None)?;
519 println!(" Encoder output shape: {:?}", encoder_output.dim());
520
521 // Decode with causal mask
522 let causal_mask = create_causal_mask(batch_size, tgt_len);
523 let decoder_output = decoder.forward(&target, Some(&causal_mask))?;
524 println!(" Decoder output shape: {:?}", decoder_output.dim());
525
526 // Cross-attention simulation (simplified)
527 println!("\n Cross-attention analysis:");
528 let cross_attention_scores = compute_cross_attention(&encoder_output, &decoder_output)?;
529 println!(
530 " Cross-attention shape: {:?}",
531 cross_attention_scores.dim()
532 );
533
534 // Analyze attention alignment
535 let max_alignment = cross_attention_scores
536 .iter()
537 .copied()
538 .fold(f64::NEG_INFINITY, f64::max);
539 let avg_alignment = cross_attention_scores.mean().unwrap_or(0.0);
540
541 println!(" Max alignment score: {max_alignment:.4}");
542 println!(" Average alignment: {avg_alignment:.4}");
543
544 // Translation quality metrics (simplified)
545 let translation_score = evaluate_translation_quality(&source, &target, &decoder_output)?;
546 println!(" Translation quality score: {translation_score:.4}");
547
548 // Quantum entanglement in cross-lingual representations
549 let cross_lingual_entanglement =
550 analyze_cross_lingual_entanglement(&encoder_output, &decoder_output)?;
551 println!(" Cross-lingual quantum entanglement: {cross_lingual_entanglement:.4}");
552
553 Ok(())
554}
555
556/// Demonstrate quantum data processing
557fn quantum_data_demo() -> Result<()> {
558 println!(" Processing quantum measurement data with transformers...");
559
560 let config = QuantumTransformerConfig {
561 model_dim: 128,
562 num_heads: 4,
563 ff_dim: 512,
564 num_layers: 3,
565 max_seq_len: 64,
566 num_qubits: 8,
567 dropout_rate: 0.05,
568 attention_type: QuantumAttentionType::FullQuantum,
569 position_encoding: PositionEncodingType::QuantumPhase,
570 };
571
572 let transformer = QuantumTransformer::new(config)?;
573
574 // Simulate quantum measurement sequences
575 let batch_size = 5;
576 let seq_len = 32;
577 let model_dim = 128;
578
579 println!(" Generating quantum measurement sequences...");
580
581 // Create quantum state evolution data
582 let quantum_data = Array3::from_shape_fn((batch_size, seq_len, model_dim), |(b, t, d)| {
583 // Simulate quantum state evolution with decoherence
584 let decoherence_factor = (-0.1 * t as f64).exp();
585 let quantum_amplitude =
586 decoherence_factor * (2.0 * std::f64::consts::PI * t as f64 / 8.0 + b as f64).sin();
587
588 // Add measurement noise
589 let noise = 0.05 * (fastrand::f64() - 0.5);
590
591 // Encode as amplitude and phase information
592 if d % 2 == 0 {
593 quantum_amplitude + noise
594 } else {
595 (d as f64)
596 .mul_add(0.1, 2.0 * std::f64::consts::PI * t as f64 / 10.0)
597 .cos()
598 + noise
599 }
600 });
601
602 println!(" Processing {batch_size} quantum sequences of {seq_len} measurements each");
603
604 // Process quantum data
605 let output = transformer.forward(&quantum_data, None)?;
606
607 // Analyze quantum data processing
608 println!("\n Quantum data analysis:");
609
610 // Coherence preservation
611 let input_coherence = compute_coherence_measure(&quantum_data)?;
612 let output_coherence = compute_coherence_measure(&output)?;
613 let coherence_preservation = output_coherence / input_coherence;
614
615 println!(" Input coherence: {input_coherence:.4}");
616 println!(" Output coherence: {output_coherence:.4}");
617 println!(
618 " Coherence preservation: {:.1}%",
619 coherence_preservation * 100.0
620 );
621
622 // Quantum information extraction
623 let quantum_features = extract_quantum_features(&output)?;
624 println!(" Extracted quantum features:");
625 println!(
626 " - Entanglement signature: {:.4}",
627 quantum_features.entanglement
628 );
629 println!(
630 " - Phase coherence: {:.4}",
631 quantum_features.phase_coherence
632 );
633 println!(
634 " - Amplitude stability: {:.4}",
635 quantum_features.amplitude_stability
636 );
637
638 // Decoherence detection
639 let decoherence_pattern = detect_decoherence_pattern(&output)?;
640 println!(" Decoherence detection:");
641 println!(" - Pattern strength: {:.4}", decoherence_pattern.strength);
642 println!(
643 " - Time constant: {:.2} steps",
644 decoherence_pattern.time_constant
645 );
646
647 // Quantum state classification
648 let state_classifications = classify_quantum_states(&output)?;
649 println!(" Quantum state classification:");
650 for (i, classification) in state_classifications.iter().enumerate() {
651 println!(
652 " - Sequence {}: {:.1}% entangled, {:.1}% coherent",
653 i,
654 classification.entangled_prob * 100.0,
655 classification.coherent_prob * 100.0
656 );
657 }
658
659 Ok(())
660}
661
662/// Demonstrate multi-scale quantum transformers
663fn multiscale_demo() -> Result<()> {
664 println!(" Multi-scale quantum transformer architecture...");
665
666 // Create transformers at different scales
667 let scales = vec![
668 (
669 "Fine-scale",
670 QuantumTransformerConfig {
671 model_dim: 128,
672 num_heads: 4,
673 ff_dim: 512,
674 num_layers: 2,
675 max_seq_len: 64,
676 num_qubits: 6,
677 dropout_rate: 0.1,
678 attention_type: QuantumAttentionType::VariationalQuantum,
679 position_encoding: PositionEncodingType::Sinusoidal,
680 },
681 ),
682 (
683 "Medium-scale",
684 QuantumTransformerConfig {
685 model_dim: 256,
686 num_heads: 8,
687 ff_dim: 1024,
688 num_layers: 4,
689 max_seq_len: 128,
690 num_qubits: 10,
691 dropout_rate: 0.1,
692 attention_type: QuantumAttentionType::HybridQuantumClassical,
693 position_encoding: PositionEncodingType::QuantumPhase,
694 },
695 ),
696 (
697 "Coarse-scale",
698 QuantumTransformerConfig {
699 model_dim: 512,
700 num_heads: 16,
701 ff_dim: 2048,
702 num_layers: 6,
703 max_seq_len: 256,
704 num_qubits: 16,
705 dropout_rate: 0.1,
706 attention_type: QuantumAttentionType::FullQuantum,
707 position_encoding: PositionEncodingType::Rotary,
708 },
709 ),
710 ];
711
712 let mut transformers = Vec::new();
713
714 for (scale_name, config) in scales {
715 let transformer = QuantumTransformer::new(config)?;
716 let num_params = transformer.num_parameters();
717
718 println!(" {scale_name} transformer: {num_params} parameters");
719 transformers.push((scale_name, transformer));
720 }
721
722 // Test hierarchical processing
723 println!("\n Hierarchical processing demonstration:");
724
725 let batch_size = 2;
726 let base_seq_len = 64;
727 let input_dim = 128;
728
729 // Create input data
730 let input_data = Array3::from_shape_fn((batch_size, base_seq_len, input_dim), |(b, s, d)| {
731 // Multi-scale signal with different frequency components
732 let fine_component = 0.3 * (s as f64 * 0.5).sin();
733 let medium_component = 0.2 * (s as f64 * 0.1).sin();
734 let coarse_component = 0.1 * (s as f64 * 0.02).sin();
735
736 let base_signal = fine_component + medium_component + coarse_component;
737 0.05f64.mul_add((d as f64).mul_add(0.01, b as f64), base_signal)
738 });
739
740 // Process at each scale
741 let mut scale_outputs = Vec::new();
742
743 for (scale_name, transformer) in &transformers {
744 // Adapt input to transformer's expected dimensions
745 let adapted_input = adapt_input_for_scale(&input_data, transformer.config())?;
746
747 println!(" Processing at {scale_name} scale...");
748 println!(" Adapted input shape: {:?}", adapted_input.dim());
749
750 let output = transformer.forward(&adapted_input, None)?;
751
752 // Analyze scale-specific patterns
753 let pattern_analysis = analyze_scale_patterns(&output)?;
754
755 scale_outputs.push((*scale_name, output));
756 println!(" Pattern analysis:");
757 println!(
758 " - Local patterns: {:.4}",
759 pattern_analysis.local_strength
760 );
761 println!(
762 " - Global patterns: {:.4}",
763 pattern_analysis.global_strength
764 );
765 println!(
766 " - Cross-scale coherence: {:.4}",
767 pattern_analysis.coherence
768 );
769 }
770
771 // Multi-scale fusion
772 println!("\n Multi-scale fusion analysis:");
773 let scale_refs: Vec<(&str, Array3<f64>)> = scale_outputs
774 .iter()
775 .map(|(name, output)| (*name, output.clone()))
776 .collect();
777 let fusion_result = fuse_multiscale_outputs(&scale_refs)?;
778 println!(
779 " Fused representation dimensions: {} features",
780 fusion_result.len()
781 );
782
783 let fusion_quality = evaluate_fusion_quality(&fusion_result)?;
784 println!(" Fusion quality metrics:");
785 println!(
786 " - Information preservation: {:.1}%",
787 fusion_quality.info_preservation * 100.0
788 );
789 println!(
790 " - Scale consistency: {:.1}%",
791 fusion_quality.scale_consistency * 100.0
792 );
793 println!(
794 " - Quantum coherence: {:.4}",
795 fusion_quality.quantum_coherence
796 );
797
798 Ok(())
799}Sourcepub fn config(&self) -> &QuantumTransformerConfig
pub fn config(&self) -> &QuantumTransformerConfig
Get model configuration
Examples found in repository?
examples/quantum_transformer.rs (line 745)
663fn multiscale_demo() -> Result<()> {
664 println!(" Multi-scale quantum transformer architecture...");
665
666 // Create transformers at different scales
667 let scales = vec![
668 (
669 "Fine-scale",
670 QuantumTransformerConfig {
671 model_dim: 128,
672 num_heads: 4,
673 ff_dim: 512,
674 num_layers: 2,
675 max_seq_len: 64,
676 num_qubits: 6,
677 dropout_rate: 0.1,
678 attention_type: QuantumAttentionType::VariationalQuantum,
679 position_encoding: PositionEncodingType::Sinusoidal,
680 },
681 ),
682 (
683 "Medium-scale",
684 QuantumTransformerConfig {
685 model_dim: 256,
686 num_heads: 8,
687 ff_dim: 1024,
688 num_layers: 4,
689 max_seq_len: 128,
690 num_qubits: 10,
691 dropout_rate: 0.1,
692 attention_type: QuantumAttentionType::HybridQuantumClassical,
693 position_encoding: PositionEncodingType::QuantumPhase,
694 },
695 ),
696 (
697 "Coarse-scale",
698 QuantumTransformerConfig {
699 model_dim: 512,
700 num_heads: 16,
701 ff_dim: 2048,
702 num_layers: 6,
703 max_seq_len: 256,
704 num_qubits: 16,
705 dropout_rate: 0.1,
706 attention_type: QuantumAttentionType::FullQuantum,
707 position_encoding: PositionEncodingType::Rotary,
708 },
709 ),
710 ];
711
712 let mut transformers = Vec::new();
713
714 for (scale_name, config) in scales {
715 let transformer = QuantumTransformer::new(config)?;
716 let num_params = transformer.num_parameters();
717
718 println!(" {scale_name} transformer: {num_params} parameters");
719 transformers.push((scale_name, transformer));
720 }
721
722 // Test hierarchical processing
723 println!("\n Hierarchical processing demonstration:");
724
725 let batch_size = 2;
726 let base_seq_len = 64;
727 let input_dim = 128;
728
729 // Create input data
730 let input_data = Array3::from_shape_fn((batch_size, base_seq_len, input_dim), |(b, s, d)| {
731 // Multi-scale signal with different frequency components
732 let fine_component = 0.3 * (s as f64 * 0.5).sin();
733 let medium_component = 0.2 * (s as f64 * 0.1).sin();
734 let coarse_component = 0.1 * (s as f64 * 0.02).sin();
735
736 let base_signal = fine_component + medium_component + coarse_component;
737 0.05f64.mul_add((d as f64).mul_add(0.01, b as f64), base_signal)
738 });
739
740 // Process at each scale
741 let mut scale_outputs = Vec::new();
742
743 for (scale_name, transformer) in &transformers {
744 // Adapt input to transformer's expected dimensions
745 let adapted_input = adapt_input_for_scale(&input_data, transformer.config())?;
746
747 println!(" Processing at {scale_name} scale...");
748 println!(" Adapted input shape: {:?}", adapted_input.dim());
749
750 let output = transformer.forward(&adapted_input, None)?;
751
752 // Analyze scale-specific patterns
753 let pattern_analysis = analyze_scale_patterns(&output)?;
754
755 scale_outputs.push((*scale_name, output));
756 println!(" Pattern analysis:");
757 println!(
758 " - Local patterns: {:.4}",
759 pattern_analysis.local_strength
760 );
761 println!(
762 " - Global patterns: {:.4}",
763 pattern_analysis.global_strength
764 );
765 println!(
766 " - Cross-scale coherence: {:.4}",
767 pattern_analysis.coherence
768 );
769 }
770
771 // Multi-scale fusion
772 println!("\n Multi-scale fusion analysis:");
773 let scale_refs: Vec<(&str, Array3<f64>)> = scale_outputs
774 .iter()
775 .map(|(name, output)| (*name, output.clone()))
776 .collect();
777 let fusion_result = fuse_multiscale_outputs(&scale_refs)?;
778 println!(
779 " Fused representation dimensions: {} features",
780 fusion_result.len()
781 );
782
783 let fusion_quality = evaluate_fusion_quality(&fusion_result)?;
784 println!(" Fusion quality metrics:");
785 println!(
786 " - Information preservation: {:.1}%",
787 fusion_quality.info_preservation * 100.0
788 );
789 println!(
790 " - Scale consistency: {:.1}%",
791 fusion_quality.scale_consistency * 100.0
792 );
793 println!(
794 " - Quantum coherence: {:.4}",
795 fusion_quality.quantum_coherence
796 );
797
798 Ok(())
799}Sourcepub fn num_parameters(&self) -> usize
pub fn num_parameters(&self) -> usize
Get number of parameters
Examples found in repository?
examples/quantum_transformer.rs (line 100)
54fn config_demo() -> Result<()> {
55 println!(" Creating various transformer configurations...");
56
57 // Small efficient model
58 let small_config = QuantumTransformerConfig::small();
59 println!(
60 " Small model: {} params, {} heads, {} layers",
61 small_config.model_dim, small_config.num_heads, small_config.num_layers
62 );
63
64 // Standard model
65 let default_config = QuantumTransformerConfig::default();
66 println!(
67 " Default model: {} params, {} heads, {} layers",
68 default_config.model_dim, default_config.num_heads, default_config.num_layers
69 );
70
71 // Large model
72 let large_config = QuantumTransformerConfig::large();
73 println!(
74 " Large model: {} params, {} heads, {} layers",
75 large_config.model_dim, large_config.num_heads, large_config.num_layers
76 );
77
78 // Custom configuration
79 let custom_config = QuantumTransformerConfig {
80 model_dim: 384,
81 num_heads: 6,
82 ff_dim: 1536,
83 num_layers: 8,
84 max_seq_len: 1024,
85 num_qubits: 12,
86 dropout_rate: 0.15,
87 attention_type: QuantumAttentionType::QuantumEnhancedMultiHead,
88 position_encoding: PositionEncodingType::Rotary,
89 };
90
91 println!(
92 " Custom model: {} dim, {} qubits, {:?} attention",
93 custom_config.model_dim, custom_config.num_qubits, custom_config.attention_type
94 );
95
96 // Create transformer with custom config
97 let transformer = QuantumTransformer::new(custom_config)?;
98 println!(
99 " Created transformer with {} total parameters",
100 transformer.num_parameters()
101 );
102
103 Ok(())
104}
105
106/// Demonstrate different quantum attention mechanisms
107fn attention_mechanisms_demo() -> Result<()> {
108 println!(" Testing various quantum attention mechanisms...");
109
110 let attention_types = vec![
111 ("Full Quantum", QuantumAttentionType::FullQuantum),
112 (
113 "Hybrid Quantum-Classical",
114 QuantumAttentionType::HybridQuantumClassical,
115 ),
116 (
117 "Variational Quantum",
118 QuantumAttentionType::VariationalQuantum,
119 ),
120 (
121 "Quantum Enhanced Multi-Head",
122 QuantumAttentionType::QuantumEnhancedMultiHead,
123 ),
124 (
125 "Quantum Self-Attention",
126 QuantumAttentionType::QuantumSelfAttention,
127 ),
128 ];
129
130 for (name, attention_type) in attention_types {
131 println!("\n --- {name} Attention ---");
132
133 let attention = QuantumMultiHeadAttention::new(4, 256, attention_type, 8)?;
134 println!(
135 " Created attention module: {} heads, {} model dim",
136 4, 256
137 ); // Fixed values since fields are private
138
139 // Test forward pass
140 let batch_size = 2;
141 let seq_len = 10;
142 let model_dim = 256;
143
144 let query = Array3::from_shape_fn((batch_size, seq_len, model_dim), |(b, s, d)| {
145 0.1 * (d as f64).mul_add(0.01, (s as f64).mul_add(0.1, b as f64))
146 });
147 let key = query.clone();
148 let value = query.clone();
149
150 let attention_output = attention.forward(&query, &key, &value, None)?;
151
152 println!(
153 " Attention output shape: {:?}",
154 attention_output.output.dim()
155 );
156 println!(
157 " Attention weights shape: {:?}",
158 attention_output.attention_weights.dim()
159 );
160
161 // Analyze quantum attention properties
162 let quantum_info = &attention_output.quantum_info;
163 let avg_entanglement = quantum_info.entanglement_matrix.mean().unwrap_or(0.0);
164 let max_coherence = quantum_info
165 .coherence_scores
166 .iter()
167 .copied()
168 .fold(f64::NEG_INFINITY, f64::max);
169
170 println!(" Average entanglement: {avg_entanglement:.4}");
171 println!(" Maximum coherence: {max_coherence:.4}");
172
173 // Attention pattern analysis
174 let attention_weights = &attention_output.attention_weights;
175 let max_attention = attention_weights
176 .iter()
177 .copied()
178 .fold(f64::NEG_INFINITY, f64::max);
179 let avg_attention = attention_weights.mean().unwrap_or(0.0);
180
181 println!(" Max attention weight: {max_attention:.4}");
182 println!(" Average attention: {avg_attention:.4}");
183
184 // Check attention sparsity
185 let sparsity = attention_weights.iter().filter(|&&x| x < 0.01).count() as f64
186 / attention_weights.len() as f64;
187 println!(" Attention sparsity: {:.1}%", sparsity * 100.0);
188 }
189
190 Ok(())
191}
192
193/// Demonstrate different position encoding types
194fn position_encoding_demo() -> Result<()> {
195 println!(" Testing quantum position encoding variants...");
196
197 let encoding_types = vec![
198 ("Sinusoidal", PositionEncodingType::Sinusoidal),
199 ("Quantum Phase", PositionEncodingType::QuantumPhase),
200 ("Learnable Quantum", PositionEncodingType::LearnableQuantum),
201 ("Relative", PositionEncodingType::Relative),
202 ("Rotary (RoPE)", PositionEncodingType::Rotary),
203 ];
204
205 let model_dim = 128;
206 let max_seq_len = 64;
207 let num_qubits = 8;
208
209 for (name, encoding_type) in encoding_types {
210 println!("\n --- {name} Position Encoding ---");
211
212 let pos_enc =
213 QuantumPositionEncoding::new(encoding_type, model_dim, max_seq_len, num_qubits)?;
214
215 let batch_size = 3;
216 let seq_len = 32;
217
218 let encodings = pos_enc.forward(seq_len, batch_size)?;
219 println!(" Encoding shape: {:?}", encodings.dim());
220
221 // Analyze position encoding properties
222 let encoding_range = {
223 let min_val = encodings.iter().copied().fold(f64::INFINITY, f64::min);
224 let max_val = encodings.iter().copied().fold(f64::NEG_INFINITY, f64::max);
225 max_val - min_val
226 };
227
228 println!(" Value range: {encoding_range:.4}");
229
230 // Check position distinguishability
231 let pos1 = encodings
232 .slice(scirs2_core::ndarray::s![0, 0, ..])
233 .to_owned();
234 let pos2 = encodings
235 .slice(scirs2_core::ndarray::s![0, seq_len - 1, ..])
236 .to_owned();
237 let position_distance = (&pos1 - &pos2).mapv(|x| x * x).sum().sqrt();
238
239 println!(" Distance between first and last position: {position_distance:.4}");
240
241 // Analyze periodicity for sinusoidal encodings
242 if name == "Sinusoidal" {
243 let mut periodicities = Vec::new();
244 for d in (0..model_dim).step_by(10) {
245 let values: Vec<f64> = (0..seq_len).map(|s| encodings[[0, s, d]]).collect();
246
247 // Simple periodicity check
248 let period = find_period(&values);
249 if period > 0 {
250 periodicities.push(period);
251 }
252 }
253
254 if !periodicities.is_empty() {
255 let avg_period =
256 periodicities.iter().sum::<usize>() as f64 / periodicities.len() as f64;
257 println!(" Average period length: {avg_period:.1}");
258 }
259 }
260
261 // Check quantum phase encoding properties
262 if name == "Quantum Phase" {
263 let phase_variance = encodings.var(0.0);
264 println!(" Phase encoding variance: {phase_variance:.4}");
265 }
266 }
267
268 Ok(())
269}
270
271/// Demonstrate complete transformer forward pass
272fn transformer_forward_demo() -> Result<()> {
273 println!(" Testing complete quantum transformer forward pass...");
274
275 let config = QuantumTransformerConfig {
276 model_dim: 256,
277 num_heads: 8,
278 ff_dim: 1024,
279 num_layers: 4,
280 max_seq_len: 128,
281 num_qubits: 10,
282 dropout_rate: 0.1,
283 attention_type: QuantumAttentionType::HybridQuantumClassical,
284 position_encoding: PositionEncodingType::QuantumPhase,
285 };
286
287 let transformer = QuantumTransformer::new(config.clone())?;
288 println!(
289 " Created transformer: {} layers, {} parameters",
290 config.num_layers,
291 transformer.num_parameters()
292 );
293
294 // Test with different sequence lengths
295 let test_sequences = vec![
296 (2, 16, 128), // small batch, short sequence
297 (4, 32, 128), // medium batch, medium sequence
298 (1, 64, 128), // single sample, long sequence
299 ];
300
301 for (batch_size, seq_len, input_dim) in test_sequences {
302 println!("\n Testing: batch={batch_size}, seq_len={seq_len}, input_dim={input_dim}");
303
304 // Create test input
305 let input = Array3::from_shape_fn((batch_size, seq_len, input_dim), |(b, s, d)| {
306 let base = 0.1 * (b as f64 + 1.0);
307 let seq_component = 0.05 * (s as f64 * 0.1).sin();
308 let dim_component = 0.02 * (d as f64 * 0.01).cos();
309 base + seq_component + dim_component
310 });
311
312 // Create causal mask for autoregressive modeling
313 let causal_mask = create_causal_mask(batch_size, seq_len);
314
315 // Forward pass
316 let start_time = std::time::Instant::now();
317 let output = transformer.forward(&input, Some(&causal_mask))?;
318 let forward_time = start_time.elapsed();
319
320 println!(" Output shape: {:?}", output.dim());
321 println!(" Forward pass time: {forward_time:.2?}");
322
323 // Analyze output properties
324 let output_mean = output.mean().unwrap_or(0.0);
325 let output_std = output.var(0.0).sqrt();
326 let output_range = {
327 let min_val = output.iter().copied().fold(f64::INFINITY, f64::min);
328 let max_val = output.iter().copied().fold(f64::NEG_INFINITY, f64::max);
329 max_val - min_val
330 };
331
332 println!(
333 " Output statistics: mean={output_mean:.4}, std={output_std:.4}, range={output_range:.4}"
334 );
335
336 // Check causality (if using causal mask)
337 let causality_check = check_causality(&input, &output, &causal_mask);
338 if causality_check {
339 println!(" ✓ Causal dependencies respected");
340 } else {
341 println!(" ⚠ Potential causality violations detected");
342 }
343
344 // Memory efficiency analysis
345 let memory_per_token = (transformer.num_parameters() * 8 + output.len() * 8) as f64
346 / (batch_size * seq_len) as f64;
347 println!(" Memory per token: {memory_per_token:.1} bytes");
348 }
349
350 Ok(())
351}
352
353/// Demonstrate quantum language modeling
354fn language_modeling_demo() -> Result<()> {
355 println!(" Quantum language modeling with transformer...");
356
357 let config = QuantumTransformerConfig {
358 model_dim: 384,
359 num_heads: 6,
360 ff_dim: 1536,
361 num_layers: 6,
362 max_seq_len: 256,
363 num_qubits: 12,
364 dropout_rate: 0.1,
365 attention_type: QuantumAttentionType::QuantumSelfAttention,
366 position_encoding: PositionEncodingType::Rotary,
367 };
368
369 let transformer = QuantumTransformer::new(config.clone())?;
370
371 // Simulate language modeling task
372 let vocab_size = 1000;
373 let batch_size = 4;
374 let seq_len = 64;
375
376 // Create tokenized sequences (simulated)
377 let input_tokens =
378 Array3::from_shape_fn((batch_size, seq_len, config.model_dim), |(b, s, d)| {
379 // Simulate token embeddings
380 let token_id = (b * seq_len + s) % vocab_size;
381 let embedding_val = (token_id as f64 / vocab_size as f64).mul_add(2.0, -1.0);
382 embedding_val * 0.1f64.mul_add(d as f64 / config.model_dim as f64, 1.0)
383 });
384
385 println!(" Processing {batch_size} sequences of length {seq_len}");
386
387 // Create causal mask for language modeling
388 let causal_mask = create_causal_mask(batch_size, seq_len);
389
390 // Forward pass
391 let logits = transformer.forward(&input_tokens, Some(&causal_mask))?;
392
393 // Simulate next token prediction
394 let mut perplexities = Vec::new();
395
396 for batch_idx in 0..batch_size {
397 let mut log_likelihood = 0.0;
398 let mut valid_predictions = 0;
399
400 for pos in 0..seq_len - 1 {
401 let current_logits = logits.slice(scirs2_core::ndarray::s![batch_idx, pos, ..]);
402
403 // Convert to probabilities (simplified softmax)
404 let max_logit = current_logits
405 .iter()
406 .copied()
407 .fold(f64::NEG_INFINITY, f64::max);
408 let exp_logits: Array1<f64> = current_logits.mapv(|x| (x - max_logit).exp());
409 let sum_exp = exp_logits.sum();
410 let probs = exp_logits / sum_exp;
411
412 // Simulate target token (next position embedding)
413 let target_embedding =
414 input_tokens.slice(scirs2_core::ndarray::s![batch_idx, pos + 1, ..]);
415 let target_prob = compute_token_probability(&probs, &target_embedding.to_owned())?;
416
417 if target_prob > 1e-10 {
418 log_likelihood += target_prob.ln();
419 valid_predictions += 1;
420 }
421 }
422
423 if valid_predictions > 0 {
424 let avg_log_likelihood = log_likelihood / f64::from(valid_predictions);
425 let perplexity = (-avg_log_likelihood).exp();
426 perplexities.push(perplexity);
427 }
428 }
429
430 if !perplexities.is_empty() {
431 let avg_perplexity = perplexities.iter().sum::<f64>() / perplexities.len() as f64;
432 println!(" Average perplexity: {avg_perplexity:.2}");
433
434 // Analyze quantum language model properties
435 println!(" Quantum language model analysis:");
436
437 // Attention pattern analysis
438 println!(" - Uses quantum self-attention for context modeling");
439 println!(" - Rotary position encoding preserves relative positions");
440 println!(
441 " - {} layers provide hierarchical representation",
442 config.num_layers
443 );
444
445 // Information flow analysis
446 let first_layer_norm = logits
447 .slice(scirs2_core::ndarray::s![0, .., ..])
448 .var(0.0)
449 .sqrt();
450 println!(" - Output layer standard deviation: {first_layer_norm:.4}");
451
452 // Quantum coherence in language representation
453 let quantum_coherence = analyze_quantum_language_coherence(&logits)?;
454 println!(" - Quantum coherence in representations: {quantum_coherence:.4}");
455 }
456
457 Ok(())
458}
459
460/// Demonstrate sequence-to-sequence tasks
461fn seq2seq_demo() -> Result<()> {
462 println!(" Quantum sequence-to-sequence modeling...");
463
464 // Encoder configuration
465 let encoder_config = QuantumTransformerConfig {
466 model_dim: 256,
467 num_heads: 8,
468 ff_dim: 1024,
469 num_layers: 4,
470 max_seq_len: 128,
471 num_qubits: 10,
472 dropout_rate: 0.1,
473 attention_type: QuantumAttentionType::HybridQuantumClassical,
474 position_encoding: PositionEncodingType::Sinusoidal,
475 };
476
477 // Decoder configuration (with causal attention)
478 let decoder_config = QuantumTransformerConfig {
479 model_dim: 256,
480 num_heads: 8,
481 ff_dim: 1024,
482 num_layers: 4,
483 max_seq_len: 128,
484 num_qubits: 10,
485 dropout_rate: 0.1,
486 attention_type: QuantumAttentionType::QuantumEnhancedMultiHead,
487 position_encoding: PositionEncodingType::QuantumPhase,
488 };
489
490 let encoder = QuantumTransformer::new(encoder_config)?;
491 let decoder = QuantumTransformer::new(decoder_config)?;
492
493 println!(" Created encoder-decoder architecture");
494 println!(" Encoder: {} parameters", encoder.num_parameters());
495 println!(" Decoder: {} parameters", decoder.num_parameters());
496
497 // Simulate translation task
498 let batch_size = 3;
499 let src_len = 32;
500 let tgt_len = 28;
501 let model_dim = 256;
502
503 // Source sequence (e.g., English)
504 let source = Array3::from_shape_fn((batch_size, src_len, model_dim), |(b, s, d)| {
505 let src_pattern = 0.3 * ((s as f64).mul_add(0.2, b as f64).sin());
506 0.1f64.mul_add(d as f64 / model_dim as f64, src_pattern)
507 });
508
509 // Target sequence (e.g., French)
510 let target = Array3::from_shape_fn((batch_size, tgt_len, model_dim), |(b, s, d)| {
511 let tgt_pattern = 0.4 * ((s as f64).mul_add(0.15, b as f64 * 0.3).cos());
512 0.12f64.mul_add(d as f64 / model_dim as f64, tgt_pattern)
513 });
514
515 println!("\n Processing translation: {src_len} -> {tgt_len} tokens");
516
517 // Encode source sequence
518 let encoder_output = encoder.forward(&source, None)?;
519 println!(" Encoder output shape: {:?}", encoder_output.dim());
520
521 // Decode with causal mask
522 let causal_mask = create_causal_mask(batch_size, tgt_len);
523 let decoder_output = decoder.forward(&target, Some(&causal_mask))?;
524 println!(" Decoder output shape: {:?}", decoder_output.dim());
525
526 // Cross-attention simulation (simplified)
527 println!("\n Cross-attention analysis:");
528 let cross_attention_scores = compute_cross_attention(&encoder_output, &decoder_output)?;
529 println!(
530 " Cross-attention shape: {:?}",
531 cross_attention_scores.dim()
532 );
533
534 // Analyze attention alignment
535 let max_alignment = cross_attention_scores
536 .iter()
537 .copied()
538 .fold(f64::NEG_INFINITY, f64::max);
539 let avg_alignment = cross_attention_scores.mean().unwrap_or(0.0);
540
541 println!(" Max alignment score: {max_alignment:.4}");
542 println!(" Average alignment: {avg_alignment:.4}");
543
544 // Translation quality metrics (simplified)
545 let translation_score = evaluate_translation_quality(&source, &target, &decoder_output)?;
546 println!(" Translation quality score: {translation_score:.4}");
547
548 // Quantum entanglement in cross-lingual representations
549 let cross_lingual_entanglement =
550 analyze_cross_lingual_entanglement(&encoder_output, &decoder_output)?;
551 println!(" Cross-lingual quantum entanglement: {cross_lingual_entanglement:.4}");
552
553 Ok(())
554}
555
556/// Demonstrate quantum data processing
557fn quantum_data_demo() -> Result<()> {
558 println!(" Processing quantum measurement data with transformers...");
559
560 let config = QuantumTransformerConfig {
561 model_dim: 128,
562 num_heads: 4,
563 ff_dim: 512,
564 num_layers: 3,
565 max_seq_len: 64,
566 num_qubits: 8,
567 dropout_rate: 0.05,
568 attention_type: QuantumAttentionType::FullQuantum,
569 position_encoding: PositionEncodingType::QuantumPhase,
570 };
571
572 let transformer = QuantumTransformer::new(config)?;
573
574 // Simulate quantum measurement sequences
575 let batch_size = 5;
576 let seq_len = 32;
577 let model_dim = 128;
578
579 println!(" Generating quantum measurement sequences...");
580
581 // Create quantum state evolution data
582 let quantum_data = Array3::from_shape_fn((batch_size, seq_len, model_dim), |(b, t, d)| {
583 // Simulate quantum state evolution with decoherence
584 let decoherence_factor = (-0.1 * t as f64).exp();
585 let quantum_amplitude =
586 decoherence_factor * (2.0 * std::f64::consts::PI * t as f64 / 8.0 + b as f64).sin();
587
588 // Add measurement noise
589 let noise = 0.05 * (fastrand::f64() - 0.5);
590
591 // Encode as amplitude and phase information
592 if d % 2 == 0 {
593 quantum_amplitude + noise
594 } else {
595 (d as f64)
596 .mul_add(0.1, 2.0 * std::f64::consts::PI * t as f64 / 10.0)
597 .cos()
598 + noise
599 }
600 });
601
602 println!(" Processing {batch_size} quantum sequences of {seq_len} measurements each");
603
604 // Process quantum data
605 let output = transformer.forward(&quantum_data, None)?;
606
607 // Analyze quantum data processing
608 println!("\n Quantum data analysis:");
609
610 // Coherence preservation
611 let input_coherence = compute_coherence_measure(&quantum_data)?;
612 let output_coherence = compute_coherence_measure(&output)?;
613 let coherence_preservation = output_coherence / input_coherence;
614
615 println!(" Input coherence: {input_coherence:.4}");
616 println!(" Output coherence: {output_coherence:.4}");
617 println!(
618 " Coherence preservation: {:.1}%",
619 coherence_preservation * 100.0
620 );
621
622 // Quantum information extraction
623 let quantum_features = extract_quantum_features(&output)?;
624 println!(" Extracted quantum features:");
625 println!(
626 " - Entanglement signature: {:.4}",
627 quantum_features.entanglement
628 );
629 println!(
630 " - Phase coherence: {:.4}",
631 quantum_features.phase_coherence
632 );
633 println!(
634 " - Amplitude stability: {:.4}",
635 quantum_features.amplitude_stability
636 );
637
638 // Decoherence detection
639 let decoherence_pattern = detect_decoherence_pattern(&output)?;
640 println!(" Decoherence detection:");
641 println!(" - Pattern strength: {:.4}", decoherence_pattern.strength);
642 println!(
643 " - Time constant: {:.2} steps",
644 decoherence_pattern.time_constant
645 );
646
647 // Quantum state classification
648 let state_classifications = classify_quantum_states(&output)?;
649 println!(" Quantum state classification:");
650 for (i, classification) in state_classifications.iter().enumerate() {
651 println!(
652 " - Sequence {}: {:.1}% entangled, {:.1}% coherent",
653 i,
654 classification.entangled_prob * 100.0,
655 classification.coherent_prob * 100.0
656 );
657 }
658
659 Ok(())
660}
661
662/// Demonstrate multi-scale quantum transformers
663fn multiscale_demo() -> Result<()> {
664 println!(" Multi-scale quantum transformer architecture...");
665
666 // Create transformers at different scales
667 let scales = vec![
668 (
669 "Fine-scale",
670 QuantumTransformerConfig {
671 model_dim: 128,
672 num_heads: 4,
673 ff_dim: 512,
674 num_layers: 2,
675 max_seq_len: 64,
676 num_qubits: 6,
677 dropout_rate: 0.1,
678 attention_type: QuantumAttentionType::VariationalQuantum,
679 position_encoding: PositionEncodingType::Sinusoidal,
680 },
681 ),
682 (
683 "Medium-scale",
684 QuantumTransformerConfig {
685 model_dim: 256,
686 num_heads: 8,
687 ff_dim: 1024,
688 num_layers: 4,
689 max_seq_len: 128,
690 num_qubits: 10,
691 dropout_rate: 0.1,
692 attention_type: QuantumAttentionType::HybridQuantumClassical,
693 position_encoding: PositionEncodingType::QuantumPhase,
694 },
695 ),
696 (
697 "Coarse-scale",
698 QuantumTransformerConfig {
699 model_dim: 512,
700 num_heads: 16,
701 ff_dim: 2048,
702 num_layers: 6,
703 max_seq_len: 256,
704 num_qubits: 16,
705 dropout_rate: 0.1,
706 attention_type: QuantumAttentionType::FullQuantum,
707 position_encoding: PositionEncodingType::Rotary,
708 },
709 ),
710 ];
711
712 let mut transformers = Vec::new();
713
714 for (scale_name, config) in scales {
715 let transformer = QuantumTransformer::new(config)?;
716 let num_params = transformer.num_parameters();
717
718 println!(" {scale_name} transformer: {num_params} parameters");
719 transformers.push((scale_name, transformer));
720 }
721
722 // Test hierarchical processing
723 println!("\n Hierarchical processing demonstration:");
724
725 let batch_size = 2;
726 let base_seq_len = 64;
727 let input_dim = 128;
728
729 // Create input data
730 let input_data = Array3::from_shape_fn((batch_size, base_seq_len, input_dim), |(b, s, d)| {
731 // Multi-scale signal with different frequency components
732 let fine_component = 0.3 * (s as f64 * 0.5).sin();
733 let medium_component = 0.2 * (s as f64 * 0.1).sin();
734 let coarse_component = 0.1 * (s as f64 * 0.02).sin();
735
736 let base_signal = fine_component + medium_component + coarse_component;
737 0.05f64.mul_add((d as f64).mul_add(0.01, b as f64), base_signal)
738 });
739
740 // Process at each scale
741 let mut scale_outputs = Vec::new();
742
743 for (scale_name, transformer) in &transformers {
744 // Adapt input to transformer's expected dimensions
745 let adapted_input = adapt_input_for_scale(&input_data, transformer.config())?;
746
747 println!(" Processing at {scale_name} scale...");
748 println!(" Adapted input shape: {:?}", adapted_input.dim());
749
750 let output = transformer.forward(&adapted_input, None)?;
751
752 // Analyze scale-specific patterns
753 let pattern_analysis = analyze_scale_patterns(&output)?;
754
755 scale_outputs.push((*scale_name, output));
756 println!(" Pattern analysis:");
757 println!(
758 " - Local patterns: {:.4}",
759 pattern_analysis.local_strength
760 );
761 println!(
762 " - Global patterns: {:.4}",
763 pattern_analysis.global_strength
764 );
765 println!(
766 " - Cross-scale coherence: {:.4}",
767 pattern_analysis.coherence
768 );
769 }
770
771 // Multi-scale fusion
772 println!("\n Multi-scale fusion analysis:");
773 let scale_refs: Vec<(&str, Array3<f64>)> = scale_outputs
774 .iter()
775 .map(|(name, output)| (*name, output.clone()))
776 .collect();
777 let fusion_result = fuse_multiscale_outputs(&scale_refs)?;
778 println!(
779 " Fused representation dimensions: {} features",
780 fusion_result.len()
781 );
782
783 let fusion_quality = evaluate_fusion_quality(&fusion_result)?;
784 println!(" Fusion quality metrics:");
785 println!(
786 " - Information preservation: {:.1}%",
787 fusion_quality.info_preservation * 100.0
788 );
789 println!(
790 " - Scale consistency: {:.1}%",
791 fusion_quality.scale_consistency * 100.0
792 );
793 println!(
794 " - Quantum coherence: {:.4}",
795 fusion_quality.quantum_coherence
796 );
797
798 Ok(())
799}Trait Implementations§
Source§impl Clone for QuantumTransformer
impl Clone for QuantumTransformer
Source§fn clone(&self) -> QuantumTransformer
fn clone(&self) -> QuantumTransformer
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for QuantumTransformer
impl !RefUnwindSafe for QuantumTransformer
impl Send for QuantumTransformer
impl Sync for QuantumTransformer
impl Unpin for QuantumTransformer
impl !UnwindSafe for QuantumTransformer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.