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
//! Abstractions for representing a Quantum State
use std::{collections::HashMap, fmt};

use rand::distributions::Uniform;
use rand::prelude::*;
use rayon::prelude::*;
use std::sync::OnceLock;

use crate::{
    config::Config,
    gates::{apply, c_apply, x_apply, y_apply, z_apply, Gate},
    math::{modulus, pow2f, Float, PI},
};

/// Reference to the Config for user passed config args
pub static CONFIG: OnceLock<Config> = OnceLock::new();

#[derive(Clone)]
/// Representation of a Quantum State. Amplitudes are split between two vectors.
pub struct State {
    /// The real components of the state.
    pub reals: Vec<Float>,
    /// The imaginary components of the state.
    pub imags: Vec<Float>,
    /// The number of qubits represented by the state.
    pub n: u8,
}

impl State {
    /// Create a new State. The state will always be of size 2^{n},
    /// where n is the number of qubits. Note that n cannot be 0.
    pub fn new(n: usize) -> Self {
        assert!(n > 0);
        let mut reals = vec![0.0; 1 << n];
        let imags = vec![0.0; 1 << n];
        reals[0] = 1.0;
        Self {
            n: n as u8,
            reals,
            imags,
        }
    }

    /// Get the size of the state vector. Size of the state should always be
    /// 2^{n}, where n is the number of qubits.
    #[allow(clippy::len_without_is_empty)]
    #[inline]
    pub fn len(&self) -> usize {
        self.imags.len()
    }
}

impl fmt::Display for State {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.reals
            .iter()
            .zip(self.imags.iter())
            .for_each(|(re, im)| {
                writeln!(f, "{re} + i{im}").unwrap();
            });
        Ok(())
    }
}

/// Reservoir for sampling
/// See <https://research.nvidia.com/sites/default/files/pubs/2020-07_Spatiotemporal-reservoir-resampling/ReSTIR.pdf>
pub struct Reservoir {
    entries: Vec<usize>,
    w_s: Float,
}

impl Reservoir {
    /// Create a new reservoir for sampling
    pub fn new(k: usize) -> Self {
        Self {
            entries: vec![0; k],
            w_s: 0.0,
        }
    }

    fn update(&mut self, e_i: usize, w_i: Float) {
        self.w_s += w_i;
        let delta = w_i / self.w_s;

        self.entries
            .par_iter_mut()
            .with_min_len(1 << 16)
            .for_each_init(thread_rng, |rng, e| {
                let epsilon_k: Float = rng.gen();
                if epsilon_k < delta {
                    *e = e_i;
                }
            });
    }

    fn weight(reals: &[Float], imags: &[Float], index: usize) -> Float {
        let (z_re, z_im) = (reals[index], imags[index]);
        modulus(z_re, z_im).powi(2)
    }

    /// Run the sampling based on the given State
    pub fn sampling(&mut self, reals: &[Float], imags: &[Float], num_tests: usize) {
        debug_assert_eq!(reals.len(), imags.len());
        let uniform_dist = Uniform::from(0..reals.len());
        let mut rng = thread_rng();

        self.w_s = 0.0;
        for _ in 0..num_tests {
            let outcome = uniform_dist.sample(&mut rng); // aka the index
            self.update(outcome, Self::weight(reals, imags, outcome));
        }
    }

    /// Create a histogram of the counts for each outcome
    pub fn get_outcome_count(&self) -> HashMap<usize, usize> {
        let mut samples = HashMap::new();
        for entry in self.entries.iter() {
            *samples.entry(*entry).or_insert(0) += 1;
        }
        samples
    }
}

/// Convenience function for running reservoir sampling
pub fn reservoir_sampling(state: &State, reservoir_size: usize, num_tests: usize) -> Reservoir {
    let mut reservoir = Reservoir::new(reservoir_size);
    reservoir.sampling(&state.reals, &state.imags, num_tests);
    reservoir
}

// pub fn g_proc(
//     state: &mut State,
//     range: std::ops::Range<usize>,
//     gate: &Gate,
//     marks: &[usize],
//     zeros: &std::collections::HashSet<usize>,
//     dist: usize,
// ) {
//     let n = marks.len();
//
//     let mut offset = 0;
//     for j in 0..n {
//         if !zeros.contains(&j) {
//             offset = offset + (1 << marks[j]);
//         }
//     }
//
//     for i in range {
//         let mut l1 = i;
//
//         for j in (0..n).rev() {
//             let k = marks[j] - j;
//             l1 = l1 + ((l1 >> k) << k);
//         }
//         l1 = l1 + offset;
//         apply(gate, state, l1 - dist, l1);
//     }
// }
//
// /// general transformation
// pub fn g_transform(
//     state: &mut [Amplitude],
//     controls: &[usize],
//     zeros: &HashSet<usize>,
//     target: usize,
//     gate: &impl Gate,
// ) {
//     let mut marks = [controls, &[target]].concat();
//     let num_pairs = state.len() >> marks.len();
//     marks.sort_unstable();
//
//     let data = SendPtr(state.as_mut_ptr());
//     let dist = 1 << target;
//
//     let range = Range {
//         start: 0,
//         end: num_pairs,
//     };
//
//     g_proc(data, range, gate, &marks, zeros, dist);
// }

/// Inverse Quantum Fourier transform
pub fn iqft(state: &mut State, targets: &[usize]) {
    for j in (0..targets.len()).rev() {
        apply(Gate::H, state, targets[j]);
        for k in (0..j).rev() {
            c_apply(Gate::P(-PI / pow2f(j - k)), state, targets[j], targets[k]);
        }
    }
}

// fn apply_bit_flip(prob: Float, target: usize) {
//     todo!()
// }

/// Compute the expectation value of a qubit measurement.
pub fn qubit_expectation_value(state: &State, target: usize) -> Float {
    let chunk_size = 1 << (target + 1);
    let dist = 1 << target;

    let prob0 = state
        .reals
        .par_chunks_exact(chunk_size)
        .zip_eq(state.imags.par_chunks_exact(chunk_size))
        .map(|(reals_chunk, imags_chunk)| {
            reals_chunk
                .par_iter()
                .take(dist)
                .zip_eq(imags_chunk.par_iter().take(dist))
                .with_min_len(1 << 16)
                .map(|(re_s0, im_s0)| re_s0.powi(2) + im_s0.powi(2))
                .sum::<Float>()
        })
        .sum::<Float>();

    // p0 - p1 == p0 - (1 - p0) == p0 - 1 + p0 == 2p0 - 1
    2.0 * prob0 - 1.0
}

/// Compute the expectation value of certain observables (either X, Y, or Z) in the given state.
pub fn xyz_expectation_value(observable: char, state: &State, targets: &[usize]) -> Vec<Float> {
    if !"xyz".contains(observable) {
        panic!("observable {observable} not supported");
    }

    let mut working_state = state.clone();
    let mut values = Vec::with_capacity(targets.len());

    for target in targets.iter() {
        if observable == 'z' {
            z_apply(&mut working_state, *target);
        } else if observable == 'y' {
            y_apply(&mut working_state, *target);
        } else {
            x_apply(&mut working_state, *target);
        }

        // v = O * psi
        // <psi | O | psi>

        // <psi | v >
        // (a + ib) * (c + id) = a * c + ibc + iad - bd = ac - bd + i(bc + ad)
        let k_re = (
            &state.reals,
            &state.imags,
            &working_state.reals,
            &working_state.imags,
        )
            .into_par_iter()
            .map(|(s_re, s_im, v_re, v_im)| {
                let a = *s_re;
                let b = *s_im;
                let c = v_re;
                let d = v_im;
                a * c + b * d
            })
            .sum();

        values.push(k_re);
        working_state = state.clone();
    }
    values
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::assert_float_closeness;

    #[test]
    fn encoded_integers() {
        const N: usize = 3;

        let state = State::new(N);
        let reservoir = reservoir_sampling(&state, state.len(), state.len() * 10_000);
        let histogram = reservoir.get_outcome_count();
        let count = *histogram.get(&0).unwrap();
        assert_eq!(count, state.len());

        for i in 1..(1 << N) {
            let mut state = State::new(N);
            state.reals[0] = 0.0;
            state.reals[i] = 1.0;

            let reservoir = reservoir_sampling(&state, state.len(), state.len() * 10_000);
            let histogram = reservoir.get_outcome_count();
            let count = *histogram.get(&i).unwrap();
            assert_eq!(count, state.len());
        }
    }

    #[test]
    fn xyz_exp_val() {
        let mut state = State::new(1);

        apply(Gate::RX(0.54), &mut state, 0);
        apply(Gate::RY(0.12), &mut state, 0);
        let exp_vals = xyz_expectation_value('z', &state, &[0]);
        assert_float_closeness(exp_vals[0], 0.8515405859048367, 0.0001);
    }

    #[test]
    #[should_panic]
    fn xyz_exp_val_bad_observable() {
        let mut state = State::new(1);
        apply(Gate::RX(0.54), &mut state, 0);
        apply(Gate::RY(0.12), &mut state, 0);
        xyz_expectation_value('a', &state, &[0]);
    }

    #[test]
    fn xyz_exp_val_x_as_observable() {
        let mut state = State::new(1);
        apply(Gate::RX(0.54), &mut state, 0);
        apply(Gate::RY(0.12), &mut state, 0);
        xyz_expectation_value('x', &state, &[0]);
    }

    #[test]
    fn xyz_exp_val_y_as_observable() {
        let mut state = State::new(1);
        apply(Gate::RX(0.54), &mut state, 0);
        apply(Gate::RY(0.12), &mut state, 0);
        xyz_expectation_value('y', &state, &[0]);
    }

    #[test]
    fn qubit_exp_val() {
        let mut state = State::new(1);
        let target = 0;

        apply(Gate::RX(0.54), &mut state, target);
        apply(Gate::RY(0.12), &mut state, target);
        let exp_vals = xyz_expectation_value('z', &state, &[target]);
        let qubit_exp_val = qubit_expectation_value(&state, target);

        assert_float_closeness(qubit_exp_val, exp_vals[0], 0.0001);
    }
}