From 8889043b5d9ac21f1680d6830b4db20669cffdfb Mon Sep 17 00:00:00 2001 From: gonfeco Date: Tue, 12 Sep 2023 15:44:03 +0200 Subject: [PATCH] Fixed Test cases --- setup.py | 4 +- tests/test_btc_01_pl.py | 8 +- tests/test_btc_02_ae.py | 189 +++++++++++++++++++++++++++++++++++++++ tests/test_btc_03_qpe.py | 85 ++++++++++++++++++ 4 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 tests/test_btc_02_ae.py create mode 100644 tests/test_btc_03_qpe.py diff --git a/setup.py b/setup.py index 3f06f13..c8efe09 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,9 @@ from setuptools import setup, find_packages from setuptools.command.test import test as TestCommand -sys.path.append("tnbs/BTC_01_PL") +#sys.path.append("tnbs/BTC_01_PL") +#sys.path.append("tnbs/BTC_02_AE") +#sys.path.append("tnbs/BTC_03_QPE") class PyTest(TestCommand): """ diff --git a/tests/test_btc_01_pl.py b/tests/test_btc_01_pl.py index fab3645..4eae734 100644 --- a/tests/test_btc_01_pl.py +++ b/tests/test_btc_01_pl.py @@ -3,8 +3,12 @@ import sys import pandas as pd import numpy as np +import re -sys.path.append("../tnbs/BTC_01_PL") +folder = os.getcwd() +folder = re.sub(r"WP3_Benchmark/.*", "WP3_Benchmark/", folder) +folder = folder + "tnbs/BTC_01_PL" +sys.path.append(folder) from my_benchmark_execution import KERNEL_BENCHMARK @@ -75,4 +79,4 @@ def test_pl(): assert(100* list(a['KS']['mean'])[0] < 1.0) shutil.rmtree(folder) -test_pl() +#test_pl() diff --git a/tests/test_btc_02_ae.py b/tests/test_btc_02_ae.py new file mode 100644 index 0000000..4a729ec --- /dev/null +++ b/tests/test_btc_02_ae.py @@ -0,0 +1,189 @@ +import os +import shutil +import sys +import pandas as pd +import numpy as np +import re + +folder = os.getcwd() +folder = re.sub(r"WP3_Benchmark/.*", "WP3_Benchmark/", folder) +folder = folder + "tnbs/BTC_02_AE" +sys.path.append(folder) +from my_benchmark_execution import KERNEL_BENCHMARK + + +def create_folder(folder_name): + """ + Check if folder exist. If not the function creates it + + Parameters + ---------- + + folder_name : str + Name of the folder + + Returns + ---------- + + folder_name : str + Name of the folder + """ + + # Check if the folder already exists + if not os.path.exists(folder_name): + # If it does not exist, create the folder + os.mkdir(folder_name) + print(f"Folder '{folder_name}' created.") + else: + print(f"Folder '{folder_name}' already exists.") + if folder_name.endswith('/') != True: + folder_name = folder_name + "/" + return folder_name + +def test_ae_iqae(): + + kernel_configuration ={ + "ae_type": "IQAE", + "epsilon": 0.001, + "alpha": 0.05, # This the alpha for the AE algorithm + "multiplexor": True, + "shots": 1000, + "qpu": "c", + "integrals": [0] + } + name = "AE_{}".format(kernel_configuration["ae_type"]) + + benchmark_arguments = { + #Pre benchmark configuration + "pre_benchmark": True, + "pre_samples": None, + "pre_save": True, + #Saving configuration + "save_append" : True, + "saving_folder": "./Results/", + "benchmark_times": "{}_times_benchmark.csv".format(name), + "csv_results": "{}_benchmark.csv".format(name), + "summary_results": "{}_SummaryResults.csv".format(name), + #Computing Repetitions configuration + "alpha": None, + "min_meas": None, + "max_meas": 10, + "relative_error": None, + "absolute_error": None, + #List number of qubits tested + "list_of_qbits": [4], + } + + benchmark_arguments.update({"kernel_configuration": kernel_configuration}) + folder = create_folder(benchmark_arguments["saving_folder"]) + ae_bench = KERNEL_BENCHMARK(**benchmark_arguments) + 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(100* list(a['KS']['mean'])[0]) + assert(100* list(a['absolute_error_sum']['mean'])[0] < 1.0) + shutil.rmtree(folder) + +def test_ae_mlae(): + + kernel_configuration ={ + "ae_type": "MLAE", + "schedule": [ + [1, 2, 4, 8, 12], + [100, 100, 100, 100, 100] + ], + "delta": 1.0e-7, + "ns": 10000, + "mcz_qlm": False, + "multiplexor": True, + "shots": 1000, + "qpu": "c", + "integrals": [0] + } + name = "AE_{}".format(kernel_configuration["ae_type"]) + + benchmark_arguments = { + #Pre benchmark configuration + "pre_benchmark": True, + "pre_samples": None, + "pre_save": True, + #Saving configuration + "save_append" : True, + "saving_folder": "./Results/", + "benchmark_times": "{}_times_benchmark.csv".format(name), + "csv_results": "{}_benchmark.csv".format(name), + "summary_results": "{}_SummaryResults.csv".format(name), + #Computing Repetitions configuration + "alpha": None, + "min_meas": None, + "max_meas": 10, + "relative_error": None, + "absolute_error": None, + #List number of qubits tested + "list_of_qbits": [4], + } + + benchmark_arguments.update({"kernel_configuration": kernel_configuration}) + folder = create_folder(benchmark_arguments["saving_folder"]) + ae_bench = KERNEL_BENCHMARK(**benchmark_arguments) + 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(100* list(a['KS']['mean'])[0]) + assert(100* list(a['absolute_error_sum']['mean'])[0] < 1.0) + shutil.rmtree(folder) + +def test_ae_rqae(): + + kernel_configuration ={ + "ae_type": "RQAE", + "epsilon": 0.01, + "gamma": 0.05, + "q": 1.2, + + "mcz_qlm": False, + "multiplexor": True, + "shots": 1000, + "qpu": "c", + "integrals": [0, 1] + } + name = "AE_{}".format(kernel_configuration["ae_type"]) + + benchmark_arguments = { + #Pre benchmark configuration + "pre_benchmark": True, + "pre_samples": None, + "pre_save": True, + #Saving configuration + "save_append" : True, + "saving_folder": "./Results/", + "benchmark_times": "{}_times_benchmark.csv".format(name), + "csv_results": "{}_benchmark.csv".format(name), + "summary_results": "{}_SummaryResults.csv".format(name), + #Computing Repetitions configuration + "alpha": None, + "min_meas": None, + "max_meas": 10, + "relative_error": None, + "absolute_error": None, + #List number of qubits tested + "list_of_qbits": [4], + } + + benchmark_arguments.update({"kernel_configuration": kernel_configuration}) + folder = create_folder(benchmark_arguments["saving_folder"]) + ae_bench = KERNEL_BENCHMARK(**benchmark_arguments) + 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"]) + assert((a["absolute_error_sum"]["mean"] < 0.01).all()) + #print(100* list(a['KS']['mean'])[0]) + #assert(100* list(a['absolute_error_sum']['mean'])[0] < 1.0) + shutil.rmtree(folder) + +#test_ae_iqae() +#test_ae_mlae() +#test_ae_rqae() diff --git a/tests/test_btc_03_qpe.py b/tests/test_btc_03_qpe.py new file mode 100644 index 0000000..d1eaefa --- /dev/null +++ b/tests/test_btc_03_qpe.py @@ -0,0 +1,85 @@ +import os +import shutil +import sys +import pandas as pd +import numpy as np +import re + +folder = os.getcwd() +folder = re.sub(r"WP3_Benchmark/.*", "WP3_Benchmark/", folder) +folder = folder + "tnbs/BTC_03_QPE" +sys.path.append(folder) +from my_benchmark_execution import KERNEL_BENCHMARK + + +def create_folder(folder_name): + """ + Check if folder exist. If not the function creates it + + Parameters + ---------- + + folder_name : str + Name of the folder + + Returns + ---------- + + folder_name : str + Name of the folder + """ + + # Check if the folder already exists + if not os.path.exists(folder_name): + # If it does not exist, create the folder + os.mkdir(folder_name) + print(f"Folder '{folder_name}' created.") + else: + print(f"Folder '{folder_name}' already exists.") + if folder_name.endswith('/') != True: + folder_name = folder_name + "/" + return folder_name + +def test_qpe(): + + kernel_configuration = { + "angles" : ["random", 'exact'], + "auxiliar_qbits_number" : [8], + "qpu" : "c", #qlmass, python, c + "fidelity_error" : None, + "ks_error" : None, + "time_error": None + } + + benchmark_arguments = { + #Pre benchmark sttuff + "pre_benchmark": True, + "pre_samples": None, + "pre_save": True, + #Saving stuff + "save_append" : True, + "saving_folder": "./Results/", + "benchmark_times": "kernel_times_benchmark.csv", + "csv_results": "kernel_benchmark.csv", + "summary_results": "kernel_SummaryResults.csv", + #Computing Repetitions stuff + "alpha": None, + "min_meas": None, + "max_meas": None, + #List number of qubits tested + "list_of_qbits": [6], + } + + benchmark_arguments.update({"kernel_configuration": kernel_configuration}) + folder = create_folder(benchmark_arguments["saving_folder"]) + kernel_bench = KERNEL_BENCHMARK(**benchmark_arguments) + kernel_bench.exe() + filename = "./Results/kernel_SummaryResults.csv" + index_columns = [0, 1, 2, 3, 4, 5] + a = pd.read_csv(filename, header=[0, 1], index_col=index_columns) + a= a.reset_index() + assert((a[a['angle_method']=="exact"]['fidelity']['mean'] > 0.999).all()) + assert((a[a['angle_method']=="random"]['KS']['mean'] < 0.05).all()) + shutil.rmtree(folder) + +#test_qpe()