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
extern crate failure;
extern crate num_complex;
extern crate ocl;
extern crate rand;

#[macro_use]
extern crate failure_derive;

pub mod backends;
pub mod error;
pub mod gate;
pub mod traits;

use backends::OpenCL;
pub use gate::{h, x, y, z, Gate};

use failure::Error;
use std::fmt;

#[derive(Debug)]
pub struct Simulator {
    backend: Box<traits::Backend>,
    num_qubits: u8,
}

impl Simulator {
    pub fn new_opencl(num_qubits: u8) -> Result<Simulator, Error> {
        let backend = OpenCL::new(num_qubits)?;

        Ok(Simulator {
            backend: Box::new(backend),
            num_qubits,
        })
    }

    pub fn apply_gate(&mut self, gate: Gate, target: u8) -> Result<(), Error> {
        self.backend.apply_gate(gate, target)
    }

    pub fn apply_all(&mut self, gate: Gate) -> Result<(), Error> {
        for i in 0..self.num_qubits {
            self.backend.apply_gate(gate, i)?
        }

        Ok(())
    }

    pub fn x(&mut self, target: u8) -> Result<(), Error> {
        self.backend.apply_gate(x(), target)
    }
    pub fn y(&mut self, target: u8) -> Result<(), Error> {
        self.backend.apply_gate(y(), target)
    }
    pub fn z(&mut self, target: u8) -> Result<(), Error> {
        self.backend.apply_gate(z(), target)
    }
    pub fn h(&mut self, target: u8) -> Result<(), Error> {
        self.backend.apply_gate(h(), target)
    }
    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<u64, Error> {
        self.backend.measure()
    }
    pub fn num_qubits(&mut self) -> u8 {
        self.backend.num_qubits()
    }
}

impl fmt::Display for Simulator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.backend)
    }
}