简体   繁体   English

为什么我在while循环中退出并退出后,函数为什么仍然重复文本?

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

Problem 问题

To reproduce the problem, run this in repl.it and input "cNOT" (without the quotation marks) when prompted. 要重现该问题,请在repl.it中运行它,并在出现提示时输入“ cNOT”(不带引号)。 After this it should ask about the gates you want for the second qubit, instead, it asks again the same question you gave "cNOT" as an answer to. 此后,它应该询问您想要第二个qubit的门,取而代之的是,它再次询问您给“ cNOT”作为答案的相同问题。

Code

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])

Explanation of code 代码说明

The code is way reduced down, but it's meant to be a simulation of an ideal quantum computer. 减少了代码的数量,但这只是为了模拟理想的量子计算机。 "qstat" is a dictionary of the states of each qubit. “ qstat”是每个量子位状态的字典。 It probably doesn't need to be a dictionary, but the way it is in the actual program is nice so I'm keeping it. 它可能不必一定是字典,但是它在实际程序中的方式很好,因此我保留了它。 Anyway, "qubits" is the number of qubits in the system and it normally can be any integer the user inputs. 无论如何,“ qubits”是系统中的qubit数,通常它可以是用户输入的任何整数。 The function "target" is the target qubit in a cNOT gate. 函数“目标”是cNOT门中的目标量子位。 The reason there's the "try...except" section is in the code is because sometimes the user will input "target" before "control" and since the control qubit needs to be checked to determine what to do with the target qubit, outputs a NameError, so I set it up to record that qubit in a list that I'm currently coding to "recalculate" each of the qubits in that list. 代码中存在“ try ... except”部分的原因是,有时用户会在“ control”之前输入“ target”,并且由于需要检查控制qubit以确定如何处理目标qubit,所以输出一个NameError,因此我将其设置为在当前正在编码的列表中记录该量子位,以“重新计算”该列表中的每个量子位。

"done" is a way of checking that the user's done with each qubit, and since it's in a while loop, I thought setting it to "y" after the "except" statement would end the while loop and move on to the next qubit, but for some reason it's staying inside the function - the same line is repeated over and over. “完成”是一种检查用户是否完成了每个量​​子位的方法,并且由于它处于while循环中,所以我认为在“ except”语句之后将其设置为“ y”将结束while循环并移至下一个量子位,但由于某种原因,它停留在函数内部-一遍又一遍地重复同一行。 If you wish to exit the function, you have to type some gibberish, because that corresponds to the last else statement, where if typegat2 doesn't equal "cNOT" it returns qstat (which hasn't changed) and moves on. 如果您想退出该函数,则必须键入一些乱码,因为它对应于最后的else语句,如果typegat2不等于“ cNOT”,它将返回qstat(未更改)并继续。 So clearly the function isn't returning anything because of the error, or something. 因此很明显,该函数不会由于错误或任何原因而返回任何内容。 But then the problem becomes, how can I get it to exit the function? 但是,问题就变成了,如何使它退出功能?

Any help would be appreciated. 任何帮助,将不胜感激。 I would be glad to answer any questions you have on the code; 我很乐意回答您对代码有任何疑问; I tried to reduce it as much as possible. 我试图尽可能减少它。

Get rid of the while True: loop, that's causing the code to repeat whenever the NameError exception occurs. 摆脱while True:循环,这将导致代码在NameError异常发生时重复执行。

Also, move the input for done into the try block. 同样,将done的输入移动到try块中。 Otherwise, that input will override the done = "y" statement in the except block. 否则,该输入将覆盖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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM