在E91協議開始時,Asja準備EPR對:
Note : 不需要像 BB84 中那樣選擇隨機位元序列或編碼base。EPR 對的特性之一就是隨機的測量結果。
對於測量,我們的用戶使用四種base:𝑍,𝑋,𝑊,𝑉
Asja 將選擇 - 𝑍,𝑋,𝑊 。
Balvis 將選擇 - 𝑍,𝑊,𝑉
他們需要獨立且隨機地為每個量子位元選擇測量base。
# import all necessary objects and methods for quantum circuits
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from random import randrange
#Registers for measurement bases and keys
basesAsja, keyAsja = [],[]
basesBalvis, keyBalvis = [],[]
for i in range(200): #Asja prepares 200 EPR pairs
qreg = QuantumRegister(2)
creg = ClassicalRegister(2)
mycircuit = QuantumCircuit(qreg, creg)
#Creating entanglement
mycircuit.h(qreg[0])
mycircuit.cx(qreg[0], qreg[1])
#Asja chooses measurement basis
choiceAsja=randrange(3)
if choiceAsja==0:#measurement in Z basis
basesAsja.append('Z')
if choiceAsja==1:#measurement in X basis
mycircuit.h(qreg[0])
basesAsja.append('X')
if choiceAsja==2:#measurement in W basis
mycircuit.s(qreg[0])
mycircuit.h(qreg[0])
mycircuit.t(qreg[0])
mycircuit.h(qreg[0])
basesAsja.append('W')
#Balvis chooses measurement basis
choiceBalvis=randrange(3)
if choiceBalvis==0:#measurement in Z basis
basesBalvis.append('Z')
if choiceBalvis==1:#measurement in W basis
mycircuit.s(qreg[1])
mycircuit.h(qreg[1])
mycircuit.t(qreg[1])
mycircuit.h(qreg[1])
basesBalvis.append('W')
if choiceBalvis==2:#measurement in V basis
mycircuit.s(qreg[1])
mycircuit.h(qreg[1])
mycircuit.tdg(qreg[1])
mycircuit.h(qreg[1])
basesBalvis.append('V')
mycircuit.measure(qreg,creg) #applying final measurement
job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=1)
counts = job.result().get_counts(mycircuit)
#Saving results
result=list(counts.keys())[0] #retrieve key from dictionary
keyAsja.append(int(result[0])) #saving first qubit value in Asja's key register
keyBalvis.append(int(result[1])) #and second to Balvis
讓我們實施篩選階段。
Asja和Balvis比較base。
如果他們使用相同的測量bases,他們會將相應的位元保存到最終的密鑰字串中。
如果bases不匹配,它們會在單獨的暫存器中保留有關它們使用的bases的相應位元和資訊。
#Registers
finalKeyAsja, finalKeyBalvis = [],[] #for matching rounds
diffAsja, diffBalvis = [],[] #missmatched rounds
diffBasesA, diffBasesB = [],[] #bases of missmatched rounds
for i in range(0, len(basesAsja)):
if basesAsja[i] == basesBalvis[i]: #When used same bases
finalKeyAsja.append(keyAsja[i])
finalKeyBalvis.append(keyBalvis[i])
else: #When used different
diffAsja.append(keyAsja[i])
diffBalvis.append(keyBalvis[i])
diffBasesA.append(basesAsja[i])
diffBasesB.append(basesBalvis[i])
當使用者使用相同的基數測量量子位元時,結果是完美的相關性。當他們使用不同的基礎時,結果是完全隨機的(不相關的)。
為了偵測竊聽,Asja 和 Balvis 可以計算 𝑆,也就是 Asja 的bases和 Balvis 的bases之間的相關係數:
𝑆= 𝑍𝑊+ 𝑋𝑊− 𝑋𝑉+ 𝑍𝑉
對於上面四種情況的值可以用以下公式計算:
⟨ 𝐴 ⊗ 𝐵 ⟩ = 𝑃( 1 , 1 ) + 𝑃( 0 , 0 ) − 𝑃( 1 , 0 ) − 𝑃(0,1)
𝑃(1,1)和𝑃(0,0)是機率:測量後 Asja 和 Balvis 獲得相同位元的回合(11和00)除以用這兩個bases進行測量的總回合數。
𝑃(1,0)和𝑃(0,1)是機率:Asja 和 Balvis 獲得不同位元的回合(10和01)除以用這兩個bases進行測量的總回合數。
#ZW
sameZW = 0
diffZW = 0
for i, (bA, bB) in enumerate(zip(diffBasesA, diffBasesB)):
if (bA == 'Z' and bB == 'W'):
if diffAsja[i]==diffBalvis[i]:
sameZW=sameZW+1
else:
diffZW=diffZW+1
totalZW=sameZW+diffZW
if totalZW!=0:
ZW=(sameZW-diffZW)/totalZW
else:
ZW=0
#XW
sameXW = 0
diffXW = 0
for i, (bA, bB) in enumerate(zip(diffBasesA, diffBasesB)):
if (bA == 'X' and bB == 'W'):
if diffAsja[i]==diffBalvis[i]:
sameXW=sameXW+1
else:
diffXW=diffXW+1
totalXW=sameXW+diffXW
if totalXW!=0:
XW=(sameXW-diffXW)/totalXW
else:
XW=0
#XV
sameXV = 0
diffXV = 0
for i, (bA, bB) in enumerate(zip(diffBasesA, diffBasesB)):
if (bA == 'X' and bB == 'V'):
if diffAsja[i]==diffBalvis[i]:
sameXV=sameXV+1
else:
diffXV=diffXV+1
totalXV=sameXV+diffXV
if totalXV!=0:
XV=(sameXV-diffXV)/totalXV
else:
XV=0
#ZV
sameZV = 0
diffZV = 0
for i, (bA, bB) in enumerate(zip(diffBasesA, diffBasesB)):
if (bA == 'Z' and bB == 'V'):
if diffAsja[i]==diffBalvis[i]:
sameZV=sameZV+1
else:
diffZV=diffZV+1
totalZV=sameZV+diffZV
if totalZV!=0:
ZV=(sameZV-diffZV)/totalZV
else:
ZV=0
S=ZW+XW-XV+ZV
print("CHSH inequality value is", S)
CHSH inequality value is 2.9161111111111113
最大程度糾纏的光子將導致 𝑆 =
如果情況並非如此,那麼 Asja 和 Balvis 可以得出結論,Espian 可能嘗試竊聽,造成量子系統中的錯誤。
如果 𝑆 值與量子系統相關連,Asja 和 Balvis 可以使用matching rounds中的位元作為密鑰!
print(finalKeyAsja)
print(finalKeyBalvis)
[1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1]