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 85-91)
77fn train_qddpg_pendulum() -> Result<()> {
78 let state_dim = 3;
79 let action_dim = 1;
80 let action_bounds = vec![(-2.0, 2.0)];
81 let num_qubits = 4;
82 let buffer_capacity = 10000;
83
84 // Create QDDPG agent
85 let mut agent = QuantumDDPG::new(
86 state_dim,
87 action_dim,
88 action_bounds,
89 num_qubits,
90 buffer_capacity,
91 )?;
92
93 // Create environment
94 let mut env = PendulumEnvironment::new();
95
96 // Create optimizers
97 let mut actor_optimizer = Adam::new(0.001);
98 let mut critic_optimizer = Adam::new(0.001);
99
100 // Train for a few episodes (reduced for demo)
101 let episodes = 50;
102 println!(" Training QDDPG for {episodes} episodes...");
103
104 let rewards = agent.train(
105 &mut env,
106 episodes,
107 &mut actor_optimizer,
108 &mut critic_optimizer,
109 )?;
110
111 // Print training statistics
112 let avg_initial = rewards[..10].iter().sum::<f64>() / 10.0;
113 let avg_final = rewards[rewards.len() - 10..].iter().sum::<f64>() / 10.0;
114
115 println!("\n Training Statistics:");
116 println!(" - Average initial reward: {avg_initial:.2}");
117 println!(" - Average final reward: {avg_final:.2}");
118 println!(" - Improvement: {:.2}", avg_final - avg_initial);
119
120 // Test trained agent
121 println!("\n Testing trained agent...");
122 test_trained_agent(&agent, &mut env)?;
123
124 Ok(())
125}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 139)
128fn test_trained_agent(agent: &QuantumDDPG, env: &mut dyn ContinuousEnvironment) -> Result<()> {
129 let test_episodes = 5;
130 let mut test_rewards = Vec::new();
131
132 for episode in 0..test_episodes {
133 let mut state = env.reset();
134 let mut episode_reward = 0.0;
135 let mut done = false;
136 let mut steps = 0;
137
138 while !done && steps < 200 {
139 let action = agent.get_action(&state, false)?; // No exploration
140 let (next_state, reward, is_done) = env.step(action.clone())?;
141
142 state = next_state;
143 episode_reward += reward;
144 done = is_done;
145 steps += 1;
146 }
147
148 test_rewards.push(episode_reward);
149 println!(
150 " Test episode {}: Reward = {:.2}, Steps = {}",
151 episode + 1,
152 episode_reward,
153 steps
154 );
155 }
156
157 let avg_test = test_rewards.iter().sum::<f64>() / f64::from(test_episodes);
158 println!(" Average test reward: {avg_test:.2}");
159
160 Ok(())
161}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 104-109)
77fn train_qddpg_pendulum() -> Result<()> {
78 let state_dim = 3;
79 let action_dim = 1;
80 let action_bounds = vec![(-2.0, 2.0)];
81 let num_qubits = 4;
82 let buffer_capacity = 10000;
83
84 // Create QDDPG agent
85 let mut agent = QuantumDDPG::new(
86 state_dim,
87 action_dim,
88 action_bounds,
89 num_qubits,
90 buffer_capacity,
91 )?;
92
93 // Create environment
94 let mut env = PendulumEnvironment::new();
95
96 // Create optimizers
97 let mut actor_optimizer = Adam::new(0.001);
98 let mut critic_optimizer = Adam::new(0.001);
99
100 // Train for a few episodes (reduced for demo)
101 let episodes = 50;
102 println!(" Training QDDPG for {episodes} episodes...");
103
104 let rewards = agent.train(
105 &mut env,
106 episodes,
107 &mut actor_optimizer,
108 &mut critic_optimizer,
109 )?;
110
111 // Print training statistics
112 let avg_initial = rewards[..10].iter().sum::<f64>() / 10.0;
113 let avg_final = rewards[rewards.len() - 10..].iter().sum::<f64>() / 10.0;
114
115 println!("\n Training Statistics:");
116 println!(" - Average initial reward: {avg_initial:.2}");
117 println!(" - Average final reward: {avg_final:.2}");
118 println!(" - Improvement: {:.2}", avg_final - avg_initial);
119
120 // Test trained agent
121 println!("\n Testing trained agent...");
122 test_trained_agent(&agent, &mut env)?;
123
124 Ok(())
125}Auto Trait Implementations§
impl Freeze for QuantumDDPG
impl RefUnwindSafe for QuantumDDPG
impl Send for QuantumDDPG
impl Sync for QuantumDDPG
impl Unpin for QuantumDDPG
impl UnsafeUnpin 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.