Skip to content

Commit

Permalink
linting in btc_01_pl
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfeco committed Sep 13, 2023
1 parent eb317b2 commit 868b09f
Show file tree
Hide file tree
Showing 21 changed files with 363 additions and 55 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ jobs:
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
#- name: Lint with flake8
# run: |
# # stop the build if there are Python syntax errors or undefined names
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: show python path
- name: Lint with flake8
run: |
python -c "import sys; print('\n'.join(sys.path)); import os; print(os.getcwd())"
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
3 changes: 1 addition & 2 deletions tests/test_btc_01_pl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
r"WP3_Benchmark/(?=WP3_Benchmark/)*.*","WP3_Benchmark/", folder)

sys.path.append(folder)
from tnbs.BTC_01_PL.my_benchmark_execution import KERNEL_BENCHMARK as PL_CLASS
from tnbs.BTC_01_PL.PL import *
from tnbs.btc_01_pl.my_benchmark_execution import KERNEL_BENCHMARK as PL_CLASS


def create_folder(folder_name):
Expand Down
189 changes: 189 additions & 0 deletions tests/test_btc_02_ae.py
Original file line number Diff line number Diff line change
@@ -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/)*.*","WP3_Benchmark/", folder)
sys.path.append(folder+"/tnbs/BTC_02_AE")
from my_benchmark_execution import KERNEL_BENCHMARK as AE_CLASS


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_IQAE/",
"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 = AE_CLASS(**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_MLAE/",
"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 = AE_CLASS(**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_RQAE/",
"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 = AE_CLASS(**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()
86 changes: 86 additions & 0 deletions tests/test_btc_03_qpe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import shutil
import sys
import pandas as pd
import numpy as np
import re

folder = os.getcwd()
folder = os.getcwd()
folder = re.sub(
r"WP3_Benchmark/(?=WP3_Benchmark/)*.*","WP3_Benchmark/", folder)
sys.path.append(folder)
from tnbs.BTC_03_QPE.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()
38 changes: 27 additions & 11 deletions tnbs/BTC_02_AE/ae_sine_integral.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@
Mandatory code for softaware implemetation of the Benchmark Test Case
of AE kernel
"""

import sys
import os
import json
import time
import re
from copy import deepcopy
import numpy as np
import pandas as pd
from copy import deepcopy
import sys
sys.path.append('../')
from QQuantLib.utils.benchmark_utils import combination_for_list
from QQuantLib.utils.qlm_solver import get_qpu
from QQuantLib.finance.quantum_integration import q_solve_integral


folder = os.getcwd()
folder = re.sub(
r"WP3_Benchmark/(?=WP3_Benchmark/)*.*","WP3_Benchmark/", folder)
sys.path.append(folder)
sys.path.append(folder)


from tnbs.BTC_02_AE.QQuantLib.utils.benchmark_utils import combination_for_list
from tnbs.BTC_02_AE.QQuantLib.utils.qlm_solver import get_qpu
from tnbs.BTC_02_AE.QQuantLib.finance.quantum_integration import q_solve_integral

def sine_integral(n_qbits, interval, ae_dictionary):
"""
Expand Down Expand Up @@ -165,19 +177,23 @@ def select_ae(ae_method):
"""

folder_json = os.getcwd()
folder_json = re.sub(
r"WP3_Benchmark/(?=WP3_Benchmark/)*.*","WP3_Benchmark", folder)
folder_json = folder_json + "/tnbs/BTC_02_AE/jsons/"
lista_ae_ = []
if ae_method == "MLAE":
lista_ae_.append("jsons/integral_mlae_configuration.json")
lista_ae_.append(folder_json + "integral_mlae_configuration.json")
elif ae_method == "IQAE":
lista_ae_.append("jsons/integral_iqae_configuration.json")
lista_ae_.append(folder_json + "integral_iqae_configuration.json")
elif ae_method == "RQAE":
lista_ae_.append("jsons/integral_rqae_configuration.json")
lista_ae_.append(folder_json + "integral_rqae_configuration.json")
elif ae_method == "CQPEAE":
lista_ae_.append("jsons/integral_cqpeae_configuration.json")
lista_ae_.append(folder_json + "integral_cqpeae_configuration.json")
elif ae_method == "IQPEAE":
lista_ae_.append("jsons/integral_iqpeae_configuration.json")
lista_ae_.append(folder_json + "integral_iqpeae_configuration.json")
elif ae_method == "MCAE":
lista_ae_.append("jsons/integral_mcae_configuration.json")
lista_ae_.append(folder_json + "integral_mcae_configuration.json")
else:
raise ValueError(
"ae_method MUST BE: MLAE, IQAE, RQAE, CQPEAE or IQPEAE")
Expand Down
Loading

0 comments on commit 868b09f

Please sign in to comment.