# Checking the version of PYTHON; we only support > 3.5
import sys
if sys.version_info < (3,5):
    raise Exception('Please use Python version 3.5 or greater.')

#Importing qiskit and math lib
from qiskit import  QuantumProgram
import math

#import Qconfig

pi = math.pi

theta_list = [0.01, 0.02, 0.03, 0.04, 0.05, 1.31, 1.32, 1.33, 1.34, 1.35]

Q_program = QuantumProgram()
#Q_program.set_api(Qconfig.APItoken, Qconfig.config['url'])

def k_means():
    # create Quantum Register called "qr" with 5 qubits
    qr = Q_program.create_quantum_register("qr", 5)
    # create Classical Register called "cr" with 5 bits
    cr = Q_program.create_classical_register("cr", 5)
    
    # Creating Quantum Circuit called "qc" involving your Quantum Register "qr"
    # and your Classical Register "cr"
    qc = Q_program.create_circuit("k_means", [qr], [cr])
    
    #Define a loop to compute the distance between each pair of points
    for i in range(9):
        for j in range(1,10-i):
            # Set the parament theta about different point
            theta_1 = theta_list[i]
            theta_2 = theta_list[i+j]
            #Achieve the quantum circuit via qiskit
            qc.h(qr[2])
            qc.h(qr[1])
            qc.h(qr[4])
            qc.u3(theta_1, pi, pi, qr[1])
            qc.u3(theta_2, pi, pi, qr[4])
            qc.cswap(qr[2], qr[1], qr[4])
            qc.h(qr[2])
                
            qc.measure(qr[2], cr[2])
            qc.reset(qr)
            
            result = Q_program.execute("k_means",   backend = 'local_qasm_simulator')   
            print(result)
            print('theta_1:' + str(theta_1))
            print('theta_2:' + str(theta_2))
            print( result.get_data("k_means"))
                
            #result = Q_program.execute(["reset"], backend='ibmqx4', shots=1024, timeout=600)

if __name__ == "__main__":
    k_means()

%run "../version.ipynb"