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
//! Honeywell Quantum Solutions / Quantinuum client implementation
//!
//! This module provides integration with Honeywell's ion trap quantum computing platform
//! and the Quantinuum H-Series quantum computers, featuring high-fidelity operations
//! and native support for all-to-all connectivity.

use std::collections::HashMap;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use reqwest::Client;
use tokio::time::sleep;
use async_trait::async_trait;

use quantrs2_circuit::prelude::Circuit;
use quantrs2_core::gate::GateOp;

use crate::{DeviceError, DeviceResult, CircuitResult, QuantumDevice, CircuitExecutor};
use crate::translation::HardwareBackend;

/// Configuration for Honeywell Quantum Solutions / Quantinuum
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoneywellConfig {
    /// Quantinuum API endpoint
    pub api_url: String,
    /// API key for authentication
    pub api_key: String,
    /// User ID or account identifier
    pub user_id: String,
    /// Default quantum computer to use (e.g., "H1-1", "H1-2", "H2-1")
    pub default_machine: Option<String>,
    /// Request timeout in seconds
    pub timeout: u64,
    /// Maximum number of retries for failed requests
    pub max_retries: u32,
    /// Enable hardware-native gate optimization
    pub use_native_gates: bool,
}

impl Default for HoneywellConfig {
    fn default() -> Self {
        Self {
            api_url: "https://qapi.quantinuum.com".to_string(),
            api_key: String::new(),
            user_id: String::new(),
            default_machine: None,
            timeout: 600, // 10 minutes for potentially long jobs
            max_retries: 3,
            use_native_gates: true,
        }
    }
}

/// Honeywell/Quantinuum quantum computer client
pub struct HoneywellClient {
    config: HoneywellConfig,
    client: Client,
}

impl HoneywellClient {
    /// Create a new Honeywell client
    pub fn new(config: HoneywellConfig) -> DeviceResult<Self> {
        let client = Client::builder()
            .timeout(Duration::from_secs(config.timeout))
            .build()
            .map_err(|e| DeviceError::Connection(format!("Failed to create HTTP client: {}", e)))?;

        Ok(Self { config, client })
    }

    /// Get available quantum machines
    pub async fn get_available_machines(&self) -> DeviceResult<Vec<HoneywellMachine>> {
        let url = format!("{}/v1/machines", self.config.api_url);

        let response = self.client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.config.api_key))
            .send()
            .await
            .map_err(|e| DeviceError::Connection(format!("Failed to get machines: {}", e)))?;

        if !response.status().is_success() {
            return Err(DeviceError::Connection(
                format!("Machine listing failed with status: {}", response.status())
            ));
        }

        let machines: Vec<HoneywellMachine> = response
            .json()
            .await
            .map_err(|e| DeviceError::Parsing(format!("Failed to parse machine response: {}", e)))?;

        Ok(machines)
    }

    /// Submit a quantum job for execution
    pub async fn submit_job(&self, job: &HoneywellJob) -> DeviceResult<String> {
        let url = format!("{}/v1/jobs", self.config.api_url);

        let response = self.client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.config.api_key))
            .header("Content-Type", "application/json")
            .json(job)
            .send()
            .await
            .map_err(|e| DeviceError::Connection(format!("Failed to submit job: {}", e)))?;

        if !response.status().is_success() {
            return Err(DeviceError::Execution(
                format!("Job submission failed with status: {}", response.status())
            ));
        }

        let job_response: HoneywellJobResponse = response
            .json()
            .await
            .map_err(|e| DeviceError::Parsing(format!("Failed to parse job response: {}", e)))?;

        Ok(job_response.job_id)
    }

    /// Get job results
    pub async fn get_job_results(&self, job_id: &str) -> DeviceResult<HoneywellJobResults> {
        let url = format!("{}/v1/jobs/{}/results", self.config.api_url, job_id);

        let response = self.client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.config.api_key))
            .send()
            .await
            .map_err(|e| DeviceError::Connection(format!("Failed to get job results: {}", e)))?;

        if !response.status().is_success() {
            return Err(DeviceError::Execution(
                format!("Failed to get results with status: {}", response.status())
            ));
        }

        let results: HoneywellJobResults = response
            .json()
            .await
            .map_err(|e| DeviceError::Parsing(format!("Failed to parse job results: {}", e)))?;

        Ok(results)
    }

    /// Convert QuantRS circuit to OpenQASM 2.0 format (Honeywell's supported format)
    pub fn circuit_to_qasm<const N: usize>(&self, circuit: &Circuit<N>) -> DeviceResult<String> {
        let mut qasm_lines = Vec::new();

        // Add QASM header
        qasm_lines.push("OPENQASM 2.0;".to_string());
        qasm_lines.push("include \"qelib1.inc\";".to_string());

        // Declare quantum and classical registers
        qasm_lines.push(format!("qreg q[{}];", N));
        qasm_lines.push(format!("creg c[{}];", N));

        // Convert gates to QASM instructions
        for gate_info in circuit.iter_gates() {
            let qasm_instruction = self.gate_to_qasm(&gate_info.gate, &gate_info.qubits)?;
            qasm_lines.push(qasm_instruction);
        }

        // Add measurements
        for i in 0..N {
            qasm_lines.push(format!("measure q[{}] -> c[{}];", i, i));
        }

        Ok(qasm_lines.join("\n"))
    }

    /// Convert individual gate to QASM instruction
    fn gate_to_qasm(&self, gate: &dyn GateOp, qubits: &[usize]) -> DeviceResult<String> {
        let gate_name = gate.name();
        match gate_name.as_str() {
            "I" => Ok(format!("id q[{}];", qubits[0])),
            "X" => Ok(format!("x q[{}];", qubits[0])),
            "Y" => Ok(format!("y q[{}];", qubits[0])),
            "Z" => Ok(format!("z q[{}];", qubits[0])),
            "H" => Ok(format!("h q[{}];", qubits[0])),
            "S" => Ok(format!("s q[{}];", qubits[0])),
            "T" => Ok(format!("t q[{}];", qubits[0])),
            "RX" => {
                if let Some(params) = gate.parameters() {
                    Ok(format!("rx({}) q[{}];", params[0], qubits[0]))
                } else {
                    Err(DeviceError::CircuitConversion("RX gate missing angle parameter".to_string()))
                }
            },
            "RY" => {
                if let Some(params) = gate.parameters() {
                    Ok(format!("ry({}) q[{}];", params[0], qubits[0]))
                } else {
                    Err(DeviceError::CircuitConversion("RY gate missing angle parameter".to_string()))
                }
            },
            "RZ" => {
                if let Some(params) = gate.parameters() {
                    Ok(format!("rz({}) q[{}];", params[0], qubits[0]))
                } else {
                    Err(DeviceError::CircuitConversion("RZ gate missing angle parameter".to_string()))
                }
            },
            "CNOT" | "CX" => Ok(format!("cx q[{}],q[{}];", qubits[0], qubits[1])),
            "CZ" => Ok(format!("cz q[{}],q[{}];", qubits[0], qubits[1])),
            "SWAP" => {
                // SWAP decomposition using 3 CNOT gates
                Ok(format!("cx q[{}],q[{}];\ncx q[{}],q[{}];\ncx q[{}],q[{}];",
                          qubits[0], qubits[1], qubits[1], qubits[0], qubits[0], qubits[1]))
            },
            "CCX" | "CCNOT" => Ok(format!("ccx q[{}],q[{}],q[{}];", qubits[0], qubits[1], qubits[2])),
            _ => {
                // Check for native ion trap gates
                if self.config.use_native_gates {
                    match gate_name.as_str() {
                        "RZZ" => {
                            if let Some(params) = gate.parameters() {
                                Ok(format!("rzz({}) q[{}],q[{}];", params[0], qubits[0], qubits[1]))
                            } else {
                                Err(DeviceError::CircuitConversion("RZZ gate missing angle parameter".to_string()))
                            }
                        },
                        "RXX" => {
                            if let Some(params) = gate.parameters() {
                                Ok(format!("rxx({}) q[{}],q[{}];", params[0], qubits[0], qubits[1]))
                            } else {
                                Err(DeviceError::CircuitConversion("RXX gate missing angle parameter".to_string()))
                            }
                        },
                        "MS" => {
                            if let Some(params) = gate.parameters() {
                                Ok(format!("ms({}) q[{}],q[{}];", params[0], qubits[0], qubits[1]))
                            } else {
                                Err(DeviceError::CircuitConversion("MS gate missing angle parameter".to_string()))
                            }
                        },
                        _ => Err(DeviceError::UnsupportedGate(format!("Gate not supported by Honeywell backend: {}", gate_name))),
                    }
                } else {
                    Err(DeviceError::UnsupportedGate(format!("Gate not supported by Honeywell backend: {}", gate_name)))
                }
            }
        }
    }
}

/// Honeywell quantum machine information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoneywellMachine {
    pub name: String,
    pub id: String,
    pub status: String,
    pub num_qubits: usize,
    pub generation: String, // H1, H2, etc.
    pub connectivity: String, // "all-to-all" for ion traps
    pub gate_times: HashMap<String, f64>,
    pub fidelities: HashMap<String, f64>,
    pub queue_length: Option<usize>,
    pub avg_queue_time: Option<f64>,
}

/// Honeywell quantum job
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoneywellJob {
    pub qasm: String,
    pub machine: String,
    pub shots: u32,
    pub name: Option<String>,
    pub priority: Option<String>,
    pub tags: Option<Vec<String>>,
}

/// Honeywell job response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoneywellJobResponse {
    pub job_id: String,
    pub status: String,
    pub created_at: String,
}

/// Honeywell job results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoneywellJobResults {
    pub job_id: String,
    pub counts: HashMap<String, u32>,
    pub execution_time: f64,
    pub queue_time: f64,
    pub fidelity_metrics: Option<HashMap<String, f64>>,
    pub raw_data: Option<serde_json::Value>,
}

/// Honeywell device implementation
pub struct HoneywellDevice {
    client: HoneywellClient,
    machine_info: HoneywellMachine,
}

impl HoneywellDevice {
    /// Create a new Honeywell device
    pub async fn new(config: HoneywellConfig) -> DeviceResult<Self> {
        let client = HoneywellClient::new(config.clone())?;

        // Get default machine info
        let machine_name = config.default_machine.as_ref()
            .ok_or_else(|| DeviceError::Configuration("No default machine specified".to_string()))?;

        let machines = client.get_available_machines().await?;
        let machine_info = machines.into_iter()
            .find(|machine| &machine.name == machine_name)
            .ok_or_else(|| DeviceError::Configuration(format!("Machine '{}' not found", machine_name)))?;

        Ok(Self { client, machine_info })
    }
}

#[async_trait]
impl QuantumDevice for HoneywellDevice {
    async fn is_available(&self) -> DeviceResult<bool> {
        Ok(self.machine_info.status == "online")
    }

    async fn qubit_count(&self) -> DeviceResult<usize> {
        Ok(self.machine_info.num_qubits)
    }

    async fn properties(&self) -> DeviceResult<HashMap<String, String>> {
        let mut props = HashMap::new();
        props.insert("backend".to_string(), "honeywell".to_string());
        props.insert("machine_name".to_string(), self.machine_info.name.clone());
        props.insert("num_qubits".to_string(), self.machine_info.num_qubits.to_string());
        props.insert("generation".to_string(), self.machine_info.generation.clone());
        props.insert("connectivity".to_string(), self.machine_info.connectivity.clone());
        props.insert("status".to_string(), self.machine_info.status.clone());
        if let Some(queue_length) = self.machine_info.queue_length {
            props.insert("queue_length".to_string(), queue_length.to_string());
        }
        Ok(props)
    }

    fn is_simulator(&self) -> bool {
        false
    }

    async fn estimated_queue_time<const N: usize>(&self, _circuit: &Circuit<N>) -> DeviceResult<Duration> {
        // Use actual queue time if available
        if let Some(avg_time) = self.machine_info.avg_queue_time {
            Ok(Duration::from_secs(avg_time as u64))
        } else {
            Ok(Duration::from_secs(120)) // Default 2 minutes
        }
    }

    async fn can_execute_circuit<const N: usize>(&self, circuit: &Circuit<N>) -> DeviceResult<bool> {
        // Check if circuit fits on device
        if N > self.machine_info.num_qubits {
            return Ok(false);
        }

        // Check gate support
        for gate_info in circuit.iter_gates() {
            let gate_name = gate_info.gate.name();
            match gate_name.as_str() {
                "I" | "X" | "Y" | "Z" | "H" | "S" | "T" |
                "RX" | "RY" | "RZ" | "CNOT" | "CX" | "CZ" | "SWAP" | "CCX" | "CCNOT" => {},
                "RZZ" | "RXX" | "MS" if self.client.config.use_native_gates => {},
                _ => return Ok(false),
            }
        }

        Ok(true)
    }
}

#[async_trait]
impl CircuitExecutor for HoneywellDevice {
    async fn execute_circuit<const N: usize>(
        &self,
        circuit: &Circuit<N>,
        shots: usize,
    ) -> DeviceResult<CircuitResult> {
        // Convert circuit to QASM
        let qasm_program = self.client.circuit_to_qasm(circuit)?;

        // Create Honeywell job
        let job = HoneywellJob {
            qasm: qasm_program,
            machine: self.machine_info.name.clone(),
            shots: shots as u32,
            name: Some(format!("quantrs_job_{}", chrono::Utc::now().timestamp())),
            priority: None,
            tags: Some(vec!["quantrs".to_string()]),
        };

        // Submit and wait for results
        let job_id = self.client.submit_job(&job).await?;

        // Poll for completion (simplified - real implementation would be more sophisticated)
        tokio::time::sleep(Duration::from_secs(10)).await;

        let results = self.client.get_job_results(&job_id).await?;

        // Convert count data to the expected format
        let mut counts = HashMap::new();
        for (state, count) in &results.counts {
            counts.insert(state.clone(), *count as usize);
        }

        Ok(CircuitResult { counts, shots })
    }

    async fn execute_circuits<const N: usize>(
        &self,
        circuits: Vec<&Circuit<N>>,
        shots: usize,
    ) -> DeviceResult<Vec<CircuitResult>> {
        let mut results = Vec::new();

        // Execute circuits sequentially (could be parallelized)
        for circuit in circuits {
            let result = self.execute_circuit(circuit, shots).await?;
            results.push(result);
        }

        Ok(results)
    }
}

/// Create a Honeywell client
pub fn create_honeywell_client(config: HoneywellConfig) -> DeviceResult<HoneywellClient> {
    HoneywellClient::new(config)
}

/// Create a Honeywell device
pub async fn create_honeywell_device(config: HoneywellConfig) -> DeviceResult<HoneywellDevice> {
    HoneywellDevice::new(config).await
}

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

    #[tokio::test]
    async fn test_honeywell_client_creation() {
        let config = HoneywellConfig {
            api_key: "test_key".to_string(),
            user_id: "test_user".to_string(),
            ..HoneywellConfig::default()
        };

        let client = HoneywellClient::new(config);
        assert!(client.is_ok());
    }

    #[test]
    fn test_circuit_to_qasm_conversion() {
        let config = HoneywellConfig::default();
        let client = HoneywellClient::new(config).expect("Failed to create Honeywell client");

        let circuit = Circuit::<2>::new();
        // Note: This is a simplified test - actual circuit building would use proper QuantRS gates

        let qasm = client.circuit_to_qasm(&circuit).expect("Failed to convert circuit to QASM");

        assert!(qasm.contains("OPENQASM 2.0"));
        assert!(qasm.contains("qreg q[2]"));
        assert!(qasm.contains("creg c[2]"));
        assert!(qasm.contains("measure q[0] -> c[0]"));
        assert!(qasm.contains("measure q[1] -> c[1]"));
    }
}