diff --git a/tnbs/BTC_04_PH/PH/notebooks/02_Using_PH_Class.ipynb b/tnbs/BTC_04_PH/PH/notebooks/02_Using_PH_Class.ipynb index 960c4e9..946422c 100644 --- a/tnbs/BTC_04_PH/PH/notebooks/02_Using_PH_Class.ipynb +++ b/tnbs/BTC_04_PH/PH/notebooks/02_Using_PH_Class.ipynb @@ -929,7 +929,7 @@ }, { "cell_type": "markdown", - "id": "a95ab5cc", + "id": "e4cdf3b2", "metadata": {}, "source": [ "## 4. run_parent_hamiltonian function\n", @@ -944,7 +944,7 @@ }, { "cell_type": "markdown", - "id": "f97d6e0f", + "id": "1cae3e40", "metadata": {}, "source": [ "## 5. Command Line execution\n", @@ -963,7 +963,7 @@ }, { "cell_type": "markdown", - "id": "016545ec", + "id": "0d4604b0", "metadata": {}, "source": [ "## 6. Massive PH computations\n", @@ -982,6 +982,14 @@ " * **--print**: for printing the parent hamiltonian configuration.\n", " * **--exe**: for executing the parent hamiltonian execution indicated by **--all** or by **-id ID**." ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d665bba2", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/tnbs/BTC_04_PH/PH/notebooks/03_ParentHamiltonian_execution.ipynb b/tnbs/BTC_04_PH/PH/notebooks/03_ParentHamiltonian_execution.ipynb index 902067a..998417a 100644 --- a/tnbs/BTC_04_PH/PH/notebooks/03_ParentHamiltonian_execution.ipynb +++ b/tnbs/BTC_04_PH/PH/notebooks/03_ParentHamiltonian_execution.ipynb @@ -7,7 +7,7 @@ "source": [ "# Using PH_EXE class\n", "\n", - "This notebook explains how to use the **PH_EXE** class inside *execution_ph* module. \n", + "This notebook explains how to use the **PH\\_EXE** class inside **vqe\\_step** module. \n", "\n", "This class, given an ansatz and its parent hamiltonian, provided as a string Pauli decomposition, computes the asociated ground state energy (that should be near zero)." ] @@ -84,9 +84,8 @@ "source": [ "Before doing the siumulation we need to perform several steps:\n", "\n", - "1. Ansatz Definition\n", - "2. Ansatz Solving\n", - "3. Parent Hamiltonian Computation" + "1. Ansatz Definition and Solving (doing with functions from **ansatzes** module).\n", + "2. Parent Hamiltonian Computation (doing with functions from **parent\\_hamiltonian** module)" ] }, { @@ -94,9 +93,19 @@ "id": "206806ed", "metadata": {}, "source": [ - "## 1. Ansatz Definition\n", + "## 1. Ansatz Definition and Solving\n", "\n", - "First we need to create the myqlm cirucit implementation of the desired ansatz (see notebook **01_Ansatzes.ipynb**) " + "We neeed to define an ansatz circuit and solving it for getting its state (see **01_Ansatzes.ipynb**. We can use the functions from **ansatzes** module" + ] + }, + { + "cell_type": "markdown", + "id": "fe58f7a8", + "metadata": {}, + "source": [ + "### Ansatz definition\n", + "\n", + "First we define an ansatz circuit" ] }, { @@ -175,7 +184,7 @@ "id": "1dfdb9e4", "metadata": {}, "source": [ - "## 2. Ansatz Solution\n", + "### Ansatz Solution\n", "\n", "Now we need to fix the parameters of the ansatz and solving it(see notebook **01_Ansatzes.ipynb**)" ] @@ -238,9 +247,9 @@ "id": "31ee289c", "metadata": {}, "source": [ - "## 3. Parent Hamiltonian Computation\n", + "## 2. Parent Hamiltonian Computation\n", "\n", - "With the obtained state of the ansatz we need to compute the Parent Hamiltonian and get its Pauli string decomposition." + "With the obtained state of the ansatz we need to compute the Parent Hamiltonian and get its Pauli string decomposition. Here we use the **parent\\_hamiltonian** module (see notebook **02_Using_PH_Class.ipynb** for more information)" ] }, { @@ -323,7 +332,7 @@ "metadata": {}, "outputs": [], "source": [ - "from execution_ph import PH_EXE" + "from vqe_step import PH_EXE" ] }, { @@ -403,6 +412,75 @@ "source": [ "If save was required the complete information (axis 1 concatenation of *exe_ph.pdf_result* and *exe_ph.pdf_result*) will be saved as: folder + filename+*_phexe.csv*" ] + }, + { + "cell_type": "markdown", + "id": "55bd78eb", + "metadata": {}, + "source": [ + "## 5. run_ph_execution\n", + "\n", + "Inside the module **vqe_step** the **run_ph_execution** function can be found. This function uses typical **CSV** files from **ansatzes** and **parent_hamiltonian** modules for executing a **VQE** step. The input of this function is a **kwargs** (called **configuration**) where following keywords are processed:\n", + "\n", + "* base_fn: Base name for the files generated by **ansatzes** and **parent_hamiltonian**. The pattern of the name must be the following: **ansatz\\_{}\\_nqubits\\_{}\\_depth\\_{}\\_qpu_ansatz\\_{}**. It is mandatory that following files exist:\n", + " * **ansatz\\_{}\\_nqubits\\_{}\\_depth\\_{}\\_qpu_ansatz\\_{}\\_parameter.csv**: with the angles of the ansatz\n", + " * **ansatz\\_{}\\_nqubits\\_{}\\_depth\\_{}\\_qpu_ansatz\\_{}\\_pauli.csv**: with the Pauli decomposition of the **PH**.\n", + " * From the *base_fn* following variables are extracted: \n", + " * ansatz\n", + " * nqubits\n", + " * depth\n", + "* qpu_ph: name with the qpu for executing the **VQE** step simulation. Valid names: [qlmass, python, c].\n", + "* nb_shots: for setting the number of shots for the **VQE** step. Only valid for **nb_shots=qlmass**\n", + "* t_inv: True if the ansatz is transaltional invariant.\n", + "* truncation: integer for tuncating the Pauli coefficients\n", + "* save: for saving the results. The results will be saved with the followi9ng pattern:\n", + " * **ansatz\\_{}\\_nqubits\\_{}\\_depth\\_{}\\_qpu_ansatz\\_{}\\_ph\\_exe.csv**" + ] + }, + { + "cell_type": "markdown", + "id": "1950d87c", + "metadata": {}, + "source": [ + "## 6. Command Line execution\n", + "\n", + "The **run_ph_execution** function from module **vqe_step** can be executed from command line. Several arguments can be passed for configuring the **VQE** step computation. A help can be obtained by:\n", + "\n", + "**python vqe_step.py -h**\n", + "\n", + "Arguments can be:\n", + "\n", + "* -basefn BASE_FN: Base Filename for Loading Files\n", + "* -nb_shots NB_SHOTS Number of shots\n", + "* -truncation TRUNCATION: Truncation for Pauli coeficients.\n", + "* -qpu_ph QPU_PH: QPU for **VQE** simulation: [qlmass, python, c]\n", + "* --t_inv: Setting translational invariant of the ansatz\n", + "* --save: For storing results" + ] + }, + { + "cell_type": "markdown", + "id": "1eea57b0", + "metadata": {}, + "source": [ + "## 7. Massive VQE step computations\n", + "\n", + "As for the case of masive ansatz state and parent hamiltonian computations (see Notebook 01_Ansatzes.ipynb and 02_Using_PH_Class.ipynb) we can execute masive **VQE** step computations.For this the 2 following files can be used:\n", + "\n", + "* **vqe\\_step.json**: JSON file with the configuration for the **VQE** step computations. For each desired computation a complete dictionary should be provided. The keys are:\n", + " * save: for saving or not the results\n", + " * t_inv: for setting if the ansatz is, or not, translational invariant.\n", + " * base_fn: base name for the inputs files.\n", + " * nb_shots: number of shots for **VQE** step.\n", + " * qpu_ph: qpu for executing **VQE** step.\n", + " * truncation: for tuncating Pauli coefficients.\n", + "* **launch_vqe_step.py**: This scripts procces the before **JSON** file creating a complete list of all posible **VQE** calculations. By providing different arguments a selected configuration (or all of them) can be chosen. For getting help use: **launch_vqe_step.py -h**. Following arguments can be provided:\n", + " * **--count**: Getting the number of posible **VQE** steps from the **vqe\\_stepjson** JSON file.\n", + " * **--all**: for selecting all the posible **VQE** steps from the **vqe\\_stepjson** JSON file.\n", + " * **-id ID**: for selecting a single (the **ID** one) **VQE** step from the **vqe\\_stepjson** JSON file.\n", + " * **--print**: for printing the **VQE** step configuration.\n", + " * **--exe**: for executing the **VQE** execution indicated by **--all** or by **-id ID**." + ] } ], "metadata": { diff --git a/tnbs/BTC_04_PH/PH/vqe_step.json b/tnbs/BTC_04_PH/PH/vqe_step.json index f315f76..9a540e8 100644 --- a/tnbs/BTC_04_PH/PH/vqe_step.json +++ b/tnbs/BTC_04_PH/PH/vqe_step.json @@ -1,7 +1,6 @@ [ { "save": [true], - "t_inv": [true], "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_1_qpu_ansatz_c"], "nb_shots": [null], "truncation": [null], @@ -11,7 +10,6 @@ }, { "save": [true], - "t_inv": [true], "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_2_qpu_ansatz_c"], "nb_shots": [null], "truncation": [null], @@ -21,7 +19,6 @@ }, { "save": [true], - "t_inv": [true], "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_3_qpu_ansatz_c"], "nb_shots": [null], "truncation": [null], @@ -31,7 +28,6 @@ }, { "save": [true], - "t_inv": [true], "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_4_qpu_ansatz_c"], "nb_shots": [null], "truncation": [null], @@ -41,7 +37,6 @@ }, { "save": [true], - "t_inv": [true], "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_1_qpu_ansatz_c"], "nb_shots": [null], "truncation": [null], @@ -51,7 +46,6 @@ }, { "save": [true], - "t_inv": [true], "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_2_qpu_ansatz_c"], "nb_shots": [null], "truncation": [null], @@ -61,7 +55,6 @@ }, { "save": [true], - "t_inv": [true], "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_3_qpu_ansatz_c"], "nb_shots": [null], "truncation": [null], @@ -71,7 +64,6 @@ }, { "save": [true], - "t_inv": [true], "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_4_qpu_ansatz_c"], "nb_shots": [null], "truncation": [null], diff --git a/tnbs/BTC_04_PH/PH/vqe_step_fromfile.json b/tnbs/BTC_04_PH/PH/vqe_step_fromfile.json deleted file mode 100644 index 1416f4b..0000000 --- a/tnbs/BTC_04_PH/PH/vqe_step_fromfile.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_fromfile.py b/tnbs/BTC_04_PH/PH/vqe_step_fromfile.py deleted file mode 100644 index 97ae929..0000000 --- a/tnbs/BTC_04_PH/PH/vqe_step_fromfile.py +++ /dev/null @@ -1,163 +0,0 @@ -""" -For executing a VQE quantum step of a ansatz and a given -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 -""" - -import logging -import ast -import re -import pandas as pd -from utils_ph import get_qpu -from ansatzes import ansatz_selector, angles_ansatz01 -from vqe_step import PH_EXE - -logger = logging.getLogger("__name__") - -def get_info_basefn(base_fn): - depth = int(re.findall(r"depth_(.*)_qpu", base_fn)[0]) - nqubits = int(re.findall(r"nqubits_(.*)_depth_", base_fn)[0]) - ansatz = re.findall(r"ansatz_(.*)_nqubits", base_fn)[0] - return depth, nqubits, ansatz - -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 an input base_fn that MUST have following pattern: - * base_fn = ansatz_{}_nqubits_{}_depth_{}_qpu_ansatz_{} - Additionally folowing files MUST exist: - * {base_fn}_parameters.csv - * {base_fn}_pauli.csv - The functions gets the information about: ansatz, nqubits and depth - and executes the following Workflow: - 1. Create QLM circuit using the ansatz type readed from the folder - 2 Loading parameters for the circuit from: {}_parameters.csv - 3. Loading Pauli Decomposition from: {}_pauli.csv - 4. Executes VQE step. - If save is True the result of the execution is stored as: - * {base_fn}_phexe.csv - """ - - # 1. Create QLM circuit using the ansatz type readed from the folder - logger.info("Creating ansatz circuit") - base_fn = configuration["base_fn"] - depth, nqubits, ansatz = get_info_basefn(base_fn) - text = "ansatz: {0}, nqubits: {1} depth: {2}".format(ansatz, nqubits, depth) - logger.debug(text) - ansatz_conf = { - "nqubits" :nqubits, - "depth" : depth, - } - circuit = ansatz_selector(ansatz, **ansatz_conf) - - # 2 Loading parameters for the circuit from: {}_parameters.csv - text = "Loading Parameters from: {}".format(base_fn + "_parameters.csv") - logger.info(text) - parameters_pdf = pd.read_csv( - base_fn + "_parameters.csv", sep=";", index_col=0) - # Formating Parameters - circuit, _ = angles_ansatz01(circuit, parameters_pdf) - # from qat.core.console import display - # display(circuit) - - # 3. Loading Pauli Decomposition from: {}_pauli.csv - text = "Loading PH Pauli decomposition from: {}".format( - base_fn + "_parameters.csv") - logger.info(text) - # Loading Pauli - pauli_pdf = pd.read_csv( - base_fn + "_pauli.csv", sep=";", index_col=0) - affected_qubits = [ast.literal_eval(i_) for i_ in list(pauli_pdf["Qbits"])] - pauli_pdf["Qbits"] = affected_qubits - - # 4. Executes VQE step. - logger.info("Executing VQE step") - vqe_conf = { - "qpu" : get_qpu(configuration["qpu_ph"]), - "nb_shots": configuration["nb_shots"], - "truncation": configuration["truncation"], - "t_inv": configuration["t_inv"], - "filename": base_fn, - "save": configuration["save"], - } - exe_ph = PH_EXE(circuit, pauli_pdf, nqubits, **vqe_conf) - exe_ph.run() - return exe_ph.pdf - -if __name__ == "__main__": - import logging - logging.basicConfig( - format='%(asctime)s-%(levelname)s: %(message)s', - datefmt='%m/%d/%Y %I:%M:%S %p', - #level=logging.INFO - level=logging.DEBUG - ) - logger = logging.getLogger('__name__') - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument( - "-basefn", - dest="base_fn", - type=str, - default="", - help="Base Filename for Loading Files", - ) - parser.add_argument( - "-nb_shots", - dest="nb_shots", - type=int, - help="Number of shots", - default=0, - ) - parser.add_argument( - "-truncation", - dest="truncation", - type=int, - help="Truncation for Pauli coeficients.", - default=None, - ) - parser.add_argument( - "-qpu_ph", - dest="qpu_ph", - type=str, - default=None, - help="QPU for parent hamiltonian simulation: [qlmass, python, c]", - ) - parser.add_argument( - "--t_inv", - dest="t_inv", - default=False, - action="store_true", - help="Setting translational invariant of the ansatz", - ) - parser.add_argument( - "--save", - dest="save", - default=False, - action="store_true", - help="For storing results", - ) - args = parser.parse_args() - - print(run_ph_execution(**vars(args))) diff --git a/tnbs/BTC_04_PH/PH/vqe_step_frominput.json b/tnbs/BTC_04_PH/PH/vqe_step_frominput.json deleted file mode 100644 index 6e721ad..0000000 --- a/tnbs/BTC_04_PH/PH/vqe_step_frominput.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "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_frominput.py b/tnbs/BTC_04_PH/PH/vqe_step_frominput.py deleted file mode 100644 index 1c3d6f5..0000000 --- a/tnbs/BTC_04_PH/PH/vqe_step_frominput.py +++ /dev/null @@ -1,158 +0,0 @@ -""" -For executing a VQE quantum step of a ansatz and a given -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 -""" - -import logging -import ast -import pandas as pd -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 - 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. - * Executes VQE step. - """ - - logger.info("Creating ansatz circuit") - nqubits = configuration["nqubits"] - depth = configuration["depth"] - ansatz_conf = { - "nqubits" :nqubits, - "depth" : depth, - } - 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 - circuit, _ = angles_ansatz01(circuit, parameters_pdf) - #from qat.core.console import display - #display(circuit) - - # Loading PH Pauli decomposition - logger.info("Loading PH Pauli decomposition") - # Loading Pauli - pauli_pdf = pd.read_csv( - base_fn + "_pauli.csv", sep=";", index_col=0) - affected_qubits = [ast.literal_eval(i_) for i_ in list(pauli_pdf["Qbits"])] - pauli_pdf["Qbits"] = affected_qubits - - # Executing VQE step - logger.info("Executing VQE step") - vqe_conf = { - "qpu" : get_qpu(configuration["qpu_ph"]), - "nb_shots": configuration["nb_shots"], - "truncation": configuration["truncation"], - "t_inv": configuration["t_inv"], - "filename": base_fn, - "save": configuration["save"], - } - exe_ph = PH_EXE(circuit, pauli_pdf, nqubits, **vqe_conf) - exe_ph.run() - return exe_ph.pdf - - -if __name__ == "__main__": - import logging - logging.basicConfig( - format='%(asctime)s-%(levelname)s: %(message)s', - datefmt='%m/%d/%Y %I:%M:%S %p', - level=logging.INFO - #level=logging.DEBUG - ) - logger = logging.getLogger('__name__') - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument( - "-basefn", - dest="base_fn", - type=str, - 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", - type=int, - help="Number of shots", - default=0, - ) - parser.add_argument( - "-truncation", - dest="truncation", - type=int, - help="Truncation for Pauli coeficients.", - default=None, - ) - parser.add_argument( - "-qpu_ph", - dest="qpu_ph", - type=str, - default=None, - help="QPU for parent hamiltonian simulation: [qlmass, python, c]", - ) - parser.add_argument( - "--t_inv", - dest="t_inv", - default=False, - action="store_true", - help="Setting translational invariant of the ansatz", - ) - parser.add_argument( - "--save", - dest="save", - default=False, - action="store_true", - help="For storing results", - ) - args = parser.parse_args() - - run_ph_execution(**vars(args)) diff --git a/tnbs/BTC_04_PH/PH/workflow.py b/tnbs/BTC_04_PH/PH/workflow.py index 29e7bc2..f08576c 100644 --- a/tnbs/BTC_04_PH/PH/workflow.py +++ b/tnbs/BTC_04_PH/PH/workflow.py @@ -7,7 +7,7 @@ import pandas as pd from ansatzes import run_ansatz from parent_hamiltonian import PH -from execution_ph import PH_EXE +from vqe_step import PH_EXE from utils_ph import get_qpu logger = logging.getLogger('__name__')