def test_ideal_harmonic_oscillator(): omega, N, D = 1, 5, 3 H = HarmonicOscillator(omega_ho=omega) psi = SimpleGaussian(alpha=0.5 * omega) sampler = ImportanceSampler(np.empty((N, D)), psi, 0.1) sampler.thermalize(10000) # Energy: E = compute_statistics_for_series(H.local_energy_array( sampler, psi, 2**10)) assert N * D * omega / 2 == E["mean"], "Energy should be exactly equal." assert E["var"] < 1e-16 # Squared radius: r2 = compute_statistics_for_series(H.mean_squared_radius_array( sampler, 2**16), method="blocking") print(r2) assert (abs(3 / omega / 2 - r2["mean"]) < 1.96 * r2["sem"]), "Should be withing 95% CI of analytic result." # Radius: r = compute_statistics_for_series(H.mean_radius_array(sampler, 2**16), method="blocking") print(r) assert (abs(2 / np.sqrt(np.pi * omega) - r["mean"]) < 1.96 * r["sem"]), "Should be withing 95% CI of analytic result."
r"Adam($\eta=0.05,\beta_1=0.7$)", ] optimizers = [ SgdOptimizer(0.1), SgdOptimizer(0.05), SgdOptimizer(0.025), SgdOptimizer(0.01), AdamOptimizer(len(psi.parameters), 0.05, 0.9), AdamOptimizer(len(psi.parameters), 0.05, 0.7), ] E = [] for opt in optimizers: # psi.parameters = org_params psi = SimpleGaussian(0.8) sampler = ImportanceSampler(system, psi, 0.1) sampler.thermalize(10000) E_training = EnergyCallback(samples=1000000, verbose=True) train( psi, H, sampler, iters=150, samples=1000, gamma=0.0, optimizer=opt, call_backs=[E_training], call_back_resolution=50, ) E.append(np.asarray(E_training)) if master_rank():
compute_statistics_for_series( H.local_energy_array(psi_bench_sampler, psi_bench, evaluation_points), method="blocking", ), compute_statistics_for_series(H.local_energy_array(psi_sampler, psi, evaluation_points), method="blocking"), compute_statistics_for_series( H.local_energy_array(psi_sorted_sampler, psi_sorted, evaluation_points), method="blocking", ), ] old = psi_sorted.parameters psi_sorted.parameters = psi.parameters psi_sorted_sampler.thermalize(10000) stats.append( compute_statistics_for_series( H.local_energy_array(psi_sorted_sampler, psi_sorted, evaluation_points), method="blocking", )) labels = [ r"$\psi_{PJ}$", r"$\psi_{DNN}$", r"$\psi_{SDNN}$", r"$\hat{\psi}_{SDNN}$" ] mpiprint(stats, pretty=True) mpiprint(statistics_to_tex(stats, labels, filename=__file__ + ".table.tex")) # mpiprint(psi.parameters)
from qflow.wavefunctions import SimpleGaussian from qflow.hamiltonians import HarmonicOscillator from qflow.samplers import ImportanceSampler from qflow.statistics import compute_statistics_for_series, statistics_to_tex from qflow.mpi import mpiprint N, D = 100, 3 system = np.empty((N, D)) H = HarmonicOscillator(omega_ho=1) psi_opt = SimpleGaussian(alpha=0.5) psi_nopt = SimpleGaussian(alpha=0.51) sampler_opt = ImportanceSampler(system, psi_opt, 0.1) sampler_nopt = ImportanceSampler(system, psi_nopt, 0.1) sampler_opt.thermalize(10000) sampler_nopt.thermalize(10000) samples = 2**23 stats = [ compute_statistics_for_series( H.local_energy_array(sampler_opt, psi_opt, 100) / N), compute_statistics_for_series( H.local_energy_array(sampler_nopt, psi_nopt, samples) / N, method="blocking"), ] labels = [r"$\alpha_G = 0.5$", r"$\alpha_G=0.51$"] mpiprint(statistics_to_tex(stats, labels, filename=__file__ + ".table1.tex"))
import matplotlib.pyplot as plt import matplotlib2tikz from qflow.wavefunctions import SimpleGaussian from qflow.hamiltonians import HarmonicOscillator from qflow.samplers import ImportanceSampler from qflow.optimizers import SgdOptimizer from qflow.training import train, EnergyCallback, ParameterCallback N, D = 10, 3 system = np.empty((N, D)) H = HarmonicOscillator(omega_ho=1) psi_G = SimpleGaussian(alpha=0.3) isampler_G = ImportanceSampler(system, psi_G, 0.1) isampler_G.thermalize(10000) E_training = EnergyCallback(samples=50000, verbose=True) G_training = ParameterCallback() train( psi_G, H, isampler_G, iters=100, samples=1000, gamma=0, optimizer=SgdOptimizer(0.001), call_backs=[E_training, G_training], ) E_training = np.asarray(E_training) / N
system = np.empty((N, D)) H = HarmonicOscillator(omega_ho=1) psi = SimpleGaussian(alpha=0.51) n_thermal = 50000 n_energy = 2**21 arm, ari = [], [] semm, semi = [], [] steps = np.logspace(-6.5, 1.1, 150) for step in tqdm(steps): ms = MetropolisSampler(system, psi, step) si = ImportanceSampler(system, psi, step) ms.thermalize(n_thermal) si.thermalize(n_thermal) arm.append(ms.acceptance_rate * 100) ari.append(si.acceptance_rate * 100) semm.append( compute_statistics_for_series(H.local_energy_array(ms, psi, n_energy), method="blocking")["sem"]) semi.append( compute_statistics_for_series(H.local_energy_array(si, psi, n_energy), method="blocking")["sem"]) if master_rank(): mask = np.asarray(semi) < np.max(semm) fig, ax1 = plt.subplots() met, = ax1.semilogx(steps, arm)