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
use std::{
    cell::{Ref, RefCell},
    rc::Rc,
};

use serde::{Deserialize, Serialize};

use crate::error::KetError;

pub trait Pid {
    fn pid(&self) -> usize;
}

#[derive(Debug, Clone)]
pub struct Qubit {
    index: usize,
    pid: usize,
    allocated: bool,
    measured: bool,
}

impl Qubit {
    pub fn new(index: usize, pid: usize) -> Qubit {
        Qubit {
            index,
            pid,
            allocated: true,
            measured: false,
        }
    }

    pub fn index(&self) -> usize {
        self.index
    }

    pub fn allocated(&self) -> bool {
        self.allocated
    }

    pub fn set_deallocated(&mut self) {
        self.allocated = false;
    }

    pub fn measured(&self) -> bool {
        self.measured
    }

    pub fn set_measured(&mut self) {
        self.measured = false;
    }

    pub fn assert_allocated(&self) -> Result<(), KetError> {
        if !self.allocated {
            Err(KetError::DeallocatedQubit)
        } else {
            Ok(())
        }
    }
}

impl Pid for Qubit {
    fn pid(&self) -> usize {
        self.pid
    }
}

#[derive(Debug)]
pub struct Future {
    index: usize,
    pid: usize,
    value: Rc<RefCell<Option<i64>>>,
}

impl Future {
    pub fn new(index: usize, pid: usize, value: Rc<RefCell<Option<i64>>>) -> Future {
        Future { index, pid, value }
    }

    pub fn value(&self) -> Ref<Option<i64>> {
        self.value.borrow()
    }

    pub fn index(&self) -> usize {
        self.index
    }
}

impl Pid for Future {
    fn pid(&self) -> usize {
        self.pid
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub enum DumpData {
    Vector {
        basis_states: Vec<Vec<u64>>,
        amplitudes_real: Vec<f64>,
        amplitudes_imag: Vec<f64>,
    },
    Probability {
        basis_states: Vec<Vec<u64>>,
        probabilities: Vec<f64>,
    },
    Shots {
        basis_states: Vec<Vec<u64>>,
        count: Vec<u32>,
        total: u64,
    },
}

impl DumpData {
    pub fn basis_states(&self) -> &[Vec<u64>] {
        match self {
            DumpData::Vector { basis_states, .. } => basis_states,
            DumpData::Shots { basis_states, .. } => basis_states,
            DumpData::Probability { basis_states, .. } => basis_states,
        }
    }

    pub fn amplitudes_real(&self) -> Option<&[f64]> {
        match self {
            DumpData::Vector {
                amplitudes_real, ..
            } => Some(amplitudes_real),
            DumpData::Shots { .. } => None,
            DumpData::Probability { .. } => None,
        }
    }

    pub fn amplitudes_imag(&self) -> Option<&[f64]> {
        match self {
            DumpData::Vector {
                amplitudes_imag, ..
            } => Some(amplitudes_imag),
            DumpData::Shots { .. } => None,
            DumpData::Probability { .. } => None,
        }
    }

    pub fn probabilities(&self) -> Option<&[f64]> {
        match self {
            DumpData::Vector { .. } => None,
            DumpData::Shots { .. } => None,
            DumpData::Probability { probabilities, .. } => Some(probabilities),
        }
    }

    pub fn count(&self) -> Option<&[u32]> {
        match self {
            DumpData::Vector { .. } => None,
            DumpData::Shots { count, .. } => Some(count),
            DumpData::Probability { .. } => None,
        }
    }

    pub fn total(&self) -> Option<u64> {
        match self {
            DumpData::Vector { .. } => None,
            DumpData::Shots { total, .. } => Some(*total),
            DumpData::Probability { .. } => None,
        }
    }
}

#[derive(Debug)]
pub struct Dump {
    value: Rc<RefCell<Option<DumpData>>>,
}

impl Dump {
    pub fn new(value: Rc<RefCell<Option<DumpData>>>) -> Dump {
        Dump { value }
    }

    pub fn value(&self) -> Ref<Option<DumpData>> {
        self.value.borrow()
    }
}

pub struct Label {
    index: usize,
    pid: usize,
}

impl Label {
    pub fn new(index: usize, pid: usize) -> Label {
        Label { index, pid }
    }

    pub fn index(&self) -> usize {
        self.index
    }
}

impl Pid for Label {
    fn pid(&self) -> usize {
        self.pid
    }
}