示例#1
0
def test_gradients_wavefunction_2d_2p():

    num_p = 2
    num_d = 2
    M = num_p * num_d
    N = 3
    sigma = 1.0

    for _ in range(50):
        a = np.random.uniform(-2, 2, M)
        b = np.random.uniform(-2, 2, N)
        W = np.random.uniform(-2, 2, (M, N))
        wave = Wavefunction(M, N, a, b, W, sigma)
        positions = np.random.uniform(-1, 1, M)
        sum1 = np.subtract(positions, a)
        sum1 = -np.sum(sum1 * sum1) / (2 * sigma * sigma)
        sum2 = 0.0
        sum3 = 0.0
        sum4 = 0.0
        for k in range(M):
            for j in range(N):
                for i in range(M):
                    sum2 += np.dot(positions, W[:, j]) / (sigma * sigma)
                exponent = math.exp(b[j] + sum2)
                sum3 += W[k, j] / (1 + exponent)
                sum4 += exponent * (W[k, j] / (1 + exponent))**2

        fd = sum1 + (1 / sigma * sigma) * sum3
        sd = -1 / (sigma * sigma) + 1 / ((sigma * sigma)**2) * sum4

        assert fd, sd == pytest.approx(wave.wavefunction(positions), abs=1e-10)
示例#2
0
def test_local_energy_3d_2p():

    num_particles = 3
    num_dimensions = 2
    M = num_particles * num_dimensions
    N = 3
    a = np.random.uniform(-2, 2, M)
    b = np.random.uniform(-2, 2, N)
    W = np.random.uniform(-2, 2, (M, N))
    omega = 1.0
    gamma = 1.0
    sigma = 1.0
    wave = Wavefunction(M, N, a, b, W, sigma)
    hamilton = Hamiltonian(gamma, omega, num_dimensions, num_particles, wave,
                           False, False)

    for _ in range(50):
        positions = np.random.uniform(-2, 2, M)
        fd = wave.gradient_wavefunction(positions)
        sd = wave.laplacian_wavefunction(positions)
        x = 0.0
        for i in range(M):
            x += positions[i] * positions[i]

        energy = 0.5 * (-fd - sd + omega * omega * x)

        assert energy == pytest.approx(hamilton.local_energy(positions),
                                       abs=1e-14)
示例#3
0
def test_gradient_wavefunction_W_3d_2p():

    num_p = 2
    num_d = 3
    M = num_p * num_d
    N = 3
    sigma = 1.0

    for _ in range(50):
        a = np.random.uniform(-2, 2, M)
        b = np.random.uniform(-2, 2, N)
        W = np.random.uniform(-2, 2, (M, N))
        wave = Wavefunction(M, N, a, b, W, sigma)
        position = np.random.uniform(-1, 1, M)
        gradient = np.zeros((M, N))
        for k in range(M):
            for j in range(N):
                term = np.dot(position, W[:, j]) / (sigma * sigma)
                factor = position[k] / (sigma * sigma)
                gradient[k, j] = factor * (1 / (1 + math.exp(-b[j] - term)))

        grad = gradient

        assert grad == pytest.approx(wave.gradient_wavefunction_W(position),
                                     abs=1e-10)
示例#4
0
def test_wavefunction_3d_2p():

    num_p = 2
    num_d = 3
    M = num_p * num_d
    N = 3
    sigma = 1.0

    for _ in range(50):
        a = np.random.uniform(-2, 2, M)
        b = np.random.uniform(-2, 2, N)
        W = np.random.uniform(-2, 2, (M, N))
        wave = Wavefunction(M, N, a, b, W, sigma)
        positions = np.random.uniform(-1, 1, M)
        sum1 = positions - a
        sum1 = np.square(sum1)
        sum1 = np.sum(sum1) / (2 * sigma * sigma)
        term1 = math.exp(-sum1)
        sum2 = 0.0
        prod = 1.0
        for j in range(N):
            sum2 = np.dot(positions, W[:, j]) / (sigma * sigma)
            prod *= (1 + math.exp(b[j] + sum2))

        wave_function = term1 * prod

        assert wave_function == pytest.approx(wave.wavefunction(positions),
                                              abs=1e-6)
示例#5
0
文件: rbm.py 项目: KariEriksen/RBM
def non_interaction_case(monte_carlo_cycles, num_particles, num_dimensions,
                         hidden_nodes):
    """Run Restricted Boltzmann Machine."""

    # Initialize weights and biases
    visible_nodes = num_particles*num_dimensions
    a_i = np.random.normal(0, 1, visible_nodes)
    b_j = np.random.normal(0, 1, hidden_nodes)
    W_ij = np.random.normal(0, 1, (visible_nodes, hidden_nodes))
    # a_i = np.random.rand(visible_nodes)
    # b_j = np.random.rand(hidden_nodes)
    # W_ij = np.random.rand(visible_nodes, hidden_nodes)
    # a_i = np.zeros(visible_nodes)
    # b_j = np.zeros(hidden_nodes)
    # W_ij = np.zeros((visible_nodes, hidden_nodes))

    sigma = 1.0
    omega = 1.0
    gamma = 1.0

    param_a = a_i
    param_b = b_j
    param_W = W_ij

    d_El_array = np.zeros(gradient_iterations)
    energy_array = np.zeros(gradient_iterations)
    parameter_array = np.zeros(gradient_iterations)
    var_array = np.zeros(gradient_iterations)
    for i in range(gradient_iterations):

        # Call system class in order to set new parameters
        wave = Wavefunction(visible_nodes, hidden_nodes,
                            param_a, param_b, param_W, sigma)
        # Hamiltonian(..., weak_interaction, strong_interaction)
        hamilton = Hamiltonian(gamma, omega, num_dimensions, num_particles,
                               wave, False, False)
        met = Metropolis(monte_carlo_cycles, step_metropolis, step_importance,
                         num_particles, num_dimensions, wave, hamilton)

        d_El = met.run_metropolis()
        # d_El = met.run_importance_sampling()
        # d_El = met.run_gibbs_sampling()
        d_El_a = d_El[0]
        d_El_b = d_El[1]
        d_El_W = d_El[2]

        new_a, new_b, new_W = opt.gradient_descent(param_a, param_b, param_W,
                                                   d_El_a, d_El_b, d_El_W)

        print ('number of gradien descent runs = ', i)
        param_a = new_a
        param_b = new_b
        param_W = new_W

        # d_El_array[i] = d_El
        # var_array[i] = var
        energy_array[i] = d_El[3]

    plt.plot(energy_array)
    plt.show()
示例#6
0
文件: rbm.py 项目: KariEriksen/RBM
def run_blocking(monte_carlo_cycles, num_particles, num_dimensions,
                 hidden_nodes):
    """Run the sampling in metropolis to be used for blocking."""

    # Set optimal values for weights and biases
    visible_nodes = num_particles*num_dimensions
    a_i = np.random.normal(0, 1, visible_nodes)
    b_j = np.random.normal(0, 1, hidden_nodes)
    W_ij = np.random.normal(0, 1, (visible_nodes, hidden_nodes))

    sigma = 1.0
    omega = 1.0
    gamma = 1.0

    # Call system class in order to set new parameters
    wave = Wavefunction(visible_nodes, hidden_nodes,
                        a_i, b_j, W_ij, sigma)
    # Hamiltonian(..., weak_interaction, strong_interaction)
    hamilton = Hamiltonian(gamma, omega, num_dimensions, num_particles,
                           wave, False, False)
    met = Metropolis(monte_carlo_cycles, step_metropolis, step_importance,
                     num_particles, num_dimensions, wave, hamilton)

    # d_El, energy = met.run_metropolis()
    # Run with analytical expression for quantum force = true
    energy = met.blocking()

    with open('/home/kari/VMC/data/blocking.csv', 'w',
              newline='') as file:
        writer = csv.writer(file)
        writer.writerow(["local_energy"])
        for i in range(len(energy)):
            writer.writerow([energy[i]])
示例#7
0
文件: rbm.py 项目: KariEriksen/RBM
def one_body_density(monte_carlo_cycles, num_particles, num_dimensions,
                     hidden_nodes):
    """Run the variational monte carlo"""
    """using brute force"""

    # Set optimal values for weights and biases
    visible_nodes = num_particles*num_dimensions
    a_i = np.random.normal(0, 1, visible_nodes)
    b_j = np.random.normal(0, 1, hidden_nodes)
    W_ij = np.random.normal(0, 1, (visible_nodes, hidden_nodes))

    sigma = 1.0
    omega = 1.0
    gamma = 1.0

    # Call system class in order to set new parameters
    wave = Wavefunction(visible_nodes, hidden_nodes,
                        a_i, b_j, W_ij, sigma)
    # Hamiltonian(..., weak_interaction, strong_interaction)
    hamilton = Hamiltonian(gamma, omega, num_dimensions, num_particles,
                           wave, False, False)
    met = Metropolis(monte_carlo_cycles, step_metropolis, step_importance,
                     num_particles, num_dimensions, wave, hamilton)

    r_vec = np.linspace(0, 4, 41)
    p_r = met.run_one_body_sampling()
    with open('/home/kari/VMC/data/obd_data.csv', 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(["r", "density"])
        for i in range(len(r_vec)):
            writer.writerow([r_vec[i], p_r[i]/monte_carlo_cycles])
示例#8
0
def test_gradient_wavefunction_a_3d_2p():

    num_p = 2
    num_d = 3
    M = num_p * num_d
    N = 3
    sigma = 1.0

    for _ in range(50):
        a = np.random.uniform(-2, 2, M)
        b = np.random.uniform(-2, 2, N)
        W = np.random.uniform(-2, 2, (M, N))
        wave = Wavefunction(M, N, a, b, W, sigma)
        position = np.random.uniform(-1, 1, M)
        grad = np.subtract(position, a) / (sigma * sigma)

        assert grad == pytest.approx(wave.gradient_wavefunction_a(position),
                                     abs=1e-10)
示例#9
0
文件: rbm.py 项目: KariEriksen/RBM
def weak_interaction_case(monte_carlo_cycles, num_particles, num_dimensions,
                          hidden_nodes):
    """Run Restricted Boltzmann Machine."""

    # Initialize weights and biases
    visible_nodes = num_particles*num_dimensions
    a_i = np.random.rand(visible_nodes)
    b_j = np.random.rand(hidden_nodes)
    W_ij = np.random.rand(visible_nodes, hidden_nodes)
    sigma = 1.0
    omega = 1.0
    gamma = 1.0

    param_a = a_i
    param_b = b_j
    param_W = W_ij
    for i in range(gradient_iterations):

        # Call system class in order to set new parameters
        wave = Wavefunction(visible_nodes, hidden_nodes,
                            param_a, param_b, param_W, sigma)
        # Hamiltonian(..., weak_interaction, strong_interaction)
        hamilton = Hamiltonian(gamma, omega, num_dimensions, num_particles,
                               wave, True, False)
        met = Metropolis(monte_carlo_cycles, step_metropolis, step_importance,
                         num_particles, num_dimensions, wave, hamilton)

        d_El = met.run_metropolis()
        # Run with analytical expression for quantum force = true
        # d_El = met.run_importance_sampling('true')
        d_El_a = d_El[0]
        d_El_b = d_El[1]
        d_El_W = d_El[2]
        new_a, new_b, new_W = opt.gradient_descent(param_a, param_b, param_W,
                                                   d_El_a, d_El_b, d_El_W)

        print ('number of gradien descent runs = ', i)
        param_a = new_a
        param_b = new_b
        param_W = new_W