qcrypto 0.0.4

Library for the design, simulation and validation of Quantum Cryptography protocols
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
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
use crate::core::errors::GateError;
use crate::core::utils;
use ndarray::{Array2, arr2};
use num_complex::Complex64;
use std::f64::consts::PI;

/// Represents a quantum gate.
///
/// A gate is defined by its unitary matrix and the number of qubits it acts on.
pub struct Gate {
    /// The unitary matrix of the gate.
    pub matrix: Array2<Complex64>,
    /// The number of qubits the gate acts on.
    pub num_qubits: usize,
}

impl Gate {
    /// Creates a new `Gate` from a unitary matrix.
    ///
    /// # Arguments
    ///
    /// * `matrix` - A square, unitary `Array2<Complex64>`.
    ///
    /// # Returns
    ///
    /// A `Result` containing the constructed `Gate`.
    ///
    /// # Errors
    ///
    /// Returns a `GateError` if:
    /// - The matrix is not square.
    /// - The matrix dimensions are not a power of 2.
    /// - The matrix is not unitary.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    /// use ndarray::arr2;
    /// use num_complex::Complex64;
    ///
    /// let matrix = arr2(&[
    ///     [Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)],
    ///     [Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
    /// ]);
    /// let gate = Gate::new(matrix).unwrap();
    /// assert_eq!(gate.num_qubits, 1);
    /// ```
    pub fn new(matrix: Array2<Complex64>) -> Result<Self, GateError> {
        let (rows, cols) = matrix.dim();

        if rows != cols {
            return Err(GateError::NotSquareMatrix);
        }

        if !rows.is_power_of_two() {
            return Err(GateError::InvalidDimensions);
        }

        if !Self::check_unitary(&matrix) {
            return Err(GateError::NonUnitary);
        }

        let num_qubits = rows.trailing_zeros() as usize;

        Ok(Self { matrix, num_qubits })
    }

    /// Checks if a given matrix is unitary
    fn check_unitary(matrix: &Array2<Complex64>) -> bool {
        let (rows, _) = matrix.dim();
        let eye = Array2::<Complex64>::eye(rows);

        let u_dagger = matrix.t().mapv(|x| x.conj());
        let product = matrix.dot(&u_dagger);

        product
            .iter()
            .zip(eye.iter())
            .all(|(a, b)| (*a - *b).norm() < 1e-12)
    }

    /// Expands a gate to act on a larger system of qubits.
    ///
    /// This function creates a new gate that acts on `num_total_qubits` by applying the original `gate`
    /// to the specified `targets` and `controls` (if any), and Identity on the rest.
    ///
    /// # Arguments
    ///
    /// * `num_total_qubits` - The total number of qubits in the system.
    /// * `gate` - The base gate to expand.
    /// * `targets` - Indices of the target qubits.
    /// * `controls` - Indices of the control qubits.
    ///
    /// # Returns
    ///
    /// A `Result` containing the expanded `Gate`.
    ///
    /// # Errors
    ///
    /// Returns `GateError` if:
    /// - Duplicate indices are found in `targets` or `controls`.
    /// - A qubit is used as both control and target.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// // Expand X gate into a CNOT (control=0, target=1) on a 2-qubit system
    /// let cnot = Gate::expand_gate(&Gate::x(), 2, &[1], &[0]).unwrap();
    /// assert_eq!(cnot.num_qubits, 2);
    /// assert_eq!(cnot.matrix.dim(), (4, 4));
    /// ```
    pub fn expand_gate(
        &self,
        num_total_qubits: usize,
        targets: &[usize],
        controls: &[usize],
    ) -> Result<Gate, GateError> {
        if let Some(dup) = utils::find_duplicate(targets) {
            return Err(GateError::DuplicateQubit(dup));
        }

        if let Some(dup) = utils::find_duplicate(controls) {
            return Err(GateError::DuplicateQubit(dup));
        }

        for &c in controls {
            if targets.contains(&c) {
                return Err(GateError::ControlTargetOverlap(c));
            }
        }

        Ok(Gate {
            matrix: utils::expand_operator(&self.matrix, num_total_qubits, targets, controls),
            num_qubits: num_total_qubits,
        })
    }

    // --- Standard Gates ---

    /// Creates an Identity gate.
    ///
    /// # Returns
    ///
    /// A single-qubit Identity gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// let gate = Gate::i();
    /// assert_eq!(gate.num_qubits, 1);
    /// assert_eq!(gate.matrix.dim(), (2, 2));
    /// ```
    pub fn i() -> Gate {
        Gate::new(arr2(&[
            [Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
            [Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)],
        ]))
        .expect("Error in I gate")
    }

    /// Creates a Pauli-X gate (NOT gate).
    ///
    /// Flips the state of a qubit: $|0> \to |1>$ and $|1> \to |0>$.
    ///
    /// # Returns
    ///
    /// A single-qubit Pauli-X gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::{QuantumState, Gate};
    ///
    /// let mut state = QuantumState::new(1); // |0>
    /// state.apply(&Gate::x(), &[0]).unwrap(); // |1>
    /// ```
    pub fn x() -> Gate {
        Gate::new(arr2(&[
            [Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)],
            [Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
        ]))
        .expect("Error in X gate")
    }

    /// Creates a Pauli-Y gate.
    ///
    /// Applies a rotation around the Y axis: $Y = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}$.
    ///
    /// # Returns
    ///
    /// A single-qubit Pauli-Y gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// let gate = Gate::y();
    /// assert_eq!(gate.num_qubits, 1);
    /// ```
    pub fn y() -> Gate {
        Gate::new(arr2(&[
            [Complex64::new(0.0, 0.0), Complex64::new(0.0, -1.0)],
            [Complex64::new(0.0, 1.0), Complex64::new(0.0, 0.0)],
        ]))
        .expect("Error in Y gate")
    }

    /// Creates a Pauli-Z gate.
    ///
    /// Applies a phase flip: $|0> \to |0>$, $|1> \to -|1>$.
    ///
    /// # Returns
    ///
    /// A single-qubit Pauli-Z gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// let gate = Gate::z();
    /// assert_eq!(gate.num_qubits, 1);
    /// ```
    pub fn z() -> Gate {
        Gate::new(arr2(&[
            [Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
            [Complex64::new(0.0, 0.0), Complex64::new(-1.0, 0.0)],
        ]))
        .expect("Error in Z gate")
    }

    /// Creates a Hadamard gate.
    ///
    /// Creates a superposition: $|0> \to |+>$ and $|1> \to |->$.
    ///
    /// # Returns
    ///
    /// A single-qubit Hadamard gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::{QuantumState, Gate, Measurement};
    ///
    /// let mut state = QuantumState::new(1); // |0>
    /// state.apply(&Gate::h(), &[0]).unwrap(); // |+>
    /// let probs = state.set_measurement(&Measurement::z_basis(), &[0]).unwrap();
    /// assert!((probs[0] - 0.5).abs() < 1e-12);
    /// assert!((probs[1] - 0.5).abs() < 1e-12);
    /// ```
    pub fn h() -> Gate {
        let factor = 1.0 / 2.0_f64.sqrt();
        Gate::new(arr2(&[
            [Complex64::new(factor, 0.0), Complex64::new(factor, 0.0)],
            [Complex64::new(factor, 0.0), Complex64::new(-factor, 0.0)],
        ]))
        .expect("Error in H gate")
    }

    /// Creates an S gate (Phase gate, $Z^{1/2}$).
    ///
    /// Applies a $\pi/2$ phase: $|1> \to i|1>$.
    ///
    /// # Returns
    ///
    /// A single-qubit S gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// let gate = Gate::s();
    /// assert_eq!(gate.num_qubits, 1);
    /// ```
    pub fn s() -> Gate {
        Gate::new(arr2(&[
            [Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
            [Complex64::new(0.0, 0.0), Complex64::new(0.0, 1.0)],
        ]))
        .expect("Error in S gate")
    }

    /// Creates a T gate ($Z^{1/4}$).
    ///
    /// Applies a $\pi/4$ phase: $|1> \to e^{i\pi/4}|1>$.
    ///
    /// # Returns
    ///
    /// A single-qubit T gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// let gate = Gate::t_gate();
    /// assert_eq!(gate.num_qubits, 1);
    /// ```
    pub fn t_gate() -> Gate {
        let angle = PI / 4.0;
        Gate::new(arr2(&[
            [Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
            [
                Complex64::new(0.0, 0.0),
                Complex64::new(angle.cos(), angle.sin()),
            ],
        ]))
        .expect("Error in T gate")
    }

    /// Creates a CNOT (Controlled-NOT) gate.
    ///
    /// Flips the target qubit (qubit 1) if the control qubit (qubit 0) is $|1>$.
    ///
    /// # Returns
    ///
    /// A two-qubit CNOT gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// let gate = Gate::cnot();
    /// assert_eq!(gate.num_qubits, 2);
    /// assert_eq!(gate.matrix.dim(), (4, 4));
    /// ```
    pub fn cnot() -> Gate {
        Gate::x()
            .expand_gate(2, &[1], &[0])
            .expect("Error in CNOT gate")
    }

    /// Creates a SWAP gate.
    ///
    /// Swaps the states of two qubits: $|01> \to |10>$.
    ///
    /// # Returns
    ///
    /// A two-qubit SWAP gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// let gate = Gate::swap();
    /// assert_eq!(gate.num_qubits, 2);
    /// assert_eq!(gate.matrix.dim(), (4, 4));
    /// ```
    pub fn swap() -> Gate {
        Gate::new(arr2(&[
            [
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
            ],
            [
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
            ],
            [
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
            ],
            [
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
            ],
        ]))
        .expect("Error in SWAP gate")
    }

    /// Creates a Toffoli gate (CCNOT).
    ///
    /// Flips the target qubit (qubit 2) only if both control qubits (qubits 0 and 1) are $|1>$.
    ///
    /// # Returns
    ///
    /// A three-qubit Toffoli gate.
    ///
    /// # Example
    /// ```rust
    /// use qcrypto::Gate;
    ///
    /// let gate = Gate::toffoli();
    /// assert_eq!(gate.num_qubits, 3);
    /// assert_eq!(gate.matrix.dim(), (8, 8));
    /// ```
    pub fn toffoli() -> Gate {
        Gate::x()
            .expand_gate(3, &[2], &[0, 1])
            .expect("Error in Toffoli gate")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ndarray::Array2;

    // --- Gate::new boundary tests ---

    #[test]
    fn test_new_non_square_matrix() {
        let matrix = Array2::from_shape_vec((2, 3), vec![Complex64::new(1.0, 0.0); 6]).unwrap();

        assert!(matches!(Gate::new(matrix), Err(GateError::NotSquareMatrix)));
    }

    #[test]
    fn test_new_non_power_of_two() {
        // 3x3 is square but not a power of 2
        let matrix = Array2::eye(3);
        assert!(matches!(
            Gate::new(matrix),
            Err(GateError::InvalidDimensions)
        ));
    }

    #[test]
    fn test_new_non_unitary() {
        // A 2x2 matrix that is NOT unitary
        let matrix = arr2(&[
            [Complex64::new(2.0, 0.0), Complex64::new(0.0, 0.0)],
            [Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)],
        ]);
        assert!(matches!(Gate::new(matrix), Err(GateError::NonUnitary)));
    }

    #[test]
    fn test_new_valid_identity() {
        let matrix: Array2<Complex64> = Array2::eye(2);
        let gate = Gate::new(matrix).unwrap();
        assert_eq!(gate.num_qubits, 1);
    }

    #[test]
    fn test_new_valid_4x4() {
        // 4x4 identity is a valid 2-qubit gate
        let matrix: Array2<Complex64> = Array2::eye(4);
        let gate = Gate::new(matrix).unwrap();
        assert_eq!(gate.num_qubits, 2);
    }

    // --- Gate::expand_gate boundary tests ---

    #[test]
    fn test_expand_gate_duplicate_targets() {
        let result = Gate::expand_gate(&Gate::x(), 2, &[0, 0], &[]);
        assert!(matches!(result, Err(GateError::DuplicateQubit(0))));
    }

    #[test]
    fn test_expand_gate_duplicate_controls() {
        let result = Gate::expand_gate(&Gate::x(), 3, &[2], &[0, 0]);
        assert!(matches!(result, Err(GateError::DuplicateQubit(0))));
    }

    #[test]
    fn test_expand_gate_control_target_overlap() {
        let result = Gate::expand_gate(&Gate::x(), 2, &[0], &[0]);
        assert!(matches!(result, Err(GateError::ControlTargetOverlap(0))));
    }

    // --- Standard gate mathematical properties ---

    #[test]
    fn test_all_standard_gates_are_unitary() {
        assert!(Gate::check_unitary(&Gate::i().matrix));
        assert!(Gate::check_unitary(&Gate::x().matrix));
        assert!(Gate::check_unitary(&Gate::y().matrix));
        assert!(Gate::check_unitary(&Gate::z().matrix));
        assert!(Gate::check_unitary(&Gate::h().matrix));
        assert!(Gate::check_unitary(&Gate::s().matrix));
        assert!(Gate::check_unitary(&Gate::t_gate().matrix));
        assert!(Gate::check_unitary(&Gate::cnot().matrix));
        assert!(Gate::check_unitary(&Gate::swap().matrix));
        assert!(Gate::check_unitary(&Gate::toffoli().matrix));
    }

    #[test]
    fn test_pauli_x_is_involution() {
        // X² = I
        let x = &Gate::x().matrix;
        let x_squared = x.dot(x);
        let eye = Array2::<Complex64>::eye(2);

        for (a, b) in x_squared.iter().zip(eye.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }
    }

    #[test]
    fn test_pauli_y_is_involution() {
        // Y² = I
        let y = &Gate::y().matrix;
        let y_squared = y.dot(y);
        let eye = Array2::<Complex64>::eye(2);

        for (a, b) in y_squared.iter().zip(eye.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }
    }

    #[test]
    fn test_pauli_z_is_involution() {
        // Z² = I
        let z = &Gate::z().matrix;
        let z_squared = z.dot(z);
        let eye = Array2::<Complex64>::eye(2);

        for (a, b) in z_squared.iter().zip(eye.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }
    }

    #[test]
    fn test_hadamard_is_involution() {
        // H² = I
        let h = &Gate::h().matrix;
        let h_squared = h.dot(h);
        let eye = Array2::<Complex64>::eye(2);

        for (a, b) in h_squared.iter().zip(eye.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }
    }

    #[test]
    fn test_s_squared_is_z() {
        // S² = Z
        let s = &Gate::s().matrix;
        let z = &Gate::z().matrix;
        let s_squared = s.dot(s);

        for (a, b) in s_squared.iter().zip(z.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }
    }

    #[test]
    fn test_swap_is_involution() {
        // SWAP² = I
        let sw = &Gate::swap().matrix;
        let sw_squared = sw.dot(sw);
        let eye = Array2::<Complex64>::eye(4);

        for (a, b) in sw_squared.iter().zip(eye.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }
    }

    #[test]
    fn test_cnot_is_involution() {
        // CNOT² = I
        let cnot = &Gate::cnot().matrix;
        let cnot_squared = cnot.dot(cnot);
        let eye = Array2::<Complex64>::eye(4);

        for (a, b) in cnot_squared.iter().zip(eye.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }
    }

    #[test]
    fn test_xhz_equals_zhx_equals_h() {
        let x = &Gate::x().matrix;
        let h = &Gate::h().matrix;
        let z = &Gate::z().matrix;

        // XHZ = H
        let xhz = x.dot(h).dot(z);
        for (a, b) in xhz.iter().zip(h.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }

        // ZHX = H
        let zhx = z.dot(h).dot(x);
        for (a, b) in zhx.iter().zip(h.iter()) {
            assert!((*a - *b).norm() < 1e-12);
        }
    }

    #[test]
    fn test_standard_gates_dimensions() {
        assert_eq!(Gate::i().num_qubits, 1);
        assert_eq!(Gate::x().num_qubits, 1);
        assert_eq!(Gate::y().num_qubits, 1);
        assert_eq!(Gate::z().num_qubits, 1);
        assert_eq!(Gate::h().num_qubits, 1);
        assert_eq!(Gate::s().num_qubits, 1);
        assert_eq!(Gate::t_gate().num_qubits, 1);
        assert_eq!(Gate::cnot().num_qubits, 2);
        assert_eq!(Gate::swap().num_qubits, 2);
        assert_eq!(Gate::toffoli().num_qubits, 3);
    }
}