prism-q 0.26.0

PRISM-Q: Performance Rust Interoperable Simulator for Quantum
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
//! Adjoint-method gradients of expectation values.
//!
//! Computes the exact gradient of `⟨H⟩ = ⟨0|U†HU|0⟩` with respect to a vector
//! of circuit parameters in a cost independent of the parameter count, by
//! back-propagating two statevectors through the circuit. For `U = U_L…U_1`
//! and a Hermitian Hamiltonian `H = Σ c_k P_k`:
//!
//! 1. Forward: apply `U` unfused, keep `|φ⟩ = U|0⟩`.
//! 2. Build `|λ⟩ = H|φ⟩`; the value `⟨H⟩ = Re⟨φ|λ⟩`.
//! 3. Backward `i = L…1`: for each trainable gate with generator `G_i`,
//!    accumulate the gradient from `⟨λ|G_i|φ⟩` (with `|φ⟩` on the output side
//!    of gate `i`), then step both states back through `U_i†`.
//!
//! Only the statevector backend is supported. The differentiated circuit must
//! be unitary (no measurement, reset, or conditional). Differentiable gates are
//! `Rx`, `Ry`, `Rz`, `Rzz`, and `P`; a trainable link on any other gate is an
//! error.

use num_complex::Complex64;

use crate::backend::statevector::StatevectorBackend;
use crate::backend::{Backend, max_statevector_qubits, reserve_dense_output};
use crate::circuit::{Circuit, Instruction};
use crate::error::{PrismError, Result};
use crate::gates::{Gate, GeneratorKind};

use super::unified_pauli::PauliTerm;
use super::{i_pow, pauli_masks, pauli_sandwich};

/// Binds one trainable gate instruction to a slot in the parameter vector.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParamLink {
    /// Index into [`Circuit::instructions`].
    pub instruction: usize,
    /// Slot in the parameter (theta) vector this gate feeds.
    pub param: usize,
}

/// Maps trainable gate instructions to parameter-vector slots for the adjoint
/// gradient. Many instructions may share one slot (weight sharing); their
/// contributions accumulate. Built by the parametric [`crate::CircuitBuilder`]
/// methods or constructed directly.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParameterMap {
    links: Vec<ParamLink>,
}

impl ParameterMap {
    /// An empty map (no trainable parameters).
    pub fn new() -> Self {
        Self { links: Vec::new() }
    }

    /// Build a map from explicit instruction-to-slot links.
    pub fn from_links(links: Vec<ParamLink>) -> Self {
        Self { links }
    }

    /// Mark every analytically differentiable gate (`Rx`, `Ry`, `Rz`, `Rzz`,
    /// `P`) as its own parameter, in circuit order. The common "every rotation
    /// is trainable" case for a variational ansatz.
    pub fn all_rotations(circuit: &Circuit) -> Self {
        let mut links = Vec::new();
        for (i, inst) in circuit.instructions.iter().enumerate() {
            if let Instruction::Gate { gate, .. } = inst {
                if gate.pauli_generator().is_some() {
                    links.push(ParamLink {
                        instruction: i,
                        param: links.len(),
                    });
                }
            }
        }
        Self { links }
    }

    /// Record that `instruction` feeds parameter slot `param`.
    pub fn push(&mut self, instruction: usize, param: usize) {
        self.links.push(ParamLink { instruction, param });
    }

    /// The recorded links.
    pub fn links(&self) -> &[ParamLink] {
        &self.links
    }

    /// True if no trainable parameters are recorded.
    pub fn is_empty(&self) -> bool {
        self.links.is_empty()
    }

    /// Number of distinct parameters, `max slot + 1` (0 if empty). The returned
    /// gradient vector has this length.
    pub fn num_params(&self) -> usize {
        self.links.iter().map(|l| l.param + 1).max().unwrap_or(0)
    }

    fn validate(&self, circuit: &Circuit) -> Result<()> {
        let n = circuit.instructions.len();
        for link in &self.links {
            if link.instruction >= n {
                return Err(PrismError::InvalidParameter {
                    message: format!(
                        "parameter link references instruction {} but the circuit has {n} instructions",
                        link.instruction
                    ),
                });
            }
            match &circuit.instructions[link.instruction] {
                Instruction::Gate { gate, .. } if gate.pauli_generator().is_some() => {}
                Instruction::Gate { gate, .. } => {
                    return Err(PrismError::InvalidParameter {
                        message: format!(
                            "instruction {} (`{}`) is not analytically differentiable; supported gates are rx, ry, rz, rzz, p",
                            link.instruction,
                            gate.name()
                        ),
                    });
                }
                _ => {
                    return Err(PrismError::InvalidParameter {
                        message: format!(
                            "parameter link references instruction {} which is not a gate",
                            link.instruction
                        ),
                    });
                }
            }
        }
        Ok(())
    }
}

/// Expectation value and its gradient with respect to each parameter slot.
#[derive(Debug, Clone, PartialEq)]
pub struct ExpectationGradient {
    /// `⟨H⟩` at the circuit's current parameter values.
    pub value: f64,
    /// `d⟨H⟩/dθ`, one entry per parameter slot.
    pub gradient: Vec<f64>,
}

/// Compute `⟨H⟩` and its exact gradient with respect to the trainable
/// parameters using the adjoint method on the statevector backend.
///
/// `hamiltonian` is a weighted Pauli sum `Σ c_k P_k` with real coefficients;
/// each `P_k` is a joint Pauli string (identity factors omitted). `params`
/// declares which gate instructions are trainable and how they map to the
/// gradient vector. The returned gradient has length `params.num_params()`.
pub fn run_expectation_gradient(
    circuit: &Circuit,
    hamiltonian: &[(f64, Vec<PauliTerm>)],
    params: &ParameterMap,
    seed: u64,
) -> Result<ExpectationGradient> {
    if super::has_nonunitary_or_classical_ops(circuit) {
        return Err(PrismError::IncompatibleBackend {
            backend: "Statevector".into(),
            reason: "adjoint gradients require a unitary circuit without measurements, resets, or conditionals".into(),
        });
    }

    if circuit.instructions.iter().any(|inst| {
        matches!(
            inst,
            Instruction::Gate {
                gate: Gate::QftBlock { .. },
                ..
            }
        )
    }) {
        return Err(PrismError::IncompatibleBackend {
            backend: "Statevector".into(),
            reason: "adjoint gradients do not support QftBlock; expand it to primitive gates first"
                .into(),
        });
    }

    params.validate(circuit)?;

    // Validate and reduce observables before the 2^n simulation.
    let mut masked = Vec::with_capacity(hamiltonian.len());
    for (coeff, terms) in hamiltonian {
        let (xmask, zmask, num_y) = pauli_masks(terms, circuit.num_qubits)?;
        masked.push((*coeff, xmask, zmask, num_y));
    }

    if circuit.num_qubits > max_statevector_qubits() {
        return Err(PrismError::IncompatibleBackend {
            backend: "Statevector".into(),
            reason: format!(
                "adjoint gradients for {} qubits exceed the statevector cap ({} qubits); the gradient path holds two statevectors",
                circuit.num_qubits,
                max_statevector_qubits()
            ),
        });
    }

    // Forward pass, unfused, to keep a 1:1 gate-to-generator correspondence.
    let mut phi = StatevectorBackend::new(seed);
    phi.init(circuit.num_qubits, circuit.num_classical_bits)?;
    phi.apply_instructions(&circuit.instructions)?;

    let (value, lambda_state) = build_lambda_and_value(phi.state_vector(), &masked)?;

    let num_params = params.num_params();
    let mut gradient = vec![0.0; num_params];
    if params.is_empty() {
        return Ok(ExpectationGradient { value, gradient });
    }

    // Inverse light cone of the Hamiltonian: a trainable gate outside it has a
    // provably zero gradient (its generator commutes through the back-evolved
    // observable), so its sandwich is skipped.
    let in_cone = observable_light_cone(circuit, hamiltonian);

    // Links sorted by descending instruction index, matching the reverse sweep.
    // A cursor walks this list so the per-instruction lookup stays O(params),
    // not O(instructions).
    let mut links = params.links().to_vec();
    links.sort_unstable_by_key(|l| std::cmp::Reverse(l.instruction));

    // Stop the backward sweep at the earliest in-cone trainable gate: nothing
    // before it contributes, so a non-trainable (or out-of-cone) prefix costs
    // no inverse applications. If no trainable gate reaches the observable, the
    // gradient is zero everywhere.
    let earliest = links
        .iter()
        .filter(|l| in_cone[l.instruction])
        .map(|l| l.instruction)
        .min();
    let Some(earliest) = earliest else {
        return Ok(ExpectationGradient { value, gradient });
    };

    let mut lambda = StatevectorBackend::new(seed);
    lambda.init_from_state(lambda_state, circuit.num_classical_bits)?;

    let mut cursor = 0;
    for i in (earliest..circuit.instructions.len()).rev() {
        let (gate, targets) = match &circuit.instructions[i] {
            Instruction::Gate { gate, targets } => (gate, targets),
            Instruction::Barrier { .. } => continue,
            _ => unreachable!("non-unitary instructions rejected above"),
        };

        if cursor < links.len() && links[cursor].instruction == i {
            if in_cone[i] {
                let kind = gate
                    .pauli_generator()
                    .expect("trainable instruction validated as differentiable");
                let contrib =
                    gradient_contribution(kind, targets, lambda.state_vector(), phi.state_vector());
                while cursor < links.len() && links[cursor].instruction == i {
                    gradient[links[cursor].param] += contrib;
                    cursor += 1;
                }
            } else {
                // Out of the light cone: contribution is zero, but the cursor
                // still advances past this instruction's links.
                while cursor < links.len() && links[cursor].instruction == i {
                    cursor += 1;
                }
            }
        }

        // The earliest in-cone trainable gate is the last one evaluated; its
        // inverse and every gate before it can be skipped.
        if i > earliest {
            let inverse = Instruction::Gate {
                gate: gate.inverse(),
                targets: targets.clone(),
            };
            phi.apply(&inverse)?;
            lambda.apply(&inverse)?;
        }
    }

    Ok(ExpectationGradient { value, gradient })
}

/// Per-instruction flag: true if the gate lies in the Hamiltonian's inverse
/// light cone (its support is connected to some observable term through the
/// gates that follow it).
fn observable_light_cone(circuit: &Circuit, hamiltonian: &[(f64, Vec<PauliTerm>)]) -> Vec<bool> {
    let union: Vec<PauliTerm> = hamiltonian
        .iter()
        .flat_map(|(_, terms)| terms.iter().copied())
        .collect();
    super::unified_pauli::inverse_light_cone(circuit, &union)
}

/// Build `|λ⟩ = Σ c_k P_k|φ⟩` into a fresh buffer and return `(⟨H⟩, |λ⟩)`,
/// where `⟨H⟩ = Re⟨φ|λ⟩`.
fn build_lambda_and_value(
    phi: &[Complex64],
    masked: &[(f64, usize, usize, u32)],
) -> Result<(f64, Vec<Complex64>)> {
    let dim = phi.len();
    let mut lambda: Vec<Complex64> = Vec::new();
    reserve_dense_output(
        &mut lambda,
        dim,
        "Statevector",
        "adjoint gradient lambda state",
    )?;
    lambda.resize(dim, Complex64::new(0.0, 0.0));

    for &(coeff, xmask, zmask, num_y) in masked {
        let factor = Complex64::new(coeff, 0.0) * i_pow(num_y);
        for (j, &amp) in phi.iter().enumerate() {
            let sign = if (j & zmask).count_ones() & 1 == 1 {
                -1.0
            } else {
                1.0
            };
            lambda[j ^ xmask] += factor * sign * amp;
        }
    }

    let value: f64 = phi
        .iter()
        .zip(&lambda)
        .map(|(p, l)| (p.conj() * l).re)
        .sum();
    Ok((value, lambda))
}

/// Gradient contribution `d⟨H⟩/dθ` of a single trainable gate, from the
/// generator sandwich `⟨λ|G|φ⟩` with `|φ⟩` on the output side of the gate.
fn gradient_contribution(
    kind: GeneratorKind,
    targets: &[usize],
    lambda: &[Complex64],
    phi: &[Complex64],
) -> f64 {
    match kind {
        GeneratorKind::RotX => {
            let x = 1usize << targets[0];
            pauli_sandwich(lambda, phi, x, 0, 0).im
        }
        GeneratorKind::RotY => {
            let b = 1usize << targets[0];
            pauli_sandwich(lambda, phi, b, b, 1).im
        }
        GeneratorKind::RotZ => {
            let z = 1usize << targets[0];
            pauli_sandwich(lambda, phi, 0, z, 0).im
        }
        GeneratorKind::RotZz => {
            let z = (1usize << targets[0]) | (1usize << targets[1]);
            pauli_sandwich(lambda, phi, 0, z, 0).im
        }
        GeneratorKind::Phase => {
            let bit = 1usize << targets[0];
            let mut acc = Complex64::new(0.0, 0.0);
            for (j, &amp) in phi.iter().enumerate() {
                if j & bit != 0 {
                    acc += lambda[j].conj() * amp;
                }
            }
            -2.0 * acc.im
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sim::unified_pauli::PauliTerm;

    fn z_obs(qubit: usize) -> Vec<(f64, Vec<PauliTerm>)> {
        vec![(1.0, vec![PauliTerm::z(qubit)])]
    }

    #[test]
    fn single_rx_gradient_matches_analytic() {
        // Rx(θ)|0>, <Z> = cos θ, d/dθ = -sin θ.
        let theta = 0.7;
        let mut c = Circuit::new(1, 0);
        c.add_gate(Gate::Rx(theta), &[0]);
        let mut params = ParameterMap::new();
        params.push(0, 0);

        let g = run_expectation_gradient(&c, &z_obs(0), &params, 42).unwrap();
        assert!((g.value - theta.cos()).abs() < 1e-12);
        assert!((g.gradient[0] - (-theta.sin())).abs() < 1e-9);
    }

    #[test]
    fn ry_generator_carries_num_y() {
        // Ry(θ)|0>, <Z> = cos θ, d/dθ = -sin θ. Generator Y has num_y = 1.
        let theta = 1.3;
        let mut c = Circuit::new(1, 0);
        c.add_gate(Gate::Ry(theta), &[0]);
        let mut params = ParameterMap::new();
        params.push(0, 0);

        let g = run_expectation_gradient(&c, &z_obs(0), &params, 42).unwrap();
        assert!((g.gradient[0] - (-theta.sin())).abs() < 1e-9);
    }

    #[test]
    fn phase_projector_gradient() {
        // H then P(θ): |ψ> = (|0> + e^{iθ}|1>)/√2, <X> = cos θ, d/dθ = -sin θ.
        let theta = 0.9;
        let mut c = Circuit::new(1, 0);
        c.add_gate(Gate::H, &[0]);
        c.add_gate(Gate::P(theta), &[0]);
        let mut params = ParameterMap::new();
        params.push(1, 0);

        let obs = vec![(1.0, vec![PauliTerm::x(0)])];
        let g = run_expectation_gradient(&c, &obs, &params, 42).unwrap();
        assert!((g.value - theta.cos()).abs() < 1e-12);
        assert!((g.gradient[0] - (-theta.sin())).abs() < 1e-9);
    }

    #[test]
    fn shared_parameter_accumulates() {
        // Two Rx gates on separate qubits sharing one slot; each contributes
        // -sin θ to <Z0 + Z1>, so the shared gradient is -2 sin θ.
        let theta = 0.4;
        let mut c = Circuit::new(2, 0);
        c.add_gate(Gate::Rx(theta), &[0]);
        c.add_gate(Gate::Rx(theta), &[1]);
        let mut params = ParameterMap::new();
        params.push(0, 0);
        params.push(1, 0);

        let obs = vec![(1.0, vec![PauliTerm::z(0)]), (1.0, vec![PauliTerm::z(1)])];
        let g = run_expectation_gradient(&c, &obs, &params, 42).unwrap();
        assert_eq!(g.gradient.len(), 1);
        assert!((g.gradient[0] - (-2.0 * theta.sin())).abs() < 1e-9);
    }

    #[test]
    fn empty_params_returns_value_only() {
        let mut c = Circuit::new(1, 0);
        c.add_gate(Gate::Rx(0.5), &[0]);
        let g = run_expectation_gradient(&c, &z_obs(0), &ParameterMap::new(), 42).unwrap();
        assert!(g.gradient.is_empty());
        assert!((g.value - 0.5f64.cos()).abs() < 1e-12);
    }

    #[test]
    fn nondifferentiable_trainable_gate_is_rejected() {
        let mut c = Circuit::new(1, 0);
        c.add_gate(Gate::H, &[0]);
        let mut params = ParameterMap::new();
        params.push(0, 0);
        assert!(run_expectation_gradient(&c, &z_obs(0), &params, 42).is_err());
    }

    #[test]
    fn nonunitary_circuit_is_rejected() {
        let mut c = Circuit::new(1, 1);
        c.add_gate(Gate::Rx(0.3), &[0]);
        c.add_measure(0, 0);
        assert!(run_expectation_gradient(&c, &z_obs(0), &ParameterMap::new(), 42).is_err());
    }
}