from IPython.display import HTML

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code for QISKit exercises."></form>''')

from initialize import *
import random

#initialize quantum program
my_alg = initialize(circuit_name = 'bb84', qubit_number=1, bit_number=1, backend = 'local_qasm_simulator', shots = 1)

#add gates to the circuit

# Alice encodes the bit 1 into a qubit
my_alg.q_circuit.x(my_alg.q_reg[0])

# Alice randomly applies the Hadamard gate to go to the Hadamard basis
a = random.randint(0,1)
if a==1:
    my_alg.q_circuit.h(my_alg.q_reg[0])
    
# Bob randomly applies the Hadamard gate to go to the Hadamard basis
b = random.randint(0,1)
if b==1:
    my_alg.q_circuit.h(my_alg.q_reg[0])

my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures first qubit

# print list of gates in the circuit
print('List of gates:')
for circuit in my_alg.q_circuit:
    print(circuit.name)

#Execute the quantum algorithm
result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)

#Show the results obtained from the quantum algorithm 
counts = result.get_counts(my_alg.circ_name)

print('\nThe measured outcomes of the circuits are:',counts)

if a == b:
    print('Alice and Bob agree on the basis, thus they keep the bit')
else: 
    print("Alice and Bob don't agree the same basis, thus they discard the bit")
    

### <span style="color:blue"> QISKit: EPR protocol </span>

#### <span style="color:blue"> 3) Show the communication of one bit </span>