1
$\begingroup$

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 
"""

def prepare_qubits(Sender,receiver,key_bit):

   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(): 
    q.H() #apply hadamard gate
    S_basis='H'
   else:
    S_basis='S'
    pass
   Sender.sendQubit(q,receiver)

   return [q,S_basis]

def receive_qubits(Receiver):
  #R_basis=[] #Receiver basis
  # Wait to  receive a qubit
  q=Receiver.recvQubit()
  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)

 #received Bob's Basis
 x=Alice.recvClassical()
 B_basis=list(x.decode())
 print("Bob basis received",B_basis)
 #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):
  c=receive_qubits(Bob)
  #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)

 #receive Alice's basis
 a=Bob.recvClassical()
 x=a.decode()
 res = ast.literal_eval(x)
 print("Alice's Basis received",res)
 key_s=[]

 for c in res:
  key_s.append(key[c])

 print("final sifted key",key_s)
Bob.close()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.