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
//! Variational Quantum Algorithms: VQE, QAOA, and hybrid solvers.
//!
//! [`VariationalSolver`] executes parameterised ansatz circuits on a
//! state-vector backend and minimises an expectation-value objective using
//! classical optimisers, supporting both VQE (chemistry) and QAOA (combinatorics).
use crate::error::{MLError, Result};
use crate::optimization::Optimizer;
use quantrs2_circuit::prelude::Circuit;
use scirs2_core::ndarray::Array1;
use scirs2_core::random::prelude::*;
use scirs2_core::Complex64;
/// Algorithm type for variational quantum algorithms
#[derive(Debug, Clone, Copy)]
pub enum VariationalAlgorithm {
/// Variational Quantum Eigensolver
VQE,
/// Quantum Approximate Optimization Algorithm
QAOA,
/// Quantum Support Vector Machine
QSVM,
/// Quantum Neural Network
QNN,
/// Custom variational algorithm
Custom,
}
/// Ansatz type for variational circuits
#[derive(Debug, Clone, Copy)]
pub enum AnsatzType {
/// Hardware efficient ansatz
HardwareEfficient,
/// Unitary Coupled Cluster Singles and Doubles
UCCSD,
/// QAOA ansatz
QAOA,
/// Custom ansatz
Custom,
}
/// Variational quantum circuit with parameterized gates
#[derive(Debug, Clone)]
pub struct VariationalCircuit {
/// Number of qubits
pub num_qubits: usize,
/// Number of parameters
pub num_params: usize,
/// Current parameters
pub parameters: Array1<f64>,
/// Number of layers
pub num_layers: usize,
/// Type of ansatz
pub ansatz_type: AnsatzType,
}
impl VariationalCircuit {
/// Creates a new variational circuit
pub fn new(
num_qubits: usize,
num_params: usize,
num_layers: usize,
ansatz_type: AnsatzType,
) -> Result<Self> {
// Initialize random parameters
let parameters = Array1::from_vec(
(0..num_params)
.map(|_| thread_rng().random::<f64>() * 2.0 * std::f64::consts::PI)
.collect(),
);
Ok(VariationalCircuit {
num_qubits,
num_params,
parameters,
num_layers,
ansatz_type,
})
}
/// Creates a circuit with the current parameters
pub fn create_circuit<const N: usize>(&self) -> Result<Circuit<N>> {
// This is a dummy implementation
// In a real system, this would create a circuit based on the ansatz type and parameters
let mut circuit = Circuit::<N>::new();
for i in 0..N.min(self.num_qubits) {
// Apply some dummy gates based on parameters
circuit.h(i)?;
if i < self.parameters.len() {
circuit.rz(i, self.parameters[i])?;
}
}
// Add entanglement based on the ansatz type
match self.ansatz_type {
AnsatzType::HardwareEfficient => {
// Linear nearest-neighbor entanglement
for i in 0..N.min(self.num_qubits) - 1 {
circuit.cnot(i, i + 1)?;
}
}
AnsatzType::UCCSD => {
// More complex entanglement pattern
for i in 0..N.min(self.num_qubits) / 2 {
let j = N.min(self.num_qubits) / 2 + i;
if j < N {
circuit.cnot(i, j)?;
}
}
}
AnsatzType::QAOA => {
// QAOA-style entanglement (fully connected)
for i in 0..N.min(self.num_qubits) {
for j in i + 1..N.min(self.num_qubits) {
circuit.cnot(i, j)?;
}
}
}
AnsatzType::Custom => {
// Custom entanglement pattern
if N >= 3 {
circuit.cnot(0, 1)?;
circuit.cnot(1, 2)?;
if N > 3 {
circuit.cnot(2, 3)?;
}
}
}
}
Ok(circuit)
}
/// Computes the expectation value of a Hamiltonian via direct statevector simulation.
///
/// `hamiltonian` is a slice of `(coefficient, pauli_term_list)` tuples.
/// Each `pauli_term_list` contains `(qubit_index, pauli_type)` pairs where
/// `pauli_type` encodes 0=I, 1=X, 2=Y, 3=Z.
///
/// The ansatz consists of `num_layers` repetitions of:
/// – H gate on every qubit
/// – RZ(θ_i) on qubit i using the i-th parameter (cycling if parameters run out)
/// – CNOT chain (linear nearest-neighbour entanglement)
///
/// Returns `Σ_k coef_k · ⟨ψ | P_k | ψ⟩`.
pub fn compute_expectation(&self, hamiltonian: &[(f64, Vec<(usize, usize)>)]) -> Result<f64> {
let n = self.num_qubits;
if n == 0 {
return Ok(0.0);
}
let dim = 1usize << n;
// Build the statevector |ψ⟩ by applying the ansatz to |0⟩^n.
let mut state = vec![Complex64::new(0.0, 0.0); dim];
state[0] = Complex64::new(1.0, 0.0);
let mut param_idx = 0usize;
for _layer in 0..self.num_layers.max(1) {
// --- Hadamard on every qubit ---
for q in 0..n {
apply_single_qubit_h(&mut state, q, n);
}
// --- RZ(θ) on every qubit ---
for q in 0..n {
let theta = if self.parameters.is_empty() {
0.0
} else {
self.parameters[param_idx % self.parameters.len()]
};
param_idx += 1;
apply_single_qubit_rz(&mut state, q, n, theta);
}
// --- CNOT chain (linear nearest-neighbour) ---
for q in 0..n.saturating_sub(1) {
apply_cnot(&mut state, q, q + 1, n);
}
}
// --- Evaluate ⟨ψ | H | ψ⟩ ---
let mut expectation = 0.0f64;
for (coef, pauli_terms) in hamiltonian {
// Compute ⟨ψ | P | ψ⟩ where P = ⊗ Pauli operators.
// Action of P on basis state |i⟩: produces coeff * |j⟩.
// ⟨ψ|P|ψ⟩ = Σ_i ψ*_i · (P|ψ⟩)_i
// = Σ_i ψ*_i · Σ_j ⟨i|P|j⟩ ψ_j
let mut psi_p = vec![Complex64::new(0.0, 0.0); dim];
for j in 0..dim {
if state[j].norm_sqr() < 1e-30 {
continue;
}
let mut coeff = state[j];
let mut target = j;
for &(qubit, pauli_type) in pauli_terms {
if qubit >= n {
continue;
}
let bit = (j >> qubit) & 1;
match pauli_type {
0 => {} // I – identity
1 => {
// X flips qubit
target ^= 1 << qubit;
}
2 => {
// Y flips qubit, phase +i if bit=0, -i if bit=1
target ^= 1 << qubit;
coeff *= if bit == 0 {
Complex64::new(0.0, 1.0)
} else {
Complex64::new(0.0, -1.0)
};
}
3 => {
// Z: phase -1 if bit=1
if bit == 1 {
coeff *= Complex64::new(-1.0, 0.0);
}
}
_ => {} // unknown – treat as identity
}
}
if target < dim {
psi_p[target] += coeff;
}
}
// ⟨ψ|P|ψ⟩ = Σ_i ψ*_i · (P|ψ⟩)_i
let pauli_exp: Complex64 = state
.iter()
.zip(psi_p.iter())
.map(|(a, b)| a.conj() * b)
.sum();
expectation += coef * pauli_exp.re;
}
Ok(expectation)
}
/// Evaluates the objective function for optimization
pub fn evaluate(&self, objective: &dyn Fn(&VariationalCircuit) -> Result<f64>) -> Result<f64> {
objective(self)
}
/// Optimizes the circuit parameters using the parameter-shift rule for gradient computation.
///
/// For each parameter θ_i the gradient is estimated as:
/// ∂f/∂θ_i ≈ (f(θ_i + π/2) − f(θ_i − π/2)) / 2
///
/// A gradient-descent update is then applied with the learning rate from the
/// supplied [`Optimizer`]. Other optimizer variants fall back to `lr = 0.01`.
pub fn optimize(
&mut self,
objective: &dyn Fn(&VariationalCircuit) -> Result<f64>,
optimizer: &Optimizer,
max_iterations: usize,
) -> Result<f64> {
let shift = std::f64::consts::PI / 2.0;
let lr = match optimizer {
Optimizer::GradientDescent { learning_rate } => *learning_rate,
Optimizer::Adam { learning_rate, .. } => *learning_rate,
Optimizer::SPSA { learning_rate, .. } => *learning_rate,
Optimizer::QuantumNaturalGradient { learning_rate, .. } => *learning_rate,
Optimizer::SciRS2 { config, .. } => {
config.get("learning_rate").copied().unwrap_or(0.01)
}
};
let mut best_value = self.evaluate(objective)?;
for _ in 0..max_iterations {
let n_params = self.parameters.len();
let mut gradient = vec![0.0f64; n_params];
for i in 0..n_params {
let mut plus = self.clone();
plus.parameters[i] += shift;
let mut minus = self.clone();
minus.parameters[i] -= shift;
gradient[i] = (plus.evaluate(objective)? - minus.evaluate(objective)?) * 0.5;
}
for i in 0..n_params {
self.parameters[i] -= lr * gradient[i];
}
let new_value = self.evaluate(objective)?;
if new_value < best_value {
best_value = new_value;
}
}
Ok(best_value)
}
}
// ---------------------------------------------------------------------------
// Statevector helper functions (runtime-dimension, no const generic N)
// ---------------------------------------------------------------------------
/// Apply a single-qubit Hadamard gate to qubit `q` in a `n`-qubit statevector.
fn apply_single_qubit_h(state: &mut Vec<Complex64>, q: usize, n: usize) {
let inv_sqrt2 = 1.0 / std::f64::consts::SQRT_2;
let dim = 1 << n;
let half_step = 1 << q;
let step = half_step << 1;
let mut base = 0;
while base < dim {
for i in base..base + half_step {
let a = state[i];
let b = state[i + half_step];
state[i] = Complex64::new((a.re + b.re) * inv_sqrt2, (a.im + b.im) * inv_sqrt2);
state[i + half_step] =
Complex64::new((a.re - b.re) * inv_sqrt2, (a.im - b.im) * inv_sqrt2);
}
base += step;
}
}
/// Apply RZ(θ) on qubit `q`: |0⟩ → e^{-iθ/2}|0⟩, |1⟩ → e^{+iθ/2}|1⟩.
fn apply_single_qubit_rz(state: &mut Vec<Complex64>, q: usize, n: usize, theta: f64) {
let dim = 1 << n;
let phase0 = Complex64::from_polar(1.0, -theta * 0.5);
let phase1 = Complex64::from_polar(1.0, theta * 0.5);
for i in 0..dim {
let bit = (i >> q) & 1;
state[i] *= if bit == 0 { phase0 } else { phase1 };
}
}
/// Apply CNOT with `control` and `target` qubits.
fn apply_cnot(state: &mut Vec<Complex64>, control: usize, target: usize, n: usize) {
let dim = 1 << n;
for i in 0..dim {
if (i >> control) & 1 == 1 {
let j = i ^ (1 << target);
if i < j {
state.swap(i, j);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_optimize_reduces_objective() {
// Simple convex objective: minimize Σ θ_i²
// Parameter-shift gradient: ∂(θ_i²)/∂θ_i = 2θ_i
// But parameter-shift gives (f(θ+π/2) - f(θ-π/2))/2 = (sin(θ+π/2)^2 - sin(θ-π/2)^2)/2
// For the pure quadratic we just need descent, not exact gradients.
let num_qubits = 2;
let num_params = 4;
let num_layers = 1;
let mut vc = VariationalCircuit::new(
num_qubits,
num_params,
num_layers,
AnsatzType::HardwareEfficient,
)
.expect("circuit creation failed");
// Override parameters to known starting values so the test is deterministic.
vc.parameters = Array1::from_vec(vec![1.0, 1.0, 1.0, 1.0]);
let objective = |vc: &VariationalCircuit| -> Result<f64> {
Ok(vc.parameters.iter().map(|p| p * p).sum())
};
let initial = objective(&vc).expect("initial evaluation failed");
let optimizer = Optimizer::GradientDescent { learning_rate: 0.1 };
let result = vc
.optimize(&objective, &optimizer, 20)
.expect("optimization failed");
assert!(
result < initial,
"optimize should reduce objective: {result} vs initial {initial}"
);
}
#[test]
fn test_compute_expectation_identity_hamiltonian() {
// H = 1.0 * I (identity on qubit 0) — ⟨ψ|I|ψ⟩ = 1 always
let vc = VariationalCircuit::new(2, 2, 1, AnsatzType::HardwareEfficient)
.expect("circuit creation failed");
let hamiltonian = vec![(1.0, vec![(0_usize, 0_usize)])]; // coef=1, I on qubit 0
let exp = vc
.compute_expectation(&hamiltonian)
.expect("expectation failed");
assert!((exp - 1.0).abs() < 1e-10, "⟨I⟩ should be 1.0, got {exp}");
}
}