我正在编写一个程序来模拟python 3中理想的量子计算机。
现在,我正在努力添加两个量子比特功能,我终于明白了我可以实际运行它(然后我很快就遇到了一个bug)。现在,我已经解决了很多问题,但程序并没有给出其中一个量子比特的正确答案。
简而言之,该程序在第二个量子位上没有执行任何操作,它只对第一个量子点进行操作,这让我觉得这个程序的方式存在问题。循环一切,但我已经盯着它看了一会儿,却找不到任何错误。
运行MWE,输出应为
your result is [ 0.70710678+0.j -0.70710678+0.j] qubit # 1
probability of |0> state is 0.5
probability of |1> state is 0.5
your result is [0 1] qubit # 2
probability of |0> state is 0
probability of |1> state is 1
=> None
输出应该具有第二个量子比特的结果,并且概率与第一个状态相同。另请注意,对于两个量子比特,它们的原始状态为[0 1]
- 换句话说,第二个量子比特的状态根本不会被更改。
import numpy as np
import cmath
qubits = 2
done = "n"
qstats = {key:np.array([0,1]) for key in range(1,qubits+1)}
def hadop(qstat):
matrix = (1/cmath.sqrt(2))*np.array([[1,1],[1,-1]])
return np.dot(matrix, qstat)
def probability(qstat, n):
if n == 0:
return np.abs((qstat[0]))**2
elif n == 1:
return np.abs((qstat[1]))**2
singates = {"Hadamard":hadop}
commands = {1:["Hadamard"],2:["Hadamard"]}
qubitnum=1
while qubitnum <= qubits:
for index,gate in enumerate(commands[qubitnum]):
qstat = qstats[index+1]
qstat = singates[gate](qstat)
qstats[index+1] = qstat
if index+1 == len(commands[qubitnum]):
print(" ")
print("your result is", qstats[qubitnum], "qubit #", qubitnum)
print("probability of |0> state is", probability(qstats[qubitnum],0))
print("probability of |1> state is", probability(qstats[qubitnum],1))
qubitnum+=1
main-file.py:
import numpy as np
from random import randint
import cmath
import math
from function import *
qubits = int(input("How many qubits would you like to use? (Currently, only supports 1): "))
done = "n"
qstatask = input("Would you like your initial qubits to be in the |0> state or |1> state? 0 or 1: ")
if qstatask == "0":
qstats = {key:np.array([0,1]) for key in range(1,qubits+1)}
elif qstatask == "1":
qstats = {key:np.array([1,0]) for key in range(1,qubits+1)}
else:
print("I'm sorry, that is not a valid input. State set to zero.")
qstats = {key:np.array([0,1]) for key in range(1,qubits+1)}
singates = {"Hadamard":hadop, "X":xop, "Z":zop, "Y":yop, "sqrtX":sqrtxop,"phase shift":phaseshiftop,"measurement":measurement,"custom":customop, "control":control, "target":target}
twgates = ["cNOT", "swap"]
thrgates = ["Toffoli"]
print(singates.keys())
print(twgates)
print(thrgates)
while done == "n":
if qubits == 1:
fstgat = input("what gate would you like to use? use the list of single gates at the top: ")
if fstgat in singates:
qstat = qstats[1]
qstat = singates[fstgat](qstat)
qstats[1] = qstat
done = input("Done with your circuit? y or n: ")
else:
print("sorry, that gate is not yet implemented. maybe try custom gate.")
done = "y"
elif qubits >= 2:
commands = {}
for i in range(1,qubits+1):
commands[i] = []
qubitnum=1
while qubitnum <= qubits:
while done == "n":
fstgat = input("what gate would you like to use for " + str(qubitnum) + " qubit? Use the list of single qubits at the top, plus control or target: ")
commands[qubitnum].append(fstgat)
done = input("Done with your " + str(qubitnum) + " qubit? y or n: ")
qubitnum+=1
done = "n"
qubitnum=1
while qubitnum <= qubits:
for index,gate in enumerate(commands[qubitnum]):
if gate in singates:
if gate != "target" or (gate == "target" and mem1 in globals()):
qstat = qstats[index+1]
qstat = singates[gate](qstat)
qstats[index+1] = qstat
print("done with a calculation")
if index+1 == len(commands[qubitnum]):
print(" ")
print("your result is", qstats[qubitnum], "qubit #", qubitnum)
print("probability of |0> state is", probability(qstats[qubitnum],0))
print("probability of |1> state is", probability(qstats[qubitnum],1))
else:
print("checking for information")
else:
print(gate, " has not yet been implemented. Maybe try the custom gate?")
break
qubitnum+=1
print("Program complete.")
done = "y"
else:
print("sorry, that functionality is not yet implemented")
done = "y"
function.py:
import cmath
import numpy as np
import math
from random import randint
def hadop(qstat):
matrix = (1/cmath.sqrt(2))*np.array([[1,1],[1,-1]])
return np.dot(matrix, qstat)
def xop(qstat):
matrix = np.array([[0,1],[1,0]])
return np.dot(matrix,qstat)
def zop(qstat):
matrix = np.array([[1,0],[0,-1]])
return np.dot(matrix,qstat)
def yop(qstat):
matrix = np.array([[0, cmath.sqrt(-1)],[-1*cmath.sqrt(-1),0]])
return np.dot(matrix,qstat)
def sqrtxop(qstat):
const1 = 1+cmath.sqrt(1)
const2 = 1-cmath.sqrt(1)
matrix = np.array([[const1/2,const2/2],[const2/2,const1/2]])
return np.dot(matrix,qstat)
def phaseshiftop(qstat):
phasepos = [math.pi/4, math.pi/2]
print(phasepos)
x = input("Please pick one of the two phase shifts, 0 for the first, 1 for the second: ")
if x == "0":
y = phasepos[0]
elif x == "1":
y = phasepos[1]
const1 = cmath.sqrt(-1)*y
matrix = np.array([[1,0],[0,math.e**const1]])
return np.dot(matrix,qstat)
def customop(qstat):
num1 = float(input("Please input a number (no pi, e, etc) for the first number in your matrix (row 1 column 1): "))
num2 = float(input("Number for matrix - row 1 column 2: "))
num3 = float(input("Number for matrix - row 2 column 1: "))
num4 = float(input("Number for matrix - row 2 column 2: "))
matrix = np.array([[num1,num3],[num2,num4]])
matrix2 = matrix.conj().T
result = np.dot(matrix, matrix2)
identity = np.identity(2)
if np.array_equal(result, identity) == True:
return np.dot(matrix, qstat)
else:
print("matrix not unitary, pretending no gate was applied")
return qstat
def probability(qstat, n):
if n == 0:
return np.abs((qstat[0]))**2
elif n == 1:
return np.abs((qstat[1]))**2
def measurement(qstat):
prob1 = probability(qstat,0)
prob2 = probability(qstat,1)
random = randint(0,1)
if random <= prob1:
qstat = np.array([0,1])
return qstat
elif prob1 < random:
qstat = np.array([1,0])
return qstat
def control(qstat):
typegat = input("Which gate is this the control qubit for? See list of two qubit gates at the top.")
if typegat == "cNOT":
global mem1
mem1 = qstat
elif typegat == "swap":
mem1 = qstat
else:
print("other gates not yet implemented")
return qstat
def target(qstat):
typegat2 = input("Which gate is this target qubit for? See list of two qubit gates at the top.")
if typegat2 == "cNOT":
if np.array_equal(mem1, [0,1]) == True:
return qstat
elif np.array_equal(mem1, [1,0]) == True:
return np.dot(qstat,mem1)
else:
print("superposition...not implemented")
return qstat
elif typegat2 == "swap":
return mem1
else:
print("other gates not yet implemented")
return qstat
如果你决定运行这里给出的完整代码,重现问题,输入2(回答有多少量子位的问题),0或1(注意这会将量子位设置为[0,1]和分别为[1,0];这里的答案都很好)然后是Hadamard,X,或者你有什么,然后是y,然后是Hadamard,X,或者其他什么,然后是y。此时它应该输出结果。我建议在两者上使用Hadamard门来看问题。这仅适用于运行完整程序的情况;否则你不需要输入任何东西。
注意:我知道有很多变数和其他疯狂,所以如果有人想要描述变量是什么,我很乐意提供它。我试图使变量相当不言自明。
答案 0 :(得分:3)
我不完全确定你对index
的意思。但是,它看起来像你的行
qstat = qstats[index+1]
应该真正阅读
qstat = qstats[qubitnum]