quant-iron 2.0.0

A high-performance, hardware-accelerated modular quantum computing library with a focus on physical applications. Quant-Iron provides tools to represent quantum states, apply standard quantum gates, perform measurements, build quantum circuits, and implement quantum algorithms.
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
use crate::{
    components::{operator::Operator, operator::Pauli, state::State, gate::Gate},
    errors::Error,
};
use num_complex::Complex;
use rayon::prelude::*;
use std::collections::HashMap;
use std::ops::{Add, Mul};

/// Represents a Pauli string, which is a product of Pauli operators (X, Y, Z) acting on qubits.
/// Used to represent a term in a Hamiltonian or a quantum operator.
#[derive(Debug, Clone, PartialEq)]
pub struct PauliString {
    /// A mapping from qubit indices to Pauli operators.
    ops: HashMap<usize, Pauli>,
    /// The coefficient of the Pauli string, which is a complex number.
    coefficient: Complex<f64>,
}

impl PauliString {
    /// Creates a new Pauli string with the given coefficient and an empty set of operators.
    ///
    /// # Arguments
    /// * `coefficient` - The coefficient of the Pauli string, represented as a complex number.
    ///
    /// # Returns
    /// A new `PauliString` instance with the specified coefficient and no operators.
    pub fn new(coefficient: Complex<f64>) -> Self {
        Self {
            ops: HashMap::new(),
            coefficient,
        }
    }

    /// Returns the length of the Pauli string, defined as the number of operators it contains.
    /// 
    /// # Returns
    /// 
    /// * `usize` - The number of operators in the Pauli string.
    pub fn len(&self) -> usize {
        self.ops.len()
    }

    /// Creates a new Pauli string with the given coefficient and a set of operators.
    ///
    /// # Arguments
    /// * `coefficient` - The coefficient of the Pauli string, represented as a complex number.
    /// * `ops` - A mapping from qubit indices to Pauli operators.
    ///
    /// # Returns
    /// A new `PauliString` instance with the specified coefficient and operators.
    ///
    /// Note that the Hashmap ensures uniqueness of operators for each qubit.
    pub fn with_ops(coefficient: Complex<f64>, ops: HashMap<usize, Pauli>) -> Self {
        Self { ops, coefficient }
    }

    /// Adds a Pauli operator to the Pauli string at the specified qubit index.
    ///
    /// # Arguments
    /// * `qubit` - The index of the qubit to which the operator is applied.
    /// * `op` - The Pauli operator to be added (X, Y, or Z).
    /// 
    /// # Panics
    /// This function will panic if an operator for the same qubit index is added more than once.
    pub fn add_op(&mut self, qubit: usize, op: Pauli) {
        if self.ops.insert(qubit, op).is_some() {
            panic!("Duplicate Pauli string operator for qubit: {}", qubit);
        }
    }

    /// Adds a Pauli operator to the Pauli string at the specified qubit index and returns the new `PauliString` instance.
    ///
    /// # Arguments
    ///
    /// * `qubit` - The index of the qubit to which the operator is applied.
    /// * `op` - The Pauli operator to be added (X, Y, or Z).
    ///
    /// # Returns
    ///
    /// * `Self` - A new `PauliString` instance with the added operator.
    /// 
    /// # Panics
    /// This function will panic if an operator for the same qubit index is added more than once.
    pub fn with_op(mut self, qubit: usize, op: Pauli) -> Self {
        self.add_op(qubit, op);
        self
    }

    /// Returns the coefficient of the Pauli string.
    ///
    /// # Returns
    ///
    /// * `Complex<f64>` - The coefficient of the Pauli string, represented as a complex number.
    pub fn coefficient(&self) -> Complex<f64> {
        self.coefficient
    }

    /// Returns a reference to the operators in the Pauli string.
    ///
    /// # Returns
    ///
    /// * `&HashMap<usize, Pauli>` - A reference to the mapping of qubit indices to Pauli operators.
    pub fn ops(&self) -> &HashMap<usize, Pauli> {
        &self.ops
    }

    /// Returns the list of targets of the Pauli string
    /// 
    /// # Returns
    /// * `Vec<usize>` - A vector of qubit indices that the Pauli string acts upon.
    pub fn get_targets(&self) -> Vec<usize> {
        let mut keys = self.ops.keys().cloned().collect::<Vec<usize>>();
        keys.sort();
        keys
    }

    /// Converts the Pauli string to a vector of operator gates.
    ///
    /// # Returns
    /// * `Vec<Gate>` - A vector of Gate structs representing the individual Pauli operators.
    pub fn to_gates(&self) -> Vec<Gate> {
        self.ops.iter().map(|(qubit, op)| {
            Gate::Operator(Box::new(*op), vec![*qubit], vec![])
        }).collect()
    }

    /// Applies the Pauli string to a given state.
    ///
    /// # Arguments
    /// * `state` - The state to which the Pauli string is applied.
    ///
    /// # Returns
    /// * `Result<State, Error>` - The resulting state after applying the Pauli string, or an error if the operation fails.
    ///
    /// # Errors
    ///
    /// * Returns an error if the operations in the Pauli string refer to qubits outside the range of the state.
    pub fn apply(&self, state: &State) -> Result<State, Error> {
        // If the Pauli string is empty, return the state multiplied by the coefficient
        if self.ops.is_empty() {
            return Ok(state.clone() * self.coefficient);
        }

        // Apply the Pauli string to the state
        let mut new_state: State = state.clone();
        for (qubit, op) in &self.ops {
            new_state = op.apply(&new_state, &[*qubit], &[])?; // Assumes op.apply can take &mut State and modify it or returns a new one
        }
        Ok(new_state * self.coefficient)
    }

    /// Applies the Pauli string to a given state and normalises the new state.
    ///
    /// # Arguments
    /// * `state` - The state to which the Pauli string is applied.
    /// 
    /// # Returns
    /// * `Result<State, Error>` - The resulting state after applying the Pauli string, or an error if the operation fails.
    /// 
    /// # Errors
    /// 
    /// * Returns an error if the operations in the Pauli string refer to qubits outside the range of the state.
    /// * Returns an error if the resulting state cannot be normalised (eg., has zero norm).
    pub fn apply_normalised(&self, state: &State) -> Result<State, Error> {
        let new_state: State = self.apply_operators(state)?;
        new_state.normalise()
    }

    /// Helper function to apply only the operator part of the Pauli string (P_ops) to a state.
    /// This does not include the PauliString's own coefficient.
    fn apply_operators(&self, state: &State) -> Result<State, Error> {
        if self.ops.is_empty() {
            // If there are no operators, P_ops is effectively Identity.
            // P_ops |state> = |state>.
            return Ok(state.clone());
        }

        let mut current_state = state.clone();
        for (qubit_idx, pauli_op) in &self.ops {
            current_state = pauli_op.apply(&current_state, &[*qubit_idx], &[])?;
        }
        Ok(current_state)
    }

    /// Applies the exponential of the Pauli string to a given state.
    ///
    /// # Arguments
    ///
    /// * `state` - The state to which the exponential of the Pauli string is applied.
    ///
    /// # Returns
    ///
    /// * `Result<State, Error>` - The resulting state after applying the exponential of the Pauli string, or an error if the operation fails.
    ///
    /// # Errors
    ///
    /// * Returns an error if the operations in the Pauli string refer to qubits outside the range of the state.
    pub fn apply_exp(&self, state: &State) -> Result<State, Error> {
        // If the Pauli string is empty, return the state multiplied by the exponential of the coefficient
        let alpha: Complex<f64> = self.coefficient;

        if self.ops.is_empty() {
            // P_ops is Identity. exp(alpha * I) |state> = exp(alpha) * |state>
            return Ok(state.clone() * alpha.exp());
        }

        // 1. Calculate P_ops |state> using the helper
        let p_ops_psi_state = self.apply_operators(state)?;

        // 2. Calculate scalar coefficients for exp(alpha * P_ops) = cosh(alpha)*I + sinh(alpha)*P_ops
        let cosh_alpha: Complex<f64> = alpha.cosh();
        let sinh_alpha: Complex<f64> = alpha.sinh();

        // 3. Calculate cosh(alpha) * |state>
        let term_identity_part: State = state.clone() * cosh_alpha;

        // 4. Calculate sinh(alpha) * (P_ops |state>)
        let term_operator_part: State = p_ops_psi_state * sinh_alpha;

        // 5. Add the two terms: cosh(alpha) * |state> + sinh(alpha) * (P_ops |state>)
        Ok(term_identity_part + term_operator_part)
    }

    /// Applies the exponential of the Pauli string to a given state with a specified factor.
    ///
    /// # Arguments
    /// * `state` - The state to which the exponential of the Pauli string is applied.
    /// * `factor` - A complex factor to be multiplied with the coefficient of the Pauli string.
    ///
    /// # Returns
    /// * `Result<State, Error>` - The resulting state after applying the exponential of the Pauli string with the factor, or an error if the operation fails.
    ///
    /// # Errors
    ///
    /// * Returns an error if the operations in the Pauli string refer to qubits outside the range of the state.
    pub fn apply_exp_factor(&self, state: &State, factor: Complex<f64>) -> Result<State, Error> {
        // Calculate the effective coefficient for the exponentiation
        let alpha: Complex<f64> = self.coefficient * factor;

        if self.ops.is_empty() {
            // P_ops is Identity. exp(alpha * I) |state> = exp(alpha) * |state>
            return Ok(state.clone() * alpha.exp());
        }

        // 1. Calculate P_ops |state> using the helper
        let p_ops_psi_state = self.apply_operators(state)?;
        // p_ops_psi_state now holds P_ops |state> (where P_ops is the product of Pauli operators without self.coefficient)

        // 2. Calculate scalar coefficients for exp(alpha * P_ops) = cosh(alpha)*I + sinh(alpha)*P_ops
        let cosh_alpha: Complex<f64> = alpha.cosh();
        let sinh_alpha: Complex<f64> = alpha.sinh();

        // 3. Calculate cosh(alpha) * |state>
        let term_identity_part: State = state.clone() * cosh_alpha;

        // 4. Calculate sinh(alpha) * (P_ops |state>)
        let term_operator_part: State = p_ops_psi_state * sinh_alpha;

        // 5. Add the two terms: cosh(alpha) * |state> + sinh(alpha) * (P_ops |state>)
        Ok(term_identity_part + term_operator_part)
    }

    /// Applies the exponential of the Pauli string to the given state with a negative imaginary factor.
    /// Represents a time evolution step of the form exp(-i * coefficient * P_ops * dt).
    /// Guaranteed to return a normalised state if the input state is normalised.
    /// 
    /// # Arguments
    /// 
    /// * `state` - The state to which the exponential of the Pauli string is applied.
    /// * `dt` - The time step for the evolution.
    /// 
    /// # Returns
    /// 
    /// * `Result<State, Error>` - The resulting state after applying the exponential of the Pauli string with the negative imaginary factor, or an error if the operation fails.
    /// 
    /// # Errors
    /// 
    /// * Returns an error if the operations in the Pauli string refer to qubits outside the range of the state.
    /// * Returns an error if the coefficient for the Pauli string has an imaginary component.
    pub fn apply_exp_neg_i_dt(&self, state: &State, dt: f64) -> Result<State, Error> {
            if self.coefficient.im != 0.0 {
                return Err(Error::InvalidPauliStringCoefficient(self.coefficient));
            }
            let factor: Complex<f64> = Complex::new(0.0, -dt);
            self.apply_exp_factor(state, factor)
    }

    /// Returns the Hermitian conjugate of the Pauli string.
    ///
    /// # Returns
    ///
    /// * `Self` - A new `PauliString` instance representing the Hermitian conjugate of the original Pauli string.
    pub fn hermitian_conjugate(&self) -> Self {
        PauliString {
            ops: self.ops.clone(),
            coefficient: self.coefficient.conj(),
        }
    }
}

impl Add for PauliString {
    type Output = SumOp;

    fn add(self, other: Self) -> Self::Output {
        // Create a new SumOp with the two Pauli strings
        let terms: Vec<PauliString> = vec![self, other];
        // Return a new SumOp instance with the terms
        SumOp::new(terms)
    }
}

impl Mul<Complex<f64>> for PauliString {
    type Output = Self;

    fn mul(self, rhs: Complex<f64>) -> Self::Output {
        // Create a new Pauli string with the product of the coefficient and the given complex number
        PauliString {
            ops: self.ops.clone(),
            coefficient: self.coefficient * rhs,
        }
    }
}

impl Mul<f64> for PauliString {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self::Output {
        // Create a new Pauli string with the product of the coefficient and the given real number
        PauliString {
            ops: self.ops.clone(),
            coefficient: self.coefficient * Complex::new(rhs, 0.0),
        }
    }
}

impl Mul<PauliString> for f64 {
    type Output = PauliString;

    fn mul(self, rhs: PauliString) -> Self::Output {
        rhs * self
    }
}

impl std::fmt::Display for PauliString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let coeff_str: String = if self.coefficient.re == 0.0 && self.coefficient.im == 0.0 {
            "0".to_string()
        } else if self.coefficient.im == 0.0 {
            format!("{}", self.coefficient.re)
        } else if self.coefficient.re == 0.0 {
            format!("{}i", self.coefficient.im)
        } else {
            format!("({} + {}i)", self.coefficient.re, self.coefficient.im)
        };

        let mut result: String = coeff_str + " * ";

            let mut sorted_ops: Vec<(&usize, &Pauli)> = self.ops.iter().collect();
            sorted_ops.sort_by(|&(qubit_a, op_a), &(qubit_b, op_b)| {
            if qubit_a == qubit_b {
                // If qubits are the same, sort by operator type (X -> Y -> Z)
                let op_a_str: String = format!("{}", op_a);
                let op_b_str: String = format!("{}", op_b);
                match (op_a_str.as_str(), op_b_str.as_str()) {
                    ("X", "Y") | ("X", "Z") | ("Y", "Z") => std::cmp::Ordering::Less,
                    ("Y", "X") | ("Z", "X") | ("Z", "Y") => std::cmp::Ordering::Greater,
                    _ => std::cmp::Ordering::Equal,
                }
            } else {
                // Otherwise, sort by qubit index
                qubit_a.cmp(qubit_b)
            }
        });
        for (qubit, op) in sorted_ops {
            result.push_str(&format!("{}[{}] ", op, qubit));
            }

        write!(f, "{}", result.trim())
    }
}

/// A vector of Pauli strings that are summed together.
/// Useful for representing Hamiltonians or observables.
///
/// # Fields
///
/// * `terms` - A vector of `PauliString` instances that are summed together.
#[derive(Debug, Clone, PartialEq)]
pub struct SumOp {
    /// A vector of Pauli strings that are summed together.
    pub terms: Vec<PauliString>,
}

impl SumOp {
    /// Creates a new `SumPauliString` instance with the given terms and number of qubits.
    ///
    /// # Arguments
    ///
    /// * `terms` - A vector of `PauliString` instances that are summed together.
    ///
    /// # Returns
    ///
    /// * A new `SumPauliString` instance with the specified terms and number of qubits.
    pub fn new(terms: Vec<PauliString>) -> Self {
        Self { terms }
    }

    /// Returns the number of terms in the sum.
    ///
    /// # Returns
    ///
    /// * `usize` - The number of terms in the sum.
    pub fn num_terms(&self) -> usize {
        self.terms.len()
    }

    /// Adds a new term to the sum.
    /// 
    /// # Arguments
    /// 
    /// * `term` - The `PauliString` term to be added to the sum.
    pub fn add_term(&mut self, term: PauliString) {
        self.terms.push(term);
    }

    /// Adds a new term to the sum and returns a new `SumOp` instance.
    /// 
    /// # Arguments
    /// 
    /// * `term` - The `PauliString` term to be added to the sum.
    /// 
    /// # Returns
    /// * `Self` - A new `SumOp` instance with the added term.
    pub fn with_term(mut self, term: PauliString) -> Self {
        self.add_term(term);
        self
    }

    /// Applies the sum of Pauli strings to a given state.
    ///
    /// # Arguments
    ///
    /// * `state` - The state to which the sum of Pauli strings is applied.
    ///
    /// # Returns
    ///
    /// * `Result<State, Error>` - The resulting state after applying the sum of Pauli strings, or an error if the operation fails.
    ///
    /// # Errors
    ///
    /// * Returns an error if the operations in the Pauli strings refer to qubits outside the range of the state.
    pub fn apply(&self, state: &State) -> Result<State, Error> {
        if self.terms.is_empty() {
            // An empty sumop is equivalent to the zero operator
            return Ok(state.clone() * 0.0);
        }

        Ok(self
            .terms
            .par_iter()
            .map(|term| term.apply(state))
            .collect::<Result<Vec<State>, Error>>()?
            .into_iter()
            .sum())
    }

    /// Calculates the expectation value <psi|H|psi> = Sum_i <psi|P_i|psi>.
    ///
    /// The expectation value is generally real for Hermitian operators and normalised states.
    /// However, this function returns a `Complex<f64>` as intermediate PauliStrings
    /// might have complex coefficients or the operator/state might not be strictly physical.
    ///
    /// # Arguments
    /// * `state` - The state |psi> for which to calculate the expectation value.
    ///             For a physically meaningful expectation value, this state should be normalised.
    ///
    /// # Returns
    /// * `Result<Complex<f64>, Error>` - The expectation value, or an error if the operation fails.
    ///
    /// # Errors
    /// * Returns an error if any underlying `PauliString::apply` fails (eg., invalid qubit index).
    /// * Returns an error if `state.inner_product` fails (eg., mismatched number of qubits,
    ///   though `PauliString::apply` should also catch qubit count issues).
    pub fn expectation_value(&self, state: &State) -> Result<Complex<f64>, Error> {
        if self.terms.is_empty() {
            // The expectation value of a zero operator is zero.
            return Ok(Complex::new(0.0, 0.0));
        }

        let expectation_values_per_term: Vec<Complex<f64>> = self
            .terms
            .par_iter()
            .map(|term| {
                // For each term P_i in the sum H = Sum_i P_i:
                // 1. Calculate |phi_i> = P_i |psi>
                let phi_i_state = term.apply(state)?;

                // 2. Calculate <psi | phi_i> = <psi | P_i | psi>
                state.inner_product(&phi_i_state)
            })
            .collect::<Result<Vec<Complex<f64>>, Error>>()?; // Collect into Result<Vec<_>, E>, propagating errors

        // Sum the individual expectation values <psi|P_i|psi>
        // Complex<f64> from num_complex implements std::iter::Sum.
        Ok(expectation_values_per_term.into_iter().sum())
    }
}

impl std::fmt::Display for SumOp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut result: String = String::new();
        for (i, term) in self.terms.iter().enumerate() {
            if i > 0 {
                result.push_str(" + ");
            }
            result.push_str(&format!("{}", term));
        }
        write!(f, "{}", result)
    }
}

impl Mul<Complex<f64>> for SumOp {
    type Output = Self;

    fn mul(self, rhs: Complex<f64>) -> Self::Output {
        // Create a new SumOp with the coefficient multiplied by the given complex number
        let terms: Vec<PauliString> = self
            .terms
            .into_iter()
            .map(|term| term * rhs)
            .collect();
        // Return a new SumOp instance with the modified terms
        SumOp::new(terms)
    }
}

impl Mul<f64> for SumOp {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self::Output {
        // Create a new SumOp with the coefficient multiplied by the given real number
        let terms: Vec<PauliString> = self
            .terms
            .into_iter()
            .map(|term| term * rhs)
            .collect();
        // Return a new SumOp instance with the modified terms
        SumOp::new(terms)
    }
}

impl Add for SumOp {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        // Create a new SumOp with the two sets of terms
        let mut terms: Vec<PauliString> = self.terms;
        terms.extend(other.terms);
        // Return a new SumOp instance with the combined terms
        SumOp::new(terms)
    }
}

impl Add<PauliString> for SumOp {
    type Output = Self;

    fn add(self, other: PauliString) -> Self::Output {
        // Create a new SumOp with the existing terms and the new Pauli string
        let mut terms: Vec<PauliString> = self.terms;
        terms.push(other);
        // Return a new SumOp instance with the combined terms
        SumOp::new(terms)
    }
}