From 30795466362a15aa8f60eff4d4ef7bf568c86058 Mon Sep 17 00:00:00 2001 From: gonfeco Date: Tue, 12 Sep 2023 15:24:23 +0200 Subject: [PATCH] Removed deepcopy stuff --- .../QQuantLib/AA/amplitude_amplification.py | 16 +- .../QQuantLib/AE/ae_classical_qpe.py | 5 +- .../QQuantLib/AE/ae_iterative_quantum_pe.py | 5 +- .../QQuantLib/AE/iterative_quantum_ae.py | 5 +- .../QQuantLib/AE/maximum_likelihood_ae.py | 5 +- tnbs/BTC_02_AE/QQuantLib/AE/montecarlo_ae.py | 5 +- .../BTC_02_AE/QQuantLib/AE/real_quantum_ae.py | 5 +- .../QQuantLib/PE/iterative_quantum_pe.py | 3 +- .../QQuantLib/utils/data_extracting.py | 6 +- tnbs/BTC_02_AE/my_benchmark_execution.py | 11 +- tnbs/BTC_03_QPE/QPE/utils/data_extracting.py | 250 ------------------ tnbs/BTC_03_QPE/QPE/utils/qpe.py | 5 +- 12 files changed, 29 insertions(+), 292 deletions(-) delete mode 100644 tnbs/BTC_03_QPE/QPE/utils/data_extracting.py diff --git a/tnbs/BTC_02_AE/QQuantLib/AA/amplitude_amplification.py b/tnbs/BTC_02_AE/QQuantLib/AA/amplitude_amplification.py index 7099660..380b7da 100644 --- a/tnbs/BTC_02_AE/QQuantLib/AA/amplitude_amplification.py +++ b/tnbs/BTC_02_AE/QQuantLib/AA/amplitude_amplification.py @@ -13,7 +13,6 @@ """ import time -from copy import deepcopy import numpy as np import qat.lang.AQASM as qlm @@ -263,7 +262,6 @@ def create_u_gate(oracle: qlm.QRoutine, mcz_qlm=True): ---------- u_gate : QLM gate """ - oracle_cp = deepcopy(oracle) number_qubits = oracle.arity @qlm.build_gate("U_" + str(time.time_ns()), [], arity=number_qubits) @@ -303,15 +301,14 @@ def grover(oracle: qlm.QRoutine, target: np.ndarray, index: np.ndarray, mcz_qlm= ---------- grover_gate : QLM gate """ - oracle_cp = deepcopy(oracle) - number_qubits = oracle_cp.arity + number_qubits = oracle.arity @qlm.build_gate("G_" + str(time.time_ns()), [], arity=number_qubits) def grover_gate(): routine = qlm.QRoutine() register = routine.new_wires(number_qubits) - routine.apply(create_u0_gate(oracle_cp, target, index, mcz_qlm), register) - routine.apply(create_u_gate(oracle_cp, mcz_qlm), register) + routine.apply(create_u0_gate(oracle, target, index, mcz_qlm), register) + routine.apply(create_u_gate(oracle, mcz_qlm), register) return routine return grover_gate() @@ -341,8 +338,7 @@ def grover_extended(oracle: qlm.QRoutine, target: np.ndarray, index: np.ndarray, ---------- grover_gate : QLM gate """ - oracle_cp = deepcopy(oracle) - number_qubits = oracle_cp.arity + number_qubits = oracle.arity @qlm.build_gate("G'_" + str(time.time_ns()), [], arity=number_qubits) def grover_extended_gate(): routine = qlm.QRoutine() @@ -352,12 +348,12 @@ def grover_extended_gate(): reflection(np.zeros(number_qubits, dtype=int), mcz_qlm), register ) - routine.apply(oracle_cp, register) + routine.apply(oracle, register) routine.apply( reflection(target, mcz_qlm), [register[i] for i in index] ) - routine.apply(oracle_cp.dag(), register) + routine.apply(oracle.dag(), register) return routine return grover_extended_gate() diff --git a/tnbs/BTC_02_AE/QQuantLib/AE/ae_classical_qpe.py b/tnbs/BTC_02_AE/QQuantLib/AE/ae_classical_qpe.py index 68550ee..db71f8c 100644 --- a/tnbs/BTC_02_AE/QQuantLib/AE/ae_classical_qpe.py +++ b/tnbs/BTC_02_AE/QQuantLib/AE/ae_classical_qpe.py @@ -17,7 +17,6 @@ """ import time -from copy import deepcopy import numpy as np import qat.lang.AQASM as qlm import sys @@ -64,7 +63,7 @@ def __init__(self, oracle: qlm.QRoutine, target: list, index: list, **kwargs): """ # Setting attributes - self._oracle = deepcopy(oracle) + self._oracle = oracle self._target = check_list_type(target, int) self._index = check_list_type(index, int) @@ -113,7 +112,7 @@ def oracle(self, value): """ setter of the oracle property """ - self._oracle = deepcopy(value) + self._oracle = value self._grover_oracle = grover( self.oracle, self.target, self.index, mcz_qlm=self.mcz_qlm ) diff --git a/tnbs/BTC_02_AE/QQuantLib/AE/ae_iterative_quantum_pe.py b/tnbs/BTC_02_AE/QQuantLib/AE/ae_iterative_quantum_pe.py index 966f82e..2108020 100644 --- a/tnbs/BTC_02_AE/QQuantLib/AE/ae_iterative_quantum_pe.py +++ b/tnbs/BTC_02_AE/QQuantLib/AE/ae_iterative_quantum_pe.py @@ -15,7 +15,6 @@ """ import time -from copy import deepcopy import numpy as np import qat.lang.AQASM as qlm import sys @@ -62,7 +61,7 @@ def __init__(self, oracle: qlm.QRoutine, target: list, index: list, **kwargs): """ # Setting attributes - self._oracle = deepcopy(oracle) + self._oracle = oracle self._target = check_list_type(target, int) self._index = check_list_type(index, int) @@ -108,7 +107,7 @@ def oracle(self, value): """ setter of the oracle property """ - self._oracle = deepcopy(value) + self._oracle = value self._grover_oracle = grover( self.oracle, self.target, self.index, mcz_qlm=self.mcz_qlm ) diff --git a/tnbs/BTC_02_AE/QQuantLib/AE/iterative_quantum_ae.py b/tnbs/BTC_02_AE/QQuantLib/AE/iterative_quantum_ae.py index d91963f..b33ecb1 100644 --- a/tnbs/BTC_02_AE/QQuantLib/AE/iterative_quantum_ae.py +++ b/tnbs/BTC_02_AE/QQuantLib/AE/iterative_quantum_ae.py @@ -12,7 +12,6 @@ """ import time -from copy import deepcopy import numpy as np import pandas as pd import qat.lang.AQASM as qlm @@ -63,7 +62,7 @@ def __init__(self, oracle: qlm.QRoutine, target: list, index: list, **kwargs): Method for initializing the class """ # Setting attributes - self._oracle = deepcopy(oracle) + self._oracle = oracle self._target = check_list_type(target, int) self._index = check_list_type(index, int) @@ -111,7 +110,7 @@ def oracle(self, value): """ setter of the oracle property """ - self._oracle = deepcopy(value) + self._oracle = value self._grover_oracle = grover( self._oracle, self.target, self.index, mcz_qlm=self.mcz_qlm ) diff --git a/tnbs/BTC_02_AE/QQuantLib/AE/maximum_likelihood_ae.py b/tnbs/BTC_02_AE/QQuantLib/AE/maximum_likelihood_ae.py index d20cdc7..dbf0420 100644 --- a/tnbs/BTC_02_AE/QQuantLib/AE/maximum_likelihood_ae.py +++ b/tnbs/BTC_02_AE/QQuantLib/AE/maximum_likelihood_ae.py @@ -12,7 +12,6 @@ """ import time -from copy import deepcopy from functools import partial import numpy as np import pandas as pd @@ -68,7 +67,7 @@ def __init__(self, oracle: qlm.QRoutine, target: list, index: list, **kwargs): """ # Setting attributes - self._oracle = deepcopy(oracle) + self._oracle = oracle self._target = check_list_type(target, int) self._index = check_list_type(index, int) @@ -134,7 +133,7 @@ def oracle(self, value): """ setter of the oracle property """ - self._oracle = deepcopy(value) + self._oracle = value self._grover_oracle = grover( self.oracle, self.target, self.index, mcz_qlm=self.mcz_qlm ) diff --git a/tnbs/BTC_02_AE/QQuantLib/AE/montecarlo_ae.py b/tnbs/BTC_02_AE/QQuantLib/AE/montecarlo_ae.py index a706290..3de8b8d 100644 --- a/tnbs/BTC_02_AE/QQuantLib/AE/montecarlo_ae.py +++ b/tnbs/BTC_02_AE/QQuantLib/AE/montecarlo_ae.py @@ -9,7 +9,6 @@ """ import time -from copy import deepcopy import numpy as np import pandas as pd import qat.lang.AQASM as qlm @@ -54,7 +53,7 @@ def __init__(self, oracle: qlm.QRoutine, target: list, index: list, **kwargs): Method for initializing the class """ # Setting attributes - self._oracle = deepcopy(oracle) + self._oracle = oracle self._target = check_list_type(target, int) self._index = check_list_type(index, int) @@ -96,7 +95,7 @@ def oracle(self, value): """ setter of the oracle property """ - self._oracle = deepcopy(value) + self._oracle = value @property def target(self): diff --git a/tnbs/BTC_02_AE/QQuantLib/AE/real_quantum_ae.py b/tnbs/BTC_02_AE/QQuantLib/AE/real_quantum_ae.py index a2e2237..0818542 100644 --- a/tnbs/BTC_02_AE/QQuantLib/AE/real_quantum_ae.py +++ b/tnbs/BTC_02_AE/QQuantLib/AE/real_quantum_ae.py @@ -11,7 +11,6 @@ """ import time -from copy import deepcopy import numpy as np import pandas as pd import qat.lang.AQASM as qlm @@ -64,7 +63,7 @@ def __init__(self, oracle: qlm.QRoutine, target: list, index: list, **kwargs): """ ########################################### # Setting attributes - self._oracle = deepcopy(oracle) + self._oracle = oracle self._target = check_list_type(target, int) self._index = check_list_type(index, int) @@ -113,7 +112,7 @@ def oracle(self, value): """ setter of the oracle property """ - self._oracle = deepcopy(value) + self._oracle = value self._grover_oracle = grover( self._oracle, self.target, self.index, mcz_qlm=self.mcz_qlm ) diff --git a/tnbs/BTC_02_AE/QQuantLib/PE/iterative_quantum_pe.py b/tnbs/BTC_02_AE/QQuantLib/PE/iterative_quantum_pe.py index 9db7b92..d7f0a10 100644 --- a/tnbs/BTC_02_AE/QQuantLib/PE/iterative_quantum_pe.py +++ b/tnbs/BTC_02_AE/QQuantLib/PE/iterative_quantum_pe.py @@ -15,7 +15,6 @@ """ import time -from copy import deepcopy import numpy as np import pandas as pd import qat.lang.AQASM as qlm @@ -141,7 +140,7 @@ def init_iqpe(self): """ self.restart() # Create quantum program based on initial state - self.q_prog = create_qprogram(deepcopy(self.initial_state)) + self.q_prog = create_qprogram(self.initial_state) self.q_aux = self.q_prog.qalloc(1) self.c_bits = self.q_prog.calloc(self.cbits_number) diff --git a/tnbs/BTC_02_AE/QQuantLib/utils/data_extracting.py b/tnbs/BTC_02_AE/QQuantLib/utils/data_extracting.py index e798f92..8d2faa3 100644 --- a/tnbs/BTC_02_AE/QQuantLib/utils/data_extracting.py +++ b/tnbs/BTC_02_AE/QQuantLib/utils/data_extracting.py @@ -7,7 +7,6 @@ """ import time -from copy import deepcopy import numpy as np import pandas as pd import qat.lang.AQASM as qlm @@ -55,7 +54,7 @@ def get_results( #print("BE AWARE!! linalg_qpu : {}".format(linalg_qpu)) # if type(quantum_object) == qlm.Program: if isinstance(quantum_object, qlm.Program): - q_prog = deepcopy(quantum_object) + q_prog = quantum_object arity = q_prog.qbit_count else: q_prog = create_qprogram(quantum_object) @@ -140,8 +139,7 @@ def create_qcircuit(prog_q): circuit : QLM circuit """ - q_prog = deepcopy(prog_q) - circuit = q_prog.to_circ(submatrices_only=True)#, inline=True) + circuit = prog_q.to_circ(submatrices_only=True)#, inline=True) return circuit diff --git a/tnbs/BTC_02_AE/my_benchmark_execution.py b/tnbs/BTC_02_AE/my_benchmark_execution.py index b132b93..460cc53 100644 --- a/tnbs/BTC_02_AE/my_benchmark_execution.py +++ b/tnbs/BTC_02_AE/my_benchmark_execution.py @@ -14,7 +14,7 @@ def build_iterator(**kwargs): list4int = [ kwargs['list_of_qbits'], - [0, 1], + kwargs['kernel_configuration']['integrals'], ] iterator = it.product(*list4int) @@ -120,7 +120,7 @@ def compute_samples(**kwargs): #statististical significance. Depends on benchmark kernel #Desired Relative Error for the elapsed Time - relative_error = bench_conf.get("relative_error", None) + relative_error = kwargs.get("relative_error", None) if relative_error is None: relative_error = 0.05 # Compute samples for realtive error metrics: @@ -129,7 +129,7 @@ def compute_samples(**kwargs): (relative_error * metrics[['elapsed_time', 'oracle_calls']].mean()))**2 #Desired Absolute Error. - absolute_error = bench_conf.get("absolute_error", None) + absolute_error = kwargs.get("absolute_error", None) if absolute_error is None: absolute_error = 1e-4 samples_ae = (zalpha * metrics[['absolute_error_sum']].std() \ @@ -300,8 +300,7 @@ def exe(self): ae_problem.update({ "qpu": "c", - "relative_error": None, - "absolute_error": None + "integrals": [0] }) benchmark_arguments = { @@ -317,6 +316,8 @@ def exe(self): "summary_results": "{}_SummaryResults.csv".format(AE), #Computing Repetitions stuff "alpha": None, + "relative_error": None, + "absolute_error": None, "min_meas": None, "max_meas": None, #List number of qubits tested diff --git a/tnbs/BTC_03_QPE/QPE/utils/data_extracting.py b/tnbs/BTC_03_QPE/QPE/utils/data_extracting.py deleted file mode 100644 index 9070c49..0000000 --- a/tnbs/BTC_03_QPE/QPE/utils/data_extracting.py +++ /dev/null @@ -1,250 +0,0 @@ -""" -This module contains auxiliary functions for executing QLM programs based -on QLM Routines or QLM gates and for post processing results from QLM -qpu executions - -Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro Costas -""" - -import time -from copy import deepcopy -import numpy as np -import pandas as pd -import qat.lang.AQASM as qlm -from qat.core import Result - -pd.options.display.float_format = "{:.6f}".format -np.set_printoptions(suppress=True) - -def check_list_type(x_input, tipo): - """Check if a list x_input is of type tipo - Parameters - ---------- - x_input : list - tipo : data type - it has to be understandable by numpy - - Returns - ---------- - y_output : np.array - numpy array of type tipo. - """ - try: - y_output = np.array(x_input).astype(tipo, casting="safe") - except TypeError: - exception = "Only a list/array of " + str(tipo) + " are aceptable types" - raise Exception(exception) from TypeError - return y_output - -def get_results( - quantum_object, - linalg_qpu, - shots: int = 0, - qubits: list = None, - complete: bool = False -): - """ - Function for testing an input gate. This function creates the - quantum program for an input gate, the correspondent circuit - and job. Execute the job and gets the results - - Parameters - ---------- - quantum_object : QLM Gate, Routine or Program - linalg_qpu : QLM solver - shots : int - number of shots for the generated job. - if 0 True probabilities will be computed - qubits : list - list with the qubits for doing the measurement when simulating - if None measurement over all allocated qubits will be provided - complete : bool - for return the complete basis state. Useful when shots is not 0 - and all the posible basis states are necessary. - - Returns - ---------- - pdf : pandas DataFrame - DataFrame with the results of the simulation - circuit : QLM circuit - q_prog : QLM Program. - job : QLM job - """ - #print("BE AWARE!! linalg_qpu : {}".format(linalg_qpu)) - # if type(quantum_object) == qlm.Program: - if isinstance(quantum_object, qlm.Program): - q_prog = deepcopy(quantum_object) - arity = q_prog.qbit_count - else: - q_prog = create_qprogram(quantum_object) - arity = quantum_object.arity - - if qubits is None: - qubits = np.arange(arity, dtype=int) - else: - qubits = check_list_type(qubits, int) - # circuit = q_prog.to_circ(submatrices_only=True) - start = time.time() - circuit = create_qcircuit(q_prog) - end = time.time() - time_q_circuit = end - start - - start = time.time() - # job = circuit.to_job(nbshots=shots, qubits=qubits) - job = create_qjob(circuit, shots=shots, qubits=qubits) - end = time.time() - time_q_job = end - start - - start = time.time() - result = linalg_qpu.submit(job) - if not isinstance(result, Result): - result = result.join() - # time_q_run = float(result.meta_data["simulation_time"]) - qpu_type = "QLM_QPU" - else: - qpu_type = "No QLM_QPU" - end = time.time() - time_q_run = end - start - # Process the results - start = time.time() - pdf = proccess_qresults(result, qubits, complete=complete) - end = time.time() - time_post_proccess = end - start - - time_dict = { - "time_q_circuit": time_q_circuit, - "time_q_job": time_q_job, - "time_q_run": time_q_run, - "time_post_proccess": time_post_proccess, - } - pdf_time = pd.DataFrame([time_dict]) - pdf_time["time_total"] = pdf_time.sum(axis=1) - pdf_time["qpu_type"] = qpu_type - - return pdf, circuit, q_prog, job - - -def create_qprogram(quantum_gate): - """ - Creates a Quantum Program from an input qlm gate or routine - - Parameters - ---------- - - quantum_gate : QLM gate or QLM routine - - Returns - ---------- - q_prog: QLM Program. - Quantum Program from input QLM gate or routine - """ - q_prog = qlm.Program() - qbits = q_prog.qalloc(quantum_gate.arity) - q_prog.apply(quantum_gate, qbits) - return q_prog - - -def create_qcircuit(prog_q): - """ - Given a QLM program creates a QLM circuit - - Parameters - ---------- - - prog_q : QLM QProgram - - Returns - ---------- - - circuit : QLM circuit - """ - q_prog = deepcopy(prog_q) - circuit = q_prog.to_circ(submatrices_only=True)#, inline=True) - return circuit - - -def create_qjob(circuit, shots=0, qubits=None): - """ - Given a QLM circuit creates a QLM job - - Parameters - ---------- - - circuit : QLM circuit - shots : int - number of measurmentes - qubits : list - with the qubits to be measured - - Returns - ---------- - - job : QLM job - job for submit to QLM QPU - """ - dict_job = {"amp_threshold": 0.0} - if qubits is None: - job = circuit.to_job(nbshots=shots, **dict_job) - else: - if isinstance(qubits, (np.ndarray, list)): - job = circuit.to_job(nbshots=shots, qubits=qubits, **dict_job) - else: - raise ValueError("qbits: sould be a list!!!") - return job - - -def proccess_qresults(result, qubits, complete=False): - """ - Post Process a QLM results for creating a pandas DataFrame - - Parameters - ---------- - - result : QLM results from a QLM qpu. - returned object from a qpu submit - qubits : int - number of qubits - complete : bool - for return the complete basis state. - """ - - # Process the results - if complete: - states = [] - list_int = [] - list_int_lsb = [] - for i in range(2**qubits.size): - reversed_i = int("{:0{width}b}".format(i, width=qubits.size)[::-1], 2) - list_int.append(reversed_i) - list_int_lsb.append(i) - states.append("|" + bin(i)[2:].zfill(qubits.size) + ">") - - probability = np.zeros(2**qubits.size) - amplitude = np.zeros(2**qubits.size, dtype=np.complex_) - for samples in result: - probability[samples.state.lsb_int] = samples.probability - amplitude[samples.state.lsb_int] = samples.amplitude - - pdf = pd.DataFrame( - { - "States": states, - "Int_lsb": list_int_lsb, - "Probability": probability, - "Amplitude": amplitude, - "Int": list_int, - } - ) - else: - list_for_results = [] - for sample in result: - list_for_results.append([ - sample.state, sample.state.lsb_int, sample.probability, - sample.amplitude, sample.state.int, - ]) - - pdf = pd.DataFrame( - list_for_results, - columns=['States', "Int_lsb", "Probability", "Amplitude", "Int"] - ) - pdf.sort_values(["Int_lsb"], inplace=True) - return pdf diff --git a/tnbs/BTC_03_QPE/QPE/utils/qpe.py b/tnbs/BTC_03_QPE/QPE/utils/qpe.py index 83809f7..fbf1573 100644 --- a/tnbs/BTC_03_QPE/QPE/utils/qpe.py +++ b/tnbs/BTC_03_QPE/QPE/utils/qpe.py @@ -4,7 +4,6 @@ """ import time -from copy import deepcopy import numpy as np import pandas as pd import qat.lang.AQASM as qlm @@ -69,7 +68,7 @@ def get_results( #print("BE AWARE!! linalg_qpu : {}".format(linalg_qpu)) # if type(quantum_object) == qlm.Program: if isinstance(quantum_object, qlm.Program): - q_prog = deepcopy(quantum_object) + q_prog = quantum_object arity = q_prog.qbit_count else: q_prog = create_qprogram(quantum_object) @@ -154,7 +153,7 @@ def create_qcircuit(prog_q): circuit : QLM circuit """ - q_prog = deepcopy(prog_q) + q_prog = prog_q circuit = q_prog.to_circ(submatrices_only=True)#, inline=True) return circuit