%matplotlib inline

# Importing standard Qiskit libraries
import random
from qiskit import execute, Aer, IBMQ
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from may4_challenge.ex3 import alice_prepare_qubit, check_bits, check_key, check_decrypted, show_message

# Configuring account
provider = IBMQ.load_account()
backend = provider.get_backend('ibmq_qasm_simulator')  # with this simulator it wouldn't work \

# Initial setup
random.seed(84) # do not change this seed, otherwise you will get a different key

# This is your 'random' bit string that determines your bases
numqubits = 100
bob_bases = str('{0:0100b}'.format(random.getrandbits(numqubits)))

def bb84():
    print('Bob\'s bases:', bob_bases)

    # Now Alice will send her bits one by one...
    all_qubit_circuits = []
    for qubit_index in range(numqubits):

        # This is Alice creating the qubit
        thisqubit_circuit = alice_prepare_qubit(qubit_index)

        # This is Bob finishing the protocol below
        bob_measure_qubit(bob_bases, qubit_index, thisqubit_circuit)

        # We collect all these circuits and put them in an array
        all_qubit_circuits.append(thisqubit_circuit)
    
    # Now execute all the circuits for each qubit
    results = execute(all_qubit_circuits, backend=backend, shots=1).result()

    # And combine the results
    bits = ''
    for qubit_index in range(numqubits):
        bits += [measurement for measurement in results.get_counts(qubit_index)][0]
        
    return bits

# Here is your task
def bob_measure_qubit(bob_bases, qubit_index, qubit_circuit):
    
    #
 
    #qc = QuantumCircuit()
    if bob_bases[qubit_index] == '1':
        qubit_circuit.h(0)
    else:
        pass
   
    #return qc

    #qubit_circuit.cx(bob_bases, qubit_index)
    
    # insert your code here to measure Alice's bits
    
    qubit_circuit.measure(0, 0)
    #
    ...

bits = bb84()        
print('Bob\'s bits: ', bits)
check_bits(bits)

alice_bases = '1000000000010001111111001101100101000111110100110111111000110000011000001001100011100111010010000110' # Alice's bases bits
bob_bases = '1100111010011111111111110100000111010100100010011001001110100001010010111011010001011001111111011111'
#bob ='1000010000010000001110100010100111100101000111001111100010000110000000011110110101101101101001011000'
#bob_bases = bob[::-1]
#print(bob_bases)


#def getKey():
key = ""
for qubit_index in range(len(bob_bases)):
    if alice_bases[qubit_index] == bob_bases[qubit_index]:
            key = key + bits[qubit_index]
    else:
        continue
print(key)




check_key(key)

m = '0011011010100011101000001100010000001000011000101110110111100111111110001111100011100101011010111010111010001'\
    '1101010010111111100101000011010011011011011101111010111000101111111001010101001100101111011' # encrypted message
key='10000010001110010011101001010000110000110011100000100000100011100100111010010100001100001100111000001000001000111001001110100101000011000011001110000010000010001110010011101001010000110000110011100000'
c=int(m, 2) ^ int(key, 2)
decrypted = "{0:b}".format(c)
print(decrypted)
check_decrypted(decrypted)

MORSE_CODE_DICT = { 'a':'.-', 'b':'-...', 
                    'c':'-.-.', 'd':'-..', 'e':'.', 
                    'f':'..-.', 'g':'--.', 'h':'....', 
                    'i':'..', 'j':'.---', 'k':'-.-', 
                    'l':'.-..', 'm':'--', 'n':'-.', 
                    'o':'---', 'p':'.--.', 'q':'--.-', 
                    'r':'.-.', 's':'...', 't':'-', 
                    'u':'..-', 'v':'...-', 'w':'.--', 
                    'x':'-..-', 'y':'-.--', 'z':'--..', 
                    '1':'.----', '2':'..---', '3':'...--', 
                    '4':'....-', '5':'.....', '6':'-....', 
                    '7':'--...', '8':'---..', '9':'----.', 
                    '0':'-----', ', ':'--..--', '.':'.-.-.-', 
                    '?':'..--..', '/':'-..-.', '-':'-....-', 
                    '(':'-.--.', ')':'-.--.-'} 
                            
# insert your code here to decode Alice's Morse code
#I solved this part of excercise manually but I will upload it's code soon IA
solution="http://reddit.com/R/MAY4QUANTUM"

show_message(solution)