def run_exp_ham(num_qubits=1,
                t=1,
                num_tests=1,
                test_custom_sparse=False,
                test_custom=False,
                test_linalg=False):
    used_memory = virtual_memory().used

    for i in range(num_tests):

        if print_mem_status and i > 0:
            print("i=", i, "\t System memory used loop : ",
                  virtual_memory().used)

        ham = h.random_hamiltonian(num_qubits)
        t = random.random()

        if test_custom_sparse:
            expd = h.exp_ham(ham, t, enable_sparse_functionality=True)
        elif test_custom:
            expd = h.exp_ham(ham, t, enable_sparse_functionality=False)
        elif test_linalg:
            expd = linalg.expm(-1j * ham * t)
        else:
            print("Must choose one method to test")

        del ham
        del expd
Пример #2
0
def pr0fromScipyNC(tvec, modpar, exppar, oplist, probestate, Hp = None, trotterize=True, use_exp_ham=ham_exp_installed):
    """Generic version to be adopted in case oplist includes non-commutative operators"""
#    use_exp_ham = True
#    print("pr0fromScipyNC using exp_ham: ", use_exp_ham)
    print_exp_ham=True
    evo = np.empty([len(modpar), len(tvec)])
    #dimension check
    if len(np.shape(oplist)) != 3:
        raise IndexError('OperatorList has the wrong shape')
    
    if not(all(np.shape(modpar)[1] == np.repeat(np.shape(oplist)[0], len(modpar)))):
        raise AttributeError('Shapes of OperatorList and Parameters do not match')

    #evolution for the system experimental parameters
    Hm = getH(exppar, oplist)
    #print(Hm)
   
    if Hp is None or len(modpar)>1:
        trueEvo = False		# call the system with the tested Hamiltonian (or the simulator Hamiltonian for particles)
        #print("Calling the false Hamiltonian")
    else:
        trueEvo = True		# call the system with the "true" Hamiltonian
        #print("Calling the true Hamiltonian")
    
    for evoidx in range(len(modpar)):
        #evolution for the system and particles in the simulator, assuming trueHam = simHam
        if not trueEvo:
            Hp = getH(modpar[evoidx, np.newaxis], oplist)
        #print(Hp)
        
        for idt in range(len(tvec)):
            
            if trotterize is False:
                if use_exp_ham:
                  backstate = np.dot(h.exp_ham(Hm, tvec[idt], plus_or_minus=1.0, print_method=print_exp_ham), probestate)
                  evostate = np.dot(h.exp_ham(Hp, tvec[idt], plus_or_minus=1.0,print_method=print_exp_ham), backstate)
                else: 
                  backstate = np.dot(sp.linalg.expm((1j)*tvec[idt]*Hm), probestate)
                  evostate = np.dot(sp.linalg.expm(-(1j)*tvec[idt]*Hp), backstate)
                #print('Evostate: ', evostate)
                
            else:
                # print('trotter')
                if use_exp_ham:
                  evostate = np.dot(h.exp_ham(Hp-Hm, tvec[idt], plus_or_minus=1.0,print_method=print_exp_ham), probestate)
                else: 
                  evostate = np.dot(sp.linalg.expm(-(1j)*tvec[idt]*(Hp-Hm)), probestate)

            evo[evoidx][idt] = np.abs(np.dot(evostate.conj(), probestate.T)) ** 2         
    
    return evo
Пример #3
0
def run_exp_ham(num_qubits=1, t=1, num_tests=1, use_sparse=True):
    if use_sparse == True:
        print("\n\nSPARSE")
    elif use_sparse == False:
        print("\n\nNONSPARSE")
    else:
        print("use_sparse must be True or False")

    for i in range(num_tests):
        print("Num qubits : ", num_qubits)
        print("Line ", lineno(), "\t Memory % used : ", virtual_memory().free)
        ham = h.random_hamiltonian(num_qubits)
        print("Line ", lineno(), "\t Memory % used : ", virtual_memory().free)
        expd = h.exp_ham(ham, t, enable_sparse_functionality=use_sparse)
        print("Line ", lineno(), "\t Memory % used : ", virtual_memory().free)
        del ham
        del expd
        print("Line ", lineno(), "\t Memory % used : ", virtual_memory().free)
def run_exp_ham(num_qubits=1, t=1, num_tests=1):
    used_memory = virtual_memory().used

    for i in range(num_tests):

        if print_mem_status:
            print("Line ", lineno(), "\t Memory % used : ",
                  virtual_memory().used - used_memory)
        used_memory = virtual_memory().used
        ham = h.random_hamiltonian(num_qubits)

        #        if print_mem_status: print("Line ", lineno(), "\t Memory % used : ", virtual_memory().used - used_memory)
        #        used_memory = virtual_memory().used

        expd = h.exp_ham(ham, t, enable_sparse_functionality=True)
        #        expd = h.exp_ham(ham, t, enable_sparse_functionality=False)
        #        expd = linalg.expm(-1j*ham*t)
        #        if print_mem_status: print("Line ", lineno(), "\t Memory % used : ", virtual_memory().used - used_memory)
        #        used_memory = virtual_memory().used

        del ham
        del expd
Пример #5
0
def get_prob(t, op_list, param_list, use_linalg=1):
    #use_linalg=1
    compare_custom_linalg = True
    prec = 1e-25

    probe_preiodicity = 50
    num_terms = len(param_list)
    hamiltonian = 0
    for i in range(num_terms):
        hamiltonian += param_list[i] * op_list[i, :, :]
        # would have to take exponentials here
        # write new fnc: hamiltonian_build_and_exp
        # - take op_list and param_list, compute e_{-iH1 dt}^n * ... * e_{-iHm dt}^n

    normalised_probe = np.array([probes[probe_counter % num_probes]])
    probe_bra = normalised_probe
    probe_ket = np.transpose(normalised_probe)

    if compare_custom_linalg:
        linalg = sp.linalg.expm(-1.j * hamiltonian * t)
        custom = h.exp_ham(hamiltonian,
                           t,
                           plus_or_minus=-1.0,
                           precision=prec,
                           scalar_cutoff=exp_scalar_cutoff,
                           print_method=True)
        print("Matrix diff: %.1E mtx" % np.max(np.abs(linalg - custom)))

        lin_u_probe = linalg @ probe_ket  # perform unitary matrix of hamiltonian on ket form of probe state
        probe_u_probe = probe_bra @ lin_u_probe  # multiply be bra form of probe
        lin_probability = abs(probe_u_probe)**2

        cust_u_probe = custom @ probe_ket  # perform unitary matrix of hamiltonian on ket form of probe state
        probe_u_probe = probe_bra @ cust_u_probe  # multiply be bra form of probe
        cust_probability = abs(probe_u_probe)**2

        if (np.abs(lin_probability - cust_probability) > 1e-5):
            print("----------- High diff -----------")
        print("Diff %.1E pr" % np.abs(lin_probability - cust_probability))
        if use_linalg == 1:
            probability = lin_probability
        else:
            probability = cust_probability

    else:
        if use_linalg == 1:
            mtx = (-1.j) * hamiltonian * t
            unitary_mtx = sp.linalg.expm(mtx)
        else:
            unitary_mtx = h.exp_ham(hamiltonian,
                                    t,
                                    plus_or_minus=-1.0,
                                    precision=prec,
                                    scalar_cutoff=exp_scalar_cutoff,
                                    print_method=True)


#      normalised_probe = np.array([probes[probe_counter%num_probes]])
#      probe_bra = normalised_probe
#      probe_ket = np.transpose(normalised_probe)
        u_probe = unitary_mtx @ probe_ket  # perform unitary matrix of hamiltonian on ket form of probe state
        probe_u_probe = probe_bra @ u_probe  # multiply be bra form of probe
        probability = abs(probe_u_probe)**2

    return float(probability)
Пример #6
0
import yep
import hamiltonian_exponentiation as h

num_qubits = 2
ham = h.random_hamiltonian(num_qubits)
t = 1

yep.start('yep_output.prof')
h.exp_ham(ham, t)
yep.stop()