gate.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. use num_complex::Complex32;
  2. use std::f32::consts::FRAC_1_SQRT_2;
  3. use std::fmt;
  4. /// Representation of a gate
  5. ///
  6. /// ```
  7. ///# extern crate qcgpu;
  8. ///# extern crate num_complex;
  9. ///# use qcgpu::Gate;
  10. ///# use num_complex::Complex32;
  11. /// Gate {
  12. /// a: Complex32::new(0.0, 0.0), b: Complex32::new(1.0, 0.0),
  13. /// c: Complex32::new(1.0, 0.0), d: Complex32::new(0.0, 0.0)
  14. /// };
  15. ///
  16. ///
  17. #[derive(Debug, Clone, Copy)]
  18. pub struct Gate {
  19. pub a: Complex32,
  20. pub b: Complex32,
  21. pub c: Complex32,
  22. pub d: Complex32,
  23. }
  24. impl fmt::Display for Gate {
  25. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  26. write!(f, "[[{}, {}], [{}, {}]]", self.a, self.b, self.c, self.d)
  27. }
  28. }
  29. /// Hadamard Gate
  30. ///
  31. /// [0.70710678118, 0.70710678118]
  32. ///
  33. /// [0.70710678118, -0.70710678118]
  34. #[inline]
  35. pub fn h() -> Gate {
  36. Gate {
  37. a: Complex32::new(FRAC_1_SQRT_2, 0.0),
  38. b: Complex32::new(FRAC_1_SQRT_2, 0.0),
  39. c: Complex32::new(FRAC_1_SQRT_2, 0.0),
  40. d: Complex32::new(-FRAC_1_SQRT_2, 0.0),
  41. }
  42. }
  43. /// Pauli X / NOT Gate
  44. ///
  45. /// [0, 1]
  46. ///
  47. /// [1, 0]
  48. #[inline]
  49. pub fn x() -> Gate {
  50. Gate {
  51. a: Complex32::new(0.0, 0.0),
  52. b: Complex32::new(1.0, 0.0),
  53. c: Complex32::new(1.0, 0.0),
  54. d: Complex32::new(0.0, 0.0),
  55. }
  56. }
  57. /// Pauli Y Gate
  58. ///
  59. /// [0, -i]
  60. ///
  61. /// [i, 0]
  62. #[inline]
  63. pub fn y() -> Gate {
  64. Gate {
  65. a: Complex32::new(0.0, 0.0),
  66. b: Complex32::new(0.0, -1.0),
  67. c: Complex32::new(0.0, 1.0),
  68. d: Complex32::new(0.0, 0.0),
  69. }
  70. }
  71. /// Pauli Z Gate
  72. ///
  73. /// [1, 0]
  74. ///
  75. /// [0, -1]
  76. #[inline]
  77. pub fn z() -> Gate {
  78. Gate {
  79. a: Complex32::new(1.0, 0.0),
  80. b: Complex32::new(0.0, 0.0),
  81. c: Complex32::new(0.0, 0.0),
  82. d: Complex32::new(-1.0, 0.0),
  83. }
  84. }