from tsp_qaoa import test_solution
from qiskit.visualization import plot_histogram
import networkx as nx
import numpy as np
import json
import csv
# Array of JSON Objects





# Sort the JSON data based on the value of the brand key
UNIFORM_CONVERGENCE_SAMPLE.sort(key=lambda x: x["mean"])

# genera las distancias
index = -1

for sample in UNIFORM_CONVERGENCE_SAMPLE:
    mean = sample["mean"]
    index += 1
    distance_p_ground_state = np.max(np.abs(UNIFORM_CONVERGENCE_SAMPLE[0]["probabilities"] - sample["probabilities"]))
    UNIFORM_CONVERGENCE_SAMPLE[index]["distance_pgs"] = distance_p_ground_state

    


header = ['instance', 'iteration', 'distance']
length_instances = 2

with open('qaoa_multiple_distance.csv', 'w', encoding='UTF8') as f:
    writer = csv.writer(f)

    # write the header
    writer.writerow(header)
    
    
    for i in range(length_instances):
         job_2, G, UNIFORM_CONVERGENCE_SAMPLE = test_solution()
        
    iteration = 0
    for sample in UNIFORM_CONVERGENCE_SAMPLE:
        iteration += 1
        mean = sample["mean"]
        distance = sample["distance_pgs"]
        state = 0
        for probability in  sample["probabilities"]:
            state += 1
            # write the data
            data = [iteration, state, probability, mean]
            writer.writerow(data)

        writer_q.writerow([iteration, distance])
        
    
    
    