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