1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use gate::Gate;
use traits::Backend;

use failure::Error;
use num_complex::{Complex, Complex32};
use ocl::{Buffer, MemFlags, ProQue};
use rand::random;
use std::fmt;

// OpenCL Kernel
pub static KERNEL: &'static str = include_str!("kernel.cl");

#[derive(Debug)]
pub struct OpenCL {
    /// OpenCL Buffer for the state vector
    pub buffer: Buffer<Complex<f32>>,
    pro_que: ProQue,
    num_qubits: u8,
}

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;

        let ocl_pq = ProQue::builder()
            .src(KERNEL)
            .device(1)
            .dims(num_amps)
            .build()?;

        let buffer: Buffer<Complex32> = Buffer::builder()
            .queue(ocl_pq.queue().clone())
            .flags(MemFlags::new().read_write())
            .len(num_amps)
            .build()?;

        let apply = ocl_pq
            .kernel_builder("initialize_register")
            .arg(&buffer)
            .arg(0)
            .build()?;

        unsafe {
            apply.enq()?;
        }

        Ok(OpenCL {
            pro_que: ocl_pq,
            buffer,
            num_qubits,
        })
    }

    /// 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()?;

        let apply = self
            .pro_que
            .kernel_builder("calculate_probabilities")
            .arg(&self.buffer)
            .arg(&result_buffer)
            .build()?;

        unsafe {
            apply.enq()?;
        }

        let mut vec_result = vec![0.0f32; self.buffer.len()];
        result_buffer.read(&mut vec_result).enq()?;

        Ok(vec_result)
    }
}

impl Backend for OpenCL {
    fn apply_gate(&mut self, gate: Gate, target: u8) -> Result<(), Error> {
        let apply = self
            .pro_que
            .kernel_builder("apply_gate")
            .global_work_size(&self.buffer.len() / 2)
            .arg(&self.buffer)
            .arg(i32::from(target))
            .arg(gate.a)
            .arg(gate.b)
            .arg(gate.c)
            .arg(gate.d)
            .build()?;

        unsafe {
            apply.enq()?;
        }

        Ok(())
    }

    fn apply_controlled_gate(&mut self, gate: Gate, control: u8, target: u8) -> Result<(), Error> {
        let apply = self
            .pro_que
            .kernel_builder("apply_controlled_gate")
            .global_work_size(&self.buffer.len() / 2)
            .arg(&self.buffer)
            .arg(i32::from(control))
            .arg(i32::from(target))
            .arg(gate.a)
            .arg(gate.b)
            .arg(gate.c)
            .arg(gate.d)
            .build()?;

        unsafe {
            apply.enq()?;
        }

        Ok(())
    }

    /// Measure the whole register, leaving the register in
    /// the measured state
    /// 
    /// 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
        // external accelerators do not have in built support
        // for random number generation
        let mut key = random::<f32>();
        if key > 1.0 {
            key %= 1.0;
        }

        let mut i = 0;
        while i < probabilities.len() {
            key -= probabilities[i];
            if key <= 0.0 {
                break;
            }
            i += 1;
        }

        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<u64, Error> {
        unimplemented!()
    }

    /// Get the number of qubits that this backend was initialized for
    fn num_qubits(&self) -> u8 {
        self.num_qubits
    }
}

impl fmt::Display for OpenCL {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut first = true;

        let mut vec_result = vec![Complex32::new(0.0, 0.0); self.buffer.len()];
        self.buffer
            .read(&mut vec_result)
            .enq()
            .expect("Error Reading Memory From Device");

        for (idx, item) in vec_result.iter().enumerate() {
            if !first {
                write!(f, ", ")?;
            } else {
                first = false;
            }

            write!(f, "[{}]: ", idx)?;

            // Do we print the imaginary part?
            if item.im == 0.0 {
                write!(f, "{}", item.re)?;
            } else if item.re == 0.0 {
                write!(f, "{}i", item.im)?;
            } else {
                write!(f, "{}", item)?;
            }
        }

        Ok(())
    }
}