pub struct QuantumDDPG { /* private fields */ }Expand description
Quantum Deep Deterministic Policy Gradient (QDDPG)
Implementations§
Source§impl QuantumDDPG
impl QuantumDDPG
Sourcepub fn new(
state_dim: usize,
action_dim: usize,
action_bounds: Vec<(f64, f64)>,
num_qubits: usize,
buffer_capacity: usize,
) -> Result<Self>
pub fn new( state_dim: usize, action_dim: usize, action_bounds: Vec<(f64, f64)>, num_qubits: usize, buffer_capacity: usize, ) -> Result<Self>
Create new QDDPG agent
Examples found in repository?
examples/continuous_rl.rs (lines 77-83)
69fn train_qddpg_pendulum() -> Result<()> {
70 let state_dim = 3;
71 let action_dim = 1;
72 let action_bounds = vec![(-2.0, 2.0)];
73 let num_qubits = 4;
74 let buffer_capacity = 10000;
75
76 // Create QDDPG agent
77 let mut agent = QuantumDDPG::new(
78 state_dim,
79 action_dim,
80 action_bounds,
81 num_qubits,
82 buffer_capacity,
83 )?;
84
85 // Create environment
86 let mut env = PendulumEnvironment::new();
87
88 // Create optimizers
89 let mut actor_optimizer = Adam::new(0.001);
90 let mut critic_optimizer = Adam::new(0.001);
91
92 // Train for a few episodes (reduced for demo)
93 let episodes = 50;
94 println!(" Training QDDPG for {episodes} episodes...");
95
96 let rewards = agent.train(
97 &mut env,
98 episodes,
99 &mut actor_optimizer,
100 &mut critic_optimizer,
101 )?;
102
103 // Print training statistics
104 let avg_initial = rewards[..10].iter().sum::<f64>() / 10.0;
105 let avg_final = rewards[rewards.len() - 10..].iter().sum::<f64>() / 10.0;
106
107 println!("\n Training Statistics:");
108 println!(" - Average initial reward: {avg_initial:.2}");
109 println!(" - Average final reward: {avg_final:.2}");
110 println!(" - Improvement: {:.2}", avg_final - avg_initial);
111
112 // Test trained agent
113 println!("\n Testing trained agent...");
114 test_trained_agent(&agent, &mut env)?;
115
116 Ok(())
117}Sourcepub fn get_action(
&self,
state: &Array1<f64>,
training: bool,
) -> Result<Array1<f64>>
pub fn get_action( &self, state: &Array1<f64>, training: bool, ) -> Result<Array1<f64>>
Get action for state
Examples found in repository?
examples/continuous_rl.rs (line 131)
120fn test_trained_agent(agent: &QuantumDDPG, env: &mut dyn ContinuousEnvironment) -> Result<()> {
121 let test_episodes = 5;
122 let mut test_rewards = Vec::new();
123
124 for episode in 0..test_episodes {
125 let mut state = env.reset();
126 let mut episode_reward = 0.0;
127 let mut done = false;
128 let mut steps = 0;
129
130 while !done && steps < 200 {
131 let action = agent.get_action(&state, false)?; // No exploration
132 let (next_state, reward, is_done) = env.step(action.clone())?;
133
134 state = next_state;
135 episode_reward += reward;
136 done = is_done;
137 steps += 1;
138 }
139
140 test_rewards.push(episode_reward);
141 println!(
142 " Test episode {}: Reward = {:.2}, Steps = {}",
143 episode + 1,
144 episode_reward,
145 steps
146 );
147 }
148
149 let avg_test = test_rewards.iter().sum::<f64>() / f64::from(test_episodes);
150 println!(" Average test reward: {avg_test:.2}");
151
152 Ok(())
153}Sourcepub fn store_experience(&mut self, exp: Experience)
pub fn store_experience(&mut self, exp: Experience)
Store experience in replay buffer
Sourcepub fn update(
&mut self,
actor_optimizer: &mut dyn Optimizer,
critic_optimizer: &mut dyn Optimizer,
) -> Result<()>
pub fn update( &mut self, actor_optimizer: &mut dyn Optimizer, critic_optimizer: &mut dyn Optimizer, ) -> Result<()>
Update networks
Sourcepub fn train(
&mut self,
env: &mut dyn ContinuousEnvironment,
episodes: usize,
actor_optimizer: &mut dyn Optimizer,
critic_optimizer: &mut dyn Optimizer,
) -> Result<Vec<f64>>
pub fn train( &mut self, env: &mut dyn ContinuousEnvironment, episodes: usize, actor_optimizer: &mut dyn Optimizer, critic_optimizer: &mut dyn Optimizer, ) -> Result<Vec<f64>>
Train on environment
Examples found in repository?
examples/continuous_rl.rs (lines 96-101)
69fn train_qddpg_pendulum() -> Result<()> {
70 let state_dim = 3;
71 let action_dim = 1;
72 let action_bounds = vec![(-2.0, 2.0)];
73 let num_qubits = 4;
74 let buffer_capacity = 10000;
75
76 // Create QDDPG agent
77 let mut agent = QuantumDDPG::new(
78 state_dim,
79 action_dim,
80 action_bounds,
81 num_qubits,
82 buffer_capacity,
83 )?;
84
85 // Create environment
86 let mut env = PendulumEnvironment::new();
87
88 // Create optimizers
89 let mut actor_optimizer = Adam::new(0.001);
90 let mut critic_optimizer = Adam::new(0.001);
91
92 // Train for a few episodes (reduced for demo)
93 let episodes = 50;
94 println!(" Training QDDPG for {episodes} episodes...");
95
96 let rewards = agent.train(
97 &mut env,
98 episodes,
99 &mut actor_optimizer,
100 &mut critic_optimizer,
101 )?;
102
103 // Print training statistics
104 let avg_initial = rewards[..10].iter().sum::<f64>() / 10.0;
105 let avg_final = rewards[rewards.len() - 10..].iter().sum::<f64>() / 10.0;
106
107 println!("\n Training Statistics:");
108 println!(" - Average initial reward: {avg_initial:.2}");
109 println!(" - Average final reward: {avg_final:.2}");
110 println!(" - Improvement: {:.2}", avg_final - avg_initial);
111
112 // Test trained agent
113 println!("\n Testing trained agent...");
114 test_trained_agent(&agent, &mut env)?;
115
116 Ok(())
117}Auto Trait Implementations§
impl Freeze for QuantumDDPG
impl RefUnwindSafe for QuantumDDPG
impl Send for QuantumDDPG
impl Sync for QuantumDDPG
impl Unpin for QuantumDDPG
impl UnwindSafe for QuantumDDPG
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.