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
665
666
667
668
use quantrs2_circuit::prelude::Circuit;
use std::collections::HashMap;
#[cfg(feature = "azure")]
use std::sync::Arc;
#[cfg(feature = "azure")]
use std::thread::sleep;
#[cfg(feature = "azure")]
use std::time::Duration;

#[cfg(feature = "azure")]
use reqwest::{header, Client};
#[cfg(feature = "azure")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "azure")]
use serde_json;
use thiserror::Error;

use crate::DeviceError;
use crate::DeviceResult;

#[cfg(feature = "azure")]
const AZURE_QUANTUM_API_URL: &str = "https://eastus.quantum.azure.com";
#[cfg(feature = "azure")]
const DEFAULT_TIMEOUT_SECS: u64 = 90;

/// Represents the available providers on Azure Quantum
#[derive(Debug, Clone)]
#[cfg_attr(feature = "azure", derive(serde::Deserialize))]
pub struct AzureProvider {
    /// Unique identifier for the provider
    pub id: String,
    /// Name of the provider (e.g., "ionq", "microsoft", "quantinuum")
    pub name: String,
    /// Provider-specific capabilities and settings
    pub capabilities: HashMap<String, String>,
}

/// Represents the available target devices on Azure Quantum
#[derive(Debug, Clone)]
#[cfg_attr(feature = "azure", derive(serde::Deserialize))]
pub struct AzureTarget {
    /// Target ID
    pub id: String,
    /// Display name of the target
    pub name: String,
    /// Provider ID
    pub provider_id: String,
    /// Whether the target is a simulator or real quantum hardware
    pub is_simulator: bool,
    /// Number of qubits on the target
    pub num_qubits: usize,
    /// Status of the target (e.g., "Available", "Offline")
    pub status: String,
    /// Target-specific capabilities and properties
    #[cfg(feature = "azure")]
    pub properties: HashMap<String, serde_json::Value>,
    #[cfg(not(feature = "azure"))]
    pub properties: HashMap<String, String>,
}

/// Configuration for a quantum circuit to be submitted to Azure Quantum
#[derive(Debug, Clone)]
#[cfg_attr(feature = "azure", derive(Serialize))]
pub struct AzureCircuitConfig {
    /// Name of the job
    pub name: String,
    /// Circuit representation (varies by provider)
    pub circuit: String,
    /// Number of shots to run
    pub shots: usize,
    /// Provider-specific parameters
    #[cfg(feature = "azure")]
    pub provider_parameters: HashMap<String, serde_json::Value>,
    #[cfg(not(feature = "azure"))]
    pub provider_parameters: HashMap<String, String>,
}

/// Status of a job in Azure Quantum
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "azure", derive(Deserialize))]
pub enum AzureJobStatus {
    #[cfg_attr(feature = "azure", serde(rename = "Waiting"))]
    Waiting,
    #[cfg_attr(feature = "azure", serde(rename = "Executing"))]
    Executing,
    #[cfg_attr(feature = "azure", serde(rename = "Succeeded"))]
    Succeeded,
    #[cfg_attr(feature = "azure", serde(rename = "Failed"))]
    Failed,
    #[cfg_attr(feature = "azure", serde(rename = "Cancelled"))]
    Cancelled,
}

/// Response from submitting a job to Azure Quantum
#[cfg(feature = "azure")]
#[derive(Debug, Deserialize)]
pub struct AzureJobResponse {
    /// Job ID
    pub id: String,
    /// Name of the job
    pub name: String,
    /// Job status
    pub status: AzureJobStatus,
    /// Provider ID
    pub provider: String,
    /// Target ID
    pub target: String,
    /// Creation timestamp
    pub creation_time: String,
    /// Execution time (if completed)
    pub execution_time: Option<String>,
}

#[cfg(not(feature = "azure"))]
#[derive(Debug)]
pub struct AzureJobResponse {
    /// Job ID
    pub id: String,
    /// Name of the job
    pub name: String,
    /// Job status
    pub status: AzureJobStatus,
}

/// Results from a completed job
#[cfg(feature = "azure")]
#[derive(Debug, Deserialize)]
pub struct AzureJobResult {
    /// Counts of each basis state
    pub histogram: HashMap<String, f64>,
    /// Total number of shots executed
    pub shots: usize,
    /// Job status
    pub status: AzureJobStatus,
    /// Error message, if any
    pub error: Option<String>,
    /// Additional metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

#[cfg(not(feature = "azure"))]
#[derive(Debug)]
pub struct AzureJobResult {
    /// Counts of each basis state (as probabilities)
    pub histogram: HashMap<String, f64>,
    /// Total number of shots executed
    pub shots: usize,
    /// Job status
    pub status: AzureJobStatus,
    /// Error message, if any
    pub error: Option<String>,
}

/// Errors specific to Azure Quantum
#[derive(Error, Debug)]
pub enum AzureQuantumError {
    #[error("Authentication error: {0}")]
    Authentication(String),

    #[error("API error: {0}")]
    API(String),

    #[error("Target not available: {0}")]
    TargetUnavailable(String),

    #[error("Circuit conversion error: {0}")]
    CircuitConversion(String),

    #[error("Job submission error: {0}")]
    JobSubmission(String),

    #[error("Timeout waiting for job completion")]
    Timeout,
}

/// Client for interacting with Azure Quantum
#[cfg(feature = "azure")]
#[derive(Clone)]
pub struct AzureQuantumClient {
    /// HTTP client for making API requests
    client: Client,
    /// Base URL for the Azure Quantum API
    api_url: String,
    /// Workspace name
    workspace: String,
    /// Subscription ID
    subscription_id: String,
    /// Resource group
    resource_group: String,
    /// Authentication token
    token: String,
}

#[cfg(not(feature = "azure"))]
#[derive(Clone)]
pub struct AzureQuantumClient;

#[cfg(feature = "azure")]
impl AzureQuantumClient {
    /// Create a new Azure Quantum client with the given token and workspace details
    pub fn new(
        token: &str,
        subscription_id: &str,
        resource_group: &str,
        workspace: &str,
        region: Option<&str>,
    ) -> DeviceResult<Self> {
        let mut headers = header::HeaderMap::new();
        headers.insert(
            header::CONTENT_TYPE,
            header::HeaderValue::from_static("application/json"),
        );

        let client = Client::builder()
            .default_headers(headers)
            .timeout(Duration::from_secs(30))
            .build()
            .map_err(|e| DeviceError::Connection(e.to_string()))?;

        let api_url = match region {
            Some(region) => format!("https://{}.quantum.azure.com", region),
            None => AZURE_QUANTUM_API_URL.to_string(),
        };

        Ok(Self {
            client,
            api_url,
            workspace: workspace.to_string(),
            subscription_id: subscription_id.to_string(),
            resource_group: resource_group.to_string(),
            token: token.to_string(),
        })
    }

    /// Get API base path for this workspace
    fn get_api_base_path(&self) -> String {
        format!(
            "/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Quantum/Workspaces/{}",
            self.subscription_id, self.resource_group, self.workspace
        )
    }

    /// List all available providers
    pub async fn list_providers(&self) -> DeviceResult<Vec<AzureProvider>> {
        let base_path = self.get_api_base_path();
        let url = format!("{}{}/providers", self.api_url, base_path);

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

        if !response.status().is_success() {
            let error_msg = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DeviceError::APIError(error_msg));
        }

        let providers: Vec<AzureProvider> = response
            .json()
            .await
            .map_err(|e| DeviceError::Deserialization(e.to_string()))?;

        Ok(providers)
    }

    /// List all available targets (devices and simulators)
    pub async fn list_targets(&self) -> DeviceResult<Vec<AzureTarget>> {
        let base_path = self.get_api_base_path();
        let url = format!("{}{}/targets", self.api_url, base_path);

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

        if !response.status().is_success() {
            let error_msg = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DeviceError::APIError(error_msg));
        }

        let targets: Vec<AzureTarget> = response
            .json()
            .await
            .map_err(|e| DeviceError::Deserialization(e.to_string()))?;

        Ok(targets)
    }

    /// Get details about a specific target
    pub async fn get_target(&self, target_id: &str) -> DeviceResult<AzureTarget> {
        let base_path = self.get_api_base_path();
        let url = format!("{}{}/targets/{}", self.api_url, base_path, target_id);

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

        if !response.status().is_success() {
            let error_msg = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DeviceError::APIError(error_msg));
        }

        let target: AzureTarget = response
            .json()
            .await
            .map_err(|e| DeviceError::Deserialization(e.to_string()))?;

        Ok(target)
    }

    /// Submit a circuit to be executed on an Azure Quantum target
    pub async fn submit_circuit(
        &self,
        target_id: &str,
        provider_id: &str,
        config: AzureCircuitConfig,
    ) -> DeviceResult<String> {
        let base_path = self.get_api_base_path();
        let url = format!("{}{}/jobs", self.api_url, base_path);

        use serde_json::json;

        let payload = json!({
            "name": config.name,
            "providerId": provider_id,
            "target": target_id,
            "input": config.circuit,
            "inputDataFormat": "qir", // Default to QIR, change based on provider
            "outputDataFormat": "microsoft.quantum-results.v1",
            "metadata": {
                "shots": config.shots
            },
            "params": config.provider_parameters
        });

        let response = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.token))
            .json(&payload)
            .send()
            .await
            .map_err(|e| DeviceError::Connection(e.to_string()))?;

        if !response.status().is_success() {
            let error_msg = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DeviceError::JobSubmission(error_msg));
        }

        let job_response: AzureJobResponse = response
            .json()
            .await
            .map_err(|e| DeviceError::Deserialization(e.to_string()))?;

        Ok(job_response.id)
    }

    /// Get the status of a job
    pub async fn get_job_status(&self, job_id: &str) -> DeviceResult<AzureJobStatus> {
        let base_path = self.get_api_base_path();
        let url = format!("{}{}/jobs/{}", self.api_url, base_path, job_id);

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

        if !response.status().is_success() {
            let error_msg = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DeviceError::APIError(error_msg));
        }

        let job: AzureJobResponse = response
            .json()
            .await
            .map_err(|e| DeviceError::Deserialization(e.to_string()))?;

        Ok(job.status)
    }

    /// Get the results of a completed job
    pub async fn get_job_result(&self, job_id: &str) -> DeviceResult<AzureJobResult> {
        let base_path = self.get_api_base_path();
        let url = format!("{}{}/jobs/{}/results", self.api_url, base_path, job_id);

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

        if !response.status().is_success() {
            let error_msg = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(DeviceError::APIError(error_msg));
        }

        let result: AzureJobResult = response
            .json()
            .await
            .map_err(|e| DeviceError::Deserialization(e.to_string()))?;

        Ok(result)
    }

    /// Wait for a job to complete with timeout
    pub async fn wait_for_job(
        &self,
        job_id: &str,
        timeout_secs: Option<u64>,
    ) -> DeviceResult<AzureJobResult> {
        let timeout = timeout_secs.unwrap_or(DEFAULT_TIMEOUT_SECS);
        let mut elapsed = 0;
        let interval = 5; // Check status every 5 seconds

        while elapsed < timeout {
            let status = self.get_job_status(job_id).await?;

            match status {
                AzureJobStatus::Succeeded => {
                    return self.get_job_result(job_id).await;
                }
                AzureJobStatus::Failed => {
                    return Err(DeviceError::JobExecution(format!(
                        "Job {} encountered an error",
                        job_id
                    )));
                }
                AzureJobStatus::Cancelled => {
                    return Err(DeviceError::JobExecution(format!(
                        "Job {} was cancelled",
                        job_id
                    )));
                }
                _ => {
                    // Still in progress, wait and check again
                    sleep(Duration::from_secs(interval));
                    elapsed += interval;
                }
            }
        }

        Err(DeviceError::Timeout(format!(
            "Timed out waiting for job {} to complete",
            job_id
        )))
    }

    /// Submit multiple circuits in parallel
    pub async fn submit_circuits_parallel(
        &self,
        target_id: &str,
        provider_id: &str,
        configs: Vec<AzureCircuitConfig>,
    ) -> DeviceResult<Vec<String>> {
        let client = Arc::new(self.clone());

        let mut handles = vec![];

        for config in configs {
            let client_clone = client.clone();
            let target_id = target_id.to_string();
            let provider_id = provider_id.to_string();

            let handle = tokio::task::spawn(async move {
                client_clone
                    .submit_circuit(&target_id, &provider_id, config)
                    .await
            });

            handles.push(handle);
        }

        let mut job_ids = vec![];

        for handle in handles {
            match handle.await {
                Ok(result) => match result {
                    Ok(job_id) => job_ids.push(job_id),
                    Err(e) => return Err(e),
                },
                Err(e) => {
                    return Err(DeviceError::JobSubmission(format!(
                        "Failed to join task: {}",
                        e
                    )));
                }
            }
        }

        Ok(job_ids)
    }

    /// Convert a Quantrs circuit to a provider-specific format
    pub fn circuit_to_provider_format<const N: usize>(
        circuit: &Circuit<N>,
        provider_id: &str,
    ) -> DeviceResult<String> {
        // Different format conversions based on provider
        match provider_id {
            "ionq" => Self::circuit_to_ionq_format(circuit),
            "microsoft" => Self::circuit_to_qir_format(circuit),
            "quantinuum" => Self::circuit_to_qasm_format(circuit),
            _ => Err(DeviceError::CircuitConversion(format!(
                "Unsupported provider: {}",
                provider_id
            ))),
        }
    }

    // IonQ specific circuit format conversion
    fn circuit_to_ionq_format<const N: usize>(_circuit: &Circuit<N>) -> DeviceResult<String> {
        // IonQ uses a JSON circuit format
        use serde_json::json;

        // This is a placeholder for the actual conversion logic
        #[allow(unused_variables)]
        let gates: Vec<serde_json::Value> = vec![]; // Convert gates to IonQ format

        let ionq_circuit = json!({
            "qubits": N,
            "circuit": gates,
        });

        Ok(ionq_circuit.to_string())
    }

    // Microsoft QIR format conversion
    fn circuit_to_qir_format<const N: usize>(_circuit: &Circuit<N>) -> DeviceResult<String> {
        // QIR is a LLVM IR based format
        // For now, this is just a placeholder
        Err(DeviceError::CircuitConversion(
            "QIR conversion not yet implemented".to_string(),
        ))
    }

    // QASM format conversion for Quantinuum
    fn circuit_to_qasm_format<const N: usize>(_circuit: &Circuit<N>) -> DeviceResult<String> {
        // Similar to IBM's QASM format
        let mut qasm = String::from("OPENQASM 2.0;\ninclude \"qelib1.inc\";\n\n");

        // Define the quantum and classical registers
        qasm.push_str(&format!("qreg q[{}];\n", N));
        qasm.push_str(&format!("creg c[{}];\n\n", N));

        // Implement conversion of gates to QASM here
        // For now, just return placeholder QASM
        Ok(qasm)
    }
}

#[cfg(not(feature = "azure"))]
impl AzureQuantumClient {
    pub fn new(
        _token: &str,
        _subscription_id: &str,
        _resource_group: &str,
        _workspace: &str,
        _region: Option<&str>,
    ) -> DeviceResult<Self> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled. Recompile with the 'azure' feature.".to_string(),
        ))
    }

    pub async fn list_providers(&self) -> DeviceResult<Vec<AzureProvider>> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }

    pub async fn list_targets(&self) -> DeviceResult<Vec<AzureTarget>> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }

    pub async fn get_target(&self, _target_id: &str) -> DeviceResult<AzureTarget> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }

    pub async fn submit_circuit(
        &self,
        _target_id: &str,
        _provider_id: &str,
        _config: AzureCircuitConfig,
    ) -> DeviceResult<String> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }

    pub async fn get_job_status(&self, _job_id: &str) -> DeviceResult<AzureJobStatus> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }

    pub async fn get_job_result(&self, _job_id: &str) -> DeviceResult<AzureJobResult> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }

    pub async fn wait_for_job(
        &self,
        _job_id: &str,
        _timeout_secs: Option<u64>,
    ) -> DeviceResult<AzureJobResult> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }

    pub async fn submit_circuits_parallel(
        &self,
        _target_id: &str,
        _provider_id: &str,
        _configs: Vec<AzureCircuitConfig>,
    ) -> DeviceResult<Vec<String>> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }

    pub fn circuit_to_provider_format<const N: usize>(
        _circuit: &Circuit<N>,
        _provider_id: &str,
    ) -> DeviceResult<String> {
        Err(DeviceError::UnsupportedDevice(
            "Azure Quantum support not enabled".to_string(),
        ))
    }
}