diff --git a/misc/idc.py b/misc/idc.py index 4d0a755..ced5e19 100644 --- a/misc/idc.py +++ b/misc/idc.py @@ -4,6 +4,7 @@ import sys +import time import itertools as it import pandas as pd sys.path.append("../") @@ -11,6 +12,29 @@ 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 @@ -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 @@ -265,6 +296,27 @@ 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", @@ -272,6 +324,13 @@ def exe(qpu, shots, model): 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", @@ -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) diff --git a/misc/qpu/model_noise.py b/misc/qpu/model_noise.py index 50c4cc7..064a2e0 100644 --- a/misc/qpu/model_noise.py +++ b/misc/qpu/model_noise.py @@ -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 @@ -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 diff --git a/misc/qpu/qpu_noisy_deterministic.json b/misc/qpu/qpu_noisy_deterministic.json index 0055f07..49f48ba 100644 --- a/misc/qpu/qpu_noisy_deterministic.json +++ b/misc/qpu/qpu_noisy_deterministic.json @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], diff --git a/misc/qpu/qpu_noisy_mpo.json b/misc/qpu/qpu_noisy_mpo.json new file mode 100644 index 0000000..5743a72 --- /dev/null +++ b/misc/qpu/qpu_noisy_mpo.json @@ -0,0 +1,192 @@ + +[ + { + "qpu_type": ["ideal"], + "qpu_name" : ["ideal"], + "kak_compiler" : ["ZXZ"], + "sim_method": [{ + "sim_method": null, + "n_samples": null + }], + "t_gate_1qb" : [null], + "t_gate_2qbs" : [null], + "t_readout": [null], + "depol_channel" : [{ + "active": false, + "error_gate_1qb" : null, + "error_gate_2qbs" : null + }], + "idle" : [{ + "amplitude_damping": false, + "dephasing_channel": false, + "t1" : null, + "t2" : null + }], + "meas": [{ + "active":false, + "readout_error": null + }] + }, + { + "qpu_type": ["noisy"], + "qpu_name" : ["noisy_0"], + "kak_compiler" : ["ZXZ"], + "sim_method": [{ + "sim_method": "mpo", + "bond_dimension": 16 + }], + "t_gate_1qb" : [35], + "t_gate_2qbs" : [660], + "t_readout": [null], + "depol_channel" : [{ + "active": true, + "error_gate_1qb" : 2.27e-9, + "error_gate_2qbs" : 7.741e-8 + }], + "idle" : [{ + "amplitude_damping": true, + "dephasing_channel": true, + "t1" : 23200.0e6, + "t2" : 13300.0e6 + }], + "meas": [{ + "active":false, + "readout_error": null + }] + }, + { + "qpu_type": ["noisy"], + "qpu_name" : ["noisy_1"], + "kak_compiler" : ["ZXZ"], + "sim_method": [{ + "sim_method": "mpo", + "bond_dimension": 16 + }], + "t_gate_1qb" : [35], + "t_gate_2qbs" : [660], + "t_readout": [null], + "depol_channel" : [{ + "active": true, + "error_gate_1qb" : 2.27e-8, + "error_gate_2qbs" : 7.741e-7 + }], + "idle" : [{ + "amplitude_damping": true, + "dephasing_channel": true, + "t1" : 2320.0e6, + "t2" : 1330.0e6 + }], + "meas": [{ + "active":false, + "readout_error": null + }] + }, + { + "qpu_type": ["noisy"], + "qpu_name" : ["noisy_2"], + "kak_compiler" : ["ZXZ"], + "sim_method": [{ + "sim_method": "mpo", + "bond_dimension": 16 + }], + "t_gate_1qb" : [35], + "t_gate_2qbs" : [660], + "t_readout": [null], + "depol_channel" : [{ + "active": true, + "error_gate_1qb" : 2.27e-7, + "error_gate_2qbs" : 7.741e-6 + }], + "idle" : [{ + "amplitude_damping": true, + "dephasing_channel": true, + "t1" : 232.0e6, + "t2" : 133.0e6 + }], + "meas": [{ + "active":false, + "readout_error": null + }] + }, + { + "qpu_type": ["noisy"], + "qpu_name" : ["noisy_3"], + "kak_compiler" : ["ZXZ"], + "sim_method": [{ + "sim_method": "mpo", + "bond_dimension": 16 + }], + "t_gate_1qb" : [35], + "t_gate_2qbs" : [660], + "t_readout": [null], + "depol_channel" : [{ + "active": true, + "error_gate_1qb" : 2.27e-6, + "error_gate_2qbs" : 7.741e-5 + }], + "idle" : [{ + "amplitude_damping": true, + "dephasing_channel": true, + "t1" : 23.2e6, + "t2" : 13.3e6 + }], + "meas": [{ + "active":false, + "readout_error": null + }] + }, + { + "qpu_type": ["noisy"], + "qpu_name" : ["noisy_4"], + "kak_compiler" : ["ZXZ"], + "sim_method": [{ + "sim_method": "mpo", + "bond_dimension": 16 + }], + "t_gate_1qb" : [35], + "t_gate_2qbs" : [660], + "t_readout": [null], + "depol_channel" : [{ + "active": true, + "error_gate_1qb" : 2.27e-5, + "error_gate_2qbs" : 7.741e-4 + }], + "idle" : [{ + "amplitude_damping": true, + "dephasing_channel": true, + "t1" : 2.32e6, + "t2" : 1.33e6 + }], + "meas": [{ + "active":false, + "readout_error": null + }] + }, + { + "qpu_type": ["noisy"], + "qpu_name" : ["noisy_5"], + "kak_compiler" : ["ZXZ"], + "sim_method": [{ + "sim_method": "mpo", + "bond_dimension": 16 + }], + "t_gate_1qb" : [35], + "t_gate_2qbs" : [660], + "t_readout": [null], + "depol_channel" : [{ + "active": true, + "error_gate_1qb" : 2.27e-4, + "error_gate_2qbs" : 7.741e-3 + }], + "idle" : [{ + "amplitude_damping": true, + "dephasing_channel": true, + "t1" : 0.232e6, + "t2" : 0.133e6 + }], + "meas": [{ + "active":false, + "readout_error": null + }] + } +] diff --git a/misc/qpu/qpu_noisy_stochastic.json b/misc/qpu/qpu_noisy_stochastic.json index 07305e8..2c21eeb 100644 --- a/misc/qpu/qpu_noisy_stochastic.json +++ b/misc/qpu/qpu_noisy_stochastic.json @@ -4,8 +4,10 @@ "qpu_type": ["ideal"], "qpu_name" : ["ideal"], "kak_compiler" : ["ZXZ"], - "sim_method": [null], - "n_samples": [null], + "sim_method": [{ + "sim_method": null, + "n_samples": null + }], "t_gate_1qb" : [null], "t_gate_2qbs" : [null], "t_readout": [null], @@ -29,8 +31,10 @@ "qpu_type": ["noisy"], "qpu_name" : ["noisy_0"], "kak_compiler" : ["ZXZ"], - "sim_method": ["stochastic"], - "n_samples": [100, 1000, 10000], + "sim_method": [{ + "sim_method": "stochastic", + "n_samples": 100 + }], "t_gate_1qb" : [35], "t_gate_2qbs" : [660], "t_readout": [null], @@ -54,8 +58,10 @@ "qpu_type": ["noisy"], "qpu_name" : ["noisy_1"], "kak_compiler" : ["ZXZ"], - "sim_method": ["stochastic"], - "n_samples": [100, 1000, 10000], + "sim_method": [{ + "sim_method": "stochastic", + "n_samples": 100 + }], "t_gate_1qb" : [35], "t_gate_2qbs" : [660], "t_readout": [null], @@ -79,8 +85,10 @@ "qpu_type": ["noisy"], "qpu_name" : ["noisy_2"], "kak_compiler" : ["ZXZ"], - "sim_method": ["stochastic"], - "n_samples": [100, 1000, 10000], + "sim_method": [{ + "sim_method": "stochastic", + "n_samples": 100 + }], "t_gate_1qb" : [35], "t_gate_2qbs" : [660], "t_readout": [null], @@ -104,8 +112,10 @@ "qpu_type": ["noisy"], "qpu_name" : ["noisy_3"], "kak_compiler" : ["ZXZ"], - "sim_method": ["stochastic"], - "n_samples": [100, 1000, 10000], + "sim_method": [{ + "sim_method": "stochastic", + "n_samples": 100 + }], "t_gate_1qb" : [35], "t_gate_2qbs" : [660], "t_readout": [null], @@ -129,8 +139,10 @@ "qpu_type": ["noisy"], "qpu_name" : ["noisy_4"], "kak_compiler" : ["ZXZ"], - "sim_method": ["stochastic"], - "n_samples": [100, 1000, 10000], + "sim_method": [{ + "sim_method": "stochastic", + "n_samples": 100 + }], "t_gate_1qb" : [35], "t_gate_2qbs" : [660], "t_readout": [null], @@ -154,8 +166,10 @@ "qpu_type": ["noisy"], "qpu_name" : ["noisy_5"], "kak_compiler" : ["ZXZ"], - "sim_method": ["stochastic"], - "n_samples": [100, 1000, 10000], + "sim_method": [{ + "sim_method": "stochastic", + "n_samples": 100 + }], "t_gate_1qb" : [35], "t_gate_2qbs" : [660], "t_readout": [null], diff --git a/misc/qpu/select_qpu.py b/misc/qpu/select_qpu.py index a02fe7a..4bdbdc9 100644 --- a/misc/qpu/select_qpu.py +++ b/misc/qpu/select_qpu.py @@ -47,6 +47,9 @@ error. """ +import sys +sys.path.append("../") + def select_qpu(hw_cfg): """ This function allows to select a QPU (a ideal or a noisy one). @@ -73,8 +76,7 @@ def select_qpu(hw_cfg): if __name__ == "__main__": import json import argparse - import sys - from benchmark_utils import combination_for_list + from qpu.benchmark_utils import combination_for_list parser = argparse.ArgumentParser() parser.add_argument(