簡體   English   中英

僅將不同文件中的特定變量導入當前文件

[英]Importing only specific variable from different file to current file

我在同一個文件夾alice.pybob.py上有 2 個文件。 這些是我寫的程序:

#alice.py

import random
import numpy as np
from numpy.random import randint

equal_to = {"upl":"u_plus", "EPR":"u_minus", "vpl":"v_plus", "vmin":"v_minus"}

np.random.seed()
n = 8

alice_bits = randint(2, size=n)

bell_state = []
for i in range(n):
    while True:
        attempt = str(random.choice(list(equal_to)))
        bell_state.append(attempt)

        if attempt == 'EPR':
            break

def eva(alice, bell):
    if alice == 1:
        if bell == 'upl' or bell == 'EPR':
            return 1
        elif bell == 'vpl' or bell == 'vmin':
            return 0
    elif alice == 0:
        if bell == 'vpl' or bell == 'vmin':
            return 1
        elif bell == 'upl' or bell == 'EPR':
            return 0

encrypted_bits = []
_tmp = bell_state[:]
for i in alice_bits:
    while _tmp:
        if _tmp[:1] != ['EPR']:
            encrypted_bits.append(eva(i, _tmp[0]))
            _tmp = _tmp[1:]
        else:
            encrypted_bits.append(eva(i, *_tmp[:1]))
            _tmp = _tmp[1:]
            break


print(alice_bits)
print(dict((i,e) for i,e in enumerate(bell_state)), len(bell_state))
print(str(encrypted_bits).replace(',', ''), len(encrypted_bits))
#bob.py

from alice_number import *
from operator import itemgetter

epr_index = [i for i,e in enumerate(bell_state) if e == 'EPR']

bob_bits = list(itemgetter(*epr_index)(encrypted_bits))

print(epr_index)
print(str(bob_bits).replace(',', ''))

我正在嘗試在bob.py上導入准備好的列表並在alice.py上使用它。 列表是bell_state=[]encrypted_bits=[] 如何在不重新運行整個alice.py程序的情況下執行此操作? 我沒有得到預期的結果,因為每次bob.py只是重新運行整個alice.py

原則上,代碼應該這樣運行:

  1. alice.py運行和 output 這些結果(值是示例):
    Alice bits: [1 0 1 1 0 0 1 0] -> 保存為alice_bits中的列表

    bell states: {0: 'vmin', 1: 'vpl', 2: 'upl', 3: 'vpl', 4: 'vmin', 5: 'upl', 6: 'vmin', 7: 'EPR', 8: 'vpl', 9: 'vmin', 10: 'upl', 11: 'vpl', 12: 'vmin', 13: 'vpl', 14: 'upl', 15: 'upl', 16: 'vmin', 17: 'vpl', 18: 'upl', 19: 'upl', 20: 'EPR', 21: 'vpl', 22: 'vmin', 23: 'vmin', 24: 'upl', 25: 'upl', 26: 'vmin', 27: 'vmin', 28: 'vmin', 29: 'vpl', 30: 'EPR', 31: 'vpl', 32: 'vmin', 33: 'upl', 34: 'vpl', 35: 'upl', 36: 'vmin', 37: 'vpl', 38: 'upl', 39: 'vmin', 40: 'EPR', 41: 'upl', 42: 'vmin', 43: 'EPR', 44: 'vpl', 45: 'vpl', 46: 'upl', 47: 'EPR', 48: 'vmin', 49: 'vmin', 50: 'EPR', 51: 'EPR'} -> 保存為bell_state中的列表

    Encrypted bits: [0 0 1 0 0 1 0 1 1 1 0 1 1 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 0 0 0 1 0] -> 保存為encrypted_bits中的列表

  2. bob.py運行。 准備好程序中使用的bell_stateencrypted_bits列表。 結果它應該給出:
    epr_index = [7, 20, 30, 40, 43, 47, 50, 51] (結果為 EPR 的索引號)

    bob_bits = [1 0 1 1 0 0 1 0]

  3. 預期結果應該始終是alice_bits == bob_bits is True

問題是每次我按此順序運行程序時,結果都會變為alice_bits != bob_bits 我想這是因為alice.pybob.py中再次執行,從而生成不同的隨機位序列。 如果我可以導入准備好的bell_state=[]encrypted_bits=[] ,這將不是問題。

nb 這個程序是模擬量子密鑰分發協議

在這里,在alice.py中執行此操作替換

print(alice_bits)
print(dict((i,e) for i,e in enumerate(bell_state)), len(bell_state))
print(str(encrypted_bits).replace(',', ''), len(encrypted_bits))

if __name__ == "__main__":
    print(alice_bits)
    print(dict((i,e) for i,e in enumerate(bell_state)), len(bell_state))
    print(str(encrypted_bits).replace(',', ''), len(encrypted_bits))

這樣,在if __name__ == "__main__":下編寫的任何代碼都不會在您導入alice.py時運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM