为什么函数在我结束 while 循环并退出后一直重复文本?

Why does function keep repeating text after I end while loop and exit?

问题

要重现该问题,请在 repl.it 中 运行 并在出现提示时输入 "cNOT"(不带引号)。在此之后,它应该询问你想要的第二个量子位的门,相反,它会再次询问你给 "cNOT" 作为答案的相同问题。

代码

import numpy as np

def target(qstat):
    typegat2 = input("Which gate is this target qubit for? See list of two qubit gates at the top.")
    if typegat2 == "cNOT":
        carray = np.array([[0,1],[1,0]])
        if np.array_equal(mem1, [0,1]) == True:
            return qstat
        elif np.array_equal(mem1, [1,0]) == True:
            return np.dot(qstat,carray)
        else:
            print("superposition...not implemented")
            return qstat
    else:
        print("other gates not yet implemented")
        return qstat

qubits = 2
qstat = {1:[0,1],2:[0,1]}
done = "n"
singates = {"target":target}

x=1
while x <= qubits:
    while done == "n":
        print("this is qubit #",x)
        fstgat = "target"
        print("first gate is target")
        if fstgat in singates:
            #checks if there is an error from mem1 being undefined (control/target out of order)
            while True:
                try:
                    qstat[x]=singates[fstgat](qstat[x])
                    break
                except NameError:
                    print("switching qubits - we'll come back to this one")
                    #error here -> keeps saying "which gate is this target qubit for"
                    done = "y"
            done = input("Done with your qubit? y or n: ")
        else:
            print("sorry, that functionality is not yet implemented, try custom gate")
            done ="y"
    x+=1
    done = "n"

print("result for qubit",1,qstat[0])
print("result for qubit",2,qstat[1])

代码说明

代码被简化了很多,但它是对理想量子计算机的模拟。 "qstat" 是每个量子比特状态的字典。它可能不需要是一本字典,但它在实际程序中的方式很好,所以我保留了它。无论如何,"qubits" 是系统中的量子比特数,通常可以是用户输入的任何整数。函数 "target" 是 cNOT 门中的目标量子位。代码中存在 "try...except" 部分的原因是,有时用户会在 "control" 之前输入 "target",并且需要检查控制量子位以确定如何处理目标量子位,输出一个 NameError,因此我将其设置为将该量子位记录在我当前正在编码的列表中 "recalculate" 该列表中的每个量子位。

"done" 是一种检查用户是否已完成每个量子位的方法,并且由于它处于 while 循环中,我认为在 "except" 语句之后将其设置为 "y" 会结束 while 循环并继续下一个量子位,但由于某种原因它留在函数内 - 一遍又一遍地重复同一行。如果你想退出这个函数,你必须输入一些乱码,因为那对应于最后一个 else 语句,其中 if typegat2 doesn't equal "cNOT" it returns qstat (没有改变)并继续前进。很明显,由于错误或其他原因,该函数没有返回任何内容。但是问题就来了,我怎样才能让它退出函数呢?

如有任何帮助,我们将不胜感激。我很乐意回答您对代码的任何问题;我尽量减少了。

摆脱 while True: 循环,它会导致代码在 NameError 异常发生时重复。

此外,将 done 的输入移动到 try 块中。否则,该输入将覆盖 except 块中的 done = "y" 语句。

while x <= qubits:
    while done == "n":
        print("this is qubit #",x)
        fstgat = "target"
        print("first gate is target")
        if fstgat in singates:
            #checks if there is an error from mem1 being undefined (control/target out of order)
            try:
                qstat[x]=singates[fstgat](qstat[x])
                done = input("Done with your qubit? y or n: ")
            except NameError:
                print("switching qubits - we'll come back to this one")
                #error here -> keeps saying "which gate is this target qubit for"
                done = "y"   
        else:
            print("sorry, that functionality is not yet implemented, try custom gate")
            done ="y"
    x+=1
    done = "n"