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
use crate::Pauli;
use sprs::vec::{NnzEither, SparseIterTools, VectorIterator};
use sprs::CsVec;
use std::error::Error;
use std::fmt;
use std::ops::Mul;
use Pauli::{X, Z};
use serde::{Serialize, Deserialize};

/// A Pauli operator optimized for sparse operations.
///
/// A Pauli operator is a string of Paulis
/// such as `IXIX` or `XIYIZ`.
/// However,
/// we usually only care about the non-identity positions
/// and we refer to the previous as operators as `X_1 X_3`
/// and `X_0 Y_2 Z_4`.
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
pub struct PauliOperator {
    paulis: CsVec<Pauli>,
}

impl PauliOperator {
    /// Builds a new Pauli Operator.
    ///
    /// To build an operator,
    /// we specify the length,
    /// the position of non-identity elements
    /// and their values.
    ///
    /// # Exemple
    ///
    /// This creates the `XIYIZ` operator.
    ///
    /// ```
    /// # use pauli::PauliOperator;
    /// # use pauli::{X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
    /// ```
    ///
    /// # Panic
    ///
    /// Panics if a position is greater or equal to the length or if
    /// the number of positions and Paulis are different.
    pub fn new(length: usize, positions: Vec<usize>, paulis: Vec<Pauli>) -> Self {
        Self::try_new(length, positions, paulis).expect("invalid operator")
    }

    /// Builds a new Pauli Operator or returns an error
    /// if either a position is greater or equal to the length or if
    /// the numbers of positions and Paulis are different.
    ///
    /// # Exemple
    /// This creates the `XIYIZ` operator.  
    /// ```
    /// # use pauli::PauliOperator;
    /// # use pauli::{X, Y, Z};
    /// let operator = PauliOperator::try_new(5, vec![0, 2, 4], vec![X, Y, Z]);
    /// assert!(operator.is_ok());
    /// ```
    pub fn try_new(
        length: usize,
        positions: Vec<usize>,
        paulis: Vec<Pauli>,
    ) -> Result<Self, PauliError> {
        if positions.len() != paulis.len() {
            Err(PauliError::IncompatibleLength(
                positions.len(),
                paulis.len(),
            ))
        } else if let Some(pos) = positions.iter().find(|pos| **pos >= length) {
            Err(PauliError::OutOfBound(*pos, length))
        } else {
            Ok(Self {
                paulis: CsVec::new(length, positions, paulis),
            })
        }
    }

    /// Creates a Pauli operator of zero length.
    pub fn empty() -> Self {
        Self {
            paulis: CsVec::empty(0),
        }
    }

    /// Checks if two operators commute.
    ///
    /// If an operator is smaller than the other,
    /// it is padded with identities.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::PauliOperator;
    /// # use pauli::{X, Y, Z};
    /// let op1 = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
    /// let op2 = PauliOperator::new(5, vec![2, 3, 4], vec![X, X, X]);
    /// let op3 = PauliOperator::new(5, vec![0, 1], vec![Z, Z]);
    ///
    /// assert!(op1.commutes_with(&op2));
    /// assert!(!op1.commutes_with(&op3));
    /// ```
    pub fn commutes_with(&self, other: &Self) -> bool {
        self.iter()
            .nnz_zip(other.iter())
            .filter(|(_, pauli, other_pauli)| pauli.anticommutes_with(**other_pauli))
            .count()
            % 2
            == 0
    }

    /// Checks if two operators anticommute.
    ///
    /// If an operator is smaller than the other,
    /// it is padded with identities.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::PauliOperator;
    /// # use pauli::{X, Y, Z};
    /// let op1 = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
    /// let op2 = PauliOperator::new(5, vec![2, 3, 4], vec![X, X, X]);
    /// let op3 = PauliOperator::new(5, vec![0, 1], vec![Z, Z]);
    ///
    /// assert!(!op1.anticommutes_with(&op2));
    /// assert!(op1.anticommutes_with(&op3));
    /// ```
    pub fn anticommutes_with(&self, other: &Self) -> bool {
        !self.commutes_with(other)
    }

    /// Returns an iterator over pairs of positions and Paulis.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::{PauliOperator, X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
    /// let mut iter = operator.iter();
    ///
    /// assert_eq!(iter.next(), Some((0, &X)));
    /// assert_eq!(iter.next(), Some((2, &Y)));
    /// assert_eq!(iter.next(), Some((4, &Z)));
    /// assert_eq!(iter.next(), None);
    /// ```
    pub fn iter(&self) -> VectorIterator<Pauli, usize> {
        self.paulis.iter()
    }

    /// Returns the Pauli at the given position
    /// or None if the position is out of bound.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::{PauliOperator, I, X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
    ///
    /// assert_eq!(operator.get(0), Some(X));
    /// assert_eq!(operator.get(1), Some(I));
    /// assert_eq!(operator.get(2), Some(Y));
    /// assert_eq!(operator.get(10), None);
    /// ```
    pub fn get(&self, position: usize) -> Option<Pauli> {
        self.paulis.get(position).cloned().or_else(|| {
            if position < self.len() {
                Some(Pauli::I)
            } else {
                None
            }
        })
    }

    /// Returns the length of the operator.
    pub fn len(&self) -> usize {
        self.paulis.dim()
    }

    /// Returns the number of non identity elements.
    pub fn weight(&self) -> usize {
        self.paulis.nnz()
    }

    /// Returns a slice of the positions
    /// where the element is not identity.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::{PauliOperator, X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
    ///
    /// assert_eq!(operator.non_trivial_positions(), &[0, 2, 4]);
    /// ```
    pub fn non_trivial_positions(&self) -> &[usize] {
        self.paulis.indices()
    }

    /// Returns two operators such that there product is the
    /// original operator and the first contains only Xs and
    /// the second only Zs.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::{PauliOperator, X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
    /// let (x_operator, z_operator) = operator.partition_x_and_z();
    ///
    /// assert_eq!(x_operator, PauliOperator::new(5, vec![0, 2], vec![X, X]));
    /// assert_eq!(z_operator, PauliOperator::new(5, vec![2, 4], vec![Z, Z]));
    /// ```
    pub fn partition_x_and_z(&self) -> (Self, Self) {
        (self.x_part(), self.z_part())
    }

    /// Returns the X part of the operator.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::{PauliOperator, X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
    /// let x_operator = operator.x_part();
    ///
    /// assert_eq!(x_operator, PauliOperator::new(5, vec![0, 2], vec![X, X]));
    /// ```
    pub fn x_part(&self) -> Self {
        let (positions, paulis) = self
            .iter()
            .filter_map(|(position, pauli)| {
                if *pauli != Z {
                    Some((position, X))
                } else {
                    None
                }
            })
            .unzip();
        Self::new(self.len(), positions, paulis)
    }

    /// Returns the Z part of the operator.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::{PauliOperator, X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
    /// let z_operator = operator.z_part();
    ///
    /// assert_eq!(z_operator, PauliOperator::new(5, vec![2, 4], vec![Z, Z]));
    /// ```
    pub fn z_part(&self) -> Self {
        let (positions, paulis) = self
            .iter()
            .filter_map(|(position, pauli)| {
                if *pauli != X {
                    Some((position, Z))
                } else {
                    None
                }
            })
            .unzip();
        Self::new(self.len(), positions, paulis)
    }

    /// Returns the element-wise product of two operators
    /// or an Error if they have different lengths.
    ///
    /// For a panicking version, use the `*` operator.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::PauliOperator;
    /// # use pauli::{X, Y, Z};
    /// let op1 = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
    /// let op2 = PauliOperator::new(5, vec![2, 3, 4], vec![Y, X, Z]);
    ///
    /// let product = PauliOperator::new(5, vec![1, 3, 4], vec![X, Y, Z]);
    ///
    /// assert_eq!(op1.multiply_with(&op2), Ok(product))
    /// ```
    pub fn multiply_with(&self, other: &Self) -> Result<Self, PauliError> {
        if self.len() != other.len() {
            Err(PauliError::IncompatibleLength(self.len(), other.len()))
        } else {
            let (positions, paulis) = self
                .iter()
                .nnz_or_zip(other.iter())
                .map(|values| match values {
                    NnzEither::Left((position, &pauli)) => (position, pauli),
                    NnzEither::Right((position, &pauli)) => (position, pauli),
                    NnzEither::Both((position, &p0, &p1)) => (position, p0 * p1),
                })
                .filter(|(_, pauli)| pauli.is_non_trivial())
                .unzip();
            Ok(PauliOperator::new(self.len(), positions, paulis))
        }
    }

    /// Converts a PauliOperator to a Vec of its non trivial positions
    /// consumming the operator.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::PauliOperator;
    /// # use pauli::{X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
    /// let positions = operator.into_raw_positions();
    /// assert_eq!(positions, vec![1, 2, 3]);
    /// ```
    pub fn into_raw_positions(self) -> Vec<usize> {
        self.paulis.into_raw_storage().0
    }

    /// Converts a PauliOperator to a Vec of the Paulis
    /// consumming the operator.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::PauliOperator;
    /// # use pauli::{X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
    /// let paulis = operator.into_raw_paulis();
    /// assert_eq!(paulis, vec![X, Y, Z]);
    /// ```
    pub fn into_raw_paulis(self) -> Vec<Pauli> {
        self.paulis.into_raw_storage().1
    }

    /// Converts a PauliOperator to a Vec of the positions and
    /// a Vec of Paulis consumming the operator.
    ///
    /// # Example
    ///
    /// ```
    /// # use pauli::PauliOperator;
    /// # use pauli::{X, Y, Z};
    /// let operator = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
    /// let (positions, paulis) = operator.into_raw();
    /// assert_eq!(positions, vec![1, 2, 3]);
    /// assert_eq!(paulis, vec![X, Y, Z]);
    /// ```
    pub fn into_raw(self) -> (Vec<usize>, Vec<Pauli>) {
        self.paulis.into_raw_storage()
    }
}

impl<'a> Mul<&'a PauliOperator> for &'a PauliOperator {
    type Output = PauliOperator;

    fn mul(self, other: Self) -> Self::Output {
        self.multiply_with(other).unwrap()
    }
}

impl fmt::Display for PauliOperator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[")?;
        for (idx, (position, pauli)) in self.iter().enumerate() {
            write!(f, "({}, {})", position, pauli)?;
            if idx + 1 < self.weight() {
                write!(f, ", ")?;
            }
        }
        write!(f, "]")
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum PauliError {
    IncompatibleLength(usize, usize),
    OutOfBound(usize, usize),
}

impl fmt::Display for PauliError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::IncompatibleLength(l0, l1) => {
                write!(f, "incompatible length {} and {}", l0, l1)
            }
            Self::OutOfBound(pos, len) => {
                write!(f, "position {} is out of bound for length {}", pos, len)
            }
        }
    }
}

impl Error for PauliError {}

#[cfg(test)]
mod test {
    use super::*;
    use Pauli::{X, Z};

    #[test]
    fn commutes_with_different_lengths() {
        let long_operator = PauliOperator::new(10, vec![0, 2, 7, 9], vec![X, X, X, X]);
        let short_operator = PauliOperator::new(4, vec![0, 1, 2], vec![Z, Z, Z]);
        assert!(long_operator.commutes_with(&short_operator));
    }

    #[test]
    fn anticommutes_with_different_lengths() {
        let long_operator = PauliOperator::new(10, vec![0, 2, 7, 9], vec![X, Z, X, Z]);
        let short_operator = PauliOperator::new(4, vec![0, 1, 2], vec![Z, Z, Z]);
        assert!(long_operator.anticommutes_with(&short_operator));
    }
}