Skip to content

Commit

Permalink
Unifying get_qpu PH case
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfeco committed Jan 11, 2024
1 parent aafe07a commit 20e8ae0
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 114 deletions.
5 changes: 4 additions & 1 deletion tnbs/BTC_04_PH/PH/ansatzes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import sys
import logging
import time
import uuid
Expand All @@ -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):
Expand Down
46 changes: 2 additions & 44 deletions tnbs/BTC_04_PH/PH/parent_hamiltonian.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
]
60 changes: 2 additions & 58 deletions tnbs/BTC_04_PH/PH/vqe_step.json
Original file line number Diff line number Diff line change
@@ -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"]
}
]
22 changes: 15 additions & 7 deletions tnbs/BTC_04_PH/PH/vqe_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
Author: Gonzalo Ferro
"""

import sys
import logging
import time
import re
import ast
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__")

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand All @@ -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))
4 changes: 3 additions & 1 deletion tnbs/BTC_04_PH/PH/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__')

Expand Down
58 changes: 58 additions & 0 deletions tnbs/BTC_04_PH/get_qpu.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions tnbs/BTC_04_PH/my_benchmark_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 20e8ae0

Please sign in to comment.