Jelajahi Sumber

Fix measurment bug

can't represent any value using a u8, so using a u64 instead
Adam Kelly 5 tahun lalu
induk
melakukan
e00040a1a4
4 mengubah file dengan 9 tambahan dan 7 penghapusan
  1. 1 1
      examples/super-dense.rs
  2. 5 3
      src/backends/opencl/mod.rs
  3. 1 1
      src/lib.rs
  4. 2 2
      src/traits.rs

+ 1 - 1
examples/super-dense.rs

@@ -9,7 +9,7 @@ extern crate qcgpu;
 use failure::Error;
 use qcgpu::Simulator;
 
-fn superdense(input: &str) -> Result<u8, Error> {
+fn superdense(input: &str) -> Result<u64, Error> {
     let mut state = Simulator::new_opencl(2)?;
     let input_str = String::from(input);
 

+ 5 - 3
src/backends/opencl/mod.rs

@@ -127,7 +127,9 @@ impl Backend for OpenCL {
 
     /// Measure the whole register, leaving the register in
     /// the measured state
-    fn measure(&mut self) -> Result<u8, Error> {
+    /// 
+    /// Note: Currently this leaves the register in the unmeasured state.
+    fn measure(&mut self) -> Result<u64, Error> {
         let probabilities = self.get_probabilities()?;
 
         // A key must be generated on the host, as most
@@ -147,13 +149,13 @@ impl Backend for OpenCL {
             i += 1;
         }
 
-        Ok(i as u8)
+        Ok(i as u64)
     }
 
     /// 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> {
+    fn measure_qubit(&mut self, _target: u8) -> Result<u64, Error> {
         unimplemented!()
     }
 

+ 1 - 1
src/lib.rs

@@ -60,7 +60,7 @@ impl Simulator {
     pub fn cx(&mut self, control: u8, target: u8) -> Result<(), Error> {
         self.backend.apply_controlled_gate(x(), control, target)
     }
-    pub fn measure(&mut self) -> Result<u8, Error> {
+    pub fn measure(&mut self) -> Result<u64, Error> {
         self.backend.measure()
     }
     pub fn num_qubits(&mut self) -> u8 {

+ 2 - 2
src/traits.rs

@@ -6,9 +6,9 @@ pub trait Backend: Debug + Display {
     fn num_qubits(&self) -> u8;
     fn apply_gate(&mut self, gate: Gate, target: u8) -> Result<(), Error>;
     fn apply_controlled_gate(&mut self, gate: Gate, control: u8, target: u8) -> Result<(), Error>;
-    fn measure_qubit(&mut self, target: u8) -> Result<u8, Error>;
+    fn measure_qubit(&mut self, target: u8) -> Result<u64, Error>;
 
-    fn measure(&mut self) -> Result<u8, Error> {
+    fn measure(&mut self) -> Result<u64, Error> {
         let mut result = 0;
         for i in 0..self.num_qubits() {
             let bit_mask = 1 << i;