Browse Source

Formatting and bug fixes

Adam Kelly 6 years ago
parent
commit
b918ce7db0
5 changed files with 95 additions and 106 deletions
  1. 11 8
      src/backends/opencl/mod.rs
  2. 0 90
      src/gate.rs
  3. 2 2
      src/lib.rs
  4. 1 1
      src/traits.rs
  5. 81 5
      tests/opencl.rs

+ 11 - 8
src/backends/opencl/mod.rs

@@ -1,11 +1,11 @@
-use traits::Backend;
 use gate::Gate;
+use traits::Backend;
 
-use ocl::{Buffer, MemFlags, ProQue};
+use failure::Error;
 use num_complex::{Complex, Complex32};
+use ocl::{Buffer, MemFlags, ProQue};
 use rand::random;
 use std::fmt;
-use failure::Error;
 
 // OpenCL Kernel
 pub static KERNEL: &'static str = include_str!("kernel.cl");
@@ -65,7 +65,8 @@ impl OpenCL {
     fn get_probabilities(&self) -> Result<Vec<f32>, Error> {
         let result_buffer: Buffer<f32> = self.pro_que.create_buffer()?;
 
-        let apply = self.pro_que
+        let apply = self
+            .pro_que
             .kernel_builder("calculate_probabilities")
             .arg(&self.buffer)
             .arg(&result_buffer)
@@ -84,7 +85,8 @@ impl OpenCL {
 
 impl Backend for OpenCL {
     fn apply_gate(&mut self, gate: Gate, target: u8) -> Result<(), Error> {
-        let apply = self.pro_que
+        let apply = self
+            .pro_que
             .kernel_builder("apply_gate")
             .global_work_size(&self.buffer.len() / 2)
             .arg(&self.buffer)
@@ -103,12 +105,13 @@ impl Backend for OpenCL {
     }
 
     fn apply_controlled_gate(&mut self, gate: Gate, control: u8, target: u8) -> Result<(), Error> {
-        let apply = self.pro_que
+        let apply = self
+            .pro_que
             .kernel_builder("apply_controlled_gate")
             .global_work_size(&self.buffer.len() / 2)
             .arg(&self.buffer)
-            .arg(control)
-            .arg(target)
+            .arg(i32::from(control))
+            .arg(i32::from(target))
             .arg(gate.a)
             .arg(gate.b)
             .arg(gate.c)

+ 0 - 90
src/gate.rs

@@ -1,90 +0,0 @@
-use num_complex::Complex32;
-use std::fmt;
-use std::f32::consts::FRAC_1_SQRT_2;
-
-/// Representation of a gate
-///
-/// ```
-///# extern crate qcgpu;
-///# extern crate num_complex;
-///# use qcgpu::Gate;
-///# use num_complex::Complex32;
-/// Gate {
-///    a: Complex32::new(0.0, 0.0), b: Complex32::new(1.0, 0.0),
-///    c: Complex32::new(1.0, 0.0), d: Complex32::new(0.0, 0.0)
-/// };
-///
-///
-#[derive(Debug, Clone, Copy)]
-pub struct Gate {
-    pub a: Complex32,
-    pub b: Complex32,
-    pub c: Complex32,
-    pub d: Complex32,
-}
-
-impl fmt::Display for Gate {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "[[{}, {}], [{}, {}]]", self.a, self.b, self.c, self.d)
-    }
-}
-
-/// Hadamard Gate
-///
-/// [0.70710678118, 0.70710678118]
-///
-/// [0.70710678118, -0.70710678118]
-#[inline]
-pub fn h() -> Gate {
-    Gate {
-        a: Complex32::new(FRAC_1_SQRT_2, 0.0),
-        b: Complex32::new(FRAC_1_SQRT_2, 0.0),
-        c: Complex32::new(FRAC_1_SQRT_2, 0.0),
-        d: Complex32::new(-FRAC_1_SQRT_2, 0.0),
-    }
-}
-
-/// Pauli X / NOT Gate
-///
-/// [0, 1]
-///
-/// [1, 0]
-#[inline]
-pub fn x() -> Gate {
-    Gate {
-        a: Complex32::new(0.0, 0.0),
-        b: Complex32::new(1.0, 0.0),
-        c: Complex32::new(1.0, 0.0),
-        d: Complex32::new(0.0, 0.0),
-    }
-}
-
-/// Pauli Y Gate
-///
-/// [0, -i]
-///
-/// [i, 0]
-#[inline]
-pub fn y() -> Gate {
-    Gate {
-        a: Complex32::new(0.0, 0.0),
-        b: Complex32::new(0.0, -1.0),
-        c: Complex32::new(0.0, 1.0),
-        d: Complex32::new(0.0, 0.0),
-    }
-}
-
-/// Pauli Z Gate
-///
-/// [1, 0]
-///
-/// [0, -1]
-#[inline]
-pub fn z() -> Gate {
-    Gate {
-        a: Complex32::new(1.0, 0.0),
-        b: Complex32::new(0.0, 0.0),
-        c: Complex32::new(0.0, 0.0),
-        d: Complex32::new(-1.0, 0.0),
-    }
-}

+ 2 - 2
src/lib.rs

@@ -6,16 +6,16 @@ extern crate rand;
 #[macro_use]
 extern crate failure_derive;
 
+pub mod backends;
 pub mod error;
 pub mod gate;
 pub mod traits;
-pub mod backends;
 
 use backends::OpenCL;
 pub use gate::{h, x, y, z, Gate};
 
-use std::fmt;
 use failure::Error;
+use std::fmt;
 
 #[derive(Debug)]
 pub struct Simulator {

+ 1 - 1
src/traits.rs

@@ -1,6 +1,6 @@
+use failure::Error;
 use gate::Gate;
 use std::fmt::{Debug, Display};
-use failure::Error;
 
 pub trait Backend: Debug + Display {
     fn num_qubits(&self) -> u8;

+ 81 - 5
tests/opencl.rs

@@ -2,14 +2,90 @@ extern crate qcgpu;
 
 use qcgpu::Simulator;
 
-#[test]
-fn can_initialize_simulator() {
-    for n in 1..25 {
-        let sim = Simulator::new_opencl(n);
+fn create_simulator(n: u8) -> Simulator {
+    let sim = Simulator::new_opencl(n);
 
-        assert!(
+    assert!(
             sim.is_ok(),
             "Error initializing OpenCL simulator"
         );
+
+    return sim.unwrap();
+}
+
+#[test]
+fn can_initialize_simulator() {
+    for n in 1..25 {
+        create_simulator(n);
+    }
+}
+
+#[test]
+fn can_apply_x_gate() {
+    for n in 1..25 {
+        let mut sim = create_simulator(n);
+
+        for i in 0..n {
+            assert!(
+                sim.x(i).is_ok(),
+                "Error applying pauli-x (not) gate to simulator"
+            );
+        }
+    }
+}
+
+#[test]
+fn can_apply_y_gate() {
+    for n in 1..25 {
+        let mut sim = create_simulator(n);
+
+        for i in 0..n {
+            assert!(
+                sim.y(i).is_ok(),
+                "Error applying pauli-y gate to simulator"
+            );
+        }
+    }
+}
+
+#[test]
+fn can_apply_z_gate() {
+    for n in 1..25 {
+        let mut sim = create_simulator(n);
+
+        for i in 0..n {
+            assert!(
+                sim.z(i).is_ok(),
+                "Error applying pauli-z gate to simulator"
+            );
+        }
+    }
+}
+
+#[test]
+fn can_apply_h_gate() {
+    for n in 1..25 {
+        let mut sim = create_simulator(n);
+
+        for i in 0..n {
+            assert!(
+                sim.h(i).is_ok(),
+                "Error applying hadamard gate to simulator"
+            );
+        }
+    }
+}
+
+#[test]
+fn can_apply_cx_gate() {
+    for n in 2..25 {
+        let mut sim = create_simulator(n);
+
+        for i in 1..n {
+            assert!(
+                sim.cx(0, i).is_ok(),
+                "Error applying controlled not gate to simulator"
+            );
+        }
     }
 }