Skip to content

Commit

Permalink
Modification to QPU package
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfeco committed Jul 9, 2024
1 parent aca5834 commit dc3e505
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 37 deletions.
71 changes: 66 additions & 5 deletions misc/idc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,37 @@


import sys
import time
import itertools as it
import pandas as pd
sys.path.append("../")
from neasqc_qrbs.qrbs import QRBS
from neasqc_qrbs.knowledge_rep import AndOperator, OrOperator, NotOperator
from selectable_qpu import SelectableQPU

def save(save, save_name, input_pdf, save_mode):
"""
For saving panda DataFrames to csvs
Parameters
----------
save: bool
For saving or not
save_nam: str
name for file
input_pdf: pandas DataFrame
save_mode: str
saving mode: overwrite (w) or append (a)
"""
if save:
with open(save_name, save_mode) as f_pointer:
input_pdf.to_csv(
f_pointer,
mode=save_mode,
header=f_pointer.tell() == 0,
sep=';'
)
def idc_qrbs(row, qpu=None, shots=None, model='cf'):
"""
QRBS implementation of the IDC
Expand Down Expand Up @@ -229,11 +253,18 @@ def prepare_input():

return tmn_final

def exe(qpu, shots, model):
def exe(qpu_cfg, qpu, shots, model, save_name, save_):
tmn_final = prepare_input()
lista = []
for row in tmn_final[:2].iterrows():
print(idc_qrbs(row[1], qpu, shots, model))
pdf = pd.DataFrame.from_dict(qpu_cfg, orient="index").T
for row in tmn_final[:3].iterrows():
tick = time.time()
step = idc_qrbs(row[1], qpu, shots, model)
tack = time.time()
step["elapsed"] = tack - tick
step = pd.concat([step, pdf], axis=1)
step["tmn"] = row[1]["tmn"]
save(save_, save_name, step, "a")

if __name__ == "__main__":
import argparse
Expand Down Expand Up @@ -265,13 +296,41 @@ def exe(qpu, shots, model):
help="For selecting the desired qpu",
default=None,
)
parser.add_argument(
"-shots",
dest="shots",
type=int,
help="Number of shots for quantum circuit",
default=100,
)
parser.add_argument(
"-model",
dest="model",
type=str,
default="cf",
help="Inferential model desired: cf, bayes, fuzzy"
)
parser.add_argument(
"-name",
dest="base_name",
type=str,
help="Additional name for the generated files",
default="",
)
parser.add_argument(
"--print",
dest="print",
default=False,
action="store_true",
help="For printing "
)
parser.add_argument(
"--save",
dest="save",
default=False,
action="store_true",
help="For saving staff"
)
parser.add_argument(
"--exe",
dest="execution",
Expand All @@ -294,5 +353,7 @@ def exe(qpu, shots, model):
print(final_list)
if args.execution:
if args.id is not None:
qpu = select_qpu(final_list[args.id])
exe(qpu, 100, "cf")
qpu_cfg = final_list[args.id]
qpu = select_qpu(qpu_cfg)
base_name = args.base_name + "_" + str(args.id) + ".csv"
exe(qpu_cfg, qpu, args.shots, args.model, base_name, args.save)
21 changes: 14 additions & 7 deletions misc/qpu/model_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def create_qpu(hw_cfg):
generated QPU (can be a noisy one)
"""

from qat.qpus import NoisyQProc, LinAlg
from qat.qpus import NoisyQProc, LinAlg, MPO

# First: Rewriter of Rotation Gates
from qat.plugins import KAKCompression
Expand All @@ -175,21 +175,28 @@ def create_qpu(hw_cfg):
# Added QPU
if hw_cfg["qpu_type"] == "noisy":
model_noisy = noisy_hw_model(hw_cfg)
if hw_cfg["sim_method"] not in ["stochastic", "deterministic", "deterministic-vectorized"]:
qpu_cfg = hw_cfg["sim_method"]
if qpu_cfg["sim_method"] not in ["stochastic", "deterministic", "deterministic-vectorized", "mpo"]:
raise ValueError("sim_method MUST BE stochastic, deterministic, deterministic-vectorized")
if hw_cfg["sim_method"] == "stochastic":
if qpu_cfg["sim_method"] == "stochastic":
my_qpu= NoisyQProc(
hardware_model=model_noisy,
sim_method=hw_cfg["sim_method"],
sim_method=qpu_cfg["sim_method"],
backend_simulator=LinAlg(),
n_samples = hw_cfg["n_samples"]
n_samples = qpu_cfg["n_samples"]
)
else:
if qpu_cfg["sim_method"] in ["deterministic", "deterministic-vectorized"]:
my_qpu= NoisyQProc(
hardware_model=model_noisy,
sim_method=hw_cfg["sim_method"],
sim_method=qpu_cfg["sim_method"],
backend_simulator=LinAlg(),
)
if qpu_cfg["sim_method"] in ["mpo"]:
my_qpu = MPO(
hardware_model=model_noisy,
bond_dimension=qpu_cfg["bond_dimension"]
)

else:
my_qpu = LinAlg()
my_plugin = my_plugin | my_qpu
Expand Down
30 changes: 21 additions & 9 deletions misc/qpu/qpu_noisy_deterministic.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"qpu_type": ["ideal"],
"qpu_name" : ["ideal"],
"kak_compiler" : ["ZXZ"],
"sim_method": ["deterministic"],
"n_samples": [null],
"sim_method": [{
"sim_method": "deterministic"
}],
"t_gate_1qb" : [null],
"t_gate_2qbs" : [null],
"t_readout": [null],
Expand All @@ -29,8 +30,9 @@
"qpu_type": ["noisy"],
"qpu_name" : ["noisy_0"],
"kak_compiler" : ["ZXZ"],
"sim_method": ["deterministic"],
"n_samples": [null],
"sim_method": [{
"sim_method": "deterministic"
}],
"t_gate_1qb" : [35],
"t_gate_2qbs" : [660],
"t_readout": [null],
Expand All @@ -54,7 +56,9 @@
"qpu_type": ["noisy"],
"qpu_name" : ["noisy_1"],
"kak_compiler" : ["ZXZ"],
"sim_method": ["deterministic"],
"sim_method": [{
"sim_method": "deterministic"
}],
"n_samples": [null],
"t_gate_1qb" : [35],
"t_gate_2qbs" : [660],
Expand All @@ -79,7 +83,9 @@
"qpu_type": ["noisy"],
"qpu_name" : ["noisy_2"],
"kak_compiler" : ["ZXZ"],
"sim_method": ["deterministic"],
"sim_method": [{
"sim_method": "deterministic"
}],
"n_samples": [null],
"t_gate_1qb" : [35],
"t_gate_2qbs" : [660],
Expand All @@ -104,7 +110,9 @@
"qpu_type": ["noisy"],
"qpu_name" : ["noisy_3"],
"kak_compiler" : ["ZXZ"],
"sim_method": ["deterministic"],
"sim_method": [{
"sim_method": "deterministic"
}],
"n_samples": [null],
"t_gate_1qb" : [35],
"t_gate_2qbs" : [660],
Expand All @@ -129,7 +137,9 @@
"qpu_type": ["noisy"],
"qpu_name" : ["noisy_4"],
"kak_compiler" : ["ZXZ"],
"sim_method": ["deterministic"],
"sim_method": [{
"sim_method": "deterministic"
}],
"n_samples": [null],
"t_gate_1qb" : [35],
"t_gate_2qbs" : [660],
Expand All @@ -154,7 +164,9 @@
"qpu_type": ["noisy"],
"qpu_name" : ["noisy_5"],
"kak_compiler" : ["ZXZ"],
"sim_method": ["deterministic"],
"sim_method": [{
"sim_method": "deterministic"
}],
"n_samples": [null],
"t_gate_1qb" : [35],
"t_gate_2qbs" : [660],
Expand Down
Loading

0 comments on commit dc3e505

Please sign in to comment.