# Import your solution function
from challenge_submission import compiler_function

# Import submission evaluation and scoring functions
from challenge_evaluation import evaluate, score

# Possibly useful other helper function
from challenge_evaluation import qasm_to_dag_circuit, load_coupling, get_layout

# Select the simulation backend to calculate the quantum states resulting from the circuits
# On Windows platform the C++ Simulator is not yet available with pip install
backend = 'local_qiskit_simulator'
#backend = 'local_qasm_simulator'     # uncomment this line if you are a Windows user


# Load example circuits and coupling maps

ex_nr = 1 # examples to add per qubit number. maximum is 10 with the provided circuits
test_circuit_filenames = {}

for i in range(ex_nr):
    test_circuit_filenames['circuits/random%d_n5_d5.qasm' % i] = get_layout(5)
for i in range(ex_nr):
    test_circuit_filenames['circuits/random%d_n16_d16.qasm' % i] = get_layout(16)
for i in range(ex_nr):
    test_circuit_filenames['circuits/random%d_n20_d20.qasm' % i] = get_layout(20)

# store circuit, coupling map pairs in test_circuits. Circuits are in qasm form.
test_circuits = {}
for filename, cmap in test_circuit_filenames.items():
    with open(filename, 'r') as infile:
        qasm = infile.read()        
        test_circuits[filename] = {"qasm": qasm, "coupling_map": cmap}

result = evaluate(compiler_function, test_circuits, verbose=True, backend = backend)

result

test_circuits['circuits/random0_n5_d5.qasm'].keys()

myres = score(compiler_function, backend = backend)
print("Your compiler scored %6.5f x better \
and was %6.5f x faster than the QISKit reference compiler." % myres)

# import some coupling map generation functions
from gencoupling import *

# generate a square coupling map with 18 qubits and nearest neighbour connectivity
# then remove 4 random links
# also try linear, circle, rect, torus, ibmqx etc.
lr = rect(6,3,order = 0.5, defects = -4)
plot_layout(lr)

# save the coupling map in the layouts folder
lr["name"] = "random_rectangular_lattice_4_defects_q18"
save_coupling(lr)

# load the saved coupling by name
rr = load_coupling("random_rectangular_lattice_4_defects_q18")
rr["coupling_map"]
plot_layout(lr)

# if your working directory is the folder with this notebook you can also use 
import os 
mydir = os.curdir
os.chdir('circuits')
!python generator.py -n 17 -d 17
os.chdir(mydir)

with open('random0_n17_d17.qasm', 'r') as infile:
        qasm = infile.read()
print(qasm)

