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
//! Azure Quantum Sampler Implementation
//!
//! This module provides integration with Microsoft Azure Quantum
//! for solving optimization problems using various quantum and quantum-inspired solvers.
use scirs2_core::ndarray::{Array, Ix2};
use scirs2_core::random::{thread_rng, Rng, RngExt};
use std::collections::HashMap;
use quantrs2_anneal::QuboModel;
use super::super::{SampleResult, Sampler, SamplerError, SamplerResult};
/// Azure Quantum solver types
#[derive(Debug, Clone)]
pub enum AzureSolver {
/// Microsoft QIO - Simulated Annealing
SimulatedAnnealing,
/// Microsoft QIO - Parallel Tempering
ParallelTempering,
/// Microsoft QIO - Tabu Search
TabuSearch,
/// Microsoft QIO - Population Annealing
PopulationAnnealing,
/// Microsoft QIO - Substrate Monte Carlo
SubstrateMonteCarlo,
/// IonQ quantum computer
IonQ,
/// Quantinuum (Honeywell) quantum computer
Quantinuum,
/// Rigetti quantum computer
Rigetti,
}
/// Azure Quantum Sampler Configuration
#[derive(Debug, Clone)]
pub struct AzureQuantumConfig {
/// Azure subscription ID
pub subscription_id: String,
/// Resource group name
pub resource_group: String,
/// Workspace name
pub workspace_name: String,
/// Solver to use
pub solver: AzureSolver,
/// Timeout in seconds
pub timeout: u64,
/// Additional solver-specific parameters
pub solver_params: HashMap<String, String>,
}
impl Default for AzureQuantumConfig {
fn default() -> Self {
Self {
subscription_id: String::new(),
resource_group: String::new(),
workspace_name: String::new(),
solver: AzureSolver::SimulatedAnnealing,
timeout: 300,
solver_params: HashMap::new(),
}
}
}
/// Azure Quantum Sampler
///
/// This sampler connects to Microsoft Azure Quantum to solve QUBO problems
/// using various quantum and quantum-inspired optimization solvers.
pub struct AzureQuantumSampler {
config: AzureQuantumConfig,
}
impl AzureQuantumSampler {
/// Create a new Azure Quantum sampler
///
/// # Arguments
///
/// * `config` - The Azure Quantum configuration
#[must_use]
pub const fn new(config: AzureQuantumConfig) -> Self {
Self { config }
}
/// Create a new Azure Quantum sampler with workspace details
///
/// # Arguments
///
/// * `subscription_id` - Azure subscription ID
/// * `resource_group` - Resource group name
/// * `workspace_name` - Workspace name
#[must_use]
pub fn with_workspace(
subscription_id: &str,
resource_group: &str,
workspace_name: &str,
) -> Self {
Self {
config: AzureQuantumConfig {
subscription_id: subscription_id.to_string(),
resource_group: resource_group.to_string(),
workspace_name: workspace_name.to_string(),
..Default::default()
},
}
}
/// Set the solver to use
#[must_use]
pub const fn with_solver(mut self, solver: AzureSolver) -> Self {
self.config.solver = solver;
self
}
/// Set the timeout
#[must_use]
pub const fn with_timeout(mut self, timeout: u64) -> Self {
self.config.timeout = timeout;
self
}
/// Add a solver-specific parameter
#[must_use]
pub fn with_param(mut self, key: String, value: String) -> Self {
self.config.solver_params.insert(key, value);
self
}
}
impl Sampler for AzureQuantumSampler {
fn run_qubo(
&self,
qubo: &(Array<f64, Ix2>, HashMap<String, usize>),
shots: usize,
) -> SamplerResult<Vec<SampleResult>> {
// Extract matrix and variable mapping
let (matrix, var_map) = qubo;
// Get the problem dimension
let n_vars = var_map.len();
// Validate problem size based on solver
match self.config.solver {
AzureSolver::IonQ => {
if n_vars > 29 {
return Err(SamplerError::InvalidParameter(
"IonQ currently supports up to 29 qubits".to_string(),
));
}
}
AzureSolver::Quantinuum => {
if n_vars > 20 {
return Err(SamplerError::InvalidParameter(
"Quantinuum currently supports up to 20 qubits for this application"
.to_string(),
));
}
}
AzureSolver::Rigetti => {
if n_vars > 40 {
return Err(SamplerError::InvalidParameter(
"Rigetti currently supports up to 40 qubits".to_string(),
));
}
}
_ => {
// QIO solvers can handle larger problems
if n_vars > 10000 {
return Err(SamplerError::InvalidParameter(
"Problem size exceeds Azure QIO limits".to_string(),
));
}
}
}
// Map from indices back to variable names
let idx_to_var: HashMap<usize, String> = var_map
.iter()
.map(|(var, &idx)| (idx, var.clone()))
.collect();
// Convert ndarray to a QuboModel
let mut qubo_model = QuboModel::new(n_vars);
// Set linear and quadratic terms
for i in 0..n_vars {
if matrix[[i, i]] != 0.0 {
qubo_model.set_linear(i, matrix[[i, i]])?;
}
for j in (i + 1)..n_vars {
if matrix[[i, j]] != 0.0 {
qubo_model.set_quadratic(i, j, matrix[[i, j]])?;
}
}
}
// Azure Quantum REST API integration
#[cfg(feature = "azure_quantum")]
{
// Validate workspace credentials before any HTTP calls
if self.config.subscription_id.is_empty()
|| self.config.resource_group.is_empty()
|| self.config.workspace_name.is_empty()
{
return Err(SamplerError::ApiError(
"Azure Quantum workspace not configured. Call with_workspace() to provide \
subscription_id, resource_group, and workspace_name."
.to_string(),
));
}
// Determine the provider and target for the selected solver
let (provider_id, target_id) = match self.config.solver {
AzureSolver::SimulatedAnnealing => {
("microsoft.qio", "microsoft.simulatedannealing.cpu")
}
AzureSolver::ParallelTempering => {
("microsoft.qio", "microsoft.paralleltempering.cpu")
}
AzureSolver::TabuSearch => ("microsoft.qio", "microsoft.tabu.cpu"),
AzureSolver::PopulationAnnealing => {
("microsoft.qio", "microsoft.populationannealing.cpu")
}
AzureSolver::SubstrateMonteCarlo => {
("microsoft.qio", "microsoft.substochastic.cpu")
}
AzureSolver::IonQ => ("ionq", "ionq.qpu"),
AzureSolver::Quantinuum => ("quantinuum", "quantinuum.hqs-lt-s1"),
AzureSolver::Rigetti => ("rigetti", "rigetti.qpu.aspen-m-3"),
};
// Build the QIO-format cost function payload
let terms: Vec<serde_json::Value> = {
let mut t = Vec::new();
for i in 0..n_vars {
if matrix[[i, i]] != 0.0 {
t.push(serde_json::json!({
"c": matrix[[i, i]],
"ids": [i]
}));
}
for j in (i + 1)..n_vars {
if matrix[[i, j]] != 0.0 {
t.push(serde_json::json!({
"c": matrix[[i, j]],
"ids": [i, j]
}));
}
}
}
t
};
let problem_payload = serde_json::json!({
"cost_function": {
"type": "ising",
"version": "1.1",
"terms": terms
}
});
let mut solver_params = serde_json::json!({
"timeout": self.config.timeout,
"seed": 42u64
});
// Merge user-supplied solver params
for (k, v) in &self.config.solver_params {
if let Some(obj) = solver_params.as_object_mut() {
obj.insert(k.clone(), serde_json::Value::String(v.clone()));
}
}
let job_payload = serde_json::json!({
"id": uuid::Uuid::new_v4().to_string(),
"name": "qubo_job",
"providerId": provider_id,
"target": target_id,
"inputDataFormat": "microsoft.qio.v2",
"outputDataFormat": "microsoft.qio-results.v2",
"inputParams": solver_params,
"inputData": problem_payload
});
// Azure Quantum REST API base URL
let base_url = format!(
"https://{region}.quantum.azure.com/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Quantum/Workspaces/{ws}",
region = "eastus",
sub = self.config.subscription_id,
rg = self.config.resource_group,
ws = self.config.workspace_name
);
let jobs_url = format!("{base_url}/jobs");
let client = reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()
.map_err(|e| SamplerError::ApiError(format!("Failed to build HTTP client: {e}")))?;
// Submit the job
let submit_resp = client
.post(&jobs_url)
.header("Content-Type", "application/json")
.json(&job_payload)
.send()
.map_err(|e| {
SamplerError::ApiError(format!(
"Failed to submit Azure Quantum job: {e}. \
Ensure workspace credentials are correct and network is accessible."
))
})?;
if !submit_resp.status().is_success() {
let status = submit_resp.status();
let body = submit_resp
.text()
.unwrap_or_else(|_| "<unreadable>".to_string());
return Err(SamplerError::ApiError(format!(
"Azure Quantum job submission failed (HTTP {status}): {body}"
)));
}
let job_response: serde_json::Value = submit_resp.json().map_err(|e| {
SamplerError::ApiError(format!("Failed to parse Azure Quantum response: {e}"))
})?;
let job_id = job_response["id"]
.as_str()
.ok_or_else(|| {
SamplerError::ApiError("Missing job ID in Azure Quantum response".to_string())
})?
.to_string();
// Poll for job completion
let poll_interval = 5u64;
let max_polls = self.config.timeout / poll_interval + 1;
let mut poll_count = 0u64;
loop {
if poll_count >= max_polls {
return Err(SamplerError::ApiError(format!(
"Azure Quantum job {job_id} timed out after {max_polls} polls ({}s)",
self.config.timeout
)));
}
poll_count += 1;
std::thread::sleep(std::time::Duration::from_secs(poll_interval));
let status_url = format!("{jobs_url}/{job_id}");
let status_resp = client.get(&status_url).send().map_err(|e| {
SamplerError::ApiError(format!("Failed to poll Azure job status: {e}"))
})?;
let status_json: serde_json::Value = status_resp.json().map_err(|e| {
SamplerError::ApiError(format!("Failed to parse Azure status: {e}"))
})?;
match status_json["status"].as_str() {
Some("Succeeded") => break,
Some("Failed") => {
let reason = status_json["errorData"]["message"]
.as_str()
.unwrap_or("unknown error");
return Err(SamplerError::ApiError(format!(
"Azure Quantum job failed: {reason}"
)));
}
Some("Cancelled") => {
return Err(SamplerError::ApiError(
"Azure Quantum job was cancelled".to_string(),
));
}
_ => continue,
}
}
// Retrieve results
let output_url = format!("{jobs_url}/{job_id}/output");
let output_resp = client.get(&output_url).send().map_err(|e| {
SamplerError::ApiError(format!("Failed to retrieve Azure results: {e}"))
})?;
let output_json: serde_json::Value = output_resp.json().map_err(|e| {
SamplerError::ApiError(format!("Failed to parse Azure result: {e}"))
})?;
// Parse QIO result format: solutions array with configuration and cost
if let Some(solutions_arr) = output_json["solutions"].as_array() {
let mut parsed: Vec<SampleResult> = solutions_arr
.iter()
.map(|sol| {
let energy = sol["cost"].as_f64().unwrap_or(0.0);
let occurrences = sol["count"].as_u64().unwrap_or(1) as usize;
let assignments: HashMap<String, bool> =
if let Some(config_obj) = sol["configuration"].as_object() {
config_obj
.iter()
.filter_map(|(k, v)| {
k.parse::<usize>().ok().and_then(|idx| {
idx_to_var.get(&idx).map(|name| {
(name.clone(), v.as_i64().unwrap_or(0) > 0)
})
})
})
.collect()
} else {
HashMap::new()
};
SampleResult {
assignments,
energy,
occurrences,
}
})
.collect();
parsed.sort_by(|a, b| {
a.energy
.partial_cmp(&b.energy)
.unwrap_or(std::cmp::Ordering::Equal)
});
return Ok(parsed);
}
// Fall through to simulation path if result parsing fails
}
// Placeholder implementation - simulate Azure Quantum behavior
let mut results = Vec::new();
let mut rng = thread_rng();
// Different solvers have different characteristics
let unique_solutions = match self.config.solver {
AzureSolver::SimulatedAnnealing => shots.min(50),
AzureSolver::ParallelTempering => shots.min(100),
AzureSolver::TabuSearch => shots.min(30),
AzureSolver::PopulationAnnealing => shots.min(200),
AzureSolver::SubstrateMonteCarlo => shots.min(150),
AzureSolver::IonQ | AzureSolver::Quantinuum | AzureSolver::Rigetti => {
// Quantum hardware typically provides many measurement samples
shots.min(1000)
}
};
for _ in 0..unique_solutions {
let assignments: HashMap<String, bool> = idx_to_var
.values()
.map(|name| (name.clone(), rng.random::<bool>()))
.collect();
// Calculate energy
let mut energy = 0.0;
for (var_name, &val) in &assignments {
let i = var_map[var_name];
if val {
energy += matrix[[i, i]];
for (other_var, &other_val) in &assignments {
let j = var_map[other_var];
if i < j && other_val {
energy += matrix[[i, j]];
}
}
}
}
// Simulate measurement counts
let occurrences = match self.config.solver {
AzureSolver::IonQ | AzureSolver::Quantinuum | AzureSolver::Rigetti => {
// Quantum solvers return actual shot counts
rng.random_range(1..=(shots / unique_solutions + 10))
}
_ => {
// Classical solvers return occurrence frequencies
1
}
};
results.push(SampleResult {
assignments,
energy,
occurrences,
});
}
// Sort by energy (best solutions first)
results.sort_by(|a, b| {
a.energy
.partial_cmp(&b.energy)
.unwrap_or(std::cmp::Ordering::Equal)
});
// Limit results to requested number
results.truncate(shots.min(100));
Ok(results)
}
fn run_hobo(
&self,
hobo: &(
Array<f64, scirs2_core::ndarray::IxDyn>,
HashMap<String, usize>,
),
shots: usize,
) -> SamplerResult<Vec<SampleResult>> {
use scirs2_core::ndarray::Ix2;
// For HOBO problems, convert to QUBO if possible
if hobo.0.ndim() <= 2 {
// If it's already 2D, just forward to run_qubo
let qubo_matrix = hobo.0.clone().into_dimensionality::<Ix2>().map_err(|e| {
SamplerError::InvalidParameter(format!(
"Failed to convert HOBO to QUBO dimensionality: {e}"
))
})?;
let qubo = (qubo_matrix, hobo.1.clone());
self.run_qubo(&qubo, shots)
} else {
// Azure Quantum doesn't directly support higher-order problems
Err(SamplerError::InvalidParameter(
"Azure Quantum doesn't support HOBO problems directly. Use a quadratization technique first.".to_string()
))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_azure_quantum_config() {
let config = AzureQuantumConfig::default();
assert_eq!(config.timeout, 300);
assert!(matches!(config.solver, AzureSolver::SimulatedAnnealing));
}
#[test]
fn test_azure_quantum_sampler_creation() {
let sampler =
AzureQuantumSampler::with_workspace("test-subscription", "test-rg", "test-workspace")
.with_solver(AzureSolver::ParallelTempering)
.with_timeout(600)
.with_param("temperature".to_string(), "0.5".to_string());
assert_eq!(sampler.config.subscription_id, "test-subscription");
assert_eq!(sampler.config.resource_group, "test-rg");
assert_eq!(sampler.config.workspace_name, "test-workspace");
assert_eq!(sampler.config.timeout, 600);
assert!(matches!(
sampler.config.solver,
AzureSolver::ParallelTempering
));
}
#[test]
fn test_azure_solver_types() {
let solvers = [
AzureSolver::SimulatedAnnealing,
AzureSolver::ParallelTempering,
AzureSolver::TabuSearch,
AzureSolver::PopulationAnnealing,
AzureSolver::SubstrateMonteCarlo,
AzureSolver::IonQ,
AzureSolver::Quantinuum,
AzureSolver::Rigetti,
];
assert_eq!(solvers.len(), 8);
}
}