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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use ocl::enums::DeviceInfo::Type;
use ocl::{Buffer, MemFlags, ProQue};
use num_complex::Complex32;
use std::fmt;
use std::collections::HashMap;
use rand::{self, random};
use rand::distributions::{Normal, Sample};

use kernel::KERNEL;
use gates::Gate;
use gates::{h, r, s, t, x, y, z};

/// Representation of a quantum register

#[derive(Debug)]
pub struct State {
    /// The OpenCL Buffer for the state vector
    pub buffer: Buffer<Complex32>,
    pro_que: ProQue,
    /// Number of amplitudes stored in the state vector
    pub num_amps: usize,
    /// Number of qubits in the register
    pub num_qubits: u32,
    /// The OpenCL Backend used. Use the method `info()` to get the devices identifier
    pub backend: usize,

    /// The amount of decoherence
    #[cfg(feature = "decoherence")]
    pub decoherence: f32,
}

impl State {
    /// Create a new quantum register, with a given number of qubits.
    /// The backend is the OpenCL ID of the accelerator to use.
    ///
    /// The register will be initialized in the state |00...0>
    ///
    /// ```rust
    /// # extern crate qcgpu;
    /// let state = qcgpu::State::new(2,0);
    /// ```
    pub fn new(num_qubits: u32, backend: usize) -> State {
        let num_amps = 2_u32.pow(num_qubits) as usize;

        let ocl_pq = ProQue::builder()
            .src(KERNEL)
            .device(backend)
            .dims(num_amps)
            .build()
            .expect("Error Building ProQue");

        // create a temporary vector with the source buffer
        let source_buffer: Buffer<Complex32> = Buffer::builder()
            .queue(ocl_pq.queue().clone())
            .flags(MemFlags::new().read_write())
            .len(num_amps)
            .build()
            .expect("Source Buffer");

        let apply = ocl_pq
            .kernel_builder("initalize_register")
            .arg(&source_buffer)
            .arg(0)
            .build()
            .unwrap();

        unsafe {
            apply.enq().unwrap();
        }

        State {
            buffer: source_buffer,
            pro_que: ocl_pq,
            num_amps,
            num_qubits,
            backend,

            #[cfg(feature = "decoherence")]
            decoherence: 0.0,
        }
    }

    /// Create a new quantum register, starting in the
    /// State given. The backend is the OpenCL ID of the
    /// accelerator to use.
    ///
    /// ```rust
    /// # extern crate qcgpu;
    /// let state = qcgpu::State::from_bit_string("|00>", 1);
    /// ```
    pub fn from_bit_string(bit_string: &str, backend: usize) -> State {
        let bits = bit_string.to_string().replace("|", "").replace(">", "");
        let num_amps = 2 << (bits.len() - 1);

        let ocl_pq = ProQue::builder()
            .src(KERNEL)
            .device(backend)
            .dims(num_amps)
            .build()
            .expect("Error Building ProQue");

        let value = i32::from_str_radix(bits.as_str(), 2).unwrap();

        // create a temporary vector with the source buffer
        let source_buffer: Buffer<Complex32> = Buffer::builder()
            .queue(ocl_pq.queue().clone())
            .flags(MemFlags::new().read_write())
            .len(num_amps)
            .build()
            .expect("Source Buffer");

        let apply = ocl_pq
            .kernel_builder("initalize_register")
            .arg(&source_buffer)
            .arg(value)
            .build()
            .unwrap();

        unsafe {
            apply.enq().unwrap();
        }

        State {
            buffer: source_buffer,
            pro_que: ocl_pq,
            num_amps,
            num_qubits: bits.len() as u32,
            backend,

            #[cfg(feature = "decoherence")]
            decoherence: 0.0,
        }
    }

    /// Apply a gate to the target qubit
    pub fn apply_gate(&mut self, target: i32, gate: Gate) {
        // create a temporary vector with the source buffer
        let result_buffer: Buffer<Complex32> = self.pro_que.create_buffer().unwrap();

        let apply = self.pro_que
            .kernel_builder("apply_gate")
            .arg(&self.buffer)
            .arg(&result_buffer)
            .arg(target)
            .arg(gate.a)
            .arg(gate.b)
            .arg(gate.c)
            .arg(gate.d)
            .build()
            .unwrap();

        unsafe {
            apply.enq().unwrap();
        }

        self.buffer = result_buffer;
    }

    /// Apply a gate to every qubit in the register
    pub fn apply_all(&mut self, gate: Gate) {
        for i in 0..self.num_qubits as i32 {
            self.apply_gate(i, gate);
        }
    }

    /// Apply a gate to the register if the control qubit is 1.
    pub fn apply_controlled_gate(&mut self, control: i32, target: i32, gate: Gate) {
        let result_buffer: Buffer<Complex32> = self.pro_que.create_buffer().unwrap();

        let apply = self.pro_que
            .kernel_builder("apply_controlled_gate")
            .arg(&self.buffer)
            .arg(&result_buffer)
            .arg(control)
            .arg(target)
            .arg(gate.a)
            .arg(gate.b)
            .arg(gate.c)
            .arg(gate.d)
            .build()
            .unwrap();

        unsafe {
            apply.enq().unwrap();
        }

        self.buffer = result_buffer;

        #[cfg(feature = "decoherence")]
        self.decohere();
    }

    /// Return the probabilities of each outcome.
    ///
    /// The probabilitity of a state a|x> being measured
    /// is |a|^2.
    pub fn get_probabilities(&mut self) -> Vec<f32> {
        let result_buffer: Buffer<f32> = self.pro_que.create_buffer().unwrap();

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

        unsafe {
            apply.enq().unwrap();
        }

        let mut vec_result = vec![0.0f32; self.num_amps];
        result_buffer.read(&mut vec_result).enq().unwrap();

        vec_result
    }

    /// Return the state vector of the quantum register
    pub fn get_amplitudes(&mut self) -> Vec<Complex32> {
        let mut vec_result = vec![Complex32::new(0.0, 0.0); self.num_amps];
        self.buffer.read(&mut vec_result).enq().unwrap();

        vec_result
    }

    /// Measure the quantum register, returning the measured result
    pub fn measure(&mut self) -> i32 {
        let probabilities = self.get_probabilities();

        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;
        }

        i as i32
    }

    /// Preform multiple measurements, returning the results
    /// as a HashMap, with the key as the result and the value as the
    /// number of times that result was measured
    pub fn measure_many(&mut self, num_iterations: i32) -> HashMap<String, i32> {
        let probabilities = self.get_probabilities();
        let mut num_results = HashMap::new();

        for _ in 0..num_iterations {
            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;
            }
            let state = format!("{:0width$b}", i, width = self.num_qubits as usize);
            let count = num_results.entry(state).or_insert(0);
            *count += 1;
        }

        num_results
    }

    /// Add qubits to the register. The qubits are initialized to zero.
    /// This should be used as scratch space.
    pub fn add_scratch(&mut self, num_scratch: u32) {
        let num_amps = 2_u32.pow(self.num_qubits + num_scratch) as usize;
        let ocl_pq = ProQue::builder()
            .src(KERNEL)
            .device(self.backend)
            .dims(num_amps)
            .build()
            .expect("Error Building ProQue");

        let mut amps = self.get_amplitudes();
        amps.extend(vec![
            Complex32::new(0.0, 0.0);
            ocl_pq.dims().to_len() - self.pro_que.dims().to_len()
        ]);

        // create a temporary vector with the source buffer
        let source_buffer = Buffer::builder()
            .queue(ocl_pq.queue().clone())
            .flags(MemFlags::new().read_write().copy_host_ptr())
            .len(num_amps)
            .copy_host_slice(&amps)
            .build()
            .expect("Source Buffer");

        self.buffer = source_buffer;
        self.pro_que = ocl_pq;
        self.num_amps = num_amps;
        self.num_qubits += num_scratch;
    }

    /// This method allows you to simulate the effects of decoherence on
    /// the simulated quantum computer. The decoherence is simulated through phase dapening.
    /// The argument `d` will be used as the strength factor
    ///
    /// Note that setting the decoherence will make the simulator have to preform twice the
    /// number of calculations, as the `decohere` function is called after each state changing
    /// method.
    #[cfg(feature = "decoherence")]
    pub fn set_decoherence(&mut self, d: f32) {
        self.decoherence = d;
    }

    /// Preforms the actual decoherence of a quantum register based on the parameter `self.decoherence`
    ///
    /// This method does not have a custom OpenCL kernel, but that will be changed in later updates.
    #[cfg(feature = "decoherence")]
    pub fn decohere(&mut self) {
        if self.decoherence != 0.0 {
            let mut normal = Normal::new(0.0, self.decoherence as f64);

            for i in 0..1 {
                let angle = normal.sample(&mut rand::thread_rng());

                // Apply a phase shift according to the normally distributed angle
                let gate = r(angle as f32);
                self.apply_gate(i, gate);
            }
        }
    }

    /// Measure the scratch qubits. The measurement is discarded, and
    /// the register size is reduced by `num_to_measure` qubits.
    pub fn measure_scratch(&mut self, num_to_measure: u32) {
        let num_amps = 2_u32.pow(self.num_qubits - num_to_measure) as usize;
        let ocl_pq = ProQue::builder()
            .src(KERNEL)
            .device(self.backend)
            .dims(num_amps)
            .build()
            .expect("Error Building ProQue");

        let mut amps = self.get_amplitudes();
        amps.truncate(num_amps);

        // create a temporary vector with the source buffer
        let source_buffer = Buffer::builder()
            .queue(ocl_pq.queue().clone())
            .flags(MemFlags::new().read_write().copy_host_ptr())
            .len(num_amps)
            .copy_host_slice(&amps)
            .build()
            .expect("Source Buffer");

        self.buffer = source_buffer;
        self.pro_que = ocl_pq;
        self.num_amps = num_amps;
        self.num_qubits -= num_to_measure;
    }

    /// Preform multiple measurements of the first `num_to_measure`, returning the results
    /// as a HashMap, with the key as the result and the value as the
    /// number of times that result was measured
    pub fn measure_first(
        &mut self,
        num_to_measure: i32,
        num_iterations: i32,
    ) -> HashMap<String, i32> {
        let probabilities = self.get_probabilities();
        let mut num_results = HashMap::new();

        for _ in 0..num_iterations {
            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;
            }
            let state = format!("{:0width$b}", i, width = self.num_qubits as usize);
            let num_chars = state.len();
            let result = state
                .chars()
                .skip(num_chars - num_to_measure as usize)
                .take(num_to_measure as usize)
                .collect();
            let count = num_results.entry(result).or_insert(0);
            *count += 1;
        }

        num_results
    }

    /// Print Information About The Device
    pub fn info(&self) {
        println!(
            "Device type: {:?}",
            self.pro_que.device().info(Type).unwrap()
        )
    }

    /* Ease Of Access / shorthand Functions*/

    /// Hadamard Gate
    /// Shorthand Method
    ///
    /// Equivilent to `state.apply_gate(target, h());`
    pub fn h(&mut self, target: i32) {
        self.apply_gate(target, h());
        #[cfg(feature = "decoherence")]
        self.decohere();
    }

    /// S Gate
    /// Shorthand Method
    ///
    /// Equivilent to `state.apply_gate(target, s());`
    pub fn s(&mut self, target: i32) {
        self.apply_gate(target, s());
        #[cfg(feature = "decoherence")]
        self.decohere();
    }

    /// T Gate
    /// Shorthand Method
    ///
    /// Equivilent to `state.apply_gate(target, t());`
    pub fn t(&mut self, target: i32) {
        self.apply_gate(target, t());
        #[cfg(feature = "decoherence")]
        self.decohere();
    }

    /// Pauli X Gate
    /// Shorthand Method
    ///
    /// Equivilent to `state.apply_gate(target, x());`
    pub fn x(&mut self, target: i32) {
        self.apply_gate(target, x());
        #[cfg(feature = "decoherence")]
        self.decohere();
    }

    /// Pauli Y Gate
    /// Shorthand Method
    ///
    /// Equivilent to `state.apply_gate(target, y());`
    pub fn y(&mut self, target: i32) {
        self.apply_gate(target, y());
        #[cfg(feature = "decoherence")]
        self.decohere();
    }

    /// Pauli Z Gate
    /// Shorthand Method
    ///
    /// Equivilent to `state.apply_gate(target, z());`
    pub fn z(&mut self, target: i32) {
        self.apply_gate(target, z());
        #[cfg(feature = "decoherence")]
        self.decohere();
    }

    /// Controlled Not Gate
    /// Shorthand method
    ///
    /// Equivilent to `state.apply_controlled_gate(control, target, x());`
    pub fn cx(&mut self, control: i32, target: i32) {
        self.apply_controlled_gate(control, target, x());
    }

    /// Toffoli (Controlled-Controlled-NOT gate)
    /// Shorthand method
    pub fn toffoli(&mut self, control1: i32, control2: i32, target: i32) {
        let result_buffer: Buffer<Complex32> = self.pro_que.create_buffer().unwrap();

        let gate = x();

        let apply = self.pro_que
            .kernel_builder("apply_controlled_controlled_gate")
            .arg(&self.buffer)
            .arg(&result_buffer)
            .arg(control1)
            .arg(control2)
            .arg(target)
            .arg(gate.a)
            .arg(gate.b)
            .arg(gate.c)
            .arg(gate.d)
            .build()
            .unwrap();

        unsafe {
            apply.enq().unwrap();
        }

        self.buffer = result_buffer;

        #[cfg(feature = "decoherence")]
        self.decohere();
    }

    /// Swap two qubits in the register
    pub fn swap(&mut self, first_qubit: i32, second_qubit: i32) {
        let result_buffer: Buffer<Complex32> = self.pro_que.create_buffer().unwrap();

        let apply = self.pro_que
            .kernel_builder("swap")
            .arg(&self.buffer)
            .arg(&result_buffer)
            .arg(first_qubit)
            .arg(second_qubit)
            .build()
            .unwrap();

        unsafe {
            apply.enq().unwrap();
        }

        self.buffer = result_buffer;
    }

    /// Caclulates f(a) = x^a mod n.
    pub fn pow_mod(&mut self, x: i32, n: i32, input_width: i32, output_width: i32) {
        let result_buffer: Buffer<Complex32> = self.pro_que.create_buffer().unwrap();

        let apply = self.pro_que
            .kernel_builder("apply_pow_mod")
            .arg(&self.buffer)
            .arg(&result_buffer)
            .arg(x)
            .arg(n)
            .arg(input_width)
            .arg(output_width)
            .build()
            .unwrap();

        unsafe {
            apply.enq().unwrap();
        }

        self.buffer = result_buffer;

        #[cfg(feature = "decoherence")]
        self.decohere();
    }
}

impl fmt::Display for State {
    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.num_amps];
        self.buffer.read(&mut vec_result).enq().unwrap();

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

        Ok(())
    }
}