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

header = ['p', 'state', 'probability', 'mean']
length_p = 10

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

    # write the header
    writer.writerow(header)
    first_p = False
    
    for p in range(length_p):
        p = p+1
        if first_p == False:
            job_2, G, UNIFORM_CONVERGENCE_SAMPLE = test_solution(p=p)
            first_p = True
        else:
            job_2, G, UNIFORM_CONVERGENCE_SAMPLE = test_solution(grafo=G, p=p)
            
        # Sort the JSON data based on the value of the brand key
        UNIFORM_CONVERGENCE_SAMPLE.sort(key=lambda x: x["mean"])


        mean = UNIFORM_CONVERGENCE_SAMPLE[0]["mean"]
        print(mean)
        state = 0
        for probability in UNIFORM_CONVERGENCE_SAMPLE[0]["probabilities"]:
            state += 1
            writer.writerow([p,state, probability,mean])
        
    
    
    

