quantrs2-device 0.1.3

Quantum device connectors for the QuantRS2 framework
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
664
//! Communication protocols for neutral atom quantum devices
//!
//! This module provides protocol implementations for communicating with
//! neutral atom quantum computers, including device discovery, command
//! execution, and status monitoring.

use crate::{DeviceError, DeviceResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, SystemTime};

/// Communication protocol types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProtocolType {
    /// HTTP/REST protocol
    Http,
    /// WebSocket protocol for real-time communication
    WebSocket,
    /// TCP/IP direct connection
    Tcp,
    /// USB serial connection
    Serial,
    /// Custom protocol
    Custom(String),
}

/// Protocol configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolConfig {
    /// Protocol type
    pub protocol_type: ProtocolType,
    /// Connection parameters
    pub connection_params: ConnectionParams,
    /// Authentication settings
    pub authentication: AuthenticationConfig,
    /// Timeout settings
    pub timeouts: TimeoutConfig,
    /// Retry configuration
    pub retry_config: RetryConfig,
}

/// Connection parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionParams {
    /// Host address
    pub host: String,
    /// Port number
    pub port: u16,
    /// Use secure connection (TLS/SSL)
    pub secure: bool,
    /// Connection endpoint path
    pub endpoint: String,
    /// Additional connection parameters
    pub parameters: HashMap<String, String>,
}

/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthenticationConfig {
    /// Authentication type
    pub auth_type: AuthenticationType,
    /// Username
    pub username: Option<String>,
    /// Password or token
    pub credentials: Option<String>,
    /// API key
    pub api_key: Option<String>,
    /// Certificate path for client certificates
    pub certificate_path: Option<String>,
}

/// Authentication types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthenticationType {
    /// No authentication
    None,
    /// Basic authentication (username/password)
    Basic,
    /// Bearer token authentication
    Bearer,
    /// API key authentication
    ApiKey,
    /// Client certificate authentication
    ClientCertificate,
    /// Custom authentication method
    Custom(String),
}

/// Timeout configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeoutConfig {
    /// Connection timeout
    pub connection_timeout: Duration,
    /// Request timeout
    pub request_timeout: Duration,
    /// Response timeout
    pub response_timeout: Duration,
    /// Keep-alive timeout
    pub keepalive_timeout: Duration,
}

/// Retry configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryConfig {
    /// Maximum number of retries
    pub max_retries: usize,
    /// Initial retry delay
    pub initial_delay: Duration,
    /// Maximum retry delay
    pub max_delay: Duration,
    /// Backoff multiplier
    pub backoff_multiplier: f64,
    /// Jitter factor
    pub jitter_factor: f64,
}

/// Protocol message types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageType {
    /// Device discovery request
    Discovery,
    /// Device status query
    Status,
    /// Circuit execution command
    Execute,
    /// Configuration update
    Configure,
    /// Calibration command
    Calibrate,
    /// Emergency stop
    EmergencyStop,
    /// Health check
    HealthCheck,
    /// Custom message
    Custom(String),
}

/// Protocol message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolMessage {
    /// Message ID
    pub message_id: String,
    /// Message type
    pub message_type: MessageType,
    /// Timestamp
    pub timestamp: SystemTime,
    /// Sender information
    pub sender: String,
    /// Recipient information
    pub recipient: String,
    /// Message payload
    pub payload: HashMap<String, serde_json::Value>,
    /// Priority level
    pub priority: MessagePriority,
}

/// Message priority levels
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum MessagePriority {
    /// Low priority
    Low,
    /// Normal priority
    Normal,
    /// High priority
    High,
    /// Critical priority
    Critical,
}

/// Protocol response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolResponse {
    /// Response ID (matches request message ID)
    pub response_id: String,
    /// Response timestamp
    pub timestamp: SystemTime,
    /// Success status
    pub success: bool,
    /// Status code
    pub status_code: u16,
    /// Status message
    pub status_message: String,
    /// Response data
    pub data: HashMap<String, serde_json::Value>,
    /// Execution time
    pub execution_time: Duration,
}

/// Device discovery protocol
pub struct DeviceDiscoveryProtocol {
    config: ProtocolConfig,
    discovered_devices: Vec<DiscoveredDevice>,
}

/// Discovered device information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiscoveredDevice {
    /// Device ID
    pub device_id: String,
    /// Device name
    pub device_name: String,
    /// Device type
    pub device_type: String,
    /// Network address
    pub address: String,
    /// Port number
    pub port: u16,
    /// Device capabilities
    pub capabilities: Vec<String>,
    /// Device status
    pub status: DeviceStatus,
    /// Last seen timestamp
    pub last_seen: SystemTime,
}

/// Device status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeviceStatus {
    /// Device is online and available
    Online,
    /// Device is online but busy
    Busy,
    /// Device is offline
    Offline,
    /// Device is in maintenance mode
    Maintenance,
    /// Device is in error state
    Error,
    /// Unknown status
    Unknown,
}

impl DeviceDiscoveryProtocol {
    /// Create a new device discovery protocol
    pub const fn new(config: ProtocolConfig) -> Self {
        Self {
            config,
            discovered_devices: Vec::new(),
        }
    }

    /// Discover available devices
    pub async fn discover_devices(&mut self) -> DeviceResult<Vec<DiscoveredDevice>> {
        // Implementation would depend on the specific protocol
        // For now, return mock devices
        let mock_device = DiscoveredDevice {
            device_id: "neutral_atom_1".to_string(),
            device_name: "Neutral Atom Device 1".to_string(),
            device_type: "NeutralAtom".to_string(),
            address: "192.168.1.100".to_string(),
            port: 8080,
            capabilities: vec![
                "rydberg_gates".to_string(),
                "optical_tweezers".to_string(),
                "hyperfine_manipulation".to_string(),
            ],
            status: DeviceStatus::Online,
            last_seen: SystemTime::now(),
        };

        self.discovered_devices = vec![mock_device.clone()];
        Ok(vec![mock_device])
    }

    /// Get discovered devices
    pub fn get_discovered_devices(&self) -> &[DiscoveredDevice] {
        &self.discovered_devices
    }

    /// Refresh device status
    pub async fn refresh_device_status(&mut self, device_id: &str) -> DeviceResult<DeviceStatus> {
        // Find the device and update its status
        for device in &mut self.discovered_devices {
            if device.device_id == device_id {
                // Mock status update
                device.last_seen = SystemTime::now();
                return Ok(device.status.clone());
            }
        }

        Err(DeviceError::DeviceNotFound(format!(
            "Device {device_id} not found"
        )))
    }
}

/// Command execution protocol
pub struct CommandExecutionProtocol {
    config: ProtocolConfig,
    pending_commands: HashMap<String, PendingCommand>,
}

/// Pending command information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PendingCommand {
    /// Command ID
    pub command_id: String,
    /// Command type
    pub command_type: String,
    /// Target device
    pub target_device: String,
    /// Command parameters
    pub parameters: HashMap<String, serde_json::Value>,
    /// Submission timestamp
    pub submitted_at: SystemTime,
    /// Expected completion time
    pub expected_completion: Option<SystemTime>,
    /// Current status
    pub status: CommandStatus,
}

/// Command execution status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CommandStatus {
    /// Command is queued
    Queued,
    /// Command is executing
    Executing,
    /// Command completed successfully
    Completed,
    /// Command failed
    Failed,
    /// Command was cancelled
    Cancelled,
    /// Command timed out
    TimedOut,
}

impl CommandExecutionProtocol {
    /// Create a new command execution protocol
    pub fn new(config: ProtocolConfig) -> Self {
        Self {
            config,
            pending_commands: HashMap::new(),
        }
    }

    /// Submit a command for execution
    pub async fn submit_command(
        &mut self,
        command_type: &str,
        target_device: &str,
        parameters: HashMap<String, serde_json::Value>,
    ) -> DeviceResult<String> {
        let command_id = format!("cmd_{}", uuid::Uuid::new_v4());

        let pending_command = PendingCommand {
            command_id: command_id.clone(),
            command_type: command_type.to_string(),
            target_device: target_device.to_string(),
            parameters,
            submitted_at: SystemTime::now(),
            expected_completion: None,
            status: CommandStatus::Queued,
        };

        self.pending_commands
            .insert(command_id.clone(), pending_command);
        Ok(command_id)
    }

    /// Get command status
    pub fn get_command_status(&self, command_id: &str) -> DeviceResult<CommandStatus> {
        self.pending_commands
            .get(command_id)
            .map(|cmd| cmd.status.clone())
            .ok_or_else(|| DeviceError::InvalidInput(format!("Command {command_id} not found")))
    }

    /// Cancel a command
    pub async fn cancel_command(&mut self, command_id: &str) -> DeviceResult<()> {
        if let Some(command) = self.pending_commands.get_mut(command_id) {
            command.status = CommandStatus::Cancelled;
            Ok(())
        } else {
            Err(DeviceError::InvalidInput(format!(
                "Command {command_id} not found"
            )))
        }
    }

    /// Get all pending commands
    pub fn get_pending_commands(&self) -> Vec<&PendingCommand> {
        self.pending_commands.values().collect()
    }
}

/// Status monitoring protocol
pub struct StatusMonitoringProtocol {
    config: ProtocolConfig,
    monitored_devices: HashMap<String, DeviceMonitoringInfo>,
}

/// Device monitoring information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceMonitoringInfo {
    /// Device ID
    pub device_id: String,
    /// Last status update
    pub last_update: SystemTime,
    /// Current status
    pub current_status: DeviceStatus,
    /// Status history
    pub status_history: Vec<StatusHistoryEntry>,
    /// Performance metrics
    pub metrics: HashMap<String, f64>,
}

/// Status history entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatusHistoryEntry {
    /// Timestamp
    pub timestamp: SystemTime,
    /// Status
    pub status: DeviceStatus,
    /// Additional information
    pub info: Option<String>,
}

impl StatusMonitoringProtocol {
    /// Create a new status monitoring protocol
    pub fn new(config: ProtocolConfig) -> Self {
        Self {
            config,
            monitored_devices: HashMap::new(),
        }
    }

    /// Start monitoring a device
    pub fn start_monitoring(&mut self, device_id: &str) -> DeviceResult<()> {
        let monitoring_info = DeviceMonitoringInfo {
            device_id: device_id.to_string(),
            last_update: SystemTime::now(),
            current_status: DeviceStatus::Unknown,
            status_history: Vec::new(),
            metrics: HashMap::new(),
        };

        self.monitored_devices
            .insert(device_id.to_string(), monitoring_info);
        Ok(())
    }

    /// Stop monitoring a device
    pub fn stop_monitoring(&mut self, device_id: &str) -> DeviceResult<()> {
        self.monitored_devices.remove(device_id);
        Ok(())
    }

    /// Update device status
    pub fn update_device_status(
        &mut self,
        device_id: &str,
        status: DeviceStatus,
        info: Option<String>,
    ) -> DeviceResult<()> {
        if let Some(monitoring_info) = self.monitored_devices.get_mut(device_id) {
            let history_entry = StatusHistoryEntry {
                timestamp: SystemTime::now(),
                status: status.clone(),
                info,
            };

            monitoring_info.current_status = status;
            monitoring_info.last_update = SystemTime::now();
            monitoring_info.status_history.push(history_entry);

            // Keep only recent history (last 1000 entries)
            if monitoring_info.status_history.len() > 1000 {
                monitoring_info.status_history.drain(0..500);
            }

            Ok(())
        } else {
            Err(DeviceError::DeviceNotFound(format!(
                "Device {device_id} not found in monitoring list"
            )))
        }
    }

    /// Get device status
    pub fn get_device_status(&self, device_id: &str) -> DeviceResult<&DeviceMonitoringInfo> {
        self.monitored_devices.get(device_id).ok_or_else(|| {
            DeviceError::DeviceNotFound(format!("Device {device_id} not found in monitoring list"))
        })
    }

    /// Get all monitored devices
    pub fn get_monitored_devices(&self) -> Vec<&DeviceMonitoringInfo> {
        self.monitored_devices.values().collect()
    }
}

impl Default for ProtocolConfig {
    fn default() -> Self {
        Self {
            protocol_type: ProtocolType::Http,
            connection_params: ConnectionParams::default(),
            authentication: AuthenticationConfig::default(),
            timeouts: TimeoutConfig::default(),
            retry_config: RetryConfig::default(),
        }
    }
}

impl Default for ConnectionParams {
    fn default() -> Self {
        Self {
            host: "localhost".to_string(),
            port: 8080,
            secure: false,
            endpoint: "/api/v1".to_string(),
            parameters: HashMap::new(),
        }
    }
}

impl Default for AuthenticationConfig {
    fn default() -> Self {
        Self {
            auth_type: AuthenticationType::None,
            username: None,
            credentials: None,
            api_key: None,
            certificate_path: None,
        }
    }
}

impl Default for TimeoutConfig {
    fn default() -> Self {
        Self {
            connection_timeout: Duration::from_secs(30),
            request_timeout: Duration::from_secs(60),
            response_timeout: Duration::from_secs(120),
            keepalive_timeout: Duration::from_secs(300),
        }
    }
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(30),
            backoff_multiplier: 2.0,
            jitter_factor: 0.1,
        }
    }
}

/// Create a protocol message
pub fn create_protocol_message(
    message_type: MessageType,
    sender: &str,
    recipient: &str,
    payload: HashMap<String, serde_json::Value>,
    priority: MessagePriority,
) -> ProtocolMessage {
    ProtocolMessage {
        message_id: uuid::Uuid::new_v4().to_string(),
        message_type,
        timestamp: SystemTime::now(),
        sender: sender.to_string(),
        recipient: recipient.to_string(),
        payload,
        priority,
    }
}

/// Create a protocol response
pub fn create_protocol_response(
    request_id: &str,
    success: bool,
    status_code: u16,
    status_message: &str,
    data: HashMap<String, serde_json::Value>,
    execution_time: Duration,
) -> ProtocolResponse {
    ProtocolResponse {
        response_id: request_id.to_string(),
        timestamp: SystemTime::now(),
        success,
        status_code,
        status_message: status_message.to_string(),
        data,
        execution_time,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_protocol_config_creation() {
        let config = ProtocolConfig::default();
        assert_eq!(config.protocol_type, ProtocolType::Http);
        assert_eq!(config.connection_params.host, "localhost");
        assert_eq!(config.connection_params.port, 8080);
    }

    #[test]
    fn test_protocol_message_creation() {
        let payload = HashMap::new();
        let message = create_protocol_message(
            MessageType::Status,
            "client",
            "device_1",
            payload,
            MessagePriority::Normal,
        );

        assert_eq!(message.message_type, MessageType::Status);
        assert_eq!(message.sender, "client");
        assert_eq!(message.recipient, "device_1");
        assert_eq!(message.priority, MessagePriority::Normal);
    }

    #[test]
    fn test_device_discovery() {
        let config = ProtocolConfig::default();
        let discovery = DeviceDiscoveryProtocol::new(config);
        assert_eq!(discovery.get_discovered_devices().len(), 0);
    }
}

// Add uuid dependency if not already present
mod uuid {
    use std::fmt;

    pub struct Uuid([u8; 16]);

    impl Uuid {
        pub fn new_v4() -> Self {
            // Simple mock implementation - in real code would use proper UUID generation
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};
            use std::time::SystemTime;

            let mut hasher = DefaultHasher::new();
            SystemTime::now().hash(&mut hasher);
            let hash = hasher.finish();

            let mut bytes = [0u8; 16];
            bytes[0..8].copy_from_slice(&hash.to_le_bytes());
            bytes[8..16].copy_from_slice(&hash.to_be_bytes());

            Self(bytes)
        }
    }

    impl fmt::Display for Uuid {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
                self.0[0], self.0[1], self.0[2], self.0[3],
                self.0[4], self.0[5],
                self.0[6], self.0[7],
                self.0[8], self.0[9],
                self.0[10], self.0[11], self.0[12], self.0[13], self.0[14], self.0[15])
        }
    }
}