Example #1
0
def run_algo(g1, g2, N, dt, d, chi_max, model, order, T, algo, bis_err,
             trunc_err_check):
    H = st.Hamiltonian(g1, g2, N, dt, d, chi_max, model, order,
                       grow_chi=trunc_err_check)
    Psi = st.StateChain(g1, g2, N, d, chi_max, algo, bis_err)
    step_num = int(T / dt)
    H.time_evolve(Psi, step_num, algo, fast_run=True)
    return Psi, H
Example #2
0
def new_mu(coup1, coup2, N, dt, d, chi, T, num_op, old_mu, start_dens,
           rho_maxerr):
    goal_dens = 1 / 2
    Meas = st.Measure()
    order = "fourth"
    model = "HCboson"
    if start_dens < goal_dens:
        mu0 = old_mu
        dens0 = start_dens
    else:
        mu1 = old_mu
        dens1 = start_dens
    for ind in range(40):
        mu_ham = st.Hamiltonian(coup1,
                                coup2,
                                N,
                                dt,
                                d,
                                chi,
                                model,
                                TO=order,
                                grow_chi=False)
        mu_psi = st.StateChain(coup1, coup2, N, d, chi, "tDMRG")

        steps = int(T / dt)
        #for ind2 in range(5):
        mu_psi = mu_ham.time_evolve(mu_psi, steps, "tDMRG", fast_run=True)

        mu_dens = 0
        for m in range(N):
            mu_dens += Meas.expec(mu_psi, num_op, m)
        mu_dens /= N

        dens_err = abs((mu_dens - goal_dens)) / mu_dens

        if mu_dens > goal_dens:
            mu1 = coup2[0]
            dens1 = mu_dens
        else:
            mu0 = coup2[0]
            dens0 = mu_dens

        if abs(dens_err) < rho_maxerr or ind == 39:
            mu = coup2[0]
            if ind == 39:
                print("Density error:", dens_err)
            break
        else:
            yp = ([mu0, mu1] if mu0 < mu1 else [mu1, mu0])
            xp = ([dens0, dens1] if mu0 < mu1 else [dens1, dens0])

            coup2[0] = np.interp(goal_dens, xp, yp)

    return mu
Example #3
0
def get_GS(filename):
    GS_mat = []

    with open(filename, "r") as fr:
        GS_mat += [line.strip("\n") for line in fr]

    # Reconstruct parameters of state
    coups = [float(GS_mat.pop(0)) for i in range(4)]
    g1, g2 = coups[:2], coups[2:]
    N = int(GS_mat.pop(0))
    d = int(GS_mat.pop(0))
    err = float(GS_mat.pop(0))
    notation = GS_mat.pop(0)
    chi = int(GS_mat.pop(0))

    GS_mat = [float(dat) for dat in GS_mat]
    tens_net = []
    lam_net = []

    # Reconstruct A/B matrices in accordance with notation
    for i in range(int(N / 2)):
        shape1 = d**i if d**i < chi else chi
        shape2 = d**(i + 1) if d**(i + 1) < chi else chi
        GS_mat, tens_comps = GS_mat[d * shape1 * shape2:], GS_mat[:d * shape1 *
                                                                  shape2]
        tens_net += [np.array(tens_comps).reshape(d, shape1, shape2)]
    for j in range(int(N / 2), 0, -1):
        shape1 = d**j if d**j < chi else chi
        shape2 = d**(j - 1) if d**(j - 1) < chi else chi
        GS_mat, tens_comps = GS_mat[d * shape1 * shape2:], GS_mat[:d * shape1 *
                                                                  shape2]
        tens_net += [np.array(tens_comps).reshape(d, shape1, shape2)]

    # Reconstructs lambda tensors
    for k in range(N + 1):
        np_dat = min([d**k, d**(N - k), chi])
        GS_mat, lam_comps = GS_mat[np_dat:], GS_mat[:np_dat]
        lam_net += [np.array(lam_comps)]

    indata = [g1, g2, err, notation, lam_net, tens_net]

    # Pass StateChain constructions with load_state flag so data is loaded and
    # not given
    Psi = st.StateChain(None,
                        None,
                        N,
                        d,
                        chi,
                        "tDMRG",
                        load_state=True,
                        load_mat=indata)

    return Psi
def main():
    N = int(sys.argv[1])
    dt = float(sys.argv[2])
    T = int(sys.argv[3])
    chi = int(sys.argv[4])
    alf = float(sys.argv[5])
    step_num = int(T / dt)
    g1 = [1., 1.]
    g2 = [1., alf]
    d = 2
    algo = "tDMRG"

    H = st.Hamiltonian(g1,
                       g2,
                       N,
                       dt,
                       d,
                       chi,
                       model="HCboson",
                       TO="fourth",
                       grow_chi=False)
    Psi = st.StateChain(g1, g2, N, d, chi, algo)

    Psi = H.time_evolve(Psi, step_num, algo)

    M = st.Measure()

    adag = np.array([[0., 1.], [0, 0]])
    a = np.array([[0, 0], [1, 0]])
    corr_matrix = M.corr_mat(Psi, adag, a)
    E_GS = sum(Psi.get_ener(H.Hchain))

    data = [E_GS, Psi.err]
    corr_matrix = np.reshape(corr_matrix, (corr_matrix.shape[0]**2))
    data += [corr for corr in corr_matrix]

    direc_name = "alf=" + str(alf)
    file_name = "chi=" + str(chi) + ",T=" + str(T) + ",dt=" + str(dt) + ".txt"

    cwd_store(direc_name, file_name, data)
    print("Success!")
Example #5
0
def main():
    # model = input("Enter model (Heisen, HCboson): ")
    model = "HCboson"

    # Fetches parameters from a file in current directory with name model.txt
    directory = (os.path.dirname(os.path.realpath(__file__)) + "/" + model +
                 ".txt")
    f = open(directory, "r")
    inputlist = []
    keylist = {}
    for line in f:
        if line.startswith('#'):
            continue
        d = []
        inputlist.append(line.rstrip('\n').split(" = "))
        x = inputlist[-1][0]
        yl = inputlist[-1][1].split(',')
        for y in yl:
            try:
                d.append(int(y))
            except ValueError:
                try:
                    d.append(float(y))
                except ValueError:
                    d.append(y)
        if len(d) == 1:
            d = d[0]
        keylist[x] = d
    f.close()

    g1 = keylist['g1']
    g2 = keylist['g2']
    dt = keylist['dt']  #Time step
    d = keylist['d']  #One-particle Hilbert space dim
    chi = keylist['chi']  #Maximum MPS dim
    N = keylist['N']  #Site number
    T = keylist['T']  #Total time evolved
    step_number = int(T / dt)  #Number of time steps
    order = keylist['order']  #Trotter order
    algo = keylist['algo']  #Which algorithm to use

    # H belongs to class Hamiltonian which features time evolution and model
    # construction.
    H = st.Hamiltonian(g1, g2, N, dt, d, chi, model, TO=order, grow_chi=False)

    # Build the initial state
    Psi = st.StateChain(g1, g2, N, d, chi, algo, bis_err=10**-11)

    # Time evolve the state (imaginary time)
    start = t.process_time()
    actstart = t.time()
    Psi = H.time_evolve(Psi, step_number, algo, fast_run=False)
    end = t.process_time()
    actend = t.time()
    print("Process time taken for algorithm is:", end - start)
    print("Actual time taken:", actend - actstart)

    # Output data to terminal
    algo_output(Psi, H)

    # Exact diagonalization trotter
    if N <= 20:
        start = t.process_time()
        ED = ed.ExactD(g1, g2, N, d, model, TO=order)
        end1 = t.process_time()
        ED.exact_GS()
        end2 = t.process_time()
        exact_output(ED)
        print("\nProcess time taken for full Hamiltonian:", end1 - start)
        print("\nProcess time taken for ED is:", end2 - end1)

    if model == "HCboson":
        Free = st.FreeFerm(g1, g2, N)
        print("\nFree fermion result:", Free.E_GS)

    # ener = 0
    # M = st.Measure()
    # for i in range(N):
    #     ener += M.correl()

    print(Psi.err)
    return Psi
Example #6
0
def SMF_loop(tperp, g1, g2, N, chi, T, rho_maxerr=1e-6, orp_maxerr=1e-6):
    dt = 0.1
    model = "HCboson"
    order = "fourth"
    d = 2
    algo = "tDMRG"

    step_num = int(T / dt)

    M = st.Measure()
    a = np.array([[0, 0], [1, 0]])
    adag = np.array([[0, 1], [0, 0]])
    num_op = np.matmul(adag, a)
    a_exp_guess = np.sqrt(1 / 2)
    i = 0
    err = 1
    g2[1] = 4 * tperp * a_exp_guess
    ord_pars = [a_exp_guess]
    mu_list = [g2[0]]
    dens = 1 / 2

    # Loops for at most 100 iterations. Some runs do not converge even at this
    # point.
    while i < 150 and err > orp_maxerr and ord_pars[-1] > 1e-8:
        H = st.Hamiltonian(g1,
                           g2,
                           N,
                           dt,
                           d,
                           chi,
                           model,
                           TO=order,
                           grow_chi=False)
        Psi = st.StateChain(g1, g2, N, d, chi, algo)
        Psi = H.time_evolve(Psi, step_num, algo, fast_run=True)

        new_dens = 0
        for k in range(N):
            new_dens += M.expec(Psi, num_op, k)
        new_dens /= N

        # Only finds a new chemical potential if density deviates too much from
        # the goal density. Finds a guess which should lie on the other side
        # of goal density.
        if abs(new_dens - dens) / dens > rho_maxerr:
            if new_dens > dens:
                over = True
            else:
                over = False
            mu_guess = guess_mu(g2[1], g1[1], tperp, over, new_dens)
            g2[0] = new_mu(g1, [mu_guess, g2[1]], N, dt, d, chi, T, num_op,
                           g2[0], new_dens, rho_maxerr)

        mu_list.append(g2[0])

        #        new_ord_par = M.expec(Psi, a, int(N / 2))
        new_ord_par = orp_meas(Psi, a, N)

        print(abs(new_ord_par))
        err = abs((abs(ord_pars[i]) - abs(new_ord_par)) / abs(ord_pars[i]))
        g2[1] = abs(4 * new_ord_par * tperp)
        i += 1
        ord_pars.append(abs(new_ord_par))

        # Some print statements to see if the convergence is coming along
        if i % 20 == 0:
            print("Error in order parameter is:", err)
            print("Truncation error:", Psi.err)
    print("Error in order parameter is:", err)
    print("Truncation error:", Psi.err)

    return ord_pars, Psi, mu_list