From 20e8ae01eb3f904fd3ac00f153513fb679c52dfe Mon Sep 17 00:00:00 2001 From: gonfeco Date: Thu, 11 Jan 2024 19:32:53 +0100 Subject: [PATCH] Unifying get_qpu PH case --- tnbs/BTC_04_PH/PH/ansatzes.py | 5 +- tnbs/BTC_04_PH/PH/parent_hamiltonian.json | 46 +---------------- tnbs/BTC_04_PH/PH/vqe_step.json | 60 +---------------------- tnbs/BTC_04_PH/PH/vqe_step.py | 22 ++++++--- tnbs/BTC_04_PH/PH/workflow.py | 4 +- tnbs/BTC_04_PH/get_qpu.py | 58 ++++++++++++++++++++++ tnbs/BTC_04_PH/my_benchmark_execution.py | 9 ++-- 7 files changed, 90 insertions(+), 114 deletions(-) create mode 100644 tnbs/BTC_04_PH/get_qpu.py diff --git a/tnbs/BTC_04_PH/PH/ansatzes.py b/tnbs/BTC_04_PH/PH/ansatzes.py index 2f3d467..591f02a 100644 --- a/tnbs/BTC_04_PH/PH/ansatzes.py +++ b/tnbs/BTC_04_PH/PH/ansatzes.py @@ -12,6 +12,7 @@ """ +import sys import logging import time import uuid @@ -22,7 +23,9 @@ from qat.qlmaas import QLMaaSConnection from qat.core import Result from qat.fermion.circuits import make_ldca_circ, make_general_hwe_circ -from utils_ph import create_folder, get_qpu +from utils_ph import create_folder +sys.path.append("../") +from get_qpu import get_qpu logger = logging.getLogger('__name__') def angles_ansatz01(circuit, pdf_parameters=None): diff --git a/tnbs/BTC_04_PH/PH/parent_hamiltonian.json b/tnbs/BTC_04_PH/PH/parent_hamiltonian.json index 90036f3..1996f45 100644 --- a/tnbs/BTC_04_PH/PH/parent_hamiltonian.json +++ b/tnbs/BTC_04_PH/PH/parent_hamiltonian.json @@ -2,49 +2,7 @@ { "save": [true], "t_inv": [true], - "state": ["Test/ansatz_simple01_nqubits_12_depth_1_qpu_ansatz_c_state.csv"], - "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_1_qpu_ansatz_c"] - }, - { - "save": [true], - "t_inv": [true], - "state": ["Test/ansatz_simple01_nqubits_12_depth_2_qpu_ansatz_c_state.csv"], - "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_2_qpu_ansatz_c"] - }, - { - "save": [true], - "t_inv": [true], - "state": ["Test/ansatz_simple01_nqubits_12_depth_3_qpu_ansatz_c_state.csv"], - "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_3_qpu_ansatz_c"] - }, - { - "save": [true], - "t_inv": [true], - "state": ["Test/ansatz_simple01_nqubits_12_depth_4_qpu_ansatz_c_state.csv"], - "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_4_qpu_ansatz_c"] - }, - { - "save": [true], - "t_inv": [true], - "state": ["Test/ansatz_simple01_nqubits_14_depth_1_qpu_ansatz_c_state.csv"], - "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_1_qpu_ansatz_c"] - }, - { - "save": [true], - "t_inv": [true], - "state": ["Test/ansatz_simple01_nqubits_14_depth_2_qpu_ansatz_c_state.csv"], - "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_2_qpu_ansatz_c"] - }, - { - "save": [true], - "t_inv": [true], - "state": ["Test/ansatz_simple01_nqubits_14_depth_3_qpu_ansatz_c_state.csv"], - "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_3_qpu_ansatz_c"] - }, - { - "save": [true], - "t_inv": [true], - "state": ["Test/ansatz_simple01_nqubits_14_depth_4_qpu_ansatz_c_state.csv"], - "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_4_qpu_ansatz_c"] + "state": ["Test/ansatz_simple01_nqubits_14_depth_1_qpu_ansatz_linalg_state.csv"], + "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_1_qpu_ansatz_linalg"] } ] diff --git a/tnbs/BTC_04_PH/PH/vqe_step.json b/tnbs/BTC_04_PH/PH/vqe_step.json index 3845eb7..4ac6c16 100644 --- a/tnbs/BTC_04_PH/PH/vqe_step.json +++ b/tnbs/BTC_04_PH/PH/vqe_step.json @@ -1,66 +1,10 @@ [ { "save": [true], - "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_3_qpu_ansatz_c"], - "nb_shots": [null], - "truncation": [1e-5], - "t_inv": [true], - "qpu_ph": ["c"] - }, - { - "save": [true], - "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_2_qpu_ansatz_c"], - "nb_shots": [null], - "truncation": [null], - "t_inv": [true], - "qpu_ph": ["c"] - }, - { - "save": [true], - "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_3_qpu_ansatz_c"], - "nb_shots": [null], - "truncation": [null], - "t_inv": [true], - "qpu_ph": ["c"] - }, - { - "save": [true], - "base_fn" : ["Test/ansatz_simple01_nqubits_12_depth_4_qpu_ansatz_c"], - "nb_shots": [null], - "truncation": [null], - "t_inv": [true], - "qpu_ph": ["c"] - }, - { - "save": [true], - "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_1_qpu_ansatz_c"], - "nb_shots": [null], - "truncation": [null], - "t_inv": [true], - "qpu_ph": ["c"] - }, - { - "save": [true], - "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_2_qpu_ansatz_c"], - "nb_shots": [null], - "truncation": [null], - "t_inv": [true], - "qpu_ph": ["c"] - }, - { - "save": [true], - "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_3_qpu_ansatz_c"], - "nb_shots": [null], - "truncation": [null], - "t_inv": [true], - "qpu_ph": ["c"] - }, - { - "save": [true], - "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_4_qpu_ansatz_c"], + "base_fn" : ["Test/ansatz_simple01_nqubits_14_depth_1_qpu_ansatz_linalg"], "nb_shots": [null], "truncation": [null], "t_inv": [true], - "qpu_ph": ["c"] + "qpu_ph": ["linalg"] } ] diff --git a/tnbs/BTC_04_PH/PH/vqe_step.py b/tnbs/BTC_04_PH/PH/vqe_step.py index 4712b23..e5d185e 100644 --- a/tnbs/BTC_04_PH/PH/vqe_step.py +++ b/tnbs/BTC_04_PH/PH/vqe_step.py @@ -5,6 +5,7 @@ Author: Gonzalo Ferro """ +import sys import logging import time import re @@ -12,7 +13,9 @@ import pandas as pd from qat.core import Observable, Term from ansatzes import ansatz_selector, angles_ansatz01 -from utils_ph import get_qpu +sys.path.append("../") +from get_qpu import get_qpu +logger = logging.getLogger('__name__') logger = logging.getLogger("__name__") @@ -71,7 +74,7 @@ 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( + logger.info( "Additional truncation of Pauli Coeficients: {}".format( len(self.pauli_pdf))) else: @@ -185,8 +188,8 @@ def run_ph_execution(**configuration): base_fn + "_parameters.csv", sep=";", index_col=0) # Formating Parameters circuit, _ = angles_ansatz01(circuit, parameters_pdf) - # from qat.core.console import display - # display(circuit) + #from qat.core.console import display + #display(circuit) # 3. Loading Pauli Decomposition from: {}_pauli.csv text = "Loading PH Pauli decomposition from: {}".format( @@ -221,6 +224,9 @@ def run_ph_execution(**configuration): ) logger = logging.getLogger('__name__') import argparse + import sys + sys.path.append("../") + from get_qpu import get_qpu parser = argparse.ArgumentParser() parser.add_argument( @@ -249,7 +255,7 @@ def run_ph_execution(**configuration): dest="qpu_ph", type=str, default=None, - help="QPU for parent hamiltonian simulation: [qlmass, python, c]", + help="QPU for parent hamiltonian simulation: See function get_qpu in get_qpu module", ) parser.add_argument( "--t_inv", @@ -266,5 +272,7 @@ def run_ph_execution(**configuration): help="For storing results", ) args = parser.parse_args() - - print(run_ph_execution(**vars(args))) + config = vars(args) + config.update({"qpu_ph": get_qpu(config["qpu_ph"])}) + print(config) + print(run_ph_execution(**config)) diff --git a/tnbs/BTC_04_PH/PH/workflow.py b/tnbs/BTC_04_PH/PH/workflow.py index f08576c..b1346e4 100644 --- a/tnbs/BTC_04_PH/PH/workflow.py +++ b/tnbs/BTC_04_PH/PH/workflow.py @@ -8,7 +8,9 @@ from ansatzes import run_ansatz from parent_hamiltonian import PH from vqe_step import PH_EXE -from utils_ph import get_qpu +import sys +sys.path.append("../") +from get_qpu import get_qpu logger = logging.getLogger('__name__') diff --git a/tnbs/BTC_04_PH/get_qpu.py b/tnbs/BTC_04_PH/get_qpu.py new file mode 100644 index 0000000..3a09fca --- /dev/null +++ b/tnbs/BTC_04_PH/get_qpu.py @@ -0,0 +1,58 @@ +""" +Selector for QPU. +""" + +def get_qpu(qpu=None): + """ + Function for selecting solver. + + Parameters + ---------- + + qpu : str + * qlmass: for trying to use QLM as a Service connection + to CESGA QLM + * python: for using PyLinalg simulator. + * c: for using CLinalg simulator + * mps: for using mps + + Returns + ---------- + + linal_qpu : solver for quantum jobs + """ + + if qpu is None: + raise ValueError( + "qpu CAN NOT BE NONE. Please select one of the three" + + " following options: qlmass, python, c") + if qpu == "qlmass_linalg": + try: + from qlmaas.qpus import LinAlg + linalg_qpu = LinAlg() + except (ImportError, OSError) as exception: + raise ImportError( + "Problem Using QLMaaS. Please create config file" + + "or use mylm solver") from exception + elif qpu == "qlmass_mps": + try: + from qlmaas.qpus import MPS + #linalg_qpu = MPS(lnnize=True) + linalg_qpu = MPS() + except (ImportError, OSError) as exception: + raise ImportError( + "Problem Using QLMaaS. Please create config file" + + "or use mylm solver") from exception + elif qpu == "python": + from qat.qpus import PyLinalg + linalg_qpu = PyLinalg() + elif qpu == "c": + from qat.qpus import CLinalg + linalg_qpu = CLinalg() + elif qpu == "linalg": + from qat.qpus import LinAlg + linalg_qpu = LinAlg() + elif qpu == "mps": + from qat.qpus import MPS + linalg_qpu = MPS() + return linalg_qpu diff --git a/tnbs/BTC_04_PH/my_benchmark_execution.py b/tnbs/BTC_04_PH/my_benchmark_execution.py index 9e7a95b..cf55d0d 100644 --- a/tnbs/BTC_04_PH/my_benchmark_execution.py +++ b/tnbs/BTC_04_PH/my_benchmark_execution.py @@ -16,7 +16,6 @@ from PH.ansatzes import ansatz_selector, angles_ansatz01 from PH.vqe_step import PH_EXE -from PH.utils_ph import get_qpu logging.basicConfig( @@ -120,7 +119,7 @@ def run_code(iterator_step, repetitions, stage_bench, **kwargs): t_inv = True vqe_conf = { - "qpu" : get_qpu(kernel_configuration["qpu_ph"]), + "qpu" : kernel_configuration["qpu_ph"], "nb_shots": nb_shots,#kernel_configuration["nb_shots"], "truncation": truncation, #kernel_configuration["truncation"], "t_inv": t_inv,#kernel_configuration["t_inv"], @@ -366,8 +365,9 @@ def exe(self): if __name__ == "__main__": + from get_qpu import get_qpu #Anstaz - depth = [1, 2, 3, 4] + depth = [1] qpu_ph = "c" nb_shots = 0 truncation = None @@ -409,8 +409,11 @@ def exe(self): "list_of_qbits": list_of_qbits, } + kernel_configuration.update({"qpu_ph": get_qpu(kernel_configuration["qpu_ph"])}) #Configuration for the benchmark kernel benchmark_arguments.update({"kernel_configuration": kernel_configuration}) + if not os.path.exists(benchmark_arguments["saving_folder"]): + os.mkdir(benchmark_arguments["saving_folder"]) kernel_bench = KERNEL_BENCHMARK(**benchmark_arguments) kernel_bench.exe()