quantrs2-tytan 0.1.3

High-level quantum annealing interface inspired by Tytan 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
# Migration Guide to QuantRS2-Tytan

This guide helps users migrate from other quantum optimization frameworks to QuantRS2-Tytan.

## Table of Contents

1. [From Python Tytan]#from-python-tytan
2. [From D-Wave Ocean]#from-d-wave-ocean
3. [From Qiskit Optimization]#from-qiskit-optimization
4. [From Microsoft QIO]#from-microsoft-qio
5. [From PennyLane]#from-pennylane
6. [General Migration Tips]#general-migration-tips

## From Python Tytan

QuantRS2-Tytan is inspired by Python Tytan, making migration straightforward.

### Symbol Definition

**Python Tytan:**
```python
from tytan import symbols

q = symbols("q")
x, y, z = symbols("x y z")
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::symbol::symbols;

let q = symbols("q");
let x = symbols("x");
let y = symbols("y");
let z = symbols("z");
```

### Expression Building

**Python Tytan:**
```python
H = (x + y + z - 2)**2
```

**QuantRS2-Tytan:**
```rust
let h = (x + y + z - 2).pow(2);
```

### Compilation

**Python Tytan:**
```python
from tytan import compile

qubo, offset = compile(H).get_qubo()
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::compile::Compile;

let (qubo, offset) = Compile::new(&h).get_qubo()?;
```

### Sampling

**Python Tytan:**
```python
from tytan.sampler import SASampler

solver = SASampler()
result = solver.run(qubo, shots=100)
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::sampler::{SASampler, Sampler};

let solver = SASampler::new(None);
let result = solver.run_qubo(&qubo, 100)?;
```

### Auto Array Processing

**Python Tytan:**
```python
from tytan import Auto_array

auto = Auto_array(result)
for r in auto.calc_score():
    print(r)
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::auto_array::Auto_array;

let auto = Auto_array::new();
let scores = auto.calc_score(result);
for r in scores {
    println!("{:?}", r);
}
```

## From D-Wave Ocean

### Binary Quadratic Model (BQM)

**D-Wave Ocean:**
```python
from dimod import BinaryQuadraticModel

bqm = BinaryQuadraticModel('BINARY')
bqm.add_variable('x', -1)
bqm.add_variable('y', -1)
bqm.add_interaction('x', 'y', 2)
```

**QuantRS2-Tytan:**
```rust
use ndarray::Array2;
use std::collections::HashMap;

let mut qubo = Array2::zeros((2, 2));
qubo[[0, 0]] = -1.0;  // x
qubo[[1, 1]] = -1.0;  // y
qubo[[0, 1]] = 2.0;   // x*y interaction
qubo[[1, 0]] = 2.0;   // symmetric

let mut var_map = HashMap::new();
var_map.insert("x".to_string(), 0);
var_map.insert("y".to_string(), 1);
```

### Constraint Satisfaction Problems (CSP)

**D-Wave Ocean:**
```python
from dimod import ConstraintSatisfactionProblem

csp = ConstraintSatisfactionProblem()
csp.add_constraint(lambda x, y: x + y == 1, ['x', 'y'])
bqm = csp.get_bqm()
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::constraints::add_equality_constraint;

// Add constraint: x + y = 1
add_equality_constraint(&mut qubo, &[0, 1], 1.0, 10.0);
```

### Embedding

**D-Wave Ocean:**
```python
from minorminer import find_embedding

embedding = find_embedding(source_graph, target_graph)
embedded_bqm = embed_bqm(bqm, embedding, target_graph)
```

**QuantRS2-Tytan:**
```rust
// Automatic embedding handled by DWaveSampler
use quantrs2_tytan::sampler::DWaveSampler;

let solver = DWaveSampler::new(None);
let result = solver.run_qubo(&qubo, 100)?;
```

### Hybrid Solvers

**D-Wave Ocean:**
```python
from hybrid import SimulatedAnnealingProblemSampler

sampler = SimulatedAnnealingProblemSampler()
result = sampler.sample(bqm, num_reads=100)
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::hybrid_algorithms::HybridSolver;

let solver = HybridSolver::new(Default::default());
let result = solver.run_qubo(&qubo, 100)?;
```

## From Qiskit Optimization

### Quadratic Program

**Qiskit:**
```python
from qiskit_optimization import QuadraticProgram

qp = QuadraticProgram()
qp.binary_var('x')
qp.binary_var('y')
qp.minimize(quadratic={'x': -1, 'y': -1, ('x', 'y'): 2})
```

**QuantRS2-Tytan:**
```rust
// Using symbolic math (requires 'dwave' feature)
use quantrs2_tytan::{symbols, Compile};

let x = symbols("x");
let y = symbols("y");
let expr = -x - y + 2*x*y;

let (qubo, _) = Compile::new(&expr).get_qubo()?;
```

### VQE Integration

**Qiskit:**
```python
from qiskit.algorithms import VQE
from qiskit.primitives import Estimator

vqe = VQE(estimator=Estimator(), ansatz=ansatz, optimizer=optimizer)
result = vqe.compute_minimum_eigenvalue(operator)
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::quantum_optimization_extensions::VQEOptimizer;

let vqe = VQEOptimizer::new(Default::default());
let result = vqe.minimize(&qubo)?;
```

### Constraint Handling

**Qiskit:**
```python
qp.linear_constraint(['x', 'y'], [1, 1], '==', 1)
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::optimization::constraints::LinearConstraint;

let constraint = LinearConstraint::equality(vec![0, 1], vec![1.0, 1.0], 1.0);
let penalized_qubo = constraint.apply_to_qubo(&qubo, 10.0);
```

## From Microsoft QIO

### Problem Definition

**Microsoft QIO:**
```python
from azure.quantum.optimization import Problem, ProblemType

problem = Problem(name="my-problem", problem_type=ProblemType.ising)
problem.add_term(c=-1, indices=[0])
problem.add_term(c=-1, indices=[1])
problem.add_term(c=2, indices=[0, 1])
```

**QuantRS2-Tytan:**
```rust
use quantrs2_anneal::{IsingModel, QuboModel};

// As QUBO
let mut qubo = QuboModel::new();
qubo.add_term(&[0], -1.0);
qubo.add_term(&[1], -1.0);
qubo.add_term(&[0, 1], 2.0);

// Or as Ising
let mut ising = IsingModel::new();
ising.add_field(0, -1.0);
ising.add_field(1, -1.0);
ising.add_coupling(0, 1, 2.0);
```

### Solver Configuration

**Microsoft QIO:**
```python
from azure.quantum.optimization import SimulatedAnnealing

solver = SimulatedAnnealing(workspace, timeout=100, seed=42)
result = solver.optimize(problem)
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::sampler::{SASampler, Config};

let solver = SASampler::new(Some(Config {
    num_sweeps: 10000,
    temperature_schedule: Schedule::Geometric(100.0, 0.1),
    seed: Some(42),
}));
let result = solver.run_qubo(&qubo_data, 100)?;
```

## From PennyLane

### QAOA

**PennyLane:**
```python
import pennylane as qml

dev = qml.device('default.qubit', wires=n_qubits)

@qml.qnode(dev)
def qaoa_circuit(params):
    # QAOA circuit implementation
    return qml.expval(cost_h)
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::quantum_optimization_extensions::QAOA;

let qaoa = QAOA::new(num_layers, Default::default());
let result = qaoa.optimize(&qubo)?;
```

### Variational Circuits

**PennyLane:**
```python
def variational_circuit(params, wires):
    for i in range(len(wires)):
        qml.RY(params[i], wires=i)
    qml.broadcast(qml.CNOT, wires=wires, pattern="chain")
```

**QuantRS2-Tytan:**
```rust
use quantrs2_tytan::variational_quantum_factoring::VariationalCircuit;

let circuit = VariationalCircuit::new(num_qubits);
circuit.add_ry_layer(&params);
circuit.add_entangling_layer();
```

## General Migration Tips

### 1. Data Structure Conversion

Most frameworks use different data structures. Here's how to convert:

```rust
// From dictionary/map representation
fn dict_to_qubo(linear: HashMap<usize, f64>, 
                quadratic: HashMap<(usize, usize), f64>, 
                num_vars: usize) -> Array2<f64> {
    let mut qubo = Array2::zeros((num_vars, num_vars));
    
    // Add linear terms
    for (var, coeff) in linear {
        qubo[[var, var]] = coeff;
    }
    
    // Add quadratic terms
    for ((i, j), coeff) in quadratic {
        qubo[[i, j]] = coeff;
        qubo[[j, i]] = coeff;  // Symmetric
    }
    
    qubo
}
```

### 2. Async/Parallel Execution

QuantRS2-Tytan supports parallel execution natively:

```rust
use rayon::prelude::*;

// Run multiple problems in parallel
let results: Vec<_> = problems
    .par_iter()
    .map(|problem| solver.run_qubo(problem, 100))
    .collect();
```

### 3. Custom Samplers

Create custom samplers by implementing the `Sampler` trait:

```rust
use quantrs2_tytan::sampler::{Sampler, SampleResult};

struct MySampler {
    // Your fields
}

impl Sampler for MySampler {
    fn run_qubo(&self, 
                qubo: &(Array2<f64>, HashMap<String, usize>), 
                num_samples: usize) -> Result<Vec<SampleResult>, Box<dyn Error>> {
        // Your implementation
        todo!()
    }
    
    fn run_hobo(&self,
                hobo: &(ArrayD<f64>, HashMap<String, usize>),
                num_samples: usize) -> Result<Vec<SampleResult>, Box<dyn Error>> {
        // Your implementation
        todo!()
    }
}
```

### 4. Performance Optimization

Enable features for better performance:

```toml
[dependencies]
quantrs2-tytan = { 
    version = "0.1.0",
    features = ["gpu", "scirs", "parallel"]
}
```

### 5. Error Handling

QuantRS2-Tytan uses Rust's Result type for error handling:

```rust
// Python style (exceptions)
# try:
#     result = solver.sample(bqm)
# except Exception as e:
#     print(f"Error: {e}")

// Rust style (Result)
match solver.run_qubo(&qubo, 100) {
    Ok(result) => {
        // Process result
    },
    Err(e) => {
        eprintln!("Error: {}", e);
    }
}

// Or use ? operator
let result = solver.run_qubo(&qubo, 100)?;
```

### 6. Integration with Existing Code

For gradual migration, you can use Python bindings:

```python
# Install the Python package
# pip install quantrs2

import quantrs2

# Use QuantRS2-Tytan from Python
solver = quantrs2.tytan.SASampler()
result = solver.run_qubo(qubo_matrix, var_map, num_samples=100)
```

## Feature Comparison Table

| Feature | D-Wave Ocean | Qiskit | QIO | PennyLane | QuantRS2-Tytan |
|---------|--------------|--------|-----|-----------|----------------|
| Symbolic Math ||||||
| GPU Support || Limited ||||
| HOBO Support ||||||
| Native Performance ||||||
| Cloud Integration ||||||
| Constraint Handling ||||||
| Hybrid Algorithms ||||||
| Problem Decomposition | Limited |||||
| ML Integration || Limited ||||

## Common Pitfalls and Solutions

### 1. Variable Indexing
- Most frameworks use string-based variable names
- QuantRS2-Tytan uses both strings and indices
- Always maintain a variable map for consistency

### 2. Matrix Representation
- Some frameworks use upper-triangular matrices
- QuantRS2-Tytan uses full symmetric matrices
- Convert carefully to avoid doubling coefficients

### 3. Energy Sign Convention
- Check whether the framework minimizes or maximizes
- QuantRS2-Tytan minimizes by default
- Negate your objective if needed

### 4. Constraint Penalties
- Different frameworks use different penalty scaling
- Start with penalty = 10 * max(|coefficients|)
- Tune based on your specific problem

### 5. Result Format
- Results may be in different formats (dict, array, etc.)
- QuantRS2-Tytan returns structured `SampleResult` objects
- Use provided conversion utilities

## Getting Help

1. Check the [API Reference]API_REFERENCE.md
2. Review [examples]../examples/
3. Open an issue on [GitHub]https://github.com/cool-japan/quantrs
4. Join our community discussions