Skip to content

Commit

Permalink
Added updated modules from QQuantLib of the FinancialApoplications so…
Browse files Browse the repository at this point in the history
…ftware package
  • Loading branch information
gonfeco committed May 13, 2024
1 parent 38c6e53 commit 6bc2bd9
Show file tree
Hide file tree
Showing 28 changed files with 3,027 additions and 1,488 deletions.
82 changes: 39 additions & 43 deletions tnbs/BTC_02_AE/QQuantLib/AE/ae_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
"""

import sys
import os
import re
import pandas as pd

from QQuantLib.utils.qlm_solver import get_qpu
from QQuantLib.AE.maximum_likelihood_ae import MLAE
from QQuantLib.AE.ae_classical_qpe import CQPEAE
from QQuantLib.AE.ae_iterative_quantum_pe import IQPEAE
from QQuantLib.AE.iterative_quantum_ae import IQAE
from QQuantLib.AE.modified_iterative_quantum_ae import mIQAE
from QQuantLib.AE.real_quantum_ae import RQAE
from QQuantLib.AE.shots_real_quantum_ae import sRQAE
from QQuantLib.AE.extended_real_quantum_ae import eRQAE
from QQuantLib.AE.modified_real_quantum_ae import mRQAE
from QQuantLib.AE.montecarlo_ae import MCAE
from QQuantLib.utils.utils import text_is_none

Expand All @@ -40,7 +39,7 @@ class AE:
dictionary that allows the configuration of the AE algorithm. \\
The different configration keys of the different AE algorithms \\
can be provided.
"""
"""
def __init__(self, oracle=None, target=None, index=None, ae_type=None, **kwargs):
"""
Expand All @@ -57,20 +56,18 @@ def __init__(self, oracle=None, target=None, index=None, ae_type=None, **kwargs)
self.index = index

#Processing kwargs
self.kwargs = kwargs
self.linalg_qpu = self.kwargs.get("qpu", None)
self.solver_dict = kwargs
# self.kwargs = kwargs
self.linalg_qpu = self.solver_dict.get("qpu", None)

# Set the QPU to use
self.linalg_qpu = kwargs.get("qpu", None)
# Provide QPU
if self.linalg_qpu is None:
print("Not QPU was provide. PyLinalg will be used")
self.linalg_qpu = get_qpu("python")
raise ValueError("Not QPU was provide. Please provide it!")

self._ae_type = ae_type
#attributes created
self.solver_ae = None
self.ae_pdf = None
self.solver_dict = None
self.oracle_calls = None
self.max_oracle_depth = None
self.schedule_pdf = None
Expand All @@ -93,7 +90,6 @@ def ae_type(self, stringvalue):
self._ae_type = stringvalue
self.solver_ae = None
self.ae_pdf = None
self.solver_dict = None
self.oracle_calls = None

def create_ae_solver(self):
Expand All @@ -102,72 +98,71 @@ def create_ae_solver(self):
"""
text_is_none(self.ae_type, "ae_type attribute", variable_type=str)
#common ae settings
self.solver_dict = {
"mcz_qlm" : self.kwargs.get("mcz_qlm", True),
"qpu" : self.kwargs.get("qpu", None)
}


if self.ae_type == "MLAE":
for par in ["delta", "ns", "schedule"]:
val_par = self.kwargs.get(par)
if val_par is not None:
self.solver_dict.update({par : val_par})
self.solver_ae = MLAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "CQPEAE":
for par in ["auxiliar_qbits_number", "shots"]:
val_par = self.kwargs.get(par)
if val_par is not None:
self.solver_dict.update({par : val_par})
self.solver_ae = CQPEAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "IQPEAE":
for par in ["cbits_number", "shots"]:
val_par = self.kwargs.get(par)
if val_par is not None:
self.solver_dict.update({par : val_par})
self.solver_ae = IQPEAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "IQAE":
for par in ["epsilon", "alpha", "shots"]:
val_par = self.kwargs.get(par)
if val_par is not None:
self.solver_dict.update({par : val_par})
self.solver_ae = IQAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "mIQAE":
self.solver_ae = mIQAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "RQAE":
for par in ["epsilon", "gamma", "q"]:
val_par = self.kwargs.get(par)
if val_par is not None:
self.solver_dict.update({par : val_par})
self.solver_ae = RQAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "mRQAE":
self.solver_ae = mRQAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "eRQAE":
self.solver_ae = eRQAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "sRQAE":
self.solver_ae = sRQAE(
self.oracle,
target=self.target,
index=self.index,
**self.solver_dict
)
elif self.ae_type == "MCAE":
for par in ["shots"]:
val_par = self.kwargs.get(par)
if val_par is not None:
self.solver_dict.update({par : val_par})
self.solver_ae = MCAE(
self.oracle,
target=self.target,
Expand All @@ -187,6 +182,7 @@ def run(self):
self.oracle_calls = self.solver_ae.oracle_calls
self.max_oracle_depth = self.solver_ae.max_oracle_depth
self.schedule_pdf = self.solver_ae.schedule_pdf

# Recover amplitude estimation from ae_solver
self.ae_pdf = pd.DataFrame(
[self.solver_ae.ae, self.solver_ae.ae_l, self.solver_ae.ae_u],
Expand Down
11 changes: 4 additions & 7 deletions tnbs/BTC_02_AE/QQuantLib/AE/ae_classical_qpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
"""

import sys
import os
import re
import time
#from copy import deepcopy
import numpy as np
import qat.lang.AQASM as qlm

from QQuantLib.utils.qlm_solver import get_qpu
from QQuantLib.PE.classical_qpe import CQPE
from QQuantLib.AA.amplitude_amplification import grover
from QQuantLib.utils.utils import check_list_type
Expand Down Expand Up @@ -71,9 +67,9 @@ def __init__(self, oracle: qlm.QRoutine, target: list, index: list, **kwargs):

# Set the QPU to use
self.linalg_qpu = kwargs.get("qpu", None)
# Provide QPU
if self.linalg_qpu is None:
print("Not QPU was provide. PyLinalg will be used")
self.linalg_qpu = get_qpu("python")
raise ValueError("Not QPU was provide. Please provide it!")
self.auxiliar_qbits_number = kwargs.get("auxiliar_qbits_number", 8)
self.shots = int(kwargs.get("shots", 100))

Expand Down Expand Up @@ -239,4 +235,5 @@ def run(self):
self.max_oracle_depth = 2 ** (int(self.auxiliar_qbits_number)-1) + 1
self.quantum_times = self.cqpe.quantum_times
self.quantum_time = sum(self.cqpe.quantum_times)

return self.ae
11 changes: 3 additions & 8 deletions tnbs/BTC_02_AE/QQuantLib/AE/ae_iterative_quantum_pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@
"""

import sys
import os
import re
import json
import time
#from copy import deepcopy
import numpy as np
import qat.lang.AQASM as qlm

from QQuantLib.utils.qlm_solver import get_qpu
from QQuantLib.PE.iterative_quantum_pe import IQPE
from QQuantLib.AA.amplitude_amplification import grover
from QQuantLib.utils.utils import check_list_type
Expand Down Expand Up @@ -70,9 +65,9 @@ def __init__(self, oracle: qlm.QRoutine, target: list, index: list, **kwargs):

# Set the QPU to use
self.linalg_qpu = kwargs.get("qpu", None)
# Provide QPU
if self.linalg_qpu is None:
print("Not QPU was provide. PyLinalg will be used")
self.linalg_qpu = get_qpu("python")
raise ValueError("Not QPU was provide. Please provide it!")

self.cbits_number = kwargs.get("cbits_number", 8)
self.shots = int(kwargs.get("shots", 100))
Expand Down
Loading

0 comments on commit 6bc2bd9

Please sign in to comment.