Python 仅将特定变量从不同文件导入当前文件
我在同一文件夹中有两个文件Python 仅将特定变量从不同文件导入当前文件,python,python-3.x,numpy,quantum-computing,Python,Python 3.x,Numpy,Quantum Computing,我在同一文件夹中有两个文件alice.py和bob.py。以下是我写的程序: #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&q
alice.py
和bob.py
。以下是我写的程序:
#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))
我试图在alice.py
上导入准备好的列表,并在bob.py
上使用它。这些列表是bell\u state=[]
和加密的\u位=[]
。如何在不重新运行整个alice.py
程序的情况下执行此操作?我没有得到预期的结果,因为每次bob.py
都只是重新运行整个alice.py
原则上,代码应该这样运行:
alice.py
运行并输出这些结果(值为示例):
Alice位:[1 0 1 1 0 0 1 0]
->保存为Alice\u位中的列表
bell声明:{0:'vmin',1:'vmin',2:'vpl',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',25:'upl',26:'vmin',27:'vmin',28:'vmin'pl',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'}
->另存为贝尔州的列表
加密位:[0 0 1 0 1 0 1 0 1 1 1 0 1 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0]
->保存为加密位中的列表
bob.py
运行。将准备好的bell\u state
和encrypted\u bits
列表用于程序中。结果应给出:
epr_索引=[7,20,30,40,43,47,50,51]
(结果为epr的索引编号)
bob_位=[1 0 1 1 0 1 0]
alice\u bits==bob\u bits为True
alice\u bits!=bob\u bits
。我想这是因为alice.py
在bob.py
中再次执行alice.py
,从而生成不同的随机位序列。如果我能导入准备好的bell\u state=[]
和加密的位就好了=[]
,这不是问题
n、 b.这个程序是用来模拟量子密钥分配协议的在这里,在
alice.py
替换
打印(alice_位)
打印(dict((i,e)表示枚举(贝尔州)中的i,e),len(贝尔州))
打印(str(加密字节)。替换(',',''),len(加密字节))
与
如果名称=“\uuuuu main\uuuuuuuu”:
打印(alice_位)
打印(dict((i,e)表示枚举(贝尔州)中的i,e),len(贝尔州))
打印(str(加密字节)。替换(',',''),len(加密字节))
这样,无论何时导入
alice.py
我认为您需要澄清“每次”的含义,并澄清“预期结果”是什么我希望我的示例能使它更清楚。这似乎是一个设计问题。我建议您将计算列表的代码封装在函数中。这样,您可以将这些函数从alice.py导入bob.py,并在那里调用它们。@MaxPowers我尝试将每个列表封装在函数中,但当我调用问题仍然存在,这取决于鲍勃。
#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(',', ''))