import Functions.Plotting as pt
import numpy as np

#%% Specify the fitting method and loading method
fit_method = 'own'


jobs = ['11_17-17_30_34']#,'11_17-17_31_06','11_17-17_31_30','11_17-17_32_20']#,'11_17-17_32_53']
circuit_name = 'FTSWAP'

#%% Load the results from file
meas_data_all = [];
for jobc,job in enumerate(jobs):
    run_type = 's'
    timestamp = job
    results_loaded = store.load_results(circuit_name, run_type, timestamp)


    #%% Gather the tomo set from the loaded results and its outcomes from the results
    tomo_set = results_loaded['Tomoset']
    results = results_loaded['Results']
    timestamp = results_loaded['Experiment time']
    Unitary = results_loaded['Unitary']
    tomo_data = an.tomo.tomography_data(results, circuit_name, tomo_set)
    meas_data_all.append(tomo_data['data'])
    n = int(np.log2(np.shape(Unitary)[0]))

#%% Tomography; obtaining chi and choi matrices, check CP and TP
B_chi = tomoself.get_pauli_basis(n, normalise = False)
B_choi = tomoself.get_choi_basis(n, B_chi)
import Functions.Data_storage as store
import Analysis.Analysis as an
import Analysis.tomography_functions as tomoself

fit_method = 'own'  # Specify the fithmethod: 'own', 'leastsq' and 'wizard'
n = 2

calcBfilter = True  # True to calculate the B_filter

#%% Gather the results from file

run_type = 'r'
circuit_name = 'Id'
timestamp = None
results_loaded = store.load_results(
    circuit_name, run_type,
    timestamp)  # Load results from Id experiment after providing date and time
timestamp = results_loaded[
    'Experiment time']  # Reload the timestamp from the results for saving the chi matrix dicitonary

#%% Gather the tomo set and its outcomes from the results
tomo_set = results_loaded['Tomoset']
results = results_loaded['Results']
tomo_data = an.tomo.tomography_data(results, circuit_name, tomo_set)

#%% Tomography;
B_chi = tomoself.get_pauli_basis(n)
B_choi = tomoself.get_choi_basis(n, B_chi)

if fit_method == 'wizard':
    # Fitting choi with qiskit functions 'wizard' method and mapping choi to chi