<jupyter_start><jupyter_text>Quantum CircuitsQuantum computers can only use a specific set of gates (universal gate set). Given the entanglers and their amplitudes found in Step 3, one can find corresponding representation of these operators in terms of elementary gates using the following procedure.<jupyter_code>import tequila as tq
from utility import * #you will need this utility.py from week2
import time
#Running with cell goes over all possible optinons  
molecules=['h2','h4','lih','h2o','n2','nh3']
methods=['hf','cisd','ccsd','fci']
basies=['sto-3g','6-31g']
qubit_transfms=['jw','bk'] 
#Fcus on one molecule
molecules=['h2']
methods=['cisd']
basies=['sto-3g']
qubit_transfms=['jw'] #Jrdan-Wigner transformation is better
R=0.8 #bond_length

for mol in molecules:

    for basis in basies:

        for qubit_transf in qubit_transfms:
                    
            start=time.time()
            
            H = get_qubit_hamiltonian(mol, geometry=R, basis=basis, qubit_transf=qubit_transf)
            print("Take a look at H!")
            
            xyz_data = get_molecular_data(mol, geometry=R, xyz_format=True)
            Htq = tq.quantumchemistry.Molecule(geometry=xyz_data, basis_set=basis)

            E_cisd=obtain_PES(mol, [R], basis, 'cisd')
            print('cisd'.upper()," ground state energy is:",E_cisd)
            
            n_spin_orbitals=2*Htq.n_orbitals
            n_electrons=Htq.n_electrons
            print("Numbers of electrons:",n_electrons,
                  "\nUsing ",qubit_transf.upper()," in ",basis.upper(),"basis.",
                  "\nNumber of spin-orbitals (qubits):",n_spin_orbitals,
                  "\nNumber of Hamiltonian terms:",len(H.terms)) 

            H_eff=taper_hamiltonian(H, n_spin_orbitals, n_electrons, qubit_transf)
            print("Number of terms in Taper effective Hamiltonian (H_eff):",len(H_eff.terms))

            
            if mol=='h2':
                # Building the matrix representation of the effective Hamiltonian
                I, X, Z = np.identity(2), np.array([[0, 1], [1, 0]]), np.array([[1, 0], [0, -1]])
                H_matrix = -0.53105134 * I + 0.19679058 * X - 0.53505729 * Z

                # Obtain the eigenvalues
                eigvals, _ = np.linalg.eigh(H_matrix)
                print("The eigenvalues of the effective Hamiltonian:{}".format(eigvals))
            else:
                print("H_matrix for qubit-tapering technique not yet implemeneted for ",mol.upper())

            print(mol.upper(),"Hamiltonian using ",qubit_transf.upper()," in basis:",
                  basis.upper(),"generated for:",time.time()-start," sec\n")
            
            
H
H_eff
qwc_list = get_qwc_group(H)
print('Fragments 1: \n{}\n'.format(qwc_list[0]))
print('Fragments 2:\n{}\n'.format(qwc_list[1]))
print('Number of fragments: {}'.format(len(qwc_list)))
comm_groups = get_commuting_group(H)
print('Number of mutually commuting fragments: {}'.format(len(comm_groups)))
print('The first commuting group')
print(comm_groups[1])
print(comm_groups[2])
def imp_form4(allz):
    new_form={}
    for keys in allz.terms:
        eeee=list('eeee')
        for key in keys:
            eeee[key[0]]="z"
        new_form["".join(eeee)]=allz.terms[keys]
    return new_form 
def imp_form(allz,n_qubits): #n_spin_orbitals
    new_form={}
    for keys in allz.terms:
        eeee=['e' for i in range(n_qubits)]
        for key in keys:
            eeee[key[0]]="z"
        new_form["".join(eeee)]=allz.terms[keys]
    return new_form 
%%time
#One has to loop over all groups

new_form=[]
for i in range(1,len(comm_groups)+1):
    uqwc = get_qwc_unitary(comm_groups[i])
    qwc = remove_complex(uqwc * comm_groups[i] * uqwc)

    uz = get_zform_unitary(qwc)
    print("Checking whether U * U^+ is identity: {}".format(uz * uz))

    allz = remove_complex(uz * qwc * uz)
    print("\nThe all-z form of qwc fragment:\n{}".format(allz))
    new_form.append(imp_form(allz,n_spin_orbitals))
new_form
n_lements=0
all_togeter={}
for group in new_form:
    print(4,len(group),'real')
    for elemet in group:
        print(elemet,group[elemet])
        if elemet in all_togeter:
            temp=all_togeter[elemet]
            temp+=group[elemet]
            all_togeter[elemet]=temp
        else:
            n_lements+=1
            temp=group[elemet]
            all_togeter[elemet]=temp

print()        
print(n_spin_orbitals,n_lements,'real',"R=",R,"E[Eh]=",E_cisd[0]," Use the lines below only!")
for key in all_togeter:
    print(key,all_togeter[key])
-0.3796856633927857+0.15183385960079301
allz.__dict__
imp_form(allz,n_spin_orbitals)
#This is E0 at R=1 for h2 using H_eff
-0.531051349433764-0.5350572998841723<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13050811722538477, 'fraction_numerical': 0.029306346194391736, 'mean_word_length': 2.9798657718120807, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '431821', 'license': 'MIT', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1613, 'n_words': 361, 'path': 'Project_4_Ising_Annealer/Week2_Hamiltonian_gen.ipynb', 'repo_name': 'VGGatGitHub/CohortProject_2020_week1', 'size': '4743'}