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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
use crate::common::{
    error::{inv_arg, Error, Result},
    util::log_2,
};
use integer_sqrt::IntegerSquareRoot;
use num_complex::Complex64;
use serde::{Deserialize, Serialize};
#[cfg(feature = "bindings")]
use std::os::raw::c_double;
use std::{
    collections::HashSet,
    convert::TryFrom,
    f64::consts::FRAC_1_SQRT_2,
    fmt,
    fmt::{Display, Formatter},
    hash::{Hash, Hasher},
    ops::{Index, IndexMut},
};

/// Matrix wrapper for `Gate` matrices.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Matrix {
    /// The elements in the matrix stored as a `Vec<Complex64>`.
    #[serde(with = "complex_serde")]
    data: Vec<Complex64>,
    /// Dimension of inner data.
    dimension: usize,
    // TODO: we can't have serde skip the above field because then it becomes 0
    // when a matrix is received, making the matrix impl not work. It's kind of
    // meh though that the serialization format now allows receiving a broken
    // matrix. OTOH, it could already do that, since for serde there's no square
    // number check on data.len().
}

/// This mod provides ser/de for Vec<Complex64>.
mod complex_serde {
    use super::Complex64;
    use serde::{
        ser::SerializeSeq,
        {Deserialize, Deserializer, Serialize, Serializer},
    };

    #[derive(Serialize, Deserialize)]
    #[serde(remote = "Complex64")]
    struct Complex64Def {
        re: f64,
        im: f64,
    }

    pub fn serialize<S>(value: &[Complex64], serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        #[derive(Serialize)]
        struct Wrapper<'a>(#[serde(with = "Complex64Def")] &'a Complex64);
        let mut seq = serializer.serialize_seq(Some(value.len()))?;
        for c in value.iter().map(Wrapper) {
            seq.serialize_element(&c)?;
        }
        seq.end()
    }

    pub fn deserialize<'de, D>(deserializer: D) -> std::result::Result<Vec<Complex64>, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Wrapper(#[serde(with = "Complex64Def")] Complex64);
        let v = Vec::deserialize(deserializer)?;
        Ok(v.into_iter().map(|Wrapper(c)| c).collect())
    }
}

impl Hash for Matrix {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // f64 is not Hash and not Eq.
        // However, since Hash here is used to cache results false negatives
        // don't break anything.
        self.data.iter().for_each(|c| {
            c.re.to_le_bytes().hash(state);
            c.im.to_le_bytes().hash(state);
        });
    }
}

// Byte-wise PartialEq for Matrix.
impl PartialEq for Matrix {
    fn eq(&self, other: &Self) -> bool {
        // This is a byte-wise comparison to determine Eq.
        self.len() == other.len()
            && self.data.iter().zip(other.data.iter()).all(|(x, y)| {
                x.re.to_le_bytes() == y.re.to_le_bytes() && x.im.to_le_bytes() == y.im.to_le_bytes()
            })
    }
}

// Byte-wise Eq for Matrix.
impl Eq for Matrix {}

impl Index<(usize, usize)> for Matrix {
    type Output = Complex64;
    fn index(&self, index: (usize, usize)) -> &Self::Output {
        &self.data[index.0 * self.dimension + index.1]
    }
}

impl IndexMut<(usize, usize)> for Matrix {
    fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
        &mut self.data[index.0 * self.dimension + index.1]
    }
}

impl Index<usize> for Matrix {
    type Output = Complex64;
    fn index(&self, index: usize) -> &Self::Output {
        &self.data[index]
    }
}

impl IndexMut<usize> for Matrix {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.data[index]
    }
}

impl TryFrom<Vec<Complex64>> for Matrix {
    type Error = Error;
    fn try_from(elements: Vec<Complex64>) -> Result<Self> {
        Matrix::new(elements)
    }
}

impl IntoIterator for Matrix {
    type Item = Complex64;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.data.into_iter()
    }
}

impl Display for Matrix {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        for row in 0..self.dimension() {
            for col in 0..self.dimension() {
                let e = self[(row, col)];
                write!(f, "{:6.3}{:+6.3}i  ", e.re, e.im)?;
            }
            writeln!(f)?;
        }
        Ok(())
    }
}

impl Matrix {
    /// Returns a new Matrix with provided elements.
    pub fn new(elements: impl IntoIterator<Item = Complex64>) -> Result<Self> {
        let elements = elements.into_iter().collect::<Vec<Complex64>>();
        let dimension = elements.len().integer_sqrt();
        if elements.len() != dimension * dimension {
            return inv_arg("matrix is not square");
        }
        Ok(Matrix {
            dimension: elements.len().integer_sqrt(),
            data: elements,
        })
    }

    /// Returns a new identity Matrix with given dimension.
    pub fn new_identity(dimension: usize) -> Self {
        let mut output = Matrix::new(vec![c!(0.); dimension.pow(2)]).unwrap();
        for i in 0..dimension {
            output[(i, i)] = c!(1.);
        }
        output
    }

    /// Approximately compares this Matrix with another Matrix.
    /// `epsilon` specifies the maximum element-wise root-mean-square error
    /// between the matrices that results in a positive match. `ignore_phase`
    /// specifies whether the aforementioned check should ignore global phase.
    pub fn approx_eq(&self, other: &Matrix, epsilon: f64, ignore_global_phase: bool) -> bool {
        // Sizes must match
        if self.len() != other.len() {
            return false;
        }
        let phase_delta = if ignore_global_phase {
            let phase_delta =
                self.data
                    .iter()
                    .zip(other.data.iter())
                    .fold(c!(0.), |mut phase_delta, (a, b)| {
                        phase_delta += a * b.conj();
                        phase_delta
                    });
            phase_delta / phase_delta.norm()
        } else {
            c!(1.)
        };
        self.data
            .iter()
            .zip(other.data.iter())
            .try_fold(epsilon * epsilon, |mut tolerance, (a, b)| {
                tolerance -= (a - b * phase_delta).norm_sqr();
                if tolerance.is_sign_negative() {
                    None
                } else {
                    Some(tolerance)
                }
            })
            .is_some()
    }

    /// Approximately compares two matrices representing bases with each other.
    ///
    /// The basis representation works as follows:
    ///
    ///  - (apply hermetian of matrix as gate if measurement)
    ///  - measure/prep Z
    ///  - apply matrix as gate
    ///
    /// Any rotation around the Z axis is don't care before and after the
    /// prep/measurement, because that information is thrown away when the
    /// prep/measurement occurs. Global phase is also don't care as usual. That
    /// means the following must hold for the bases to be equal:
    ///
    /// ```text
    ///               / e^ix   0  \  ~
    ///     basis_a * |           |  =  basis_b
    ///               \  0   e^iy /
    ///
    ///     / a_a e^ix   a_b e^iy \  ~  / b_a   b_b \
    ///     |                     |  =  |           |
    ///     \ a_c e^ix   a_d e^iy /     \ b_c   b_d /
    /// ```
    ///
    /// with x and y being free variables.
    pub fn basis_approx_eq(&self, other: &Matrix, epsilon: f64) -> bool {
        // Sizes must both be 2x2
        if self.dimension() != 2 || other.dimension() != 2 {
            return false;
        }
        let mut tolerance = epsilon * epsilon;
        for col in 0..2 {
            let phase_delta =
                self[(0, col)] * other[(0, col)].conj() + self[(1, col)] * other[(1, col)].conj();
            let phase_delta = phase_delta.unscale(phase_delta.norm());
            for row in 0..2 {
                tolerance -= (self[(row, col)] - other[(row, col)] * phase_delta).norm_sqr();
                if tolerance < 0. {
                    return false;
                }
            }
        }
        true
    }

    /// Checks whether this Matrix is approximately unitary by multiplying
    /// itself with its hermetian, and measuring the RMS difference of the
    /// result and the identity matrix. If this difference is more than epsilon
    /// the matrix is not (sufficiently) unitary and this function returns
    /// false, otherwise it returns true.
    pub fn approx_unitary(&self, epsilon: f64) -> bool {
        let mut tolerance = epsilon * epsilon;

        // Check if result of matrix multiplication of self and our hermetian
        // is identity.
        for i in 0..self.dimension {
            for j in 0..self.dimension {
                let element = (0..self.dimension).fold(Complex64::new(0., 0.), |acc, k| {
                    acc + (self[i * self.dimension + k] * self[j * self.dimension + k].conj())
                });
                let expected = if i == j {
                    Complex64::new(1., 0.)
                } else {
                    Complex64::new(0., 0.)
                };
                tolerance -= (expected - element).norm_sqr();
                if tolerance < 0. {
                    return false;
                }
            }
        }
        true
    }

    /// Returns new Matrix with `number_of_control` qubits added.
    pub fn add_controls(&self, number_of_controls: usize) -> Self {
        let dimension = self.dimension() * 2usize.pow(number_of_controls as u32);
        let mut output = Matrix::new_identity(dimension);
        for row in 0..self.dimension() {
            for col in 0..self.dimension() {
                output[(
                    row + dimension - self.dimension(),
                    col + dimension - self.dimension(),
                )] = self[(row, col)];
            }
        }
        output
    }

    /// Returns new Matrix with control behavior removed from the Matrix, and
    /// the control indices corresponding to the target qubits acting as
    /// control in the original Matrix.
    ///
    /// `epsilon` specifies the maximum magitude of the difference between the
    /// column vectors of the input matrix and the identity matrix (after
    /// dephasing if `ignore_phase` is set) for the column vector to be
    /// considered to not affect the respective entry in the quantum state
    /// vector. Note that if this is greater than zero, the resulting gate may
    /// not be exactly equivalent. If `ignore_global_phase` is set, any global
    /// phase in the matrix is ignored, but note that if control qubits are
    /// stripped the "global" phase of the resulting submatrix is always
    /// significant.
    ///
    /// This function assumes that the incoming matrix is unitary (within
    /// `epsilon`) without verifying that this is the case. The results may
    /// thus be invalid if it was not.
    ///
    /// The identity matrix special case is handled by interpreting all qubits
    /// as non-control (any set of qubits would satisfy the controlled matrix
    /// criterium).
    pub fn strip_control(
        &self,
        epsilon: f64,
        ignore_global_phase: bool,
    ) -> (HashSet<usize>, Matrix) {
        // If we're to ignore the global phase of the matrix, we dephase using
        // the phase of the first matrix entry; for any controlled matrix, this
        // entry will have a unit magnitude.
        let phase = if ignore_global_phase {
            Complex64::from_polar(1.0, self[(0, 0)].arg())
        } else {
            c!(1.0)
        };

        // Determine which qubits are control qubits. This is done by detecting
        // which column vectors match the column vectors of the identity matrix
        // phased by `phase`. If a column vector is not approximately equal, it
        // may indicate that some qubits cannot be controls. Note that the last
        // column vector is never checked, because it never rules out anything.
        // Note also that we short-circuit to returning the original matrix
        // when all qubits are ruled out as being controls.
        let epsilon_sqr = epsilon.powi(2);
        let mut controls_int = self.dimension() - 1;
        for i in 0..self.dimension() - 1 {
            let mut error_sqr = 0.0;
            for j in 0..self.dimension() {
                if i == j {
                    error_sqr += (self[(i, j)] - phase).norm_sqr();
                } else {
                    error_sqr += self[(i, j)].norm_sqr();
                }
                if error_sqr > epsilon_sqr {
                    controls_int &= i;
                    if controls_int == 0 {
                        return (HashSet::new(), self.clone());
                    }
                    break;
                }
            }
        }

        // If all qubits could be control qubits, we're actually dealing with
        // an identity matrix.
        if controls_int == self.dimension() - 1 {
            return (HashSet::new(), self.clone());
        }

        // Construct a HashSet of the qubits that were found to be controls.
        // Note that the qubit indices used in DQCsim are reversed with respect
        // to the matrix indices, so we do said reversal here.
        let mut controls = HashSet::new();
        let num_qubits = self.num_qubits().unwrap();
        for q in 0..num_qubits {
            if controls_int & (1 << q) != 0 {
                controls.insert(num_qubits - q - 1);
            }
        }

        // Compute the submatrix.
        let mut entries =
            Vec::with_capacity((self.dimension() / 2_usize.pow(controls.len() as u32)).pow(2));
        for row in 0..self.dimension() {
            if row & controls_int == controls_int {
                for col in 0..self.dimension() {
                    if col & controls_int == controls_int {
                        entries.push(self[(row, col)] * phase.conj());
                    }
                }
            }
        }

        (controls, Matrix::new(entries).unwrap())
    }

    /// Returns the number of elements in the Matrix.
    pub fn len(&self) -> usize {
        self.data.len()
    }
    /// Returns true if the Matrix is empty.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Returns the dimension of the Matrix.
    /// The dimension equals the square root of the number of elements.
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Returns the number of qubits for this Matrix.
    pub fn num_qubits(&self) -> Option<usize> {
        log_2(self.dimension)
    }

    /// Returns the element at given row and colum index.
    /// Returns `None` for out of bound indices.
    pub fn get(&self, row: usize, column: usize) -> Option<&Complex64> {
        self.data.get(row * self.dimension + column)
    }

    #[cfg(feature = "bindings")]
    pub(crate) fn as_ptr(&self) -> *const c_double {
        self.data.as_ptr() as *const c_double
    }
}

/// Predefined measurement/prep bases.
#[derive(Clone, Copy, Debug)]
pub enum Basis {
    X,
    Y,
    Z,
}

impl From<Basis> for Matrix {
    fn from(basis: Basis) -> Matrix {
        match basis {
            Basis::X => matrix!(
                (FRAC_1_SQRT_2), (-FRAC_1_SQRT_2);
                (FRAC_1_SQRT_2), (FRAC_1_SQRT_2);
            ),
            Basis::Y => matrix!(
                (FRAC_1_SQRT_2), (0., FRAC_1_SQRT_2);
                (0., FRAC_1_SQRT_2), (FRAC_1_SQRT_2);
            ),
            Basis::Z => matrix!(
                1., 0.;
                0., 1.;
            ),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::gates::UnboundUnitaryGate;
    use std::iter::FromIterator;

    #[test]
    fn matrix() {
        let mut a = matrix!(
            1., 1.;
            1., (-1.);
        );
        let b = matrix!(
            (0., 1.), 0.;
            1., (-1., -1.);
        );
        let c = matrix!(
            1., (-1., -1.);
            (0., 1.), 0.;
        );
        assert_eq!(a, a);
        assert_eq!(b, b);
        assert_eq!(c, c);
        assert_ne!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);
        assert_eq!(a.len(), 4);
        assert_eq!(b.dimension(), 2);
        assert_eq!(b.get(0, 0).unwrap(), &b[(0, 0)]);
        assert!(b.get(4, 4).is_none());
        assert_eq!(b[3], c[1]);
        a[0] = b[3];
        assert_eq!(b[3], a[0]);
        assert_eq!(a[(0, 0)], b[(1, 1)]);
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        a.hash(&mut hasher);
        let h = hasher.finish();
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        a.hash(&mut hasher);
        let hh = hasher.finish();
        assert_eq!(h, hh);
    }

    #[test]
    fn matrix_approx_eq() {
        let x1 = matrix!(
            0., 1.;
            1., 0.;
        );
        let x2 = matrix!(
            0., (-1.);
            (-1.), 0.;
        );
        assert!(x1.approx_eq(&x2, 0., true));
        assert!(x2.approx_eq(&x1, 0., true));

        let h1 = matrix!(
            (FRAC_1_SQRT_2), (FRAC_1_SQRT_2);
            (FRAC_1_SQRT_2), (-FRAC_1_SQRT_2);
        );
        let h2 = matrix!(
            (-FRAC_1_SQRT_2), (-FRAC_1_SQRT_2);
            (-FRAC_1_SQRT_2), (FRAC_1_SQRT_2);
        );
        let h3 = matrix!(
            (0., FRAC_1_SQRT_2), (0., FRAC_1_SQRT_2);
            (0., FRAC_1_SQRT_2), (0., -FRAC_1_SQRT_2);
        );
        let h4 = matrix!(
            (0., -FRAC_1_SQRT_2), (0., -FRAC_1_SQRT_2);
            (0., -FRAC_1_SQRT_2), (0., FRAC_1_SQRT_2);
        );
        assert!(h1.approx_eq(&h2, 0., true));
        assert!(h1.approx_eq(&h3, 0., true));
        assert!(h1.approx_eq(&h4, 0., true));
        assert!(h2.approx_eq(&h1, 0., true));
        assert!(h2.approx_eq(&h3, 0., true));
        assert!(h2.approx_eq(&h4, 0., true));
        assert!(h3.approx_eq(&h1, 0., true));
        assert!(h3.approx_eq(&h2, 0., true));
        assert!(h3.approx_eq(&h4, 0., true));
        assert!(h4.approx_eq(&h1, 0., true));
        assert!(h3.approx_eq(&h2, 0., true));
        assert!(h3.approx_eq(&h3, 0., true));
    }

    #[test]
    #[allow(clippy::cognitive_complexity)]
    fn basis_approx_eq() {
        let x: Matrix = Basis::X.into();
        let y: Matrix = Basis::Y.into();
        let z: Matrix = Basis::Z.into();
        let x2: Matrix = matrix!(
            (FRAC_1_SQRT_2), (FRAC_1_SQRT_2);
            (FRAC_1_SQRT_2), (-FRAC_1_SQRT_2);
        );
        let y2: Matrix = matrix!(
            (FRAC_1_SQRT_2), (FRAC_1_SQRT_2);
            (0., FRAC_1_SQRT_2), (0., -FRAC_1_SQRT_2);
        );

        assert!(x.basis_approx_eq(&x, 0.));
        assert!(!x.basis_approx_eq(&y, 0.));
        assert!(!x.basis_approx_eq(&z, 0.));
        assert!(x.basis_approx_eq(&x2, 0.));
        assert!(!x.basis_approx_eq(&y2, 0.));

        assert!(!y.basis_approx_eq(&x, 0.));
        assert!(y.basis_approx_eq(&y, 0.));
        assert!(!y.basis_approx_eq(&z, 0.));
        assert!(!y.basis_approx_eq(&x2, 0.));
        assert!(y.basis_approx_eq(&y2, 0.));

        assert!(!z.basis_approx_eq(&x, 0.));
        assert!(!z.basis_approx_eq(&y, 0.));
        assert!(z.basis_approx_eq(&z, 0.));
        assert!(!z.basis_approx_eq(&x2, 0.));
        assert!(!z.basis_approx_eq(&y2, 0.));

        assert!(x2.basis_approx_eq(&x, 0.));
        assert!(!x2.basis_approx_eq(&y, 0.));
        assert!(!x2.basis_approx_eq(&z, 0.));
        assert!(x2.basis_approx_eq(&x2, 0.));
        assert!(!x2.basis_approx_eq(&y2, 0.));

        assert!(!y2.basis_approx_eq(&x, 0.));
        assert!(y2.basis_approx_eq(&y, 0.));
        assert!(!y2.basis_approx_eq(&z, 0.));
        assert!(!y2.basis_approx_eq(&x2, 0.));
        assert!(y2.basis_approx_eq(&y2, 0.));
    }

    #[test]
    fn add_controls() {
        let x: Matrix = UnboundUnitaryGate::X.into();
        assert!(x.add_controls(1).approx_eq(
            &matrix!(
                1., 0., 0., 0.;
                0., 1., 0., 0.;
                0., 0., 0., 1.;
                0., 0., 1., 0.;
            ),
            0.0001,
            false
        ));
    }

    #[test]
    fn strip_control() {
        let cnot_a = matrix!(
            1., 0., 0., 0.;
            0., 1., 0., 0.;
            0., 0., 0., 1.;
            0., 0., 1., 0.;
        );
        let cnot_b = matrix!(
            1., 0., 0., 0.;
            0., 0., 0., 1.;
            0., 0., 1., 0.;
            0., 1., 0., 0.;
        );

        let (map_a, matrix_a) = cnot_a.strip_control(0.0001, false);
        assert_eq!(map_a, HashSet::from_iter(vec![0]));
        let (map_b, matrix_b) = cnot_b.strip_control(0.0001, false);
        assert_eq!(map_b, HashSet::from_iter(vec![1]));
        assert!(matrix_a.approx_eq(&matrix_b, 0.0001, false));
        assert_ne!(map_a, map_b);

        let i = matrix!(
            1., 0., 0., 0., 0., 0., 0., 0.;
            0., 1., 0., 0., 0., 0., 0., 0.;
            0., 0., 1., 0., 0., 0., 0., 0.;
            0., 0., 0., 1., 0., 0., 0., 0.;
            0., 0., 0., 0., 1., 0., 0., 0.;
            0., 0., 0., 0., 0., 1., 0., 0.;
            0., 0., 0., 0., 0., 0., 1., 0.;
            0., 0., 0., 0., 0., 0., 0., 1.;
        );

        let (map_a, matrix_a) = i.strip_control(0.0001, false);
        assert!(matrix_a.approx_eq(&Matrix::new_identity(8), 0.0001, false));
        assert!(map_a.is_empty());

        let fredkin = matrix!(
            1., 0., 0., 0., 0., 0., 0., 0.;
            0., 1., 0., 0., 0., 0., 0., 0.;
            0., 0., 1., 0., 0., 0., 0., 0.;
            0., 0., 0., 1., 0., 0., 0., 0.;
            0., 0., 0., 0., 1., 0., 0., 0.;
            0., 0., 0., 0., 0., 0., 1., 0.;
            0., 0., 0., 0., 0., 1., 0., 0.;
            0., 0., 0., 0., 0., 0., 0., 1.;
        );

        let (map_a, matrix_a) = fredkin.strip_control(0.0001, false);
        assert_eq!(map_a, HashSet::from_iter(vec![0]));
        let x: Matrix = UnboundUnitaryGate::SWAP.into();
        assert!(matrix_a.approx_eq(&x, 0.0001, false));

        let toffoli = matrix!(
            1., 0., 0., 0., 0., 0., 0., 0.;
            0., 1., 0., 0., 0., 0., 0., 0.;
            0., 0., 1., 0., 0., 0., 0., 0.;
            0., 0., 0., 1., 0., 0., 0., 0.;
            0., 0., 0., 0., 1., 0., 0., 0.;
            0., 0., 0., 0., 0., 1., 0., 0.;
            0., 0., 0., 0., 0., 0., 0., 1.;
            0., 0., 0., 0., 0., 0., 1., 0.;
        );

        let (map_a, matrix_a) = toffoli.strip_control(0.0001, false);
        assert_eq!(map_a, HashSet::from_iter(vec![0, 1]));

        let x: Matrix = UnboundUnitaryGate::X.into();
        assert!(matrix_a.approx_eq(&x, 0.001, false));
    }
}