To reproduce the problem, run this in repl.it and input "cNOT" (without the quotation marks) when prompted. 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.
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])
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. 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. The function "target" is the target qubit in a cNOT gate. 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.
"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. 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. 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.
Also, move the input for done
into the try
block. Otherwise, that input will override the done = "y"
statement in the except
block.
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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.