# Running HEA Retraction Error Analysis (eta = 0.1)
# ------------------------------------------------------------
# 10 runs 2 layers
# Qubits:  2 | Parameters (P):  8 | Mean Error: 0.01204 ± 0.00399
# Qubits:  4 | Parameters (P): 16 | Mean Error: 0.01458 ± 0.00257

# Qubits:  6 | Parameters (P): 24 | Mean Error: 0.05106 ± 0.02940
# Qubits:  6 | Parameters (P): 24 | Mean Error: 0.03024 ± 0.00674 Good
# Qubits:  6 | Parameters (P): 24 | Mean Error: 0.04839 ± 0.02144 BAD 
# Qubits:  6 | Parameters (P): 24 | Mean Error: 0.04217 ± 0.02558
# Qubits:  6 | Parameters (P): 24 | Mean Error: 0.01702 ± 0.00395
# Qubits:  6 | Parameters (P): 24 | Mean Error: 0.02948 ± 0.00970

# Qubits:  8 | Parameters (P): 32 | Mean Error: 0.06090 ± 0.03625
# Qubits:  8 | Parameters (P): 32 | Mean Error: 0.03494 ± 0.01578
# Qubits:  8 | Parameters (P): 32 | Mean Error: 0.02432 ± 0.00634
# Qubits:  8 | Parameters (P): 32 | Mean Error: 0.02330 ± 0.00639 Good too small
# Qubits:  8 | Parameters (P): 32 | Mean Error: 0.03767 ± 0.01238 Fine
# Qubits:  8 | Parameters (P): 32 | Mean Error: 0.05687 ± 0.04056 BAD
# Qubits:  8 | Parameters (P): 32 | Mean Error: 0.02315 ± 0.00495
# Qubits:  8 | Parameters (P): 32 | Mean Error: 0.01654 ± 0.00362


selected_data = {  
    "n_qubits" : {2,4,6,8},
    "n_params" : {8,16,24,32},
    "ret_error" : {0.01204,0.01458,0.01702,0.02432},
    "std_dev" : {0.00318, 0.00257, 0.00395,0.00634}
}


import matplotlib.pyplot as plt
import numpy as np

params = np.array([8, 16, 24, 32])
err    = np.array([0.03048, 0.02945, 0.02596, 0.00399])
std    = np.array([0.00967, 0.00751, 0.01049, 0.00255])
rel_err    = np.array([0.17588, 0.21506, 0.29823, 0.52184])
rel_errstd = np.array([0.04862, 0.05226, 0.12416, 0.13161])
eta    = 0.1

fig, ax1 = plt.subplots(figsize=(7, 4.5))
ax2 = ax1.twinx()

ax1.errorbar(params, rel_err, yerr=rel_errstd, fmt='o-', color='tab:red', capsize=5, label='Relative error (% of step)')
ax2.errorbar(params, err, yerr=std, fmt='s--', capsize=5, label='Retraction error')

ax1.set_xlabel('Number of parameters', fontsize=14)
ax1.set_ylabel('Relative retraction error (%)', fontsize=13, color='tab:red')
ax2.set_ylabel('Retraction error', fontsize=13, color='tab:blue')

ax1.tick_params(axis='both', labelsize=13)
ax2.tick_params(axis='y', labelsize=13)
ax1.set_xticks(params)

lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, fontsize=11)

plt.tight_layout()
# plt.savefig('retraction_error.png', dpi=150)
plt.show()





# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.03048 ± 0.00967 | Mean Rel Error: 0.17588 ± 0.04862 # Good
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.02945 ± 0.00751 | Mean Rel Error: 0.21506 ± 0.05226 # Good
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.02596 ± 0.01049 | Mean Rel Error: 0.29823 ± 0.12416 # Good
# Qubits:  8 | Parameters (P): 32 | Mean Abs Error: 0.00399 ± 0.00255 | Mean Rel Error: 0.52184 ± 0.43161 # Good


# err    = np.array([0.03048, 0.02945, 0.02596, 0.00399])
# std    = np.array([0.00967, 0.00751, 0.01049, 0.00255])
# rel_err    = np.array([0.17588, 0.21506, 0.29823, 0.52184])
# rel_errstd = np.array([0.04862, 0.05226, 0.12416, 0.43161])




# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.20791 ± 0.04680 | Mean Rel Error: 1.06044 ± 0.23567
# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.27487 ± 0.09625 | Mean Rel Error: 1.39079 ± 0.47963
# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.60403 ± 0.52966 | Mean Rel Error: 3.04482 ± 2.64906
# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.87750 ± 0.65991 | Mean Rel Error: 4.40308 ± 3.30208
# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.10217 ± 0.03762 | Mean Rel Error: 0.52761 ± 0.18646
# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.03048 ± 0.00967 | Mean Rel Error: 0.17588 ± 0.04862 # Good
# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.03961 ± 0.00834 | Mean Rel Error: 0.21291 ± 0.04003 # Good
# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.64480 ± 0.45812 | Mean Rel Error: 3.24727 ± 2.28932
# Qubits:  2 | Parameters (P):  8 | Mean Abs Error: 0.08030 ± 0.02852 | Mean Rel Error: 0.42565 ± 0.14087

# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.67137 ± 0.58274 | Mean Rel Error: 7.14395 ± 6.38651
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.17324 ± 0.07217 | Mean Rel Error: 1.84453 ± 1.02418
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.04110 ± 0.00970 | Mean Rel Error: 0.34216 ± 0.07375 # Good
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.02945 ± 0.00751 | Mean Rel Error: 0.21506 ± 0.05226 # Good
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.04018 ± 0.01326 | Mean Rel Error: 0.36108 ± 0.09626 # Good
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.16652 ± 0.12381 | Mean Rel Error: 1.23896 ± 0.88540
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.05801 ± 0.01501 | Mean Rel Error: 0.47820 ± 0.10942
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.02921 ± 0.00893 | Mean Rel Error: 0.23644 ± 0.05387 # Good
# Qubits:  4 | Parameters (P): 16 | Mean Abs Error: 0.06471 ± 0.01697 | Mean Rel Error: 0.54976 ± 0.16656

# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.02057 ± 0.00829 | Mean Rel Error: 0.21421 ± 0.05165
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.01951 ± 0.00752 | Mean Rel Error: 0.24858 ± 0.08352
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.00647 ± 0.00171 | Mean Rel Error: 0.10809 ± 0.02212
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.00769 ± 0.00248 | Mean Rel Error: 0.14928 ± 0.04216
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.01770 ± 0.01272 | Mean Rel Error: 0.20310 ± 0.12607
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.00661 ± 0.00187 | Mean Rel Error: 0.11905 ± 0.03671
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.02596 ± 0.01049 | Mean Rel Error: 0.29823 ± 0.12416 # Good
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.01763 ± 0.00651 | Mean Rel Error: 0.28260 ± 0.10577 # Good
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.01101 ± 0.00369 | Mean Rel Error: 0.18700 ± 0.09020
# Qubits:  6 | Parameters (P): 24 | Mean Abs Error: 0.10130 ± 0.06584 | Mean Rel Error: 1.70381 ± 1.15698

# Qubits:  8 | Parameters (P): 32 | Mean Abs Error: 0.01756 ± 0.01330 | Mean Rel Error: 0.37530 ± 0.27495
# Qubits:  8 | Parameters (P): 32 | Mean Abs Error: 0.00109 ± 0.00026 | Mean Rel Error: 0.04596 ± 0.00812
# Qubits:  8 | Parameters (P): 32 | Mean Abs Error: 0.00298 ± 0.00193 | Mean Rel Error: 0.15934 ± 0.10416
# Qubits:  8 | Parameters (P): 32 | Mean Abs Error: 0.00399 ± 0.00255 | Mean Rel Error: 0.52184 ± 0.43161 # Good
# Qubits:  8 | Parameters (P): 32 | Mean Abs Error: 0.00869 ± 0.00472 | Mean Rel Error: 0.18296 ± 0.05999
# Qubits:  8 | Parameters (P): 32 | Mean Abs Error: 0.00272 ± 0.00174 | Mean Rel Error: 0.28665 ± 0.25057
