From daa966eb35dd390585031da2422433b26f1d232f Mon Sep 17 00:00:00 2001 From: gonfeco Date: Tue, 31 Oct 2023 17:23:20 +0100 Subject: [PATCH] Minor reorganization of code --- tests/test_btc_02_ae.py | 2 +- tnbs/BTC_04_PH/PH/launch_vqe_step_fromfile.py | 52 +++++++++---- ...folder.py => launch_vqe_step_frominput.py} | 56 ++++---------- tnbs/BTC_04_PH/PH/vqe_step.py | 4 +- tnbs/BTC_04_PH/PH/vqe_step_fromfile.json | 19 ++--- tnbs/BTC_04_PH/PH/vqe_step_fromfile.py | 75 ++++++++----------- tnbs/BTC_04_PH/PH/vqe_step_fromfolder.json | 7 -- tnbs/BTC_04_PH/PH/vqe_step_frominput.json | 12 +++ ...ep_fromfolder.py => vqe_step_frominput.py} | 65 ++++++++++------ 9 files changed, 153 insertions(+), 139 deletions(-) rename tnbs/BTC_04_PH/PH/{launch_vqe_step_fromfolder.py => launch_vqe_step_frominput.py} (55%) delete mode 100644 tnbs/BTC_04_PH/PH/vqe_step_fromfolder.json create mode 100644 tnbs/BTC_04_PH/PH/vqe_step_frominput.json rename tnbs/BTC_04_PH/PH/{vqe_step_fromfolder.py => vqe_step_frominput.py} (66%) diff --git a/tests/test_btc_02_ae.py b/tests/test_btc_02_ae.py index f2839df..0ab23fd 100644 --- a/tests/test_btc_02_ae.py +++ b/tests/test_btc_02_ae.py @@ -180,7 +180,7 @@ def test_ae_rqae(): ae_bench.exe() filename = folder + benchmark_arguments["summary_results"] a = pd.read_csv(filename, header=[0, 1], index_col=[0, 1]) - print(a["absolute_error_sum"]["mean"]) + #print(a["absolute_error_sum"]["mean"]) assert((a["IntegralAbsoluteError"]["mean"] < 0.01).all()) #print(100* list(a['KS']['mean'])[0]) #assert(100* list(a['absolute_error_sum']['mean'])[0] < 1.0) diff --git a/tnbs/BTC_04_PH/PH/launch_vqe_step_fromfile.py b/tnbs/BTC_04_PH/PH/launch_vqe_step_fromfile.py index e605846..25e1812 100644 --- a/tnbs/BTC_04_PH/PH/launch_vqe_step_fromfile.py +++ b/tnbs/BTC_04_PH/PH/launch_vqe_step_fromfile.py @@ -3,13 +3,24 @@ Author: Gonzalo Ferro """ +import os import json -from utils_ph import combination_for_list -from vqe_step_fromfile import run_ph_execution +import pandas as pd +from utils_ph import get_filelist +from vqe_step_fromfolder import run_ph_execution +def list_files(folder, filelistname): + #filelist = os.listdir(folder) + filelist = list(pd.read_csv(filelistname, header=None)[0]) + final_files = [] + for file_ in filelist: + final_files = final_files + get_filelist(folder + file_+"/") + return final_files -def run_id(**configuration): +def run_id(basefn, **configuration): + configuration.update({"base_fn": basefn}) pdf = run_ph_execution(**configuration) + print(pdf) if __name__ == "__main__": import logging @@ -25,6 +36,20 @@ def run_id(**configuration): parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() + parser.add_argument( + "-filelist", + dest="filelist", + type=str, + default="./", + help="Filename with folder to use", + ) + parser.add_argument( + "-folder", + dest="folder", + type=str, + default="./", + help="Path for searching the folder", + ) parser.add_argument( "--count", dest="count", @@ -63,25 +88,24 @@ def run_id(**configuration): ) args = parser.parse_args() # Load json file - json_file = "vqe_step_fromfile.json" + json_file = "vqe_step_fromfolder.json" f_ = open(json_file) conf = json.load(f_) - # Creating Combination list - combination_list = combination_for_list(conf) - + print(conf) + files_list = list_files(args.folder, args.filelist) if args.print: if args.id is not None: - print(combination_list[args.id]) + print(files_list[args.id]) elif args.all: - print(combination_list) + print(files_list) else: print("Provide -id or --all") if args.count: - print("Number of elements: {}".format(len(combination_list))) + print("Number of elements: {}".format(len(files_list))) if args.execution: if args.id is not None: - configuration = combination_list[args.id] - run_id(**configuration) + configuration = files_list[args.id] + run_id(configuration, **conf) if args.all: - for configuration in combination_list: - run_id(**configuration) + for configuration in files_list: + run_id(configuration, **conf) diff --git a/tnbs/BTC_04_PH/PH/launch_vqe_step_fromfolder.py b/tnbs/BTC_04_PH/PH/launch_vqe_step_frominput.py similarity index 55% rename from tnbs/BTC_04_PH/PH/launch_vqe_step_fromfolder.py rename to tnbs/BTC_04_PH/PH/launch_vqe_step_frominput.py index 25e1812..91457d5 100644 --- a/tnbs/BTC_04_PH/PH/launch_vqe_step_fromfolder.py +++ b/tnbs/BTC_04_PH/PH/launch_vqe_step_frominput.py @@ -1,26 +1,15 @@ """ -For launching a VQE quantum step execution +For masive execution of VQE step using vqe_step_fromfile.py script Author: Gonzalo Ferro """ -import os import json -import pandas as pd -from utils_ph import get_filelist -from vqe_step_fromfolder import run_ph_execution +from utils_ph import combination_for_list +from vqe_step_fromfile import run_ph_execution -def list_files(folder, filelistname): - #filelist = os.listdir(folder) - filelist = list(pd.read_csv(filelistname, header=None)[0]) - final_files = [] - for file_ in filelist: - final_files = final_files + get_filelist(folder + file_+"/") - return final_files -def run_id(basefn, **configuration): - configuration.update({"base_fn": basefn}) +def run_id(**configuration): pdf = run_ph_execution(**configuration) - print(pdf) if __name__ == "__main__": import logging @@ -36,20 +25,6 @@ def run_id(basefn, **configuration): parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() - parser.add_argument( - "-filelist", - dest="filelist", - type=str, - default="./", - help="Filename with folder to use", - ) - parser.add_argument( - "-folder", - dest="folder", - type=str, - default="./", - help="Path for searching the folder", - ) parser.add_argument( "--count", dest="count", @@ -76,7 +51,7 @@ def run_id(basefn, **configuration): dest="print", default=False, action="store_true", - help="For printing the AE algorihtm configuration." + help="For printing the configuration." ) #Execution argument parser.add_argument( @@ -88,24 +63,25 @@ def run_id(basefn, **configuration): ) args = parser.parse_args() # Load json file - json_file = "vqe_step_fromfolder.json" + json_file = "vqe_step_frominput.json" f_ = open(json_file) conf = json.load(f_) - print(conf) - files_list = list_files(args.folder, args.filelist) + # Creating Combination list + combination_list = combination_for_list(conf) + if args.print: if args.id is not None: - print(files_list[args.id]) + print(combination_list[args.id]) elif args.all: - print(files_list) + print(combination_list) else: print("Provide -id or --all") if args.count: - print("Number of elements: {}".format(len(files_list))) + print("Number of elements: {}".format(len(combination_list))) if args.execution: if args.id is not None: - configuration = files_list[args.id] - run_id(configuration, **conf) + configuration = combination_list[args.id] + run_id(**configuration) if args.all: - for configuration in files_list: - run_id(configuration, **conf) + for configuration in combination_list: + run_id(**configuration) diff --git a/tnbs/BTC_04_PH/PH/vqe_step.py b/tnbs/BTC_04_PH/PH/vqe_step.py index a98c43c..e80889b 100644 --- a/tnbs/BTC_04_PH/PH/vqe_step.py +++ b/tnbs/BTC_04_PH/PH/vqe_step.py @@ -67,7 +67,9 @@ def run(self): if self.truncation is not None: index = abs(self.pauli_ph["PauliCoefficients"]) > self.truncation self.pauli_pdf = self.pauli_ph[index] - logger.debug("Additional truncation of Pauli Coeficients") + logger.debug( + "Additional truncation of Pauli Coeficients: {}".format( + len(self.pauli_pdf))) else: self.pauli_pdf = self.pauli_ph pauli_coefs = list(self.pauli_pdf["PauliCoefficients"]) diff --git a/tnbs/BTC_04_PH/PH/vqe_step_fromfile.json b/tnbs/BTC_04_PH/PH/vqe_step_fromfile.json index 6e721ad..1416f4b 100644 --- a/tnbs/BTC_04_PH/PH/vqe_step_fromfile.json +++ b/tnbs/BTC_04_PH/PH/vqe_step_fromfile.json @@ -1,12 +1,7 @@ -[ - { - "nqubits": [28, 29, 30], - "depth": [1], - "t_inv": [true], - "truncation": [null], - "nb_shots": [0], - "qpu_ph": ["qlmass"], - "save": [true], - "base_fn": ["/home/cesga/gferro/Codigo/TensorNetworks/Zalo"] - } -] +{ + "nb_shots": null, + "truncation": null, + "t_inv": true, + "qpu_ph": "c", + "save": false +} diff --git a/tnbs/BTC_04_PH/PH/vqe_step_fromfile.py b/tnbs/BTC_04_PH/PH/vqe_step_fromfile.py index 9a8331a..0b8c965 100644 --- a/tnbs/BTC_04_PH/PH/vqe_step_fromfile.py +++ b/tnbs/BTC_04_PH/PH/vqe_step_fromfile.py @@ -1,14 +1,23 @@ """ For executing a VQE quantum step of a ansatz and a given -Parent Hamiltonian -In this case the input are a folder, base_fn, and it is expected -that in the folder exist files with following pattern names - - nqubits_{}_depth_{}_parameters.csv - nqubits_{}_depth_{}_pauli.csv - -The nqubits and the depth is passed as input (keyowrd arguments) - +Parent Hamiltonian. +Here the input is a base file name that should have following pattern: + ansatz_{}_nqubits_{}_depth_{}_qpu_ansatz_{} +From this name following information should be extracted: +* ansatz_{}: the name of the ansatz +* nqubits_{}: the number of qubits for the ansatz +* depth_{}: the depth of the ansatz +* qpu_ansatz_{}: the qpu used for solving the ansatz + +For the input base file name following files has to exist: +* {}_parameters.csv +* {}_pauli.csv + +Here alwways talk about complete file names. The pattern can be found +in the bare name of the file, or in the folder that contain the file. +Example of valid names: +* ansatz_simple01_nqubits_27_depth_4_qpu_ansatz_qat_qlmass/8b961e30-5fc2-11ee-b12a-080038bfd786 +* ansatz_simple01_nqubits_16_depth_4_qpu_c.csv Author: Gonzalo Ferro """ @@ -16,44 +25,41 @@ import logging import ast import pandas as pd -from utils_ph import get_qpu +from utils_ph import get_info_basefn, get_qpu from ansatzes import ansatz_selector, angles_ansatz01 from vqe_step import PH_EXE logger = logging.getLogger("__name__") - def run_ph_execution(**configuration): """ Given an ansatz circuit, the parameters and the Pauli decomposition of the corresponding local PH executes a VQE step for computing the energy of the ansatz under the Hamiltonian that MUST BE near 0 + Given a Folder with following pattern: + * ansatz_{}_nqubits_{}_depth_{}_qpu_ansatz_{} + That contains files with following patter: + * {}_parameters.csv + * {}_pauli.csv Workflow: - * Getting the nqubits and depth from keyword arguments. - * Using them and the base_fn following file names are created: - base_fn + nqubits_{}_depth_{}_parameters.csv - base_fn + nqubits_{}_depth_{}_pauli.csv - * The Parameter are loading from parameters file. - * The QLM circuit is created (simplest ansatz) using - nqubits and depth. - * Parameters are loading in QLM circuit. - * The Pauli decomposition is loaded from pauli files. + * Create QLM circuit using the ansatz type readed from the folder + * Loading parameters for the circuit from: {}_parameters.csv + * Loading Pauli Decomposition from: {}_pauli.csv * Executes VQE step. + * Stores the result of the execution: {}_phexe.csv """ logger.info("Creating ansatz circuit") - nqubits = configuration["nqubits"] - depth = configuration["depth"] + base_fn = configuration["base_fn"] + print(base_fn) + depth, nqubits, ansatz = get_info_basefn(base_fn) ansatz_conf = { "nqubits" :nqubits, "depth" : depth, } - circuit = ansatz_selector("simple01", **ansatz_conf) + circuit = ansatz_selector(ansatz, **ansatz_conf) logger.info("Loading Parameters") - base_fn = configuration["base_fn"] + "/nqubits_{}_depth_{}".format( - str(nqubits).zfill(2), depth) - print(base_fn) parameters_pdf = pd.read_csv( base_fn + "_parameters.csv", sep=";", index_col=0) # Formating Parameters @@ -83,7 +89,6 @@ def run_ph_execution(**configuration): exe_ph.run() return exe_ph.pdf - if __name__ == "__main__": import logging logging.basicConfig( @@ -103,20 +108,6 @@ def run_ph_execution(**configuration): default="", help="Base Filename for Loading Files", ) - parser.add_argument( - "-nqubits", - dest="nqubits", - type=int, - help="Number of qbits for the ansatz.", - default=None, - ) - parser.add_argument( - "-depth", - dest="depth", - type=int, - help="Depth for ansatz.", - default=None, - ) parser.add_argument( "-nb_shots", dest="nb_shots", @@ -154,4 +145,4 @@ def run_ph_execution(**configuration): ) args = parser.parse_args() - run_ph_execution(**vars(args)) + print(run_ph_execution(**vars(args))) diff --git a/tnbs/BTC_04_PH/PH/vqe_step_fromfolder.json b/tnbs/BTC_04_PH/PH/vqe_step_fromfolder.json deleted file mode 100644 index 1416f4b..0000000 --- a/tnbs/BTC_04_PH/PH/vqe_step_fromfolder.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "nb_shots": null, - "truncation": null, - "t_inv": true, - "qpu_ph": "c", - "save": false -} diff --git a/tnbs/BTC_04_PH/PH/vqe_step_frominput.json b/tnbs/BTC_04_PH/PH/vqe_step_frominput.json new file mode 100644 index 0000000..6e721ad --- /dev/null +++ b/tnbs/BTC_04_PH/PH/vqe_step_frominput.json @@ -0,0 +1,12 @@ +[ + { + "nqubits": [28, 29, 30], + "depth": [1], + "t_inv": [true], + "truncation": [null], + "nb_shots": [0], + "qpu_ph": ["qlmass"], + "save": [true], + "base_fn": ["/home/cesga/gferro/Codigo/TensorNetworks/Zalo"] + } +] diff --git a/tnbs/BTC_04_PH/PH/vqe_step_fromfolder.py b/tnbs/BTC_04_PH/PH/vqe_step_frominput.py similarity index 66% rename from tnbs/BTC_04_PH/PH/vqe_step_fromfolder.py rename to tnbs/BTC_04_PH/PH/vqe_step_frominput.py index 4682daf..1c3d6f5 100644 --- a/tnbs/BTC_04_PH/PH/vqe_step_fromfolder.py +++ b/tnbs/BTC_04_PH/PH/vqe_step_frominput.py @@ -1,12 +1,15 @@ """ For executing a VQE quantum step of a ansatz and a given -Parent Hamiltonian. -In this case the input are a folder with the following pattern name: - ansatz_{}_nqubits_{}_depth_{}_qpu_ansatz_{} -Inside the folder it is expected that following pattern files can be -found: - {}_parameters.csv - {}_pauli.csv +Parent Hamiltonian +In this case the inputs are: +1. Number of the qubits of the ansatz (nqubits, keyword). +2. Depth of the ansatz (depth, keyword). +3. base_fn: folder where the csv with parameters and pauli decomposition + is stored. Patterns for the name of these files: + * nqubits_{}_depth_{}_parameters.csv + * nqubits_{}_depth_{}_pauli.csv +The keytowrd inputs (nqubits, depth) are used for building the number of +the parameters and pauli decomposition files. Author: Gonzalo Ferro """ @@ -14,41 +17,44 @@ import logging import ast import pandas as pd -from utils_ph import get_info_basefn, get_qpu +from utils_ph import get_qpu from ansatzes import ansatz_selector, angles_ansatz01 from vqe_step import PH_EXE logger = logging.getLogger("__name__") + def run_ph_execution(**configuration): """ Given an ansatz circuit, the parameters and the Pauli decomposition of the corresponding local PH executes a VQE step for computing the energy of the ansatz under the Hamiltonian that MUST BE near 0 - Given a Folder with following pattern: - * ansatz_{}_nqubits_{}_depth_{}_qpu_ansatz_{} - That contains files with following patter: - * {}_parameters.csv - * {}_pauli.csv Workflow: - * Create QLM circuit using the ansatz type readed from the folder - * Loading parameters for the circuit from: {}_parameters.csv - * Loading Pauli Decomposition from: {}_pauli.csv + * Getting the nqubits and depth from keyword arguments. + * Using them and the base_fn following file names are created: + base_fn + nqubits_{}_depth_{}_parameters.csv + base_fn + nqubits_{}_depth_{}_pauli.csv + * The Parameter are loading from parameters file. + * The QLM circuit is created (simplest ansatz) using + nqubits and depth. + * Parameters are loading in QLM circuit. + * The Pauli decomposition is loaded from pauli files. * Executes VQE step. - * Stores the result of the execution: {}_phexe.csv """ logger.info("Creating ansatz circuit") - base_fn = configuration["base_fn"] - print(base_fn) - depth, nqubits, ansatz = get_info_basefn(base_fn) + nqubits = configuration["nqubits"] + depth = configuration["depth"] ansatz_conf = { "nqubits" :nqubits, "depth" : depth, } - circuit = ansatz_selector(ansatz, **ansatz_conf) + circuit = ansatz_selector("simple01", **ansatz_conf) logger.info("Loading Parameters") + base_fn = configuration["base_fn"] + "/nqubits_{}_depth_{}".format( + str(nqubits).zfill(2), depth) + print(base_fn) parameters_pdf = pd.read_csv( base_fn + "_parameters.csv", sep=";", index_col=0) # Formating Parameters @@ -78,6 +84,7 @@ def run_ph_execution(**configuration): exe_ph.run() return exe_ph.pdf + if __name__ == "__main__": import logging logging.basicConfig( @@ -97,6 +104,20 @@ def run_ph_execution(**configuration): default="", help="Base Filename for Loading Files", ) + parser.add_argument( + "-nqubits", + dest="nqubits", + type=int, + help="Number of qbits for the ansatz.", + default=None, + ) + parser.add_argument( + "-depth", + dest="depth", + type=int, + help="Depth for ansatz.", + default=None, + ) parser.add_argument( "-nb_shots", dest="nb_shots", @@ -134,4 +155,4 @@ def run_ph_execution(**configuration): ) args = parser.parse_args() - print(run_ph_execution(**vars(args))) + run_ph_execution(**vars(args))