quantum/
computer.rs

1//! Main consumer module allowing easy control of whole quantum computer.
2
3use gate::Gate;
4use registers::ClassicalRegister;
5use registers::QuantumRegister;
6
7#[derive(Debug, Eq, PartialEq)]
8enum State {
9    /// The computer has been set up, but the qubits could be anything.
10    Initializing,
11
12    /// The comuter is running, with qubits in arbitrary superposition.
13    Running,
14
15    /// The system is collapsed/decomposed into a classical state.
16    Collapsed,
17}
18
19/// Represents a quantum computer of one register.
20///
21/// This is essentially a wrapping around a quantum register
22/// with convenience methods to run algorithms, log and read
23/// results.
24#[derive(Debug)]
25pub struct QuantumComputer {
26    state: State,
27    width: usize,
28
29    /// Only makes sense if `State::Running == state`
30    register: QuantumRegister,
31
32    /// Only makes sense if `State::Collapsed == state`
33    classical: ClassicalRegister,
34}
35
36impl QuantumComputer {
37    /// Construct a new quantum computer with register of given `width`.
38    pub fn new(width: usize) -> QuantumComputer {
39        QuantumComputer {
40            state: State::Initializing,
41            width: width,
42            register: QuantumRegister::new(width, &ClassicalRegister::zeroed(width)),
43            classical: ClassicalRegister::zeroed(width),
44        }
45    }
46
47    /// Initialize the quantum register qubits to a certian classical integer state.
48    ///
49    /// # Panics
50    ///
51    /// We panic if the state is anything other than `State::Initializing`.
52    pub fn initialize(&mut self, value: u32) {
53        assert_eq!(State::Initializing, self.state);
54
55        let classical = ClassicalRegister::from_int(self.width, value);
56        self.register = QuantumRegister::new(self.width, &classical);
57
58        self.state = State::Running;
59    }
60
61    /// Apply a quantum gate to the quantum register qubits.
62    ///
63    /// # Panics
64    ///
65    /// We panic if the state is anything other than `State::Running`.
66    pub fn apply(&mut self, gate: Gate) {
67        assert_eq!(State::Running, self.state);
68
69        self.register.apply(gate);
70    }
71
72    /// Collapse the quantum register to a classical state.
73    ///
74    /// # Panics
75    ///
76    /// We panic if the state is anything other than `State::Running`.
77    pub fn collapse(&mut self) {
78        assert_eq!(State::Running, self.state);
79
80        self.classical = self.register.collapse();
81
82        self.state = State::Collapsed;
83    }
84
85    /// Reset the quantum register, ready to be initialized again.
86    ///
87    /// # Panics
88    ///
89    /// We panic if the state is anything other than `State::Collapsed`.
90    pub fn reset(&mut self) {
91        self.state = State::Initializing;
92    }
93
94    /// Read the collapsed register qubits as an integer.
95    ///
96    /// # Panics
97    ///
98    /// We panic if the state is anything other than `State::Collapsed`.
99    pub fn value(&self) -> u32 {
100        assert_eq!(State::Collapsed, self.state);
101
102        self.classical.to_int()
103    }
104}
105
106#[test]
107fn state_test() {
108    let mut c = QuantumComputer::new(3);
109    assert_eq!(State::Initializing, c.state);
110
111    c.initialize(5);
112    assert_eq!(State::Running, c.state);
113
114    c.collapse();
115    assert_eq!(State::Collapsed, c.state);
116
117    c.value();
118
119    c.reset();
120    assert_eq!(State::Initializing, c.state);
121}
122
123#[test]
124fn compute_test() {
125    use gates;
126
127    let mut c = QuantumComputer::new(3);
128
129    c.initialize(5);
130
131    c.apply(gates::identity(3));
132
133    c.collapse();
134
135    assert_eq!(5, c.value());
136}