<jupyter_start><jupyter_text>Convert into Openfermion format<jupyter_code>import conversion_scripts as conv_scr
updated_hamiltonians={}
for key in csvqe_results.keys(): # <- note using csvqe_results (has less molecules than hamiltonians.keys())
    

    ham_noncon = hamiltonians[key][3]
    ep_state = [list(hamiltonians[key][5][1]), list(hamiltonians[key][5][2])] # ground state of noncontextual Ham
    ham = hamiltonians[key][2]

    model = c.quasi_model(ham_noncon)
    fn_form = c.energy_function_form(ham_noncon, model)
    
#     _, _, _, ham_out, ham_noncon_out = c.diagonalize_G(model,fn_form,ep_state, ham, ham_noncon)

    N_qubits = hamiltonians[key][1]
    order= csvqe_results[key][-1] # <-- order specified from greedy run by Will
    qubit_removal_order=copy(order)
    
    reduced_Con_hamiltonians = c.get_reduced_hamiltonians(ham ,model, fn_form, ep_state, order)
    
    
    Contextual_Hamiltonian_list=[]
    qNo_list=[]
    for qNo, Ham in enumerate(reduced_Con_hamiltonians):            
#         if c.contextualQ_ham(Ham) is False:
#             raise ValueError('have not found contextual H')
        Ham_Openf = conv_scr.Get_Openfermion_Hamiltonian(Ham)
        Contextual_Hamiltonian_list.append(list(Ham_Openf))
        qNo_list.append(qNo)
    
    updated_hamiltonians[key] = {
                                'encoding':hamiltonians[key][0],
                                'n_qubits': hamiltonians[key][1],
                                'full_H': conv_scr.Get_Openfermion_Hamiltonian(hamiltonians[key][2]),
                                'nonC_H_greedyDFS': conv_scr.Get_Openfermion_Hamiltonian(hamiltonians[key][3]),
                                'FCI':hamiltonians[key][4],                         
                                'gstate_noncon':hamiltonians[key][5], 
                                'Contextual_Hamiltonian_list': Contextual_Hamiltonian_list, #ham_out,   
                                'Contextual_Hamiltonian_qubitNo_list': qNo_list,  
                                'non_Contextual_H': conv_scr.Get_Openfermion_Hamiltonian(ham_noncon),# ham_noncon_out, 
                                'qubit_removal_order':qubit_removal_order
                             }
print(updated_hamiltonians['Mg1_STO-3G_singlet']['qubit_removal_order'])
# updated_hamiltonians['Mg1_STO-3G_singlet']['Contextual_Hamiltonian_list']
# checking keys match!
for key in updated_hamiltonians.keys():
    if key not in csvqe_results.keys():
        raise ValueError(f'{key} not in csvqe_results')
import tqdm
from tqdm.notebook import tqdm
from quchem.Unitary_Partitioning.Graph import Clique_cover_Hamiltonian
unitary_paritioning_of_Con_H={}

commutativity_flag = 'AC' ## <- defines relationship between sets!!!
plot_graph = False
Graph_colouring_strategy='largest_first'


for key in tqdm(list(updated_hamiltonians.keys())):
    
    H_con_List = updated_hamiltonians[key]['Contextual_Hamiltonian_list']
    
    
    anti_commuting_different_red_list=[]
    for i, H_con in enumerate(H_con_List):
        n_qubits = updated_hamiltonians[key]['Contextual_Hamiltonian_qubitNo_list'][i]
        anti_commuting_sets = Clique_cover_Hamiltonian(H_con, 
                                                             n_qubits, 
                                                             commutativity_flag, 
                                                             Graph_colouring_strategy)
        
        anti_commuting_different_red_list.append(anti_commuting_sets)
    
    H_con_reduced_and_antiC_set = list(zip(H_con_List, anti_commuting_different_red_list))
    
    unitary_paritioning_of_Con_H[key] = H_con_reduced_and_antiC_set
for key in unitary_paritioning_of_Con_H:
    
    H_con_and_antiC_set_list_different_reductions = unitary_paritioning_of_Con_H[key]
    
    for H_con, AC in H_con_and_antiC_set_list_different_reductions:
        AC = len(AC)
        standard=len(list(H_con))
        print(f'AC : {AC: <6} vs standard: {standard: >6}')
    
    print('\n')
import pickle
import datetime
unique_file_id = datetime.datetime.now().strftime('%Y%b%d-%H%M%S%f')
output_dir = os.path.join(working_dir, 'Pickle_out')

file_name1 = 'EXP_ordering_anticommuting_vs_standard_conH_{}.pickle'.format(unique_file_id)

file_out1=os.path.join(output_dir, file_name1)
with open(file_out1, 'wb') as outfile:
    pickle.dump(unitary_paritioning_of_Con_H, outfile)

file_name2 = 'EXP_ordering_updated_hamiltonians_{}.pickle'.format(unique_file_id)
file_out2=os.path.join(output_dir, file_name2)
with open(file_out2, 'wb') as outfile:
    pickle.dump(updated_hamiltonians, outfile)
## test if pickle worked
file_name = 'EXP_ordering_anticommuting_vs_standard_conH.pickle'
with open(file_name, 'rb') as infile:
    loaded_dict = pickle.load(infile)
    

updated_hamiltonians['H3_STO-3G_singlet_1+']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0932621199671323, 'fraction_numerical': 0.006368118323746919, 'mean_word_length': 3.060884070058382, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '529154', 'license': 'MIT', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1614, 'n_words': 231, 'path': 'Projects/CS_VQE/jupyter_notebooks/UNITARY_PARTITIONING_paper_ordering.ipynb', 'repo_name': 'AlexisRalli/VQE-code', 'size': '4868'}