pub struct QuantumPositionEncoding { /* private fields */ }Expand description
Quantum position encoding module
Implementations§
Source§impl QuantumPositionEncoding
impl QuantumPositionEncoding
Sourcepub fn new(
encoding_type: PositionEncodingType,
model_dim: usize,
max_seq_len: usize,
num_qubits: usize,
) -> Result<Self>
pub fn new( encoding_type: PositionEncodingType, model_dim: usize, max_seq_len: usize, num_qubits: usize, ) -> Result<Self>
Create new quantum position encoding
Examples found in repository?
examples/quantum_transformer.rs (line 211)
192fn position_encoding_demo() -> Result<()> {
193 println!(" Testing quantum position encoding variants...");
194
195 let encoding_types = vec![
196 ("Sinusoidal", PositionEncodingType::Sinusoidal),
197 ("Quantum Phase", PositionEncodingType::QuantumPhase),
198 ("Learnable Quantum", PositionEncodingType::LearnableQuantum),
199 ("Relative", PositionEncodingType::Relative),
200 ("Rotary (RoPE)", PositionEncodingType::Rotary),
201 ];
202
203 let model_dim = 128;
204 let max_seq_len = 64;
205 let num_qubits = 8;
206
207 for (name, encoding_type) in encoding_types {
208 println!("\n --- {} Position Encoding ---", name);
209
210 let pos_enc =
211 QuantumPositionEncoding::new(encoding_type, model_dim, max_seq_len, num_qubits)?;
212
213 let batch_size = 3;
214 let seq_len = 32;
215
216 let encodings = pos_enc.forward(seq_len, batch_size)?;
217 println!(" Encoding shape: {:?}", encodings.dim());
218
219 // Analyze position encoding properties
220 let encoding_range = {
221 let min_val = encodings.iter().cloned().fold(f64::INFINITY, f64::min);
222 let max_val = encodings.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
223 max_val - min_val
224 };
225
226 println!(" Value range: {:.4}", encoding_range);
227
228 // Check position distinguishability
229 let pos1 = encodings.slice(ndarray::s![0, 0, ..]).to_owned();
230 let pos2 = encodings.slice(ndarray::s![0, seq_len - 1, ..]).to_owned();
231 let position_distance = (&pos1 - &pos2).mapv(|x| x * x).sum().sqrt();
232
233 println!(
234 " Distance between first and last position: {:.4}",
235 position_distance
236 );
237
238 // Analyze periodicity for sinusoidal encodings
239 if name == "Sinusoidal" {
240 let mut periodicities = Vec::new();
241 for d in (0..model_dim).step_by(10) {
242 let values: Vec<f64> = (0..seq_len).map(|s| encodings[[0, s, d]]).collect();
243
244 // Simple periodicity check
245 let period = find_period(&values);
246 if period > 0 {
247 periodicities.push(period);
248 }
249 }
250
251 if !periodicities.is_empty() {
252 let avg_period =
253 periodicities.iter().sum::<usize>() as f64 / periodicities.len() as f64;
254 println!(" Average period length: {:.1}", avg_period);
255 }
256 }
257
258 // Check quantum phase encoding properties
259 if name == "Quantum Phase" {
260 let phase_variance = encodings.var(0.0);
261 println!(" Phase encoding variance: {:.4}", phase_variance);
262 }
263 }
264
265 Ok(())
266}Sourcepub fn forward(&self, seq_len: usize, batch_size: usize) -> Result<Array3<f64>>
pub fn forward(&self, seq_len: usize, batch_size: usize) -> Result<Array3<f64>>
Generate position encodings for input sequence
Examples found in repository?
examples/quantum_transformer.rs (line 216)
192fn position_encoding_demo() -> Result<()> {
193 println!(" Testing quantum position encoding variants...");
194
195 let encoding_types = vec![
196 ("Sinusoidal", PositionEncodingType::Sinusoidal),
197 ("Quantum Phase", PositionEncodingType::QuantumPhase),
198 ("Learnable Quantum", PositionEncodingType::LearnableQuantum),
199 ("Relative", PositionEncodingType::Relative),
200 ("Rotary (RoPE)", PositionEncodingType::Rotary),
201 ];
202
203 let model_dim = 128;
204 let max_seq_len = 64;
205 let num_qubits = 8;
206
207 for (name, encoding_type) in encoding_types {
208 println!("\n --- {} Position Encoding ---", name);
209
210 let pos_enc =
211 QuantumPositionEncoding::new(encoding_type, model_dim, max_seq_len, num_qubits)?;
212
213 let batch_size = 3;
214 let seq_len = 32;
215
216 let encodings = pos_enc.forward(seq_len, batch_size)?;
217 println!(" Encoding shape: {:?}", encodings.dim());
218
219 // Analyze position encoding properties
220 let encoding_range = {
221 let min_val = encodings.iter().cloned().fold(f64::INFINITY, f64::min);
222 let max_val = encodings.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
223 max_val - min_val
224 };
225
226 println!(" Value range: {:.4}", encoding_range);
227
228 // Check position distinguishability
229 let pos1 = encodings.slice(ndarray::s![0, 0, ..]).to_owned();
230 let pos2 = encodings.slice(ndarray::s![0, seq_len - 1, ..]).to_owned();
231 let position_distance = (&pos1 - &pos2).mapv(|x| x * x).sum().sqrt();
232
233 println!(
234 " Distance between first and last position: {:.4}",
235 position_distance
236 );
237
238 // Analyze periodicity for sinusoidal encodings
239 if name == "Sinusoidal" {
240 let mut periodicities = Vec::new();
241 for d in (0..model_dim).step_by(10) {
242 let values: Vec<f64> = (0..seq_len).map(|s| encodings[[0, s, d]]).collect();
243
244 // Simple periodicity check
245 let period = find_period(&values);
246 if period > 0 {
247 periodicities.push(period);
248 }
249 }
250
251 if !periodicities.is_empty() {
252 let avg_period =
253 periodicities.iter().sum::<usize>() as f64 / periodicities.len() as f64;
254 println!(" Average period length: {:.1}", avg_period);
255 }
256 }
257
258 // Check quantum phase encoding properties
259 if name == "Quantum Phase" {
260 let phase_variance = encodings.var(0.0);
261 println!(" Phase encoding variance: {:.4}", phase_variance);
262 }
263 }
264
265 Ok(())
266}Trait Implementations§
Source§impl Clone for QuantumPositionEncoding
impl Clone for QuantumPositionEncoding
Source§fn clone(&self) -> QuantumPositionEncoding
fn clone(&self) -> QuantumPositionEncoding
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 QuantumPositionEncoding
impl !RefUnwindSafe for QuantumPositionEncoding
impl Send for QuantumPositionEncoding
impl Sync for QuantumPositionEncoding
impl Unpin for QuantumPositionEncoding
impl !UnwindSafe for QuantumPositionEncoding
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.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.