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
use crate::sse::qmc_traits::{DiagonalUpdater, Hamiltonian};
use crate::sse::Op;
use rand::Rng;
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

/// Bond weight storage for fast lookup.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct BondWeights {
    max_weight_and_cumulative: Vec<(usize, f64, f64)>,
}

impl BondWeights {
    /// Make a new BondWeights using an iterator of each individual bond's weight.
    pub fn new<It>(max_bond_weights: It) -> Self
    where
        It: IntoIterator<Item = f64>,
    {
        let max_weight_and_cumulative =
            max_bond_weights
                .into_iter()
                .enumerate()
                .fold(vec![], |mut acc, (b, w)| {
                    if acc.is_empty() {
                        acc.push((b, w, w));
                    } else {
                        acc.push((b, w, w + acc[acc.len() - 1].2));
                    };
                    acc
                });
        Self {
            max_weight_and_cumulative,
        }
    }

    fn get_random_bond_and_max_weight<R: Rng>(&self, mut rng: R) -> Result<(usize, f64), &str> {
        if let Some(total) = self.total() {
            let c = rng.gen_range(0., total);
            let index = self.index_for_cumulative(c);
            Ok((
                self.max_weight_and_cumulative[index].0,
                self.max_weight_and_cumulative[index].1,
            ))
        } else {
            Err("No bonds provided")
        }
    }

    fn total(&self) -> Option<f64> {
        self.max_weight_and_cumulative
            .last()
            .map(|(_, _, tot)| *tot)
    }

    fn index_for_cumulative(&self, val: f64) -> usize {
        self.max_weight_and_cumulative
            .binary_search_by(|(_, _, c)| c.partial_cmp(&val).unwrap())
            .unwrap_or_else(|x| x)
    }
}

/// Heatbath updates for a diagonal updater.
pub trait HeatBathDiagonalUpdater: DiagonalUpdater {
    /// Perform a single heatbath update.
    fn make_heatbath_diagonal_update<'b, H: Hamiltonian<'b>>(
        &mut self,
        cutoff: usize,
        beta: f64,
        state: &[bool],
        hamiltonian: &H,
        bond_weights: &BondWeights,
    ) {
        self.make_heatbath_diagonal_update_with_rng(
            cutoff,
            beta,
            state,
            hamiltonian,
            bond_weights,
            &mut rand::thread_rng(),
        )
    }

    /// Perform a single heatbath update.
    fn make_heatbath_diagonal_update_with_rng<'b, H: Hamiltonian<'b>, R: Rng>(
        &mut self,
        cutoff: usize,
        beta: f64,
        state: &[bool],
        hamiltonian: &H,
        bond_weights: &BondWeights,
        rng: &mut R,
    ) {
        let mut state = state.to_vec();
        self.make_heatbath_diagonal_update_with_rng_and_state_ref(
            cutoff,
            beta,
            &mut state,
            hamiltonian,
            bond_weights,
            rng,
        )
    }

    /// Perform a single heatbath update.
    fn make_heatbath_diagonal_update_with_rng_and_state_ref<'b, H: Hamiltonian<'b>, R: Rng>(
        &mut self,
        cutoff: usize,
        beta: f64,
        state: &mut [bool],
        hamiltonian: &H,
        bond_weights: &BondWeights,
        rng: &mut R,
    ) {
        self.mutate_ps(0, cutoff, (state, rng), |s, op, (state, rng)| {
            let op = Self::heat_bath_single_diagonal_update(
                op,
                cutoff,
                s.get_n(),
                beta,
                state,
                (hamiltonian, bond_weights),
                rng,
            );
            (op, (state, rng))
        });
    }

    /// Make the bond weights struct for this container.
    fn make_bond_weights<'b, H, E>(hamiltonian: H, num_bonds: usize, bonds_fn: E) -> BondWeights
    where
        H: Fn(&[usize], usize, &[bool], &[bool]) -> f64,
        E: Fn(usize) -> &'b [usize],
    {
        let max_weights = (0..num_bonds).map(|i| {
            let vars = bonds_fn(i);
            (0..1 << vars.len())
                .map(|substate| {
                    let substate =
                        Self::Op::make_substate((0..vars.len()).map(|v| (substate >> v) & 1 == 1));
                    hamiltonian(vars, i, substate.as_ref(), substate.as_ref())
                })
                .fold(0.0, |acc, w| if w > acc { w } else { acc })
        });
        BondWeights::new(max_weights)
    }

    /// Perform a single heatbath update.
    fn heat_bath_single_diagonal_update<'b, H: Hamiltonian<'b>, R: Rng>(
        op: Option<&Self::Op>,
        cutoff: usize,
        n: usize,
        beta: f64,
        state: &mut [bool],
        hamiltonian_and_weights: (&H, &BondWeights),
        rng: &mut R,
    ) -> Option<Option<Self::Op>> {
        let (hamiltonian, bond_weights) = hamiltonian_and_weights;
        let new_op = match op {
            None => {
                let numerator = beta * bond_weights.total().unwrap();
                let denominator = (cutoff - n) as f64 + numerator;
                if rng.gen_bool(numerator / denominator) {
                    // For usage later.
                    let p = rng.gen_range(0.0, 1.0);
                    // Find the bond to use, weighted by their matrix element.
                    let (b, maxweight) = bond_weights.get_random_bond_and_max_weight(rng).unwrap();
                    let (vars, constant) = hamiltonian.edge_fn(b);
                    let substate = Self::Op::make_substate(vars.iter().map(|v| state[*v]));
                    let vars = Self::Op::make_vars(vars.iter().cloned());

                    let weight = hamiltonian.hamiltonian(
                        vars.as_ref(),
                        b,
                        substate.as_ref(),
                        substate.as_ref(),
                    );

                    if p * maxweight < weight {
                        let op = Self::Op::diagonal(vars, b, substate, constant);
                        Some(Some(op))
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            Some(op) if op.is_diagonal() => {
                let numerator = (cutoff - n + 1) as f64;
                let denominator = numerator + beta * bond_weights.total().unwrap();

                if rng.gen_bool(numerator / denominator) {
                    Some(None)
                } else {
                    None
                }
            }
            // Update state
            Some(op) => {
                op.get_vars()
                    .iter()
                    .zip(op.get_outputs().iter())
                    .for_each(|(v, b)| state[*v] = *b);
                None
            }
        };
        new_op
    }
}