quantrs2-ml 0.1.3

Quantum Machine Learning module for QuantRS2
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! Quantum Reinforcement Learning with Continuous Actions
//!
//! This module extends quantum reinforcement learning to support continuous action spaces,
//! implementing algorithms like DDPG, TD3, and SAC adapted for quantum circuits.

use crate::autodiff::optimizers::Optimizer;
use crate::error::{MLError, Result};
use crate::optimization::OptimizationMethod;
use crate::qnn::{QNNLayerType, QuantumNeuralNetwork};
use quantrs2_circuit::builder::{Circuit, Simulator};
use quantrs2_core::gate::{
    single::{RotationX, RotationY, RotationZ},
    GateOp,
};
use quantrs2_sim::statevector::StateVectorSimulator;
use scirs2_core::ndarray::{Array1, Array2, ArrayView1};
use scirs2_core::random::prelude::*;
use std::collections::{HashMap, VecDeque};
use std::f64::consts::PI;

/// Continuous action environment trait
pub trait ContinuousEnvironment {
    /// Gets the current state
    fn state(&self) -> Array1<f64>;

    /// Gets the action space bounds (min, max) for each dimension
    fn action_bounds(&self) -> Vec<(f64, f64)>;

    /// Takes a continuous action and returns reward and next state
    fn step(&mut self, action: Array1<f64>) -> Result<(Array1<f64>, f64, bool)>;

    /// Resets the environment
    fn reset(&mut self) -> Array1<f64>;

    /// Get state dimension
    fn state_dim(&self) -> usize;

    /// Get action dimension
    fn action_dim(&self) -> usize;
}

/// Experience replay buffer for continuous RL
#[derive(Debug, Clone)]
pub struct ReplayBuffer {
    /// Maximum buffer size
    capacity: usize,

    /// Buffer storage
    buffer: VecDeque<Experience>,
}

/// Single experience tuple
#[derive(Debug, Clone)]
pub struct Experience {
    pub state: Array1<f64>,
    pub action: Array1<f64>,
    pub reward: f64,
    pub next_state: Array1<f64>,
    pub done: bool,
}

impl ReplayBuffer {
    /// Create new replay buffer
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity,
            buffer: VecDeque::with_capacity(capacity),
        }
    }

    /// Add experience to buffer
    pub fn push(&mut self, exp: Experience) {
        if self.buffer.len() >= self.capacity {
            self.buffer.pop_front();
        }
        self.buffer.push_back(exp);
    }

    /// Sample batch from buffer
    pub fn sample(&self, batch_size: usize) -> Result<Vec<Experience>> {
        if self.buffer.len() < batch_size {
            return Err(MLError::ModelCreationError(
                "Not enough experiences in buffer".to_string(),
            ));
        }

        let mut batch = Vec::new();
        let mut rng = thread_rng();

        for _ in 0..batch_size {
            let idx = rng.random_range(0..self.buffer.len());
            batch.push(self.buffer[idx].clone());
        }

        Ok(batch)
    }

    /// Get buffer size
    pub fn len(&self) -> usize {
        self.buffer.len()
    }
}

/// Quantum actor network for continuous actions
pub struct QuantumActor {
    /// Quantum neural network
    qnn: QuantumNeuralNetwork,

    /// Action bounds
    action_bounds: Vec<(f64, f64)>,

    /// State dimension
    state_dim: usize,

    /// Action dimension
    action_dim: usize,
}

impl QuantumActor {
    /// Create new quantum actor
    pub fn new(
        state_dim: usize,
        action_dim: usize,
        action_bounds: Vec<(f64, f64)>,
        num_qubits: usize,
    ) -> Result<Self> {
        let layers = vec![
            QNNLayerType::EncodingLayer {
                num_features: state_dim,
            },
            QNNLayerType::VariationalLayer {
                num_params: num_qubits * 3,
            },
            QNNLayerType::EntanglementLayer {
                connectivity: "circular".to_string(),
            },
            QNNLayerType::VariationalLayer {
                num_params: num_qubits * 3,
            },
            QNNLayerType::MeasurementLayer {
                measurement_basis: "Pauli-Z".to_string(),
            },
        ];

        let qnn = QuantumNeuralNetwork::new(layers, num_qubits, state_dim, action_dim)?;

        Ok(Self {
            qnn,
            action_bounds,
            state_dim,
            action_dim,
        })
    }

    /// Get action from state
    pub fn get_action(&self, state: &Array1<f64>, add_noise: bool) -> Result<Array1<f64>> {
        // Placeholder - would use quantum circuit to generate actions
        let raw_actions = self.extract_continuous_actions_placeholder()?;

        // Apply bounds and noise
        let mut actions = Array1::zeros(self.action_dim);
        for i in 0..self.action_dim {
            let (min_val, max_val) = self.action_bounds[i];

            // Map quantum output to action range
            actions[i] = min_val + (max_val - min_val) * (raw_actions[i] + 1.0) / 2.0;

            // Add exploration noise if requested
            if add_noise {
                let noise = 0.1 * (max_val - min_val) * (2.0 * thread_rng().random::<f64>() - 1.0);
                actions[i] = (actions[i] + noise).clamp(min_val, max_val);
            }
        }

        Ok(actions)
    }

    /// Extract continuous actions from quantum state (placeholder)
    fn extract_continuous_actions_placeholder(&self) -> Result<Array1<f64>> {
        // Placeholder - would measure expectation values
        let mut actions = Array1::zeros(self.action_dim);

        for i in 0..self.action_dim {
            // Simulate measurement of Pauli-Z on different qubits
            actions[i] = 2.0 * thread_rng().random::<f64>() - 1.0; // [-1, 1]
        }

        Ok(actions)
    }
}

/// Quantum critic network for value estimation
pub struct QuantumCritic {
    /// Quantum neural network
    qnn: QuantumNeuralNetwork,

    /// Input dimension (state + action)
    input_dim: usize,
}

impl QuantumCritic {
    /// Create new quantum critic
    pub fn new(state_dim: usize, action_dim: usize, num_qubits: usize) -> Result<Self> {
        let input_dim = state_dim + action_dim;

        let layers = vec![
            QNNLayerType::EncodingLayer {
                num_features: input_dim,
            },
            QNNLayerType::VariationalLayer {
                num_params: num_qubits * 3,
            },
            QNNLayerType::EntanglementLayer {
                connectivity: "full".to_string(),
            },
            QNNLayerType::VariationalLayer {
                num_params: num_qubits * 3,
            },
            QNNLayerType::MeasurementLayer {
                measurement_basis: "computational".to_string(),
            },
        ];

        let qnn = QuantumNeuralNetwork::new(
            layers, num_qubits, input_dim, 1, // Q-value output
        )?;

        Ok(Self { qnn, input_dim })
    }

    /// Estimate Q-value for state-action pair
    pub fn get_q_value(&self, state: &Array1<f64>, action: &Array1<f64>) -> Result<f64> {
        // Concatenate state and action
        let mut input = Array1::zeros(self.input_dim);
        for i in 0..state.len() {
            input[i] = state[i];
        }
        for i in 0..action.len() {
            input[state.len() + i] = action[i];
        }

        // Placeholder - would use quantum circuit to estimate Q-value
        Ok(0.5 + 0.5 * (2.0 * thread_rng().random::<f64>() - 1.0))
    }
}

/// Quantum Deep Deterministic Policy Gradient (QDDPG)
pub struct QuantumDDPG {
    /// Actor network
    actor: QuantumActor,

    /// Critic network
    critic: QuantumCritic,

    /// Target actor network
    target_actor: QuantumActor,

    /// Target critic network
    target_critic: QuantumCritic,

    /// Replay buffer
    replay_buffer: ReplayBuffer,

    /// Discount factor
    gamma: f64,

    /// Soft update coefficient
    tau: f64,

    /// Batch size
    batch_size: usize,
}

impl QuantumDDPG {
    /// Create new QDDPG agent
    pub fn new(
        state_dim: usize,
        action_dim: usize,
        action_bounds: Vec<(f64, f64)>,
        num_qubits: usize,
        buffer_capacity: usize,
    ) -> Result<Self> {
        let actor = QuantumActor::new(state_dim, action_dim, action_bounds.clone(), num_qubits)?;
        let critic = QuantumCritic::new(state_dim, action_dim, num_qubits)?;

        // Clone for target networks
        let target_actor = QuantumActor::new(state_dim, action_dim, action_bounds, num_qubits)?;
        let target_critic = QuantumCritic::new(state_dim, action_dim, num_qubits)?;

        Ok(Self {
            actor,
            critic,
            target_actor,
            target_critic,
            replay_buffer: ReplayBuffer::new(buffer_capacity),
            gamma: 0.99,
            tau: 0.005,
            batch_size: 64,
        })
    }

    /// Get action for state
    pub fn get_action(&self, state: &Array1<f64>, training: bool) -> Result<Array1<f64>> {
        self.actor.get_action(state, training)
    }

    /// Store experience in replay buffer
    pub fn store_experience(&mut self, exp: Experience) {
        self.replay_buffer.push(exp);
    }

    /// Update networks
    pub fn update(
        &mut self,
        actor_optimizer: &mut dyn Optimizer,
        critic_optimizer: &mut dyn Optimizer,
    ) -> Result<()> {
        if self.replay_buffer.len() < self.batch_size {
            return Ok(());
        }

        // Sample batch
        let batch = self.replay_buffer.sample(self.batch_size)?;

        // Update critic
        self.update_critic(&batch, critic_optimizer)?;

        // Update actor
        self.update_actor(&batch, actor_optimizer)?;

        // Soft update target networks
        self.soft_update()?;

        Ok(())
    }

    /// Update critic network
    fn update_critic(&mut self, batch: &[Experience], optimizer: &mut dyn Optimizer) -> Result<()> {
        // Compute target Q-values
        let mut target_q_values = Vec::new();

        for exp in batch {
            let target_action = self.target_actor.get_action(&exp.next_state, false)?;
            let target_q = self
                .target_critic
                .get_q_value(&exp.next_state, &target_action)?;
            let y = exp.reward + if exp.done { 0.0 } else { self.gamma * target_q };
            target_q_values.push(y);
        }

        // Placeholder - would compute loss and update parameters

        Ok(())
    }

    /// Update actor network
    fn update_actor(&mut self, batch: &[Experience], optimizer: &mut dyn Optimizer) -> Result<()> {
        // Compute policy gradient
        let mut policy_loss = 0.0;

        for exp in batch {
            let action = self.actor.get_action(&exp.state, false)?;
            let q_value = self.critic.get_q_value(&exp.state, &action)?;
            policy_loss -= q_value; // Maximize Q-value
        }

        policy_loss /= batch.len() as f64;

        // Placeholder - would compute gradients and update

        Ok(())
    }

    /// Soft update target networks
    fn soft_update(&mut self) -> Result<()> {
        // Update target actor parameters
        for i in 0..self.actor.qnn.parameters.len() {
            self.target_actor.qnn.parameters[i] = self.tau * self.actor.qnn.parameters[i]
                + (1.0 - self.tau) * self.target_actor.qnn.parameters[i];
        }

        // Update target critic parameters
        for i in 0..self.critic.qnn.parameters.len() {
            self.target_critic.qnn.parameters[i] = self.tau * self.critic.qnn.parameters[i]
                + (1.0 - self.tau) * self.target_critic.qnn.parameters[i];
        }

        Ok(())
    }

    /// Train on environment
    pub fn train(
        &mut self,
        env: &mut dyn ContinuousEnvironment,
        episodes: usize,
        actor_optimizer: &mut dyn Optimizer,
        critic_optimizer: &mut dyn Optimizer,
    ) -> Result<Vec<f64>> {
        let mut episode_rewards = Vec::new();

        for episode in 0..episodes {
            let mut state = env.reset();
            let mut episode_reward = 0.0;
            let mut done = false;

            while !done {
                // Get action
                let action = self.get_action(&state, true)?;

                // Step environment
                let (next_state, reward, is_done) = env.step(action.clone())?;

                // Store experience
                self.store_experience(Experience {
                    state: state.clone(),
                    action,
                    reward,
                    next_state: next_state.clone(),
                    done: is_done,
                });

                // Update networks
                self.update(actor_optimizer, critic_optimizer)?;

                state = next_state;
                episode_reward += reward;
                done = is_done;
            }

            episode_rewards.push(episode_reward);

            if episode % 10 == 0 {
                println!("Episode {}: Reward = {:.2}", episode, episode_reward);
            }
        }

        Ok(episode_rewards)
    }
}

/// Quantum Soft Actor-Critic (QSAC)
pub struct QuantumSAC {
    /// Actor network
    actor: QuantumActor,

    /// Two Q-networks for stability
    q1: QuantumCritic,
    q2: QuantumCritic,

    /// Target Q-networks
    target_q1: QuantumCritic,
    target_q2: QuantumCritic,

    /// Temperature parameter for entropy
    alpha: f64,

    /// Replay buffer
    replay_buffer: ReplayBuffer,

    /// Hyperparameters
    gamma: f64,
    tau: f64,
    batch_size: usize,
}

impl QuantumSAC {
    /// Create new QSAC agent
    pub fn new(
        state_dim: usize,
        action_dim: usize,
        action_bounds: Vec<(f64, f64)>,
        num_qubits: usize,
        buffer_capacity: usize,
    ) -> Result<Self> {
        let actor = QuantumActor::new(state_dim, action_dim, action_bounds, num_qubits)?;

        let q1 = QuantumCritic::new(state_dim, action_dim, num_qubits)?;
        let q2 = QuantumCritic::new(state_dim, action_dim, num_qubits)?;

        let target_q1 = QuantumCritic::new(state_dim, action_dim, num_qubits)?;
        let target_q2 = QuantumCritic::new(state_dim, action_dim, num_qubits)?;

        Ok(Self {
            actor,
            q1,
            q2,
            target_q1,
            target_q2,
            alpha: 0.2,
            replay_buffer: ReplayBuffer::new(buffer_capacity),
            gamma: 0.99,
            tau: 0.005,
            batch_size: 64,
        })
    }

    /// Get action with entropy regularization
    pub fn get_action(&self, state: &Array1<f64>, training: bool) -> Result<Array1<f64>> {
        // SAC uses stochastic policy even during evaluation
        self.actor.get_action(state, true)
    }

    /// Compute log probability of action (for entropy)
    fn log_prob(&self, state: &Array1<f64>, action: &Array1<f64>) -> Result<f64> {
        // Placeholder - would compute actual log probability
        Ok(-0.5 * action.mapv(|a| a * a).sum())
    }
}

/// Pendulum environment for continuous control
pub struct PendulumEnvironment {
    /// Angle (radians)
    theta: f64,

    /// Angular velocity
    theta_dot: f64,

    /// Time step
    dt: f64,

    /// Maximum steps per episode
    max_steps: usize,

    /// Current step
    current_step: usize,
}

impl PendulumEnvironment {
    /// Create new pendulum environment
    pub fn new() -> Self {
        Self {
            theta: 0.0,
            theta_dot: 0.0,
            dt: 0.05,
            max_steps: 200,
            current_step: 0,
        }
    }
}

impl ContinuousEnvironment for PendulumEnvironment {
    fn state(&self) -> Array1<f64> {
        Array1::from_vec(vec![self.theta.cos(), self.theta.sin(), self.theta_dot])
    }

    fn action_bounds(&self) -> Vec<(f64, f64)> {
        vec![(-2.0, 2.0)] // Torque bounds
    }

    fn step(&mut self, action: Array1<f64>) -> Result<(Array1<f64>, f64, bool)> {
        let torque = action[0].clamp(-2.0, 2.0);

        // Physics simulation
        let g = 10.0;
        let m = 1.0;
        let l = 1.0;

        // Update dynamics
        let theta_acc = -3.0 * g / (2.0 * l) * self.theta.sin() + 3.0 * torque / (m * l * l);
        self.theta_dot += theta_acc * self.dt;
        self.theta_dot = self.theta_dot.clamp(-8.0, 8.0);
        self.theta += self.theta_dot * self.dt;

        // Normalize angle to [-pi, pi]
        self.theta = ((self.theta + PI) % (2.0 * PI)) - PI;

        // Compute reward (penalize angle and velocity)
        let reward = -(self.theta.powi(2) + 0.1 * self.theta_dot.powi(2) + 0.001 * torque.powi(2));

        self.current_step += 1;
        let done = self.current_step >= self.max_steps;

        Ok((self.state(), reward, done))
    }

    fn reset(&mut self) -> Array1<f64> {
        self.theta = PI * (2.0 * thread_rng().random::<f64>() - 1.0);
        self.theta_dot = 2.0 * thread_rng().random::<f64>() - 1.0;
        self.current_step = 0;
        self.state()
    }

    fn state_dim(&self) -> usize {
        3
    }

    fn action_dim(&self) -> usize {
        1
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::autodiff::optimizers::Adam;

    #[test]
    fn test_replay_buffer() {
        let mut buffer = ReplayBuffer::new(100);

        for i in 0..150 {
            let exp = Experience {
                state: Array1::zeros(4),
                action: Array1::zeros(2),
                reward: i as f64,
                next_state: Array1::zeros(4),
                done: false,
            };
            buffer.push(exp);
        }

        assert_eq!(buffer.len(), 100);

        let batch = buffer.sample(10).expect("Buffer sampling should succeed");
        assert_eq!(batch.len(), 10);
    }

    #[test]
    fn test_pendulum_environment() {
        let mut env = PendulumEnvironment::new();
        let state = env.reset();
        assert_eq!(state.len(), 3);

        let action = Array1::from_vec(vec![1.0]);
        let (next_state, reward, done) = env.step(action).expect("Environment step should succeed");

        assert_eq!(next_state.len(), 3);
        assert!(reward <= 0.0); // Reward should be negative
        assert!(!done); // Not done after one step
    }

    #[test]
    fn test_quantum_actor() {
        let actor = QuantumActor::new(
            3, // state_dim
            1, // action_dim
            vec![(-2.0, 2.0)],
            4, // num_qubits
        )
        .expect("Failed to create quantum actor");

        let state = Array1::from_vec(vec![1.0, 0.0, 0.5]);
        let action = actor
            .get_action(&state, false)
            .expect("Get action should succeed");

        assert_eq!(action.len(), 1);
        assert!(action[0] >= -2.0 && action[0] <= 2.0);
    }

    #[test]
    fn test_quantum_critic() {
        let critic = QuantumCritic::new(3, 1, 4).expect("Failed to create quantum critic");

        let state = Array1::from_vec(vec![1.0, 0.0, 0.5]);
        let action = Array1::from_vec(vec![1.5]);

        let q_value = critic
            .get_q_value(&state, &action)
            .expect("Get Q-value should succeed");
        assert!(q_value.is_finite());
    }
}