python – 没有正确附加Matrix

我的代码是供用户创建应用于起始状态的自定义矩阵.因为我希望它能够生成用户希望的任何方形矩阵,所以我必须做一些时髦的事情.我的基本方法是让用户输入不同的元素,这些元素都放在一个列表中.根据列表中元素的位置,它们被放入不同的行中.我使用numpy.append()来做到这一点.但是,它给出了错误

Traceback (most recent call last):
  File "/home/physicsnerd/Documents/Quantum-Computer-Simulator/tests.py", line 39, in <module>
    customop(qstat)
  File "/home/physicsnerd/Documents/Quantum-Computer-Simulator/tests.py", line 21, in customop
    np.append(matrix,current_row,axis=0)
  File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 4575, in append
    return concatenate((arr, values), axis=axis)
ValueError: all the input arrays must have same number of dimensions

响应我的.append()行.我究竟做错了什么?

要在此特定代码情况下重现错误,请键入“2”,输入“0”,输入“1”,输入“1”,输入“0”,输入,尽管这似乎打破了任何数字最后四个.另一个注意事项 – print(current_row)行用于调试参考.与打印(矩阵)线相同.

import numpy as np
import math

def customop(qstat):
    dimensions = float(input("What are the dimensions of your (square) matrix? Please input a single number: "))
    iterator = 1
    iterator_2 = 1
    elements = []
    while iterator <= dimensions:
        while iterator_2 <= dimensions:
            elements.append(float(input("Matrix element at "+str(iterator)+","+str(iterator_2)+": ")))
            iterator_2+=1
        iterator_2 = 1
        iterator+=1
    matrix = np.matrix([])
    element_places = list(range(len(elements)))
    current_row = []
    for i in element_places:
        print(i%dimensions)
        if i%dimensions == 0 and i > 0:#does this work? column vs row, elements, etc
            np.append(matrix,current_row,axis=0)
            current_row = []
            current_row.append(elements[i])
        elif i == 0:
            current_row.append(elements[i])
            print(current_row)
        else:
            current_row.append(elements[i])
            print(current_row)
    if np.array_equal(np.dot(matrix, matrix.conj().T), np.identity(2)) == True:
        print(matrix)
        return np.dot(matrix, qstat)
    else:
        print(matrix)
        print("matrix not unitary, pretending no gate was applied")
        return qstat

qstat = np.matrix([[0],[1]])
customop(qstat)

最佳答案 给定您在上面指定的输入(大小2和元素0,1,1,0),错误来自于您尝试将一行2个元素追加到空矩阵的事实.您的(空)矩阵具有形状(1,0),而current_row具有形状(2,),如果变成np.array.

正如上面提到的DYZ,您已经知道了矩阵的尺寸,因此您可以将输入重新整形为方形矩阵,如下所示

np.matrix(elements).reshape((int(dimensions), int(dimensions)))

由于您要求元素的顺序与重塑函数的默认方式一致,因此您无需添加任何其他内容.注意我必须转换为上面的整数,因为您将维度解析为浮点数.

如此简化,您的代码将如下所示:

# matrix.py

import numpy as np
import math

def customop(qstat):
    dimensions = int(input("What are the dimensions of your (square) matrix? Please input a single number: "))
    iterator = 1
    iterator_2 = 1
    elements = []
    while iterator <= dimensions:
        while iterator_2 <= dimensions:
            elements.append(float(input("Matrix element at "+str(iterator)+","+str(iterator_2)+": ")))
            iterator_2+=1
        iterator_2 = 1
        iterator+=1
    matrix = np.matrix(elements).reshape(dimensions, dimensions)
    if np.array_equal(np.dot(matrix, matrix.conj().T), np.identity(2)) == True:
        print(matrix)
        return np.dot(matrix, qstat)
    else:
        print(matrix)
        print("matrix not unitary, pretending no gate was applied")
        return qstat

qstat = np.matrix([[0],[1]])
customop(qstat)

示例输出

$python3 matrix.py
What are the dimensions of your (square) matrix? Please input a single number: 3
Matrix element at 1,1: 1
Matrix element at 1,2: 2
Matrix element at 1,3: 3
Matrix element at 2,1: 1
Matrix element at 2,2: 2
Matrix element at 2,3: 3
Matrix element at 3,1: 1
Matrix element at 3,2: 2
Matrix element at 3,3: 3
[[ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]]

其他优化

如果您知道矩阵将是正方形,那么您可以推断维度将是输入元素数量的平方根

dimensions = math.sqrt(len(elements))

请注意,这可能会使错误处理变得复杂并影响UX.

边注

您可以使用一个有用的工具来查看正在发生的事情是ipdb.我放弃了这条线

import ipdb; ipdb.set_trace()

就在你的原始np.append行之前,这就是帮助我突出你的错误的原因.

点赞