Skip to content

Commit

Permalink
noisy QPU added to BTC_02_AE
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfeco committed May 27, 2024
1 parent 3120aca commit 36eb83e
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 138 deletions.
34 changes: 28 additions & 6 deletions tests/test_btc_02_ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,30 @@
sys.path.append(l_path)
sys.path.append(l_path+"BTC_02_AE")
from BTC_02_AE.my_benchmark_execution import KERNEL_BENCHMARK as AE_CLASS
from get_qpu import get_qpu

from BTC_02_AE.QQuantLib.qpu.select_qpu import select_qpu

# Naive qpu configuration
qpu_conf = {
"qpu_type": "c",
"t_gate_1qb" : None,
"t_gate_2qbs" : None,
"t_readout": None,
"depol_channel" : {
"active": False,
"error_gate_1qb" : None,
"error_gate_2qbs" : None
},
"idle" : {
"amplitude_damping": False,
"dephasing_channel": False,
"t1" : None,
"t2" : None
},
"meas": {
"active":False,
"readout_error": None
}
}


def create_folder(folder_name):
Expand Down Expand Up @@ -51,9 +73,10 @@ def test_ae_iqae():
"alpha": 0.05, # This the alpha for the AE algorithm
"multiplexor": True,
"shots": 1000,
"qpu": "c",
"integrals": [0]
}
kernel_configuration.update({"qpu": select_qpu(qpu_conf)})

name = "AE_{}".format(kernel_configuration["ae_type"])

benchmark_arguments = {
Expand All @@ -77,7 +100,6 @@ def test_ae_iqae():
"list_of_qbits": [4],
}

kernel_configuration.update({"qpu": get_qpu(kernel_configuration['qpu'])})
benchmark_arguments.update({"kernel_configuration": kernel_configuration})
folder = create_folder(benchmark_arguments["saving_folder"])
ae_bench = AE_CLASS(**benchmark_arguments)
Expand Down Expand Up @@ -105,6 +127,7 @@ def test_ae_mlae():
"qpu": "c",
"integrals": [0]
}
kernel_configuration.update({"qpu": select_qpu(qpu_conf)})
name = "AE_{}".format(kernel_configuration["ae_type"])

benchmark_arguments = {
Expand All @@ -128,7 +151,6 @@ def test_ae_mlae():
"list_of_qbits": [4],
}

kernel_configuration.update({"qpu": get_qpu(kernel_configuration['qpu'])})
benchmark_arguments.update({"kernel_configuration": kernel_configuration})
folder = create_folder(benchmark_arguments["saving_folder"])
ae_bench = AE_CLASS(**benchmark_arguments)
Expand All @@ -154,9 +176,9 @@ def test_ae_rqae():
"qpu": "c",
"integrals": [0, 1]
}
kernel_configuration.update({"qpu": select_qpu(qpu_conf)})
name = "AE_{}".format(kernel_configuration["ae_type"])

kernel_configuration.update({"qpu": get_qpu(kernel_configuration['qpu'])})
benchmark_arguments = {
#Pre benchmark configuration
"pre_benchmark": True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
import numpy as np
import pandas as pd
from copy import deepcopy
from get_qpu import get_qpu

import sys

sys.path.append("../")


from QQuantLib.qpu.select_qpu import select_qpu
from QQuantLib.utils.benchmark_utils import combination_for_list
from QQuantLib.utils.benchmark_utils import create_ae_pe_solution
from QQuantLib.finance.quantum_integration import q_solve_integral

def sine_integral(n_qbits, interval, ae_dictionary):
Expand Down Expand Up @@ -181,7 +187,7 @@ def run_id(
folder_path=None,
):

ae_config.update({"qpu":get_qpu(qpu)})
ae_config.update({"qpu":select_qpu(ae_config)})

if save_:
if folder_path is None:
Expand Down Expand Up @@ -211,63 +217,10 @@ def run_id(



def select_ae(ae_method):
"""
Function for selecting the AE algorithm used in the benchmark
Parameters
----------
ae_method : string
Amplitude Estimation method used in the benchmark
Returns
_______
ae_configuration : dict
Dictionary with the complete configuration of the Amplitude
"""

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

ae_list_ = []
for ae_json_ in lista_ae_:
with open(ae_json_) as json_file_:
ae_list_ = ae_list_ + json.load(json_file_)
#Creates the complete configuration for AE solvers
final_list_ = combination_for_list(ae_list_)
if len(final_list_) > 1:
text = "There are more than 1 AE algorithm configuration. "\
"FOR BENCHMARK only 1 configuration should be given. "\
"Please change the correspondent json!!"
raise ValueError(text)
ae_configuration = final_list_[0]
return ae_configuration



if __name__ == "__main__":
import argparse
from qpu.select_qpu import select_qpu

parser = argparse.ArgumentParser()
#Arguments for execution
Expand All @@ -293,6 +246,13 @@ def select_ae(ae_method):
"Default: 1",
default=1,
)
parser.add_argument(
"-id",
dest="id",
type=int,
help="For executing only one element of the list",
default=None,
)
#AE algorithm configuration arguments
parser.add_argument(
"-json_ae",
Expand All @@ -301,6 +261,14 @@ def select_ae(ae_method):
default=None,
help="JSON AE algorithm configuration",
)
#QPU configuration
parser.add_argument(
"-json_qpu",
dest="json_qpu",
type=str,
default="qpu/qpu.json",
help="JSON with the qpu configuration",
)
parser.add_argument(
"--count",
dest="count",
Expand All @@ -316,21 +284,6 @@ def select_ae(ae_method):
action="store_true",
help="For printing the AE algorihtm configuration."
)
parser.add_argument(
"-id",
dest="id",
type=int,
help="For executing only one element of the list",
default=None,
)
#QPU argument
parser.add_argument(
"-qpu",
dest="qpu",
type=str,
default="c",
help="QPU for simulation: See function get_qpu in get_qpu module",
)
#Saving results arguments
parser.add_argument(
"--save",
Expand Down Expand Up @@ -359,7 +312,12 @@ def select_ae(ae_method):
with open(args.json_ae) as json_file:
ae_cfg = json.load(json_file)
# Creates the complete configuration for AE solvers
final_list = combination_for_list(ae_cfg)
ae_list = combination_for_list(ae_cfg)

with open(args.json_qpu) as json_file:
noisy_cfg = json.load(json_file)
qpu_list = combination_for_list(noisy_cfg)
final_list = create_ae_pe_solution(ae_list, qpu_list)

if args.count:
print(len(final_list))
Expand All @@ -376,7 +334,6 @@ def select_ae(ae_method):
interval=args.interval,
repetitions=args.repetitions,
ae_config=final_list[args.id],
qpu=args.qpu,
folder_path=args.folder_path,
#qpu=args.qpu,
save_=args.save,
Expand Down
65 changes: 49 additions & 16 deletions tnbs/BTC_02_AE/my_benchmark_execution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""
This module execute a complete BTC of the AE kernel
This module execute a complete BTC of the AE kernel
"""

import json
from datetime import datetime
import pandas as pd

from ae_sine_integral import sine_integral
from QQuantLib.ae_sine_integral import sine_integral

def build_iterator(**kwargs):
"""
Expand Down Expand Up @@ -294,17 +293,45 @@ def exe(self):

if __name__ == "__main__":
import os
from get_qpu import get_qpu
from ae_sine_integral import select_ae
from QQuantLib.qpu.select_qpu import select_qpu
from QQuantLib.utils.benchmark_utils import combination_for_list
from QQuantLib.utils.benchmark_utils import create_ae_pe_solution

AE = "RQAE"
#Setting the AE algorithm configuration
ae_problem = select_ae(AE)
############## CONFIGURE THE BTC ###################

#Setting the AE algorithm configuration
ae_json_file = "jsons/integral_mlae_configuration.json"
with open(ae_json_file) as json_file:
ae_cfg = json.load(json_file)

# Creates the complete configuration for AE solvers.
# BE AWARE only one AE configuration MUST BE provided
ae_problem = combination_for_list(ae_cfg)[0]

#Setting the QPU configuration
qpu_json_file = "QQuantLib/qpu/qpu_ideal.json"
with open(qpu_json_file) as json_file:
noisy_cfg = json.load(json_file)
# BE AWARE only one QPU configuration MUST BE provided
qpu_conf = combination_for_list(noisy_cfg)[0]
ae_problem.update(qpu_conf)

# Setting the integral interval
ae_problem.update({
"qpu": "c",
"integrals": [0, 1]
"integrals": [0]
})
# Setting the list of qubits for domain discretization
list_of_qbits = [6]
############## CONFIGURE THE BTC ###################

############## CONFIGURE THE BENCHMARK EXECUTION #################

AE = ae_problem["ae_type"]
# Configure the save Folder and the name of the files
saving_folder = "./{}_Results/".format(AE)
benchmark_times = "{}_times_benchmark.csv".format(AE)
csv_results = "{}_benchmark.csv".format(AE)
summary_results = "{}_SummaryResults.csv".format(AE)

benchmark_arguments = {
#Pre benchmark sttuff
Expand All @@ -313,19 +340,20 @@ def exe(self):
"pre_save": True,
#Saving stuff
"save_append" : True,
"saving_folder": "./{}_Results/".format(AE),
"benchmark_times": "{}_times_benchmark.csv".format(AE),
"csv_results": "{}_benchmark.csv".format(AE),
"summary_results": "{}_SummaryResults.csv".format(AE),
"saving_folder": saving_folder,
"benchmark_times": benchmark_times,
"csv_results": csv_results,
"summary_results": summary_results,
#Computing Repetitions stuff
"alpha": None,
"relative_error": None,
"absolute_error": None,
"min_meas": None,
"max_meas": None,
#List number of qubits tested
"list_of_qbits": [4],
"list_of_qbits": list_of_qbits,
}
############## CONFIGURE THE BENCHMARK EXECUTION #################


json_object = json.dumps(ae_problem)
Expand All @@ -336,8 +364,13 @@ def exe(self):
"benchmark_ae_conf.json"
with open(conf_file, "w") as outfile:
outfile.write(json_object)
# Store the QPU configuration
qpu_file = benchmark_arguments["saving_folder"] + \
"qpu_configuration.json"
with open(qpu_file, "w") as outfile:
json.dump(qpu_conf, outfile)
#Added ae configuration
ae_problem.update({"qpu":get_qpu(ae_problem["qpu"])})
ae_problem.update({"qpu":select_qpu(ae_problem)})
benchmark_arguments.update({
"kernel_configuration": ae_problem,
})
Expand Down
Loading

0 comments on commit 36eb83e

Please sign in to comment.