from tsp_qaoa import test_solution
from qiskit.visualization import plot_histogram
import networkx as nx
import numpy as np

 job_2, G, UNIFORM_CONVERGENCE_SAMPLE = test_solution()


plot_histogram(job_2.result().get_counts(), color='midnightblue', title="New Histogram", figsize=(30, 5))


plot_histogram(job_2.result().get_counts(), color='midnightblue', title="New Histogram", figsize=(30, 5))

G

labels = nx.get_edge_attributes(G,'weight')

labels

import json

# Array of JSON Objects
products = [{"name": "HDD", "brand": "Samsung", "price": "$100"},
            {"name": "Monitor", "brand": "Dell", "price": "$120"},
            {"name": "Mouse", "brand": "Logitech", "price": "$10"}]

# Print the original data
print("The original JSON data:\n{0}".format(products))
# Sort the JSON data based on the value of the brand key
products.sort(key=lambda x: x["price"])

# Print the sorted JSON data
print("The sorted JSON data based on the value of the brand:\n{0}".format(products))

_LAMBDA

UNIFORM_CONVERGENCE_SAMPLE

import json
# 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"])

# Print the sorted JSON data
UNIFORM_CONVERGENCE_SAMPLE

np.max(UNIFORM_CONVERGENCE_SAMPLE[0]["probabilities"] - UNIFORM_CONVERGENCE_SAMPLE[220]["probabilities"]) 

# generamos las distancias entre para la convergencia uniforme

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
UNIFORM_CONVERGENCE_SAMPLE

import csv

header = ['iteration', 'state', 'probability', 'mean']
header_q = ['iteration', 'distance']

with open('qaoa_cu.csv', 'w', encoding='UTF8') as f:
    with open('qaoa_distance.csv', 'w', encoding='UTF8') as q:
        writer = csv.writer(f)
        writer_q = csv.writer(q)

        # write the header
        writer.writerow(header)
        writer_q.writerow(header_q)

        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])
        
    
    
    
    #plot_histogram(job_2, color='midnightblue', title=str(mean), figsize=(30, 5)).savefig(str(contador) + "_2.png")
    #print(sample["mean"])

plot_histogram(job_2, color='midnightblue', title="New Histogram", figsize=(30, 5)).savefig('out.png')

