quantrs2-anneal 0.1.3

Quantum annealing support 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
# Quick Start Guide

Get up and running with QuantRS2-Anneal in minutes! This guide covers installation, basic usage, and your first optimization problems.

## Installation

### Basic Installation

Add QuantRS2-Anneal to your `Cargo.toml`:

```toml
[dependencies]
quantrs2-anneal = "0.1"
```

### With Cloud Features

For cloud quantum hardware access:

```toml
[dependencies]
quantrs2-anneal = { version = "0.1", features = ["dwave", "braket", "fujitsu"] }
```

### Build and Test

```bash
# Basic build
cargo build

# Build with all features
cargo build --all-features

# Run tests
cargo test

# Run examples
cargo run --example simple_penalty_optimization
```

## Your First Optimization Problem

### Hello World: Max-Cut Problem

Let's solve a simple Maximum Cut problem on a 4-node graph:

```rust
use quantrs2_anneal::{
    ising::IsingModel,
    simulator::{ClassicalAnnealingSimulator, AnnealingParams}
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a 4-qubit Max-Cut problem (square graph)
    let mut model = IsingModel::new(4);
    
    // Add edges with negative couplings (encourages cutting)
    model.set_coupling(0, 1, -1.0)?;  // Edge 0-1
    model.set_coupling(1, 2, -1.0)?;  // Edge 1-2
    model.set_coupling(2, 3, -1.0)?;  // Edge 2-3
    model.set_coupling(3, 0, -1.0)?;  // Edge 3-0
    
    // Create and configure simulator
    let params = AnnealingParams {
        num_sweeps: 1000,
        num_repetitions: 10,
        initial_temperature: 2.0,
        final_temperature: 0.1,
        ..Default::default()
    };
    
    let simulator = ClassicalAnnealingSimulator::new(params)?;
    
    // Solve the problem
    let result = simulator.solve(&model)?;
    
    // Print results
    println!("Max-Cut Solution:");
    println!("  Best energy: {}", result.best_energy);
    println!("  Best solution: {:?}", result.best_spins);
    println!("  Cut edges: {}", count_cut_edges(&result.best_spins));
    
    Ok(())
}

fn count_cut_edges(spins: &[i8]) -> usize {
    let edges = [(0, 1), (1, 2), (2, 3), (3, 0)];
    edges.iter()
        .filter(|(i, j)| spins[*i] != spins[*j])
        .count()
}
```

### Output
```
Max-Cut Solution:
  Best energy: -4.0
  Best solution: [1, -1, 1, -1]
  Cut edges: 4
```

## Common Problem Types

### 1. QUBO Problem with Constraints

Portfolio selection: choose exactly 3 out of 5 assets to maximize return while minimizing risk.

```rust
use quantrs2_anneal::{
    qubo::{QuboBuilder, QuboFormulation},
    simulator::ClassicalAnnealingSimulator
};

fn portfolio_optimization() -> Result<(), Box<dyn std::error::Error>> {
    // Expected returns for 5 assets
    let returns = vec![0.12, 0.08, 0.15, 0.10, 0.09];
    
    // Risk matrix (simplified)
    let risk_matrix = vec![
        vec![0.04, 0.01, 0.02, 0.01, 0.01],
        vec![0.01, 0.02, 0.01, 0.01, 0.01],
        vec![0.02, 0.01, 0.06, 0.02, 0.01],
        vec![0.01, 0.01, 0.02, 0.03, 0.01],
        vec![0.01, 0.01, 0.01, 0.01, 0.02],
    ];
    
    let mut qubo = QuboBuilder::new(5);
    
    // Objective: maximize return - risk_penalty * risk
    for i in 0..5 {
        qubo.add_linear_term(i, -returns[i])?; // Negative for maximization
        for j in 0..5 {
            qubo.add_quadratic_term(i, j, 0.5 * risk_matrix[i][j])?;
        }
    }
    
    // Constraint: select exactly 3 assets
    let selection_vars: Vec<usize> = (0..5).collect();
    let selection_coeffs = vec![1.0; 5];
    qubo.add_constraint_eq(&selection_vars, &selection_coeffs, 3.0, 10.0)?;
    
    // Solve
    let formulation = qubo.build()?;
    let ising_model = formulation.to_ising_model()?;
    
    let simulator = ClassicalAnnealingSimulator::new(Default::default())?;
    let result = simulator.solve(&ising_model)?;
    
    // Convert back to binary solution
    let portfolio = formulation.to_binary_solution(&result.best_spins)?;
    
    println!("Portfolio Optimization:");
    println!("  Selected assets: {:?}", portfolio);
    
    let selected_assets: Vec<usize> = portfolio.iter()
        .enumerate()
        .filter(|(_, &selected)| selected == 1)
        .map(|(i, _)| i)
        .collect();
    
    println!("  Asset indices: {:?}", selected_assets);
    
    let total_return: f64 = selected_assets.iter()
        .map(|&i| returns[i])
        .sum();
    
    println!("  Expected return: {:.1}%", total_return * 100.0);
    
    Ok(())
}
```

### 2. Graph Coloring Problem

Color a graph with minimum colors such that no adjacent nodes have the same color.

```rust
use quantrs2_anneal::{
    ising::IsingModel,
    applications::graph_problems::GraphColoringBuilder
};

fn graph_coloring() -> Result<(), Box<dyn std::error::Error>> {
    // Define a simple graph (triangle + one extra node)
    let edges = vec![(0, 1), (1, 2), (2, 0), (0, 3)];
    let num_nodes = 4;
    let num_colors = 3;
    
    let builder = GraphColoringBuilder::new(num_nodes, num_colors);
    let ising_model = builder.build_ising_model(&edges)?;
    
    let simulator = ClassicalAnnealingSimulator::new(Default::default())?;
    let result = simulator.solve(&ising_model)?;
    
    // Decode coloring
    let coloring = builder.decode_coloring(&result.best_spins)?;
    
    println!("Graph Coloring:");
    for (node, color) in coloring.iter().enumerate() {
        println!("  Node {}: Color {}", node, color);
    }
    
    // Verify no conflicts
    let conflicts = edges.iter()
        .filter(|(i, j)| coloring[*i] == coloring[*j])
        .count();
    
    println!("  Conflicts: {}", conflicts);
    println!("  Valid coloring: {}", conflicts == 0);
    
    Ok(())
}
```

## Cloud Quantum Computing

### D-Wave Leap (Requires API Token)

```rust
#[cfg(feature = "dwave")]
use quantrs2_anneal::dwave::{DWaveClient, SolverSelector, SolverCategory};

#[cfg(feature = "dwave")]
async fn dwave_example() -> Result<(), Box<dyn std::error::Error>> {
    // Set your D-Wave API token
    let api_token = std::env::var("DWAVE_API_TOKEN")
        .expect("Set DWAVE_API_TOKEN environment variable");
    
    // Create client
    let client = DWaveClient::new(api_token, None)?;
    
    // Auto-select a QPU solver
    let selector = SolverSelector {
        category: SolverCategory::QPU,
        online_only: true,
        ..Default::default()
    };
    
    match client.select_solver(Some(&selector)) {
        Ok(solver) => {
            println!("Selected solver: {}", solver.name);
            
            // Create a simple problem
            let mut model = IsingModel::new(4);
            model.set_coupling(0, 1, -1.0)?;
            model.set_coupling(1, 2, -1.0)?;
            model.set_coupling(2, 3, -1.0)?;
            model.set_coupling(3, 0, -1.0)?;
            
            // Submit with automatic embedding
            let solution = client.submit_ising_with_embedding(
                &model,
                None, // Auto-select solver
                None, // Default parameters
                None, // Auto-embedding
            )?;
            
            println!("D-Wave Solution:");
            println!("  Problem ID: {}", solution.problem_id);
            println!("  Best energy: {}", 
                     solution.energies.iter().fold(f64::INFINITY, |a, &b| a.min(b)));
        }
        Err(e) => {
            println!("No QPU available: {}. Using simulator instead.", e);
            // Fall back to classical simulation
        }
    }
    
    Ok(())
}

#[cfg(not(feature = "dwave"))]
fn dwave_example() {
    println!("D-Wave support not compiled. Enable with --features dwave");
}
```

### AWS Braket (Requires AWS Credentials)

```rust
#[cfg(feature = "braket")]
use quantrs2_anneal::braket::{BraketClient, DeviceSelector, DeviceType};

#[cfg(feature = "braket")]
async fn braket_example() -> Result<(), Box<dyn std::error::Error>> {
    // Set up AWS credentials (or use IAM roles)
    let access_key = std::env::var("AWS_ACCESS_KEY_ID")?;
    let secret_key = std::env::var("AWS_SECRET_ACCESS_KEY")?;
    let region = std::env::var("AWS_REGION").unwrap_or("us-east-1".to_string());
    
    let client = BraketClient::new(access_key, secret_key, region)?;
    
    // Auto-select a simulator (free)
    let selector = DeviceSelector {
        device_type: Some(DeviceType::Simulator),
        ..Default::default()
    };
    
    match client.select_device(Some(&selector)) {
        Ok(device) => {
            println!("Selected device: {}", device.device_name);
            
            // Create problem
            let mut model = IsingModel::new(4);
            model.set_coupling(0, 1, -1.0)?;
            model.set_coupling(1, 2, -1.0)?;
            
            // Submit task
            let task_result = client.submit_ising(&model, None, None)?;
            
            println!("AWS Braket Task:");
            println!("  Task ARN: {}", task_result.task_arn);
            println!("  Status: {:?}", task_result.task_status);
        }
        Err(e) => {
            println!("No devices available: {}", e);
        }
    }
    
    Ok(())
}

#[cfg(not(feature = "braket"))]
fn braket_example() {
    println!("AWS Braket support not compiled. Enable with --features braket");
}
```

## Advanced Features

### Population Annealing (High Quality Solutions)

```rust
use quantrs2_anneal::population_annealing::{
    PopulationAnnealingSimulator, PopulationParams
};

fn high_quality_optimization() -> Result<(), Box<dyn std::error::Error>> {
    // Create a more complex problem
    let mut model = IsingModel::new(10);
    
    // Add random couplings
    use rand::Rng;
    let mut rng = rand::thread_rng();
    
    for i in 0..10 {
        for j in (i + 1)..10 {
            if rng.gen::<f64>() < 0.3 { // 30% connectivity
                let strength = rng.gen_range(-2.0..2.0);
                model.set_coupling(i, j, strength)?;
            }
        }
    }
    
    // Use population annealing for high-quality solutions
    let pop_params = PopulationParams {
        population_size: 1000,
        num_sweeps: 2000,
        resampling_threshold: 0.5,
        temperature_schedule: vec![2.0, 1.5, 1.0, 0.5, 0.2, 0.1, 0.05],
        ..Default::default()
    };
    
    let pop_simulator = PopulationAnnealingSimulator::new(pop_params)?;
    let result = pop_simulator.solve(&model)?;
    
    println!("Population Annealing Result:");
    println!("  Best energy: {}", result.best_energy);
    println!("  Population diversity: {:.3}", result.final_diversity);
    println!("  Effective sample size: {}", result.effective_sample_size);
    
    Ok(())
}
```

### Coherent Ising Machine (Large Problems)

```rust
use quantrs2_anneal::coherent_ising_machine::{
    CoherentIsingMachine, CIMParams
};

fn large_scale_optimization() -> Result<(), Box<dyn std::error::Error>> {
    // Create a large sparse problem
    let mut model = IsingModel::new(1000);
    
    // Add sparse connectivity (ring + random long-range)
    for i in 0..1000 {
        // Ring connectivity
        model.set_coupling(i, (i + 1) % 1000, -1.0)?;
        
        // Random long-range connections
        if rand::random::<f64>() < 0.01 {
            let j = rand::random::<usize>() % 1000;
            if i != j {
                model.set_coupling(i, j, rand::random::<f64>() * 2.0 - 1.0)?;
            }
        }
    }
    
    // Use CIM for large-scale optimization
    let cim_params = CIMParams {
        pump_power: 2.0,
        detuning: 0.1,
        coupling_strength: 0.5,
        num_iterations: 1000,
        convergence_threshold: 1e-6,
        ..Default::default()
    };
    
    let cim = CoherentIsingMachine::new(cim_params)?;
    let result = cim.solve(&model)?;
    
    println!("Coherent Ising Machine Result:");
    println!("  Problem size: {} variables", model.num_qubits);
    println!("  Best energy: {}", result.best_energy);
    println!("  Convergence iterations: {}", result.iterations_to_convergence);
    println!("  Final amplitude variance: {:.6}", result.final_variance);
    
    Ok(())
}
```

## Problem Builder DSL

For complex problems, use the Domain-Specific Language:

```rust
use quantrs2_anneal::dsl::{ProblemBuilder, Variable, Constraint};

fn dsl_example() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = ProblemBuilder::new();
    
    // Define variables
    let x = builder.binary_var("x")?;
    let y = builder.binary_var("y")?;
    let z = builder.binary_var("z")?;
    
    // Define objective: minimize x + y - 2*z + x*y
    builder.minimize(
        x + y - 2*z + x*y
    )?;
    
    // Add constraints
    builder.add_constraint(
        x + y + z == 2  // Exactly 2 variables should be 1
    )?;
    
    builder.add_constraint(
        x - y >= 0      // x should be >= y
    )?;
    
    // Build and solve
    let problem = builder.build()?;
    let ising_model = problem.to_ising_model()?;
    
    let simulator = ClassicalAnnealingSimulator::new(Default::default())?;
    let result = simulator.solve(&ising_model)?;
    
    // Extract solution
    let solution = problem.extract_solution(&result.best_spins)?;
    
    println!("DSL Problem Solution:");
    for (var_name, value) in solution {
        println!("  {}: {}", var_name, value);
    }
    
    Ok(())
}
```

## Performance Tips

### 1. Choose the Right Algorithm

```rust
fn choose_algorithm(problem_size: usize, quality_requirement: f64) -> Box<dyn Optimizer> {
    match (problem_size, quality_requirement) {
        // Small problems, any quality
        (size, _) if size < 50 => 
            Box::new(ClassicalAnnealingSimulator::default()),
        
        // Medium problems, high quality needed
        (size, quality) if size < 500 && quality > 0.95 => 
            Box::new(PopulationAnnealingSimulator::default()),
        
        // Large problems
        (size, _) if size > 1000 => 
            Box::new(CoherentIsingMachine::default()),
        
        // Default
        _ => Box::new(ClassicalAnnealingSimulator::default())
    }
}
```

### 2. Use Parallel Processing

```rust
use quantrs2_anneal::simulator::ParallelAnnealingSimulator;

fn parallel_optimization() -> Result<(), Box<dyn std::error::Error>> {
    let params = AnnealingParams::default();
    let parallel_simulator = ParallelAnnealingSimulator::new(
        params,
        num_threads: num_cpus::get(),
    )?;
    
    let result = parallel_simulator.solve(&model)?;
    
    println!("Parallel result: {}", result.best_energy);
    Ok(())
}
```

## Next Steps

### 1. Explore Examples

Check out the [`examples/`](../examples/) directory for more comprehensive examples:

```bash
# Run all examples
cargo run --example simple_penalty_optimization
cargo run --example advanced_embedding --features dwave
cargo run --example coherent_ising_machine_example
cargo run --example energy_landscape_visualization
```

### 2. Read Documentation

- [Embedding Guide]embedding_guide.md - Graph embedding techniques
- [Performance Guide]performance.md - Optimization strategies
- [D-Wave Client Guide]dwave_leap_client.md - Cloud quantum computing
- [AWS Braket Guide]aws_braket_client.md - Multi-vendor quantum access

### 3. Set Up Cloud Access

#### For D-Wave Leap:
1. Register at [D-Wave Leap]https://cloud.dwavesys.com/
2. Get your API token
3. Set environment variable: `export DWAVE_API_TOKEN="your_token"`

#### For AWS Braket:
1. Set up AWS account with Braket access
2. Configure credentials:
   ```bash
   export AWS_ACCESS_KEY_ID="your_key"
   export AWS_SECRET_ACCESS_KEY="your_secret"
   export AWS_REGION="us-east-1"
   ```

### 4. Try Real Applications

```rust
// Energy optimization
use quantrs2_anneal::applications::energy::PowerGridOptimizer;

// Financial optimization  
use quantrs2_anneal::applications::finance::PortfolioOptimizer;

// Logistics optimization
use quantrs2_anneal::applications::logistics::VehicleRoutingOptimizer;
```

## Troubleshooting

### Common Issues

1. **Compilation Errors with Features**
   ```bash
   # Make sure you enable the right features
   cargo build --features dwave,braket
   ```

2. **Poor Solution Quality**
   ```rust
   // Increase annealing parameters
   let params = AnnealingParams {
       num_sweeps: 10000,        // More sweeps
       num_repetitions: 100,     // More runs
       ..Default::default()
   };
   ```

3. **Memory Issues with Large Problems**
   ```rust
   // Use sparse matrices
   let sparse_config = SparseMatrixConfig::default();
   let model = IsingModel::with_sparse_config(num_qubits, sparse_config)?;
   ```

### Getting Help

- [GitHub Issues]https://github.com/cool-japan/quantrs/issues
- [API Documentation]https://docs.rs/quantrs2-anneal
- [Examples Directory]../examples/

---

**🎉 Congratulations! You're now ready to solve optimization problems with quantum annealing!**

For more advanced features and optimization techniques, check out the comprehensive guides in the [`docs/`](../docs/) directory.