Skip to content

Commit

Permalink
Implements some manual checks for distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
gkanwar committed Sep 11, 2021
1 parent f23e1a7 commit 3fc5f4a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.'cfg(all(target_os = "macos"))']
rustflags = ["-C", "link-args=-L/usr/local/opt/openblas/lib -lopenblas"]
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "rust_qcd"
name = "rs-lattice"
version = "0.1.0"
edition = "2018"

Expand Down
22 changes: 22 additions & 0 deletions src/bin/test_rand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use lqft::{sample_su2_theta, ColorMat, SampleHaar, IntoBytes};
use std::fs::File;
use std::io::Write;

fn main() -> Result<(), std::io::Error> {
{
let beta = 1.0;
let mut file = File::create("su2_theta.dat")?;
for _ in 0..10000 {
let th: f64 = sample_su2_theta(beta);
file.write_all(&th.to_le_bytes())?;
}
}
{
let mut file = File::create("u2_haar.dat")?;
for _ in 0..10000 {
let u = ColorMat::<2>::sample_haar();
file.write_all(&u.into_bytes())?;
}
}
Ok(())
}
16 changes: 15 additions & 1 deletion src/lib/color_matrix.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::math::{c64, CMat, CVec};
use crate::rand_dist::{SampleHaar};
use crate::{IntoBytes};

use std::ops::{Add, Index, IndexMut};

// Color matrices
#[derive(Clone)]
pub struct ColorMat<const N: usize> {
data: CMat<N>
pub data: CMat<N>
}

impl<const N: usize> ColorMat<N> {
Expand All @@ -23,6 +24,19 @@ impl<const N: usize> ColorMat<N> {
}
}

impl<const N: usize> IntoBytes for ColorMat<N> {
fn into_bytes(&self) -> Vec<u8> {
let mut bs: Vec<u8> = vec!();
for i in 0..N {
for j in 0..N {
bs.extend_from_slice(&self.data[i][j].re.to_le_bytes());
bs.extend_from_slice(&self.data[i][j].im.to_le_bytes());
}
}
bs
}
}

impl<const N: usize> Index<(usize, usize)> for ColorMat<N> {
type Output = c64;

Expand Down
4 changes: 4 additions & 0 deletions src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use ndarray::Array4;
use std::convert::TryInto;
use std::ops::Index;

pub trait IntoBytes {
fn into_bytes(&self) -> Vec<u8>;
}

pub trait LatticeObject<const ND: usize> {
fn latt_shape(&self) -> [usize; ND];
fn nd(&self) -> usize {
Expand Down
9 changes: 4 additions & 5 deletions src/lib/rand_dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use ndarray_linalg::qr::QR;
// on a wrapped Cauchy base distribution.
pub fn sample_von_mises(beta: f64) -> f64 {
let mut rng = thread_rng();
let u1: f64 = rng.gen();
let u2: f64 = rng.gen();
let u3: f64 = rng.gen();

let tau = 1.0 + (1.0 + 4.0 * beta * beta).sqrt();
let rho = (tau - (2.0 * tau).sqrt()) / (2.0 * beta);

loop {
let u1: f64 = rng.gen();
let u2: f64 = rng.gen();
let u3: f64 = rng.gen();
let r = (1.0 + rho * rho) / (2.0 * rho);
let z = (PI * u1).cos();
let f = (1.0 + r * z) / (r + z);
Expand Down Expand Up @@ -78,4 +77,4 @@ macro_rules! impl_sample_haar {
};
}
impl_sample_haar!(2);
impl_sample_haar!(3);
impl_sample_haar!(3);

0 comments on commit 3fc5f4a

Please sign in to comment.