Python VisibleDepractionWarning-这是从哪里来的?

Python VisibleDepractionWarning-这是从哪里来的?,python,python-3.x,runtime-error,quantum-computing,Python,Python 3.x,Runtime Error,Quantum Computing,我正在用python编写一些代码来模拟量子计算机。我刚刚添加了一个部分,开始集成超过一个量子位的功能,然后出现了这个奇怪的错误。它没有说明是哪条线引起的,所以我甚至不知道从哪里开始修复它,而且我以前从未见过它。此外,程序会继续运行,并在我运行过的几个测试用例中输出正确的答案,即使有这个错误 错误 完整程序 打印错误后,它将继续正常运行 我很乐意补充任何其他必要的信息;请告诉我。任何帮助都将不胜感激 它没有说明是哪条线引起的,所以我甚至不知道从哪里开始修复它,而且我以前从未见过它 一种简单的方法是

我正在用python编写一些代码来模拟量子计算机。我刚刚添加了一个部分,开始集成超过一个量子位的功能,然后出现了这个奇怪的错误。它没有说明是哪条线引起的,所以我甚至不知道从哪里开始修复它,而且我以前从未见过它。此外,程序会继续运行,并在我运行过的几个测试用例中输出正确的答案,即使有这个错误

错误 完整程序 打印错误后,它将继续正常运行

我很乐意补充任何其他必要的信息;请告诉我。任何帮助都将不胜感激

它没有说明是哪条线引起的,所以我甚至不知道从哪里开始修复它,而且我以前从未见过它

一种简单的方法是将警告升级为异常,然后使用调试器检查变量

所以你可以预先准备这个:

import warnings

warnings.simplefilter("error", np.VisibleDeprecationWarning)
然后运行代码:

how many qubits: 2
would you like to start in the 0 or 1 state: 0
[[1]
 [0]
 [0]
 [0]]
dict_keys(['X', 'Z', 'sqrtX', 'Hadamard', 'measurement', 'Y', 'custom', 'phase shift'])
what gate would you like to use? (proceed at your own risk): X
what qubit would you like that gate to be applied to: 1

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-e3d1cca2e826> in <module>()
    136         ap_qubit = int(input('what qubit would you like that gate to be applied to: '))
    137         if fstgat in gates:
--> 138             qstat = gates[fstgat](qstat,ap_qubit)
    139             done = input('done with your circuit? y or n: ')
    140         else:

<ipython-input-43-e3d1cca2e826> in xop(qstat, ap_qubit)
     36 def xop(qstat, ap_qubit):
     37     matrix = np.array([[0,1],[1,0]])
---> 38     matrix = gate_scale(matrix, ap_qubit)
     39     return np.dot(matrix,qstat)
     40 

<ipython-input-43-e3d1cca2e826> in gate_scale(gate, ap_qubit)
     16         iterator = 1
     17         kron_num = []
---> 18         identity = np.identity(dimensions, np.matrix)
     19         while iterator <= dimensions:
     20             kron_num.append(identity)

-\lib\site-packages\numpy\core\numeric.py in identity(n, dtype)
   2392     """
   2393     from numpy import eye
-> 2394     return eye(n, dtype=dtype)
   2395 
   2396 

\lib\site-packages\numpy\lib\twodim_base.py in eye(N, M, k, dtype)
    178     if M is None:
    179         M = N
--> 180     m = zeros((N, M), dtype=dtype)
    181     if k >= M:
    182         return m

TypeError: 'float' object cannot be interpreted as an integer
多少量子位:2
是否要在0或1状态下启动:0
[[1]
[0]
[0]
[0]]
dict_键(['X','Z','sqrtX','Hadamard','measurement','Y','custom','phase shift'])
你想用哪个门?(自行承担风险):X
你希望这个门应用于什么量子位:1
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
136 ap_qubit=int(输入('您希望该门应用于哪个qubit:'))
137如果闸门内有fstgat:
-->138 qstat=门[fstgat](qstat,ap_qubit)
139完成=输入('电路完成?y或n:')
140其他:
在xop中(qstat,ap_qubit)
36 def xop(qstat,ap_qubit):
37矩阵=np.数组([[0,1],[1,0]])
--->38矩阵=门刻度(矩阵,ap量子比特)
39返回np.点(矩阵,qstat)
40
门内刻度(门,ap_量子位)
16迭代器=1
17克朗数量=[]
--->18恒等式=np.恒等式(维度,np.矩阵)

19而迭代器我有点困惑。没有浮点被传入-
ap\u qubit
int
…真的吗?您可能需要查看
math.sqrt
返回的值。这就是调试器的优点:您可以轻松地检查所有变量,如上图所示,传递给
identity
的值是第一个参数
2.0
(它是一个浮点)。
import cmath
import numpy as np
import math
from random import randint

def gate_scale(gate, ap_qubit):
    dimensions = math.sqrt(np.size(gate))
    ap_qubit-=1
    if 2**qnum == dimensions:
        return gate
    else:
        iterator = 1
        kron_num = []
        identity = np.identity(dimensions, np.matrix)
        while iterator <= dimensions:
            kron_num.append(identity)
            iterator+=1
        kron_num[ap_qubit] = gate
        kron_iterator = list(range(len(kron_num)))
        for i in kron_iterator:
            if i == 0:
                x = kron_num[i]
            if i > 0:
                x = np.kron(x, kron_num[i])
        return x

def xop(qstat, ap_qubit):
    matrix = np.array([[0,1],[1,0]])
    matrix = gate_scale(matrix, ap_qubit)
    return np.dot(matrix,qstat)

def probability(qstat, n): #fix to handle larger state vectors (see printing)
    if n == 0:
        return (qstat[0])**2
    elif n == 1:
        return (qstat[-1])**2

def measurement(qstat, ap_qubit): #fix to handle larger state vectors
    prob1 = probability(qstat,0)
    prob2 = probability(qstat,1)
    random = randint(0,1)
    if random <= prob1:
        qstat = np.array([0,1])
    elif prob1 < random:
        qstat = np.array([1,0])
    return qstat

qnum = int(input("how many qubits: "))
zero_state = np.matrix([[1],[0]])
one_state = np.matrix([[0],[1]])
z_or_o = input('would you like to start in the 0 or 1 state: ')
iterate = 1
while iterate <= qnum:
    if iterate == 1:
        if z_or_o == '0':
            x = zero_state
        elif z_or_o == '1':
            x = one_state
    if iterate == qnum:
        qstat = x
        print(qstat)
    else:
        x = np.kron(x,zero_state)
    iterate+=1


gates = {"Hadamard":hadop, "X":xop, "Z":zop, "Y":yop, "sqrtX":sqrtxop,"phase shift":phaseshiftop,"measurement":measurement,"custom":customop}#, "control":control, "target":target
print(gates.keys())

done = "n"#needs to handle more than 1 qubit
while done == "n":
    if qnum == 1:
        fstgat = input("what gate would you like to use? use the list of gates at the top minus control and target: ")
        ap_qubit = int(input("what qubit would you like it to be applied to?"))#handling control/target...
        if fstgat in gates:
            qstat = gates[fstgat](qstat,ap_qubit)
            done = input("Done with your circuit? y or n: ")
        else:
            print("sorry, that gate is not yet implemented. maybe try custom gate.")
    else:
        fstgat = input('what gate would you like to use? (proceed at your own risk): ')
        ap_qubit = int(input('what qubit would you like that gate to be applied to: '))
        if fstgat in gates:
            qstat = gates[fstgat](qstat,ap_qubit)
            done = input('done with your circuit? y or n: ')
        else:
            print('sorry, gate not implemented, maybe try custom gate.')

#printing - fix to handle larger state vectors
print(" ")
print("final state:", qstat)
print("probability of |0> state upon measurement is", probability(qstat,0))#this needs to iterate for qubits
print("probability of |1> state upon measurement is", probability(qstat,1))
how many qubits: 2
would you like to start in the 0 or 1 state: 0
[[1]
 [0]
 [0]
 [0]]
dict_keys(['X', 'sqrtX', 'Hadamard', 'Z', 'phase shift', 'measurement', 'custom', 'Y'])
what gate would you like to use? (proceed at your own risk): X
what qubit would you like that gate to be applied to: 1
import warnings

warnings.simplefilter("error", np.VisibleDeprecationWarning)
how many qubits: 2
would you like to start in the 0 or 1 state: 0
[[1]
 [0]
 [0]
 [0]]
dict_keys(['X', 'Z', 'sqrtX', 'Hadamard', 'measurement', 'Y', 'custom', 'phase shift'])
what gate would you like to use? (proceed at your own risk): X
what qubit would you like that gate to be applied to: 1

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-e3d1cca2e826> in <module>()
    136         ap_qubit = int(input('what qubit would you like that gate to be applied to: '))
    137         if fstgat in gates:
--> 138             qstat = gates[fstgat](qstat,ap_qubit)
    139             done = input('done with your circuit? y or n: ')
    140         else:

<ipython-input-43-e3d1cca2e826> in xop(qstat, ap_qubit)
     36 def xop(qstat, ap_qubit):
     37     matrix = np.array([[0,1],[1,0]])
---> 38     matrix = gate_scale(matrix, ap_qubit)
     39     return np.dot(matrix,qstat)
     40 

<ipython-input-43-e3d1cca2e826> in gate_scale(gate, ap_qubit)
     16         iterator = 1
     17         kron_num = []
---> 18         identity = np.identity(dimensions, np.matrix)
     19         while iterator <= dimensions:
     20             kron_num.append(identity)

-\lib\site-packages\numpy\core\numeric.py in identity(n, dtype)
   2392     """
   2393     from numpy import eye
-> 2394     return eye(n, dtype=dtype)
   2395 
   2396 

\lib\site-packages\numpy\lib\twodim_base.py in eye(N, M, k, dtype)
    178     if M is None:
    179         M = N
--> 180     m = zeros((N, M), dtype=dtype)
    181     if k >= M:
    182         return m

TypeError: 'float' object cannot be interpreted as an integer
import pdb

pdb.pm()


> \lib\site-packages\numpy\lib\twodim_base.py(180)eye()
-> m = zeros((N, M), dtype=dtype)
(Pdb) args
N = 2.0
M = 2.0
k = 0
dtype = <class 'numpy.matrixlib.defmatrix.matrix'>
(Pdb) u
> \lib\site-packages\numpy\core\numeric.py(2394)identity()
-> return eye(n, dtype=dtype)
(Pdb) args
n = 2.0
dtype = <class 'numpy.matrixlib.defmatrix.matrix'>