pub struct QuantumLanguageModel {
pub num_qubits: usize,
pub embedding_strategy: EmbeddingStrategy,
pub preprocessor: TextPreprocessor,
pub embedding: WordEmbedding,
pub qnn: QuantumNeuralNetwork,
pub task: NLPTaskType,
pub labels: Vec<String>,
}
Expand description
Quantum language model for NLP tasks
Fields§
§num_qubits: usize
Number of qubits
embedding_strategy: EmbeddingStrategy
Embedding strategy
preprocessor: TextPreprocessor
Text preprocessor
embedding: WordEmbedding
Word embedding
qnn: QuantumNeuralNetwork
Quantum neural network
task: NLPTaskType
Type of NLP task
labels: Vec<String>
Class labels (for classification tasks)
Implementations§
Source§impl QuantumLanguageModel
impl QuantumLanguageModel
Sourcepub fn new(
num_qubits: usize,
embedding_dimension: usize,
strategy: EmbeddingStrategy,
task: NLPTaskType,
labels: Vec<String>,
) -> Result<Self>
pub fn new( num_qubits: usize, embedding_dimension: usize, strategy: EmbeddingStrategy, task: NLPTaskType, labels: Vec<String>, ) -> Result<Self>
Creates a new quantum language model
Examples found in repository?
examples/quantum_nlp.rs (lines 33-44)
23fn run_text_classification() -> Result<()> {
24 println!("\nText Classification Example");
25 println!("--------------------------");
26
27 // Create quantum language model for classification
28 let num_qubits = 6;
29 let embedding_dim = 16;
30 let embedding_strategy = EmbeddingStrategy::from(64); // Was max_seq_length before
31
32 println!("Creating quantum language model with {} qubits", num_qubits);
33 let mut model = QuantumLanguageModel::new(
34 num_qubits,
35 embedding_dim,
36 embedding_strategy,
37 NLPTaskType::Classification,
38 vec![
39 "technology".to_string(),
40 "sports".to_string(),
41 "politics".to_string(),
42 "entertainment".to_string(),
43 ],
44 )?;
45
46 // Create training data
47 println!("Preparing training data...");
48 let training_texts = vec![
49 "Latest smartphone features advanced AI capabilities".to_string(),
50 "The football team won the championship yesterday".to_string(),
51 "New legislation passed regarding climate change".to_string(),
52 "The movie premiere attracted numerous celebrities".to_string(),
53 "Software engineers developed a new programming language".to_string(),
54 "Athletes compete in the international tournament next week".to_string(),
55 "Senator announces campaign for presidential election".to_string(),
56 "Actor receives award for outstanding performance".to_string(),
57 ];
58
59 let training_labels = vec![0, 1, 2, 3, 0, 1, 2, 3];
60
61 // Build vocabulary
62 println!("Building vocabulary from training texts...");
63 let vocab_size = model.build_vocabulary(&training_texts)?;
64 println!("Vocabulary size: {}", vocab_size);
65
66 // Train embeddings
67 println!("Training word embeddings...");
68 model.train_embeddings(&training_texts)?;
69
70 // Train model
71 println!("Training quantum language model...");
72 let start = Instant::now();
73 model.train(&training_texts, &training_labels, 10, 0.05)?;
74 println!("Training completed in {:.2?}", start.elapsed());
75
76 // Test classification
77 let test_texts = [
78 "New computer processor breaks performance records",
79 "Basketball player scores winning point in final seconds",
80 "Government announces new tax policy",
81 "New series premieres with record viewership",
82 ];
83
84 println!("\nClassifying test texts:");
85 for text in &test_texts {
86 let start = Instant::now();
87 let (category, confidence) = model.classify(text)?;
88
89 println!("Text: \"{}\"", text);
90 println!(
91 "Classification: {} (confidence: {:.2})",
92 category, confidence
93 );
94 println!("Classification time: {:.2?}\n", start.elapsed());
95 }
96
97 Ok(())
98}
Source§impl QuantumLanguageModel
Implementation of missing methods for QuantumLanguageModel
impl QuantumLanguageModel
Implementation of missing methods for QuantumLanguageModel
Sourcepub fn build_vocabulary(&mut self, texts: &[String]) -> Result<usize>
pub fn build_vocabulary(&mut self, texts: &[String]) -> Result<usize>
Builds vocabulary from a set of texts
Examples found in repository?
examples/quantum_nlp.rs (line 63)
23fn run_text_classification() -> Result<()> {
24 println!("\nText Classification Example");
25 println!("--------------------------");
26
27 // Create quantum language model for classification
28 let num_qubits = 6;
29 let embedding_dim = 16;
30 let embedding_strategy = EmbeddingStrategy::from(64); // Was max_seq_length before
31
32 println!("Creating quantum language model with {} qubits", num_qubits);
33 let mut model = QuantumLanguageModel::new(
34 num_qubits,
35 embedding_dim,
36 embedding_strategy,
37 NLPTaskType::Classification,
38 vec![
39 "technology".to_string(),
40 "sports".to_string(),
41 "politics".to_string(),
42 "entertainment".to_string(),
43 ],
44 )?;
45
46 // Create training data
47 println!("Preparing training data...");
48 let training_texts = vec![
49 "Latest smartphone features advanced AI capabilities".to_string(),
50 "The football team won the championship yesterday".to_string(),
51 "New legislation passed regarding climate change".to_string(),
52 "The movie premiere attracted numerous celebrities".to_string(),
53 "Software engineers developed a new programming language".to_string(),
54 "Athletes compete in the international tournament next week".to_string(),
55 "Senator announces campaign for presidential election".to_string(),
56 "Actor receives award for outstanding performance".to_string(),
57 ];
58
59 let training_labels = vec![0, 1, 2, 3, 0, 1, 2, 3];
60
61 // Build vocabulary
62 println!("Building vocabulary from training texts...");
63 let vocab_size = model.build_vocabulary(&training_texts)?;
64 println!("Vocabulary size: {}", vocab_size);
65
66 // Train embeddings
67 println!("Training word embeddings...");
68 model.train_embeddings(&training_texts)?;
69
70 // Train model
71 println!("Training quantum language model...");
72 let start = Instant::now();
73 model.train(&training_texts, &training_labels, 10, 0.05)?;
74 println!("Training completed in {:.2?}", start.elapsed());
75
76 // Test classification
77 let test_texts = [
78 "New computer processor breaks performance records",
79 "Basketball player scores winning point in final seconds",
80 "Government announces new tax policy",
81 "New series premieres with record viewership",
82 ];
83
84 println!("\nClassifying test texts:");
85 for text in &test_texts {
86 let start = Instant::now();
87 let (category, confidence) = model.classify(text)?;
88
89 println!("Text: \"{}\"", text);
90 println!(
91 "Classification: {} (confidence: {:.2})",
92 category, confidence
93 );
94 println!("Classification time: {:.2?}\n", start.elapsed());
95 }
96
97 Ok(())
98}
Sourcepub fn train_embeddings(&mut self, texts: &[String]) -> Result<()>
pub fn train_embeddings(&mut self, texts: &[String]) -> Result<()>
Trains word embeddings
Examples found in repository?
examples/quantum_nlp.rs (line 68)
23fn run_text_classification() -> Result<()> {
24 println!("\nText Classification Example");
25 println!("--------------------------");
26
27 // Create quantum language model for classification
28 let num_qubits = 6;
29 let embedding_dim = 16;
30 let embedding_strategy = EmbeddingStrategy::from(64); // Was max_seq_length before
31
32 println!("Creating quantum language model with {} qubits", num_qubits);
33 let mut model = QuantumLanguageModel::new(
34 num_qubits,
35 embedding_dim,
36 embedding_strategy,
37 NLPTaskType::Classification,
38 vec![
39 "technology".to_string(),
40 "sports".to_string(),
41 "politics".to_string(),
42 "entertainment".to_string(),
43 ],
44 )?;
45
46 // Create training data
47 println!("Preparing training data...");
48 let training_texts = vec![
49 "Latest smartphone features advanced AI capabilities".to_string(),
50 "The football team won the championship yesterday".to_string(),
51 "New legislation passed regarding climate change".to_string(),
52 "The movie premiere attracted numerous celebrities".to_string(),
53 "Software engineers developed a new programming language".to_string(),
54 "Athletes compete in the international tournament next week".to_string(),
55 "Senator announces campaign for presidential election".to_string(),
56 "Actor receives award for outstanding performance".to_string(),
57 ];
58
59 let training_labels = vec![0, 1, 2, 3, 0, 1, 2, 3];
60
61 // Build vocabulary
62 println!("Building vocabulary from training texts...");
63 let vocab_size = model.build_vocabulary(&training_texts)?;
64 println!("Vocabulary size: {}", vocab_size);
65
66 // Train embeddings
67 println!("Training word embeddings...");
68 model.train_embeddings(&training_texts)?;
69
70 // Train model
71 println!("Training quantum language model...");
72 let start = Instant::now();
73 model.train(&training_texts, &training_labels, 10, 0.05)?;
74 println!("Training completed in {:.2?}", start.elapsed());
75
76 // Test classification
77 let test_texts = [
78 "New computer processor breaks performance records",
79 "Basketball player scores winning point in final seconds",
80 "Government announces new tax policy",
81 "New series premieres with record viewership",
82 ];
83
84 println!("\nClassifying test texts:");
85 for text in &test_texts {
86 let start = Instant::now();
87 let (category, confidence) = model.classify(text)?;
88
89 println!("Text: \"{}\"", text);
90 println!(
91 "Classification: {} (confidence: {:.2})",
92 category, confidence
93 );
94 println!("Classification time: {:.2?}\n", start.elapsed());
95 }
96
97 Ok(())
98}
Sourcepub fn train(
&mut self,
texts: &[String],
labels: &[usize],
epochs: usize,
learning_rate: f64,
) -> Result<()>
pub fn train( &mut self, texts: &[String], labels: &[usize], epochs: usize, learning_rate: f64, ) -> Result<()>
Trains the language model
Examples found in repository?
examples/quantum_nlp.rs (line 73)
23fn run_text_classification() -> Result<()> {
24 println!("\nText Classification Example");
25 println!("--------------------------");
26
27 // Create quantum language model for classification
28 let num_qubits = 6;
29 let embedding_dim = 16;
30 let embedding_strategy = EmbeddingStrategy::from(64); // Was max_seq_length before
31
32 println!("Creating quantum language model with {} qubits", num_qubits);
33 let mut model = QuantumLanguageModel::new(
34 num_qubits,
35 embedding_dim,
36 embedding_strategy,
37 NLPTaskType::Classification,
38 vec![
39 "technology".to_string(),
40 "sports".to_string(),
41 "politics".to_string(),
42 "entertainment".to_string(),
43 ],
44 )?;
45
46 // Create training data
47 println!("Preparing training data...");
48 let training_texts = vec![
49 "Latest smartphone features advanced AI capabilities".to_string(),
50 "The football team won the championship yesterday".to_string(),
51 "New legislation passed regarding climate change".to_string(),
52 "The movie premiere attracted numerous celebrities".to_string(),
53 "Software engineers developed a new programming language".to_string(),
54 "Athletes compete in the international tournament next week".to_string(),
55 "Senator announces campaign for presidential election".to_string(),
56 "Actor receives award for outstanding performance".to_string(),
57 ];
58
59 let training_labels = vec![0, 1, 2, 3, 0, 1, 2, 3];
60
61 // Build vocabulary
62 println!("Building vocabulary from training texts...");
63 let vocab_size = model.build_vocabulary(&training_texts)?;
64 println!("Vocabulary size: {}", vocab_size);
65
66 // Train embeddings
67 println!("Training word embeddings...");
68 model.train_embeddings(&training_texts)?;
69
70 // Train model
71 println!("Training quantum language model...");
72 let start = Instant::now();
73 model.train(&training_texts, &training_labels, 10, 0.05)?;
74 println!("Training completed in {:.2?}", start.elapsed());
75
76 // Test classification
77 let test_texts = [
78 "New computer processor breaks performance records",
79 "Basketball player scores winning point in final seconds",
80 "Government announces new tax policy",
81 "New series premieres with record viewership",
82 ];
83
84 println!("\nClassifying test texts:");
85 for text in &test_texts {
86 let start = Instant::now();
87 let (category, confidence) = model.classify(text)?;
88
89 println!("Text: \"{}\"", text);
90 println!(
91 "Classification: {} (confidence: {:.2})",
92 category, confidence
93 );
94 println!("Classification time: {:.2?}\n", start.elapsed());
95 }
96
97 Ok(())
98}
Sourcepub fn classify(&self, text: &str) -> Result<(String, f64)>
pub fn classify(&self, text: &str) -> Result<(String, f64)>
Classifies a text
Examples found in repository?
examples/quantum_nlp.rs (line 87)
23fn run_text_classification() -> Result<()> {
24 println!("\nText Classification Example");
25 println!("--------------------------");
26
27 // Create quantum language model for classification
28 let num_qubits = 6;
29 let embedding_dim = 16;
30 let embedding_strategy = EmbeddingStrategy::from(64); // Was max_seq_length before
31
32 println!("Creating quantum language model with {} qubits", num_qubits);
33 let mut model = QuantumLanguageModel::new(
34 num_qubits,
35 embedding_dim,
36 embedding_strategy,
37 NLPTaskType::Classification,
38 vec![
39 "technology".to_string(),
40 "sports".to_string(),
41 "politics".to_string(),
42 "entertainment".to_string(),
43 ],
44 )?;
45
46 // Create training data
47 println!("Preparing training data...");
48 let training_texts = vec![
49 "Latest smartphone features advanced AI capabilities".to_string(),
50 "The football team won the championship yesterday".to_string(),
51 "New legislation passed regarding climate change".to_string(),
52 "The movie premiere attracted numerous celebrities".to_string(),
53 "Software engineers developed a new programming language".to_string(),
54 "Athletes compete in the international tournament next week".to_string(),
55 "Senator announces campaign for presidential election".to_string(),
56 "Actor receives award for outstanding performance".to_string(),
57 ];
58
59 let training_labels = vec![0, 1, 2, 3, 0, 1, 2, 3];
60
61 // Build vocabulary
62 println!("Building vocabulary from training texts...");
63 let vocab_size = model.build_vocabulary(&training_texts)?;
64 println!("Vocabulary size: {}", vocab_size);
65
66 // Train embeddings
67 println!("Training word embeddings...");
68 model.train_embeddings(&training_texts)?;
69
70 // Train model
71 println!("Training quantum language model...");
72 let start = Instant::now();
73 model.train(&training_texts, &training_labels, 10, 0.05)?;
74 println!("Training completed in {:.2?}", start.elapsed());
75
76 // Test classification
77 let test_texts = [
78 "New computer processor breaks performance records",
79 "Basketball player scores winning point in final seconds",
80 "Government announces new tax policy",
81 "New series premieres with record viewership",
82 ];
83
84 println!("\nClassifying test texts:");
85 for text in &test_texts {
86 let start = Instant::now();
87 let (category, confidence) = model.classify(text)?;
88
89 println!("Text: \"{}\"", text);
90 println!(
91 "Classification: {} (confidence: {:.2})",
92 category, confidence
93 );
94 println!("Classification time: {:.2?}\n", start.elapsed());
95 }
96
97 Ok(())
98}
Trait Implementations§
Source§impl Clone for QuantumLanguageModel
impl Clone for QuantumLanguageModel
Source§fn clone(&self) -> QuantumLanguageModel
fn clone(&self) -> QuantumLanguageModel
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 QuantumLanguageModel
impl RefUnwindSafe for QuantumLanguageModel
impl Send for QuantumLanguageModel
impl Sync for QuantumLanguageModel
impl Unpin for QuantumLanguageModel
impl UnwindSafe for QuantumLanguageModel
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.