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 = ['instance', 'iteration', 'distance']
length_instances = 40

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

    # write the header
    writer.writerow(header)
    
    
    for instance in range(length_instances):
        
        job_2, G, UNIFORM_CONVERGENCE_SAMPLE = test_solution()
        # Sort the JSON data based on the value of the brand key
        UNIFORM_CONVERGENCE_SAMPLE.sort(key=lambda x: x["mean"])
        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

        
        iteration = 0
        for sample in UNIFORM_CONVERGENCE_SAMPLE:
            iteration += 1
            mean = sample["mean"]
            distance = sample["distance_pgs"]
            writer.writerow([instance,iteration, distance])
        
    
    
    

