瀏覽代碼

Add documentation and remove old functions

Adam Kelly 6 年之前
父節點
當前提交
d722a9ba38
共有 2 個文件被更改,包括 27 次插入53 次删除
  1. 7 53
      src/backends/opencl/kernel.cl
  2. 20 0
      src/backends/opencl/mod.rs

+ 7 - 53
src/backends/opencl/kernel.cl

@@ -58,6 +58,11 @@ static uint nth_cleared(uint n, uint target)
 
     return (n & mask) | ((n & not_mask) << 1);
 }
+
+///////////////////////////////////////////////
+// KERNELS
+///////////////////////////////////////////////
+
 /*
  * Applies a single qubit gate to the register.
  * The gate matrix must be given in the form:
@@ -124,6 +129,7 @@ __kernel void apply_controlled_gate(
 
 /*
  * Applies a controlled-controlled single qubit gate to the register.
+ * NOT MIGRATED
  */
 __kernel void apply_controlled_controlled_gate(
     __global complex_f *const amplitudes,
@@ -168,6 +174,7 @@ __kernel void apply_controlled_controlled_gate(
 
 /*
  * Swaps the states of two qubits in the register
+ * NOT MIGRATED
  */
 __kernel void swap(
     __global complex_f *const amplitudes,
@@ -188,59 +195,6 @@ __kernel void swap(
     amps[new_state] = amplitudes[state];
 }
 
-static uint pow_mod(uint x, uint y, uint n)
-{
-    uint r = 1;
-    while (y > 0)
-    {
-        if ((y & 1) == 1)
-        {
-            r = r * x % n;
-        }
-        y /= 2;
-        x = x * x % n;
-    }
-
-    return r;
-}
-
-/*
- *  Calculates f(a) = x^a mod N
- */
-__kernel void apply_pow_mod(
-    __global complex_f *const amplitudes,
-    __global complex_f *amps,
-    uint x,
-    uint n,
-    uint input_width,
-    uint output_width)
-{
-    uint input_bit_range_from = output_width;
-    uint input_bit_range_to = output_width + input_width - 1;
-    uint target_bit_range_from = 0;
-    uint target_bit_range_to = output_width - 1;
-
-    uint high_bit_mask = (1 << (input_bit_range_to + 1)) - 1;
-    uint target_bit_mask = ((1 << (1 + target_bit_range_to - target_bit_range_from)) - 1) << target_bit_range_from;
-
-    uint const state = get_global_id(0);
-
-    uint input = (state & high_bit_mask) >> input_bit_range_from;
-    uint result = (pow_mod(x, input, n) << target_bit_range_from) & target_bit_mask;
-    uint result_state = state ^ result;
-
-    if (result_state == state)
-    {
-        amps[state] = amplitudes[state];
-    }
-    else
-    {
-        amps[state] = amplitudes[result_state];
-        amps[result_state] = amplitudes[state];
-    }
-
-    amps[result_state] = amplitudes[state];
-}
 
 /**
  * Calculates The Probabilities Of A State Vector

+ 20 - 0
src/backends/opencl/mod.rs

@@ -19,6 +19,10 @@ pub struct OpenCL {
 }
 
 impl OpenCL {
+    /// Initialize a new OpenCL Backend
+    ///
+    /// Takes an argument of the number of qubits to use
+    /// in the register, and returns a result with the backend.
     pub fn new(num_qubits: u8) -> Result<OpenCL, Error> {
         // How many amplitudes needed?
         let num_amps = 2_usize.pow(u32::from(num_qubits)) as usize;
@@ -52,6 +56,12 @@ impl OpenCL {
         })
     }
 
+    /// Note that this method doesn't mutate the state, thus
+    /// a new vector must be created, which means you will have to have
+    /// enough memory to store another object half the size of the
+    /// state vector
+    ///
+    /// **This methods is very likely to change!**
     fn get_probabilities(&self) -> Result<Vec<f32>, Error> {
         let result_buffer: Buffer<f32> = self.pro_que.create_buffer()?;
 
@@ -112,9 +122,14 @@ impl Backend for OpenCL {
         Ok(())
     }
 
+    /// Measure the whole register, leaving the register in
+    /// the measured state
     fn measure(&mut self) -> Result<u8, Error> {
         let probabilities = self.get_probabilities()?;
 
+        // A key must be generated on the host, as most
+        // external accelerators do not have in built support
+        // for random number generation
         let mut key = random::<f32>();
         if key > 1.0 {
             key %= 1.0;
@@ -132,10 +147,15 @@ impl Backend for OpenCL {
         Ok(i as u8)
     }
 
+
+    /// Measure the value of a single qubit, leaving the register in
+    /// the state where only that qubit (or any entangled qubits) have
+    /// been collapsed
     fn measure_qubit(&mut self, target: u8) -> Result<u8, Error> {
         unimplemented!()
     }
 
+    /// Get the number of qubits that this backend was initialized for
     fn num_qubits(&self) -> u8 {
         self.num_qubits
     }