## Why Alice and Bob don't get the same result when they measure in the same basis?

1

I'm simulating the BB84 protocol using simulaqron. My problem is that when Alice and Bob measure in the same basis, they don't get the same result.

How can I fix this error?

.

BB84.py

from cqc.pythonLib import CQCConnection, qubit
import numpy as np
"""
BB84 protocol
"""

q=qubit(Sender)
S_basis="" # The sender basis

if key_bit == 1:  #prepare qubits in |1> state
q.X() #apply X gate
else:
pass
# convert  to  Hadamard  basis randomly
if 0.5 < np.random.random():
S_basis='H'
else:
S_basis='S'
pass

return [q,S_basis]

# Wait to  receive a qubit
C="" # for basis choice
# If we  chose  the  Hadamard  basis
# to  measure in, apply H
if 0.5 < np.random.random():
q.H()
C='H'
else:
C='S' #S: Standard Basis
pass
# Measure  the  qubit  in the  standard
# basis  and  store  the  outcome  in m
m=q.measure ()
return [m,C]

Alice.py

from cqc.pythonLib import CQCConnection, qubit
import numpy as np
from BB84 import *
from CAC.classical_comm import *

# Establish a connection  to  SimulaQron
with CQCConnection("Alice") as Alice:

print("***********************BB84 protocol***************************")
#Alice.closeClassicalServer()
m=[]
B=[]
S=''
n=2 #number of qubits
Alice.sendClassical("Bob",n)#send the number of qubits
key= np.random.randint(0, high=2**n)#to replace with QRNG() function
k=np.binary_repr(key,n) #binary representation
print("Alice's random key",key)
print("Alice's binary key:",k)
print("*********************************")
for x in k:

m.append(str(x))
s=prepare_qubits(Alice,"Bob",x) #send BB84 states
#receive a confirmation msg from Bob via the CAC
B.append(s[1])
S+=str(s[1])
"""
#send Alice's basis to Bob
ff=S.encode()#conversion to byte
Alice.sendClassical("Bob",ff)
"""

print('a basis',B)

x=Alice.recvClassical()
B_basis=list(x.decode())
#print(B_basis.split(""))
aa=sifted_key(B,B_basis,m)
print("sifted key",aa)

listToStr ="["+','.join(map(str, aa[1]))+"]"
ff=listToStr.encode()
Alice.sendClassical("Bob",ff)

#Close  connection  to  SimulaQron
Alice.close()

Bob.py

from cqc.pythonLib import CQCConnection, qubit
import numpy as np
from BB84 import *
from CAC.classical_comm import *
import ast

# Establish  connection  to  SimulaQron
with CQCConnection("Bob") as Bob:
#Bob.closeClassicalServer() #if I want to use my socket functions
n=Bob.recvClassical()[0] #number of qubits given by Bob ("Eve")
key=[]
B=''
for i in range(0,n):
#send confirmation to Alice via CAC
#print("out meas.",c[0])
key.append(c[0])
B+=str(c[1]) #Bob basis

print("Bob off",B)
#send Basis to Alice
print("bob's initial key :",key)
ff=B.encode()#conversion to byte
Bob.sendClassical("Alice",ff)