!pip install qiskit

!git clone https://github.com/arnavdas88/QuGlassyIsing

!mv QuGlassyIsing/research/pickle-files/ ./pickle

from qiskit.providers.aer import QasmSimulator  
from qiskit.providers.aer import AerSimulator
from qiskit import Aer, transpile
from qiskit.circuit.library import TwoLocal
from qiskit.algorithms import VQE
from qiskit.algorithms.optimizers import COBYLA 
from qiskit.circuit.library import EfficientSU2
# opflow is Qiskit's module for creating operators like yours
from qiskit import *
from qiskit.opflow import OperatorBase
from qiskit.opflow import Z, X, I  # Pauli Z, X matrices and identity
import pylab
import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline

import os
import pickle

file_list = []
for dirlist in os.walk('./pickle'):
    base, dir, files = dirlist
    if 'j=1' in base:
        for f in files:
            file_list += [os.path.join(base, f)]

print(file_list) 

print("Loading Optimization History")
counts_values = {}
for f in file_list:
    with open(f, 'rb') as handle:
        counts_values[f] = pickle.load(handle)


list(counts_values.values())[0]

tempdata = [ v for v in counts_values.values() ]

def flatten(x): return sum(x, [])

def build_graph_data(x):
    sqrt = len(x)**(float(1)/2)
    sqrt = int(sqrt)
    if not sqrt**2 == len(x):
        raise Exception("not able to form a square")
    result = []
    xa = np.asarray([float(i) for i in x])
    return xa.reshape((sqrt, sqrt)), x


import numpy as np
import seaborn as sns
import matplotlib.pylab as plt

def draw_graph(x, t):
    plt.figure(figsize=(7, 7))
    plt.title(t, pad=6.2, fontsize=18, wrap=True)
    plt.imshow(x, cmap='copper', interpolation='nearest', aspect='auto')
    # plt.xticks(X, X/10, label="Bx", rotation=90, fontsize=16)
    # plt.xlabel('Bx →', fontsize=24)
    # plt.yticks(Y, label="Bz", fontsize=16)
    # plt.ylabel('Bz →', fontsize=24)
    # plt.colorbar()
    # plt.savefig("Heatmap representing the change of `Minimum Ground State Energy` in Bx vs Bz.png", bbox_inches='tight', dpi=1080)
    plt.show()

def build_graph_data_from_list(x):
    for i in x:
        p, t = build_graph_data(i)
        draw_graph(p, t)

import numpy as np
import seaborn as sns
import matplotlib.pylab as plt
from qiskit.visualization import plot_histogram

def show_counts_data_1st(tempdata_i, max_val = 0):
    arr = tempdata_i['vqe_result']
    arr = arr['eigenstate']
    data = np.asarray(arr)
    # print(list(arr.keys()))
    arr = {a: arr[a] for a in arr if arr[a] == max(list(arr.values()))}
    # return plot_histogram(arr)
    return arr
    # return len(list(arr.keys()))

    # return data

de = {  k: show_counts_data_2nd(v)  for k, v in counts_values.items() if not "AF1" in k }

for k, e in de.items():
    print(k)
    build_graph_data_from_list(list(e.keys()))

import numpy as np
import seaborn as sns
import matplotlib.pylab as plt
from qiskit.visualization import plot_histogram

def max_2nd(x):
    x.remove(max(x))
    return max(x)

def show_counts_data_2nd(tempdata_i, max_val = 0):
    arr = tempdata_i['vqe_result']
    arr = arr['eigenstate']
    data = np.asarray(arr)
    # print(list(arr.keys()))
    arr = {a: arr[a] for a in arr if arr[a] == max_2nd(list(arr.values()))}
    # return plot_histogram(arr)
    return arr
    # return len(list(arr.keys()))

    # return data

de = {  k: show_counts_data_2nd(v)  for k, v in counts_values.items() if not "AF1" in k }

for k, e in de.items():
    print(k)
    build_graph_data_from_list(list(e.keys()))


def max_3rd(x):
    x.remove(max(x))
    x.remove(max(x))
    return max(x)

def show_counts_data_3rd(tempdata_i, max_val = 0):
    arr = tempdata_i['vqe_result']
    arr = arr['eigenstate']
    data = np.asarray(arr)
    # print(list(arr.keys()))
    arr = {a: arr[a] for a in arr if arr[a] == max_3rd(list(arr.values()))}
    # return plot_histogram(arr)
    return arr

de = {  k: show_counts_data_3rd(v)  for k, v in counts_values.items()  if not "AF1" in k  }

for k, e in de.items():
    print(k)
    build_graph_data_from_list(list(e.keys()))

