예제 #1
0
 def qho(self, dim = 4, sites = 10, spacing = 1.):
     """checks that QHO can be initialised and all functions run"""
     
     passed = True
     shape = (sites,)*dim
     raw_lattice = np.arange(sites**dim).reshape(shape)
     
     self.pot = QHO()
     self.x = Periodic_Lattice(raw_lattice)
     self.p = np.asarray(shape)
     idx_list = [(0,)*dim, (sites,)*dim, (sites-1,)*dim]
     
     passed = self._TestFns("QHO Potential", passed, self.x, self.p, idx_list)
     
     return passed
if __name__ == '__main__':

    # utils.logs.logging.root.setLevel(utils.logs.logging.DEBUG)

    dim = 1
    n = 10
    spacing = 1.
    step_size = [0.01, .1]
    n_steps = [1, 500]

    samples = 5
    step_sample = np.linspace(n_steps[0], n_steps[1], samples, True,
                              dtype=int),
    step_sizes = np.linspace(step_size[0], step_size[1], samples, True)

    for pot in [SHO(), KG(), QHO()]:

        x_nd = np.random.random((n, ) * dim)
        p0 = np.random.random((n, ) * dim)
        x0 = Periodic_Lattice(x_nd)

        dynamics = Leap_Frog(duE=pot.duE,
                             n_steps=n_steps[-1],
                             step_size=step_size[-1])

        test = Constant_Energy(pot, dynamics)
        utils.newTest(test.id)
        test.run(p0, x0, step_sample, step_sizes)

        test = Reversibility(pot, dynamics)
        utils.newTest(test.id)
#!/usr/bin/env python
import numpy as np
from scipy.stats import norm

from common import hmc_sample_1d
from hmc.potentials import Quantum_Harmonic_Oscillator as QHO

file_name = __file__
pot = QHO()

dim = 1; n = 100
x0 = np.random.random((n,)*dim)

n_burn_in, n_samples = 15, 100

theory_label = r'$|\psi_0(x)|^2 = \sqrt{\frac{\omega}{\pi}}e^{-\omega x^2}$ for $\omega=\sqrt{\frac{5}{4}}$'
def theory(x, samples):
    """The actual PDF curve
    As per the paper by Creutz and Fredman
    
    Required Inputs
        x :: np.array :: 1D array of x-axis
        samples :: np.array :: 1D array of HMC samples
    """
    w = np.sqrt(1.25)       # w  = 1/(sigma)^2
    sigma = 1./np.sqrt(2*w)
    c = np.sqrt(w / np.pi)  # this is the theory
    theory = np.exp(-x**2*1.1)*c
    return theory
#
def fitted(x, samples):