opencl.rs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. extern crate qcgpu;
  2. use qcgpu::Simulator;
  3. use qcgpu::gate::x;
  4. fn create_simulator(n: u8) -> Simulator {
  5. let sim = Simulator::new_opencl(n);
  6. assert!(
  7. sim.is_ok(),
  8. "Error initializing OpenCL simulator"
  9. );
  10. return sim.unwrap();
  11. }
  12. #[test]
  13. fn can_initialize_simulator() {
  14. for n in 1..25 {
  15. create_simulator(n);
  16. }
  17. }
  18. #[test]
  19. fn can_apply_x_gate() {
  20. for n in 1..25 {
  21. let mut sim = create_simulator(n);
  22. for i in 0..n {
  23. assert!(
  24. sim.x(i).is_ok(),
  25. "Error applying pauli-x (not) gate to simulator"
  26. );
  27. }
  28. }
  29. }
  30. #[test]
  31. fn can_apply_y_gate() {
  32. for n in 1..25 {
  33. let mut sim = create_simulator(n);
  34. for i in 0..n {
  35. assert!(
  36. sim.y(i).is_ok(),
  37. "Error applying pauli-y gate to simulator"
  38. );
  39. }
  40. }
  41. }
  42. #[test]
  43. fn can_apply_z_gate() {
  44. for n in 1..25 {
  45. let mut sim = create_simulator(n);
  46. for i in 0..n {
  47. assert!(
  48. sim.z(i).is_ok(),
  49. "Error applying pauli-z gate to simulator"
  50. );
  51. }
  52. }
  53. }
  54. #[test]
  55. fn can_apply_h_gate() {
  56. for n in 1..25 {
  57. let mut sim = create_simulator(n);
  58. for i in 0..n {
  59. assert!(
  60. sim.h(i).is_ok(),
  61. "Error applying hadamard gate to simulator"
  62. );
  63. }
  64. }
  65. }
  66. #[test]
  67. fn can_apply_cx_gate() {
  68. for n in 2..25 {
  69. let mut sim = create_simulator(n);
  70. for i in 1..n {
  71. assert!(
  72. sim.cx(0, i).is_ok(),
  73. "Error applying controlled not gate to simulator"
  74. );
  75. }
  76. }
  77. }
  78. #[test]
  79. fn can_perform_measurement() {
  80. for n in 1..25 {
  81. let mut sim = create_simulator(n);
  82. sim.apply_all(x()).unwrap();
  83. assert_eq!(sim.measure().unwrap() as i32, 2i32.pow(n as u32) - 1);
  84. }
  85. }