pub struct QuantumTransferLearning { /* private fields */ }Expand description
Quantum transfer learning framework
Implementations§
Source§impl QuantumTransferLearning
impl QuantumTransferLearning
Sourcepub fn new(
source_model: PretrainedModel,
target_layers: Vec<QNNLayerType>,
strategy: TransferStrategy,
) -> Result<Self>
pub fn new( source_model: PretrainedModel, target_layers: Vec<QNNLayerType>, strategy: TransferStrategy, ) -> Result<Self>
Create a new transfer learning instance
Examples found in repository?
examples/transfer_learning.rs (lines 45-51)
12fn main() -> Result<()> {
13 println!("=== Quantum Transfer Learning Demo ===\n");
14
15 // Step 1: Load a pre-trained model from the model zoo
16 println!("1. Loading pre-trained image classifier...");
17 let pretrained = QuantumModelZoo::get_image_classifier()?;
18
19 println!(" Pre-trained model info:");
20 println!(" - Task: {}", pretrained.task_description);
21 println!(
22 " - Original accuracy: {:.2}%",
23 pretrained
24 .performance_metrics
25 .get("accuracy")
26 .unwrap_or(&0.0)
27 * 100.0
28 );
29 println!(" - Number of qubits: {}", pretrained.qnn.num_qubits);
30
31 // Step 2: Create new layers for the target task
32 println!("\n2. Creating new layers for text classification task...");
33 let new_layers = vec![
34 QNNLayerType::VariationalLayer { num_params: 6 },
35 QNNLayerType::MeasurementLayer {
36 measurement_basis: "Pauli-Z".to_string(),
37 },
38 ];
39
40 // Step 3: Initialize transfer learning with different strategies
41 println!("\n3. Testing different transfer learning strategies:");
42
43 // Strategy 1: Fine-tuning
44 println!("\n a) Fine-tuning strategy (train last 2 layers only)");
45 let mut transfer_finetune = QuantumTransferLearning::new(
46 pretrained.clone(),
47 new_layers.clone(),
48 TransferStrategy::FineTuning {
49 num_trainable_layers: 2,
50 },
51 )?;
52
53 // Strategy 2: Feature extraction
54 println!(" b) Feature extraction strategy (freeze all pre-trained layers)");
55 let transfer_feature = QuantumTransferLearning::new(
56 pretrained.clone(),
57 new_layers.clone(),
58 TransferStrategy::FeatureExtraction,
59 )?;
60
61 // Strategy 3: Progressive unfreezing
62 println!(" c) Progressive unfreezing (unfreeze one layer every 5 epochs)");
63 let transfer_progressive = QuantumTransferLearning::new(
64 pretrained,
65 new_layers,
66 TransferStrategy::ProgressiveUnfreezing { unfreeze_rate: 5 },
67 )?;
68
69 // Step 4: Generate synthetic training data for the new task
70 println!("\n4. Generating synthetic training data...");
71 let num_samples = 50;
72 let num_features = 4;
73 let training_data = Array2::from_shape_fn((num_samples, num_features), |(i, j)| {
74 (i as f64).mul_add(0.1, j as f64 * 0.2).sin()
75 });
76 let labels = Array1::from_shape_fn(num_samples, |i| if i % 2 == 0 { 0.0 } else { 1.0 });
77
78 // Step 5: Train with fine-tuning strategy
79 println!("\n5. Training with fine-tuning strategy...");
80 let mut optimizer = Adam::new(0.01);
81
82 let result = transfer_finetune.train(
83 &training_data,
84 &labels,
85 &mut optimizer,
86 20, // epochs
87 10, // batch_size
88 )?;
89
90 println!(" Training complete!");
91 println!(" - Final loss: {:.4}", result.final_loss);
92 println!(" - Accuracy: {:.2}%", result.accuracy * 100.0);
93
94 // Step 6: Extract features using pre-trained layers
95 println!("\n6. Extracting features from pre-trained layers...");
96 let features = transfer_feature.extract_features(&training_data)?;
97 println!(" Extracted feature dimensions: {:?}", features.dim());
98
99 // Step 7: Demonstrate model zoo
100 println!("\n7. Available pre-trained models in the zoo:");
101 println!(" - Image classifier (4 qubits, MNIST subset)");
102 println!(" - Chemistry model (6 qubits, molecular energy)");
103
104 // Load chemistry model
105 let chemistry_model = QuantumModelZoo::get_chemistry_model()?;
106 println!("\n Chemistry model info:");
107 println!(" - Task: {}", chemistry_model.task_description);
108 println!(
109 " - MAE: {:.4}",
110 chemistry_model
111 .performance_metrics
112 .get("mae")
113 .unwrap_or(&0.0)
114 );
115 println!(
116 " - R² score: {:.4}",
117 chemistry_model
118 .performance_metrics
119 .get("r2_score")
120 .unwrap_or(&0.0)
121 );
122
123 println!("\n=== Transfer Learning Demo Complete ===");
124
125 Ok(())
126}Sourcepub fn train(
&mut self,
training_data: &Array2<f64>,
labels: &Array1<f64>,
optimizer: &mut dyn Optimizer,
epochs: usize,
batch_size: usize,
) -> Result<TrainingResult>
pub fn train( &mut self, training_data: &Array2<f64>, labels: &Array1<f64>, optimizer: &mut dyn Optimizer, epochs: usize, batch_size: usize, ) -> Result<TrainingResult>
Train the target model on new data
Examples found in repository?
examples/transfer_learning.rs (lines 82-88)
12fn main() -> Result<()> {
13 println!("=== Quantum Transfer Learning Demo ===\n");
14
15 // Step 1: Load a pre-trained model from the model zoo
16 println!("1. Loading pre-trained image classifier...");
17 let pretrained = QuantumModelZoo::get_image_classifier()?;
18
19 println!(" Pre-trained model info:");
20 println!(" - Task: {}", pretrained.task_description);
21 println!(
22 " - Original accuracy: {:.2}%",
23 pretrained
24 .performance_metrics
25 .get("accuracy")
26 .unwrap_or(&0.0)
27 * 100.0
28 );
29 println!(" - Number of qubits: {}", pretrained.qnn.num_qubits);
30
31 // Step 2: Create new layers for the target task
32 println!("\n2. Creating new layers for text classification task...");
33 let new_layers = vec![
34 QNNLayerType::VariationalLayer { num_params: 6 },
35 QNNLayerType::MeasurementLayer {
36 measurement_basis: "Pauli-Z".to_string(),
37 },
38 ];
39
40 // Step 3: Initialize transfer learning with different strategies
41 println!("\n3. Testing different transfer learning strategies:");
42
43 // Strategy 1: Fine-tuning
44 println!("\n a) Fine-tuning strategy (train last 2 layers only)");
45 let mut transfer_finetune = QuantumTransferLearning::new(
46 pretrained.clone(),
47 new_layers.clone(),
48 TransferStrategy::FineTuning {
49 num_trainable_layers: 2,
50 },
51 )?;
52
53 // Strategy 2: Feature extraction
54 println!(" b) Feature extraction strategy (freeze all pre-trained layers)");
55 let transfer_feature = QuantumTransferLearning::new(
56 pretrained.clone(),
57 new_layers.clone(),
58 TransferStrategy::FeatureExtraction,
59 )?;
60
61 // Strategy 3: Progressive unfreezing
62 println!(" c) Progressive unfreezing (unfreeze one layer every 5 epochs)");
63 let transfer_progressive = QuantumTransferLearning::new(
64 pretrained,
65 new_layers,
66 TransferStrategy::ProgressiveUnfreezing { unfreeze_rate: 5 },
67 )?;
68
69 // Step 4: Generate synthetic training data for the new task
70 println!("\n4. Generating synthetic training data...");
71 let num_samples = 50;
72 let num_features = 4;
73 let training_data = Array2::from_shape_fn((num_samples, num_features), |(i, j)| {
74 (i as f64).mul_add(0.1, j as f64 * 0.2).sin()
75 });
76 let labels = Array1::from_shape_fn(num_samples, |i| if i % 2 == 0 { 0.0 } else { 1.0 });
77
78 // Step 5: Train with fine-tuning strategy
79 println!("\n5. Training with fine-tuning strategy...");
80 let mut optimizer = Adam::new(0.01);
81
82 let result = transfer_finetune.train(
83 &training_data,
84 &labels,
85 &mut optimizer,
86 20, // epochs
87 10, // batch_size
88 )?;
89
90 println!(" Training complete!");
91 println!(" - Final loss: {:.4}", result.final_loss);
92 println!(" - Accuracy: {:.2}%", result.accuracy * 100.0);
93
94 // Step 6: Extract features using pre-trained layers
95 println!("\n6. Extracting features from pre-trained layers...");
96 let features = transfer_feature.extract_features(&training_data)?;
97 println!(" Extracted feature dimensions: {:?}", features.dim());
98
99 // Step 7: Demonstrate model zoo
100 println!("\n7. Available pre-trained models in the zoo:");
101 println!(" - Image classifier (4 qubits, MNIST subset)");
102 println!(" - Chemistry model (6 qubits, molecular energy)");
103
104 // Load chemistry model
105 let chemistry_model = QuantumModelZoo::get_chemistry_model()?;
106 println!("\n Chemistry model info:");
107 println!(" - Task: {}", chemistry_model.task_description);
108 println!(
109 " - MAE: {:.4}",
110 chemistry_model
111 .performance_metrics
112 .get("mae")
113 .unwrap_or(&0.0)
114 );
115 println!(
116 " - R² score: {:.4}",
117 chemistry_model
118 .performance_metrics
119 .get("r2_score")
120 .unwrap_or(&0.0)
121 );
122
123 println!("\n=== Transfer Learning Demo Complete ===");
124
125 Ok(())
126}Sourcepub fn predict(&self, data: &Array2<f64>) -> Result<Array1<f64>>
pub fn predict(&self, data: &Array2<f64>) -> Result<Array1<f64>>
Make predictions using the target model
Sourcepub fn extract_features(&self, data: &Array2<f64>) -> Result<Array2<f64>>
pub fn extract_features(&self, data: &Array2<f64>) -> Result<Array2<f64>>
Extract features using the pre-trained layers
Examples found in repository?
examples/transfer_learning.rs (line 96)
12fn main() -> Result<()> {
13 println!("=== Quantum Transfer Learning Demo ===\n");
14
15 // Step 1: Load a pre-trained model from the model zoo
16 println!("1. Loading pre-trained image classifier...");
17 let pretrained = QuantumModelZoo::get_image_classifier()?;
18
19 println!(" Pre-trained model info:");
20 println!(" - Task: {}", pretrained.task_description);
21 println!(
22 " - Original accuracy: {:.2}%",
23 pretrained
24 .performance_metrics
25 .get("accuracy")
26 .unwrap_or(&0.0)
27 * 100.0
28 );
29 println!(" - Number of qubits: {}", pretrained.qnn.num_qubits);
30
31 // Step 2: Create new layers for the target task
32 println!("\n2. Creating new layers for text classification task...");
33 let new_layers = vec![
34 QNNLayerType::VariationalLayer { num_params: 6 },
35 QNNLayerType::MeasurementLayer {
36 measurement_basis: "Pauli-Z".to_string(),
37 },
38 ];
39
40 // Step 3: Initialize transfer learning with different strategies
41 println!("\n3. Testing different transfer learning strategies:");
42
43 // Strategy 1: Fine-tuning
44 println!("\n a) Fine-tuning strategy (train last 2 layers only)");
45 let mut transfer_finetune = QuantumTransferLearning::new(
46 pretrained.clone(),
47 new_layers.clone(),
48 TransferStrategy::FineTuning {
49 num_trainable_layers: 2,
50 },
51 )?;
52
53 // Strategy 2: Feature extraction
54 println!(" b) Feature extraction strategy (freeze all pre-trained layers)");
55 let transfer_feature = QuantumTransferLearning::new(
56 pretrained.clone(),
57 new_layers.clone(),
58 TransferStrategy::FeatureExtraction,
59 )?;
60
61 // Strategy 3: Progressive unfreezing
62 println!(" c) Progressive unfreezing (unfreeze one layer every 5 epochs)");
63 let transfer_progressive = QuantumTransferLearning::new(
64 pretrained,
65 new_layers,
66 TransferStrategy::ProgressiveUnfreezing { unfreeze_rate: 5 },
67 )?;
68
69 // Step 4: Generate synthetic training data for the new task
70 println!("\n4. Generating synthetic training data...");
71 let num_samples = 50;
72 let num_features = 4;
73 let training_data = Array2::from_shape_fn((num_samples, num_features), |(i, j)| {
74 (i as f64).mul_add(0.1, j as f64 * 0.2).sin()
75 });
76 let labels = Array1::from_shape_fn(num_samples, |i| if i % 2 == 0 { 0.0 } else { 1.0 });
77
78 // Step 5: Train with fine-tuning strategy
79 println!("\n5. Training with fine-tuning strategy...");
80 let mut optimizer = Adam::new(0.01);
81
82 let result = transfer_finetune.train(
83 &training_data,
84 &labels,
85 &mut optimizer,
86 20, // epochs
87 10, // batch_size
88 )?;
89
90 println!(" Training complete!");
91 println!(" - Final loss: {:.4}", result.final_loss);
92 println!(" - Accuracy: {:.2}%", result.accuracy * 100.0);
93
94 // Step 6: Extract features using pre-trained layers
95 println!("\n6. Extracting features from pre-trained layers...");
96 let features = transfer_feature.extract_features(&training_data)?;
97 println!(" Extracted feature dimensions: {:?}", features.dim());
98
99 // Step 7: Demonstrate model zoo
100 println!("\n7. Available pre-trained models in the zoo:");
101 println!(" - Image classifier (4 qubits, MNIST subset)");
102 println!(" - Chemistry model (6 qubits, molecular energy)");
103
104 // Load chemistry model
105 let chemistry_model = QuantumModelZoo::get_chemistry_model()?;
106 println!("\n Chemistry model info:");
107 println!(" - Task: {}", chemistry_model.task_description);
108 println!(
109 " - MAE: {:.4}",
110 chemistry_model
111 .performance_metrics
112 .get("mae")
113 .unwrap_or(&0.0)
114 );
115 println!(
116 " - R² score: {:.4}",
117 chemistry_model
118 .performance_metrics
119 .get("r2_score")
120 .unwrap_or(&0.0)
121 );
122
123 println!("\n=== Transfer Learning Demo Complete ===");
124
125 Ok(())
126}Sourcepub fn save_model(&self, path: &str) -> Result<()>
pub fn save_model(&self, path: &str) -> Result<()>
Save the fine-tuned model
Sourcepub fn load_pretrained(path: &str) -> Result<PretrainedModel>
pub fn load_pretrained(path: &str) -> Result<PretrainedModel>
Load a pre-trained model for transfer learning
Auto Trait Implementations§
impl Freeze for QuantumTransferLearning
impl RefUnwindSafe for QuantumTransferLearning
impl Send for QuantumTransferLearning
impl Sync for QuantumTransferLearning
impl Unpin for QuantumTransferLearning
impl UnwindSafe for QuantumTransferLearning
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> 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.