QuantumModelZoo

Struct QuantumModelZoo 

Source
pub struct QuantumModelZoo;
Expand description

Model zoo for pre-trained quantum models

Implementations§

Source§

impl QuantumModelZoo

Source

pub fn get_image_classifier() -> Result<PretrainedModel>

Get a pre-trained model for image classification

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

pub fn get_chemistry_model() -> Result<PretrainedModel>

Get a pre-trained model for quantum chemistry

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

pub fn vqe_feature_extractor(n_qubits: usize) -> Result<PretrainedModel>

Get a VQE feature extractor model

Source

pub fn qaoa_classifier( n_qubits: usize, n_layers: usize, ) -> Result<PretrainedModel>

Get a QAOA classifier model

Source

pub fn quantum_autoencoder( n_qubits: usize, latent_dim: usize, ) -> Result<PretrainedModel>

Get a quantum autoencoder model

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V