Exemple #1
0
def test_1d_free_fermion_renyi(tolerance=0.02):
    N = 10
    F = 5

    lattice = Lattice([N])
    orbitals = Bands([F], [periodic])

    if False:
        # a constant Jastrow factor shouldn't change anything
        from pyvmc.core.wavefunction import TwoBodyJastrowFactor

        jastrow_mat = numpy.empty((N, N))
        jastrow_mat.fill(1)
        jastrow = TwoBodyJastrowFactor(jastrow_mat)
    else:
        jastrow = None
    wf = FreeFermionWavefunction(lattice=lattice, orbitals=[orbitals], jastrow=jastrow)

    exact_values = [
        free_fermions.exact_renyi(SimpleSubsystem([i], lattice), orbitals, [periodic], 2) for i in range(1, N // 2 + 1)
    ]

    plans = [RenyiEntropyMeasurementPlan(wf, SimpleSubsystem([i], lattice)) for i in range(1, N // 2 + 1)]
    calc = SimulationUniverse(plans, 500000)
    while True:
        calc.iterate(200000)
        results = calc.get_overall_measurement_dict()
        measured_values = [plan.calculate(lambda p, k=None: results[p].get_estimate(k).result) for plan in plans]

        differences = [
            measured_value - exact_value for measured_value, exact_value in zip(measured_values, exact_values)
        ]
        logger.info("differences from expected: %s", differences)
        if all(numpy.abs(d) < tolerance for d in differences):
            break

    # test hdf5
    import h5py

    filename = "/tmp/hdf5test_renyi.hdf5"
    with h5py.File(filename, "w") as f:
        calc.to_hdf5(f)
    with h5py.File(filename, "r") as f:
        universe2 = SimulationUniverse.from_hdf5(f, wf)
    results2 = universe2.get_overall_measurement_dict()
    measured_values2 = [plan.calculate(lambda p, k=None: results2[p].get_estimate(k).result) for plan in plans]
    assert all(
        numpy.abs(measured_value - exact_value) < tolerance
        for measured_value, exact_value in zip(measured_values, exact_values)
    )
Exemple #2
0
def test_dmetal_energy(tolerance=None):
    wf = DMetalWavefunction(**{
        'lattice': Lattice([12, 2]),
        'd1': Bands([5, 3], (periodic, periodic)),
        'd2': Bands([8, 0], (antiperiodic, periodic)),
        'f_up': Bands([4, 0], (antiperiodic, periodic)),
        'f_dn': Bands([4, 0], (antiperiodic, periodic)),
        'd1_exponent': 0.7,
        'd2_exponent': -0.4,
    })

    from pyvmc.operators import TJKHamiltonian
    from pyvmc.measurements import BasicOperatorMeasurementPlan
    from pyvmc.core.universe import SimulationUniverse

    hamiltonian = TJKHamiltonian((periodic, periodic), wf.lattice)
    plans = [BasicOperatorMeasurementPlan(wf, o) for o in hamiltonian.get_basic_operators()]
    universe = SimulationUniverse(plans, equilibrium_sweeps=500000)
    universe.iterate(1000000)
    context = {mp.operator: m.get_estimate().result for mp, m in universe.get_overall_measurement_dict().items()}
    energy = hamiltonian.evaluate(context)(t=1, J=2, K=2) / len(wf.lattice)

    logger.info("Energy: %f", energy)
    assert -0.8 < energy < -0.74

    # test hdf5
    import h5py
    filename = '/tmp/hdf5test_dmetal.hdf5'
    with h5py.File(filename, 'w') as f:
        grp = f.create_group('testgroup')
        universe.to_hdf5(grp)
    with h5py.File(filename, 'r') as f:
        universe2 = SimulationUniverse.from_hdf5(f['testgroup'], wf)
    context2 = {mp.operator: m.get_estimate().result for mp, m in universe2.get_overall_measurement_dict().items()}
    energy = hamiltonian.evaluate(context)(t=1, J=2, K=2) / len(wf.lattice)
    logger.info("Energy: %f", energy)
    assert -0.8 < energy < -0.74