import sys
sys.path.append('../')
import CreativeQiskit

from qiskit import IBMQ
IBMQ.load_accounts()

L = CreativeQiskit.layout('ibmq_16_melbourne')

L.num

L.pairs

L.pos

L.plot()

colors = {}
labels = {}
sizes = {}
import random
for node in L.pos:
    colors[node] = (random.random(),random.random(),random.random())
    labels[node] = random.choice([';',':','8']) + random.choice([')','(','D'])
    sizes[node] = 1000+2000*(random.random())
    
L.plot(colors=colors,labels=labels,sizes=sizes)

stats = CreativeQiskit.bitstring_superposer(['1100011001111100','0110010101011001'])
print(stats)

probs = L.calculate_probs(stats)
print(probs)

L.plot(probs=probs)

L.plot(probs=probs,colors={0:(0.5,0.5,0.5)})

from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, execute, Aer
import numpy as np
qr = QuantumRegister(16)
cr = ClassicalRegister(16)
qc = QuantumCircuit(qr, cr)
correct_pairs = ['A','P','D','N','H','I']
for pair in correct_pairs:
    qc.rx(random.random()*np.pi,qr[L.pairs[pair][0]])
    qc.cx(qr[L.pairs[pair][0]],qr[L.pairs[pair][1]])
qc.measure(qr,cr)

device = 'qasm_simulator'
backend = CreativeQiskit.get_backend(device)

noisy = True
noise_model = CreativeQiskit.get_noise(noisy)

try:
    job = execute(qc,backend,noise_model=noise_model)
except:
    job = execute(qc,backend)
stats = job.result().get_counts()

probs = L.calculate_probs(stats) 

pair_labels = {}
for node in L.pos:
    if type(node)==str:
        pair_labels[node] = node

chosen_pairs = []
colors = {}
while len(chosen_pairs)<6:
    L.plot(probs=probs,labels=pair_labels,colors=colors)
    pair = str.upper(input("    > Type the name of a pair of qubits whose numbers are the same (or very similar)...\n"))
    chosen_pairs.append( pair )
    colors[pair] = (0.5,0.5,0.5)
    for j in range(2):
        colors[L.pairs[pair][j]] = (0.5,0.5,0.5)

L.plot(probs=probs,labels=pair_labels,colors=colors)
if set(chosen_pairs)==set(correct_pairs):
    print("\n    **You got all the correct pairs! :) **\n")
else:
    print("\n    **You didn't get all the correct pairs! :( **\n")

