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
//! Main consumer module allowing easy control of whole quantum computer.

use gate::Gate;
use registers::ClassicalRegister;
use registers::QuantumRegister;

#[derive(Debug, Eq, PartialEq)]
enum State {
    /// The computer has been set up, but the qubits could be anything.
    Initializing,

    /// The comuter is running, with qubits in arbitrary superposition.
    Running,

    /// The system is collapsed/decomposed into a classical state.
    Collapsed,
}

/// Represents a quantum computer of one register.
///
/// This is essentially a wrapping around a quantum register
/// with convenience methods to run algorithms, log and read
/// results.
#[derive(Debug)]
pub struct QuantumComputer {
    state: State,
    width: usize,

    /// Only makes sense if `State::Running == state`
    register: QuantumRegister,

    /// Only makes sense if `State::Collapsed == state`
    classical: ClassicalRegister,
}

impl QuantumComputer {
    /// Construct a new quantum computer with register of given `width`.
    pub fn new(width: usize) -> QuantumComputer {
        QuantumComputer {
            state: State::Initializing,
            width: width,
            register: QuantumRegister::new(width, &ClassicalRegister::zeroed(width)),
            classical: ClassicalRegister::zeroed(width),
        }
    }

    /// Initialize the quantum register qubits to a certian classical integer state.
    ///
    /// # Panics
    ///
    /// We panic if the state is anything other than `State::Initializing`.
    pub fn initialize(&mut self, value: u32) {
        assert_eq!(State::Initializing, self.state);

        let classical = ClassicalRegister::from_int(self.width, value);
        self.register = QuantumRegister::new(self.width, &classical);

        self.state = State::Running;
    }

    /// Apply a quantum gate to the quantum register qubits.
    ///
    /// # Panics
    ///
    /// We panic if the state is anything other than `State::Running`.
    pub fn apply(&mut self, gate: Gate) {
        assert_eq!(State::Running, self.state);

        self.register.apply(gate);
    }

    /// Collapse the quantum register to a classical state.
    ///
    /// # Panics
    ///
    /// We panic if the state is anything other than `State::Running`.
    pub fn collapse(&mut self) {
        assert_eq!(State::Running, self.state);

        self.classical = self.register.collapse();

        self.state = State::Collapsed;
    }

    /// Reset the quantum register, ready to be initialized again.
    ///
    /// # Panics
    ///
    /// We panic if the state is anything other than `State::Collapsed`.
    pub fn reset(&mut self) {
        self.state = State::Initializing;
    }

    /// Read the collapsed register qubits as an integer.
    ///
    /// # Panics
    ///
    /// We panic if the state is anything other than `State::Collapsed`.
    pub fn value(&self) -> u32 {
        assert_eq!(State::Collapsed, self.state);

        self.classical.to_int()
    }

    /// Compute the probabilities of each register state without collapsing.
    ///
    /// This function is intended for test purposes.
    ///
    /// We return a vector of probabilities mirroring a ket, but without trailing zeroes.
    pub fn probabilities(&self) -> Vec<f64> {
        assert_eq!(State::Running, self.state);

        self.register.probabilities()
    }
}

#[test]
fn state_test() {
    let mut c = QuantumComputer::new(3);
    assert_eq!(State::Initializing, c.state);

    c.initialize(5);
    assert_eq!(State::Running, c.state);

    c.collapse();
    assert_eq!(State::Collapsed, c.state);

    c.value();

    c.reset();
    assert_eq!(State::Initializing, c.state);
}

#[test]
fn compute_test() {
    use gates;

    let mut c = QuantumComputer::new(3);

    c.initialize(5);

    c.apply(gates::identity(3));

    c.collapse();

    assert_eq!(5, c.value());
}

#[test]
fn probabilities_test() {
    use float_cmp::ApproxEqUlps;
    use gates;

    let mut c = QuantumComputer::new(1);

    c.initialize(0);

    c.apply(gates::hadamard(1));

    assert_eq!(2, c.probabilities().len());
    assert!(0.5f64.approx_eq_ulps(&c.probabilities()[0], 10));
    assert!(0.5f64.approx_eq_ulps(&c.probabilities()[1], 10));
}