【问题标题】:Matrix isn't being appended to correctly矩阵未正确附加
【发布时间】:2017-08-17 00:18:48
【问题描述】:

我的代码是让用户创建一个应用于起始状态的自定义矩阵。因为我希望它能够生成用户希望的任何方阵,所以我必须做一些时髦的事情。我的基本方法是让用户输入不同的元素,这些元素都放在一个列表中。根据列表中元素的位置,将它们放入不同的行中。我使用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) 行用于调试参考。与print(matrix) 行相同。

代码

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)

【问题讨论】:

  • 请附上完整的错误信息。
  • @DYZ 我编辑这样做。
  • 你现在提前matrix的尺寸,不是吗?为什么不创建整个完整的矩阵广告,只需将elements[i] 存储到正确的位置,而不需要任何appends?
  • @DYZ 不,不一定-我只是在特定情况下(2x2 矩阵)进行测试。我希望代码可用于任何维度的矩阵,只要它是正方形的。
  • 你没有len(elements) 行和列吗?还是dimensions

标签: python python-3.x numpy matrix


【解决方案1】:

鉴于您在上面指定的输入(大小 2 和 0、1、1、0 的元素),错误来自您试图将一行 2 个元素附加到空矩阵的事实。如果变成 np.array,您的(空)矩阵的形状为 (1, 0) 而 current_row 的形状为 (2, )。

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

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

由于您要求元素的顺序与 reshape 功能的默认工作方式一致,因此您无需添加任何其他内容。请注意,我必须转换为上面的整数,因为您将尺寸解析为浮点数。

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

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

请注意,这可能会使错误处理复杂化并影响用户体验。

旁注

您可以使用一个有用的工具来查看正在发生的事情是 ipdb。我掉线了

import ipdb; ipdb.set_trace()

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

【讨论】:

    【解决方案2】:

    如果我理解正确,确定矩阵维度,附加用户的值,然后调整列表大小并将其转换为矩阵应该可以工作:

    dimension = int(input("你的(方)矩阵的维数是多少?请输入一个数字:"))

    ls = []
    for y in range(dimension):
        for x in range(dimension):
            ls.append(float(input('What value for position ({}, {}): '.format(y+1, x+1))))
    
    np.matrix(np.resize(ls, (dimension, dimension)))
    

    输出:

    What are the dimensions of your (square) matrix? Please input a single number: 3
    What value for position (1, 1): 1
    What value for position (1, 2): 2
    What value for position (1, 3): 3
    What value for position (2, 1): 1
    What value for position (2, 2): 2
    What value for position (2, 3): 3
    What value for position (3, 1): 1
    What value for position (3, 2): 2
    What value for position (3, 3): 3
    
    Out[29]:
    matrix([[ 1.,  2.,  3.],
            [ 1.,  2.,  3.],
            [ 1.,  2.,  3.]])
    

    【讨论】:

      【解决方案3】:

      其他人指出了为什么您的方法会引发该错误。我将添加另一种创建矩阵的方法。请注意,在您要求输入之前,您会从用户那里获得尺寸(并且它始终是方阵)。因此,您可以创建一个由零组成的矩阵,然后使用用户提供给您的条目随时填充它,如下所示:

      def customop(qstat):
          dimensions = input("What are the dimensions of your (square) matrix? Please input a single number: ")
          matrix = np.zeros([dimensions, dimensions])
      
          for iterator in range(dimensions):
              for iterator_2 in range(dimensions):
                   matrix[iterator, iterator_2] = float(input("Matrix element at "+str(iterator)+","+str(iterator_2)+": "))
      
          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)
      

      我用 for 循环替换了 while 循环,以便自动处理初始化和递增。

      【讨论】:

        【解决方案4】:

        好的,这里发生了一些不同的事情,其中​​一些已经处理,还有一些尚未处理。

        正如@DYZ 所指出的,第一件事是您试图将行向量附加到一个空矩阵。这可以通过重塑空矩阵来解决。

        代码也可以大大简化为:

        import numpy as np
        
        def custom_operator(state):
            dimension = int(input("What are the dimensions of your (square) matrix? Please input a single number:"))
            elements = list()
            for x in range(dimension):
                for y in range(dimension):
                    value = float(input("Matrix element at ({x}, {y}):".format(x=x+1, y=y+1)))
                    elements.append(value)
        
            operator = np.matrix(np.resize(elements, (dimension, dimension)))
            output_state = np.dot(operator, state)
            return output_state
        
        state = np.matrix([[0], [1]])
        custom_operator(state)
        

        请注意,我已删除您未使用的 import math 语句,并且当您询问尺寸时,您应该将响应转换为 int 而不是浮点数。

        也没有必要检查门是否是单一的。无论哪种方式,您仍然返回状态操作的输出。 (除非您真的想知道它是否是。)如果您确实想知道,如果维度不是 2,您当前的检查将失败。更好的检查将是

        np.allclose(operator.dot(operator.T.conj()), np.eye(len(dimension)))
        

        但是,如果您想对您的状态应用任何门,您就知道矩阵的维数必须是多少。允许用户指定他们想要将 qutrit 门应用于 qubit 状态只会让他们引入错误。所以更好的代码版本是:

        import numpy as np
        
        def custom_operator(input_state):
            dimension, width = input_state.shape
        
            if width != 1:
                error_message = "Input state must be a column vector"
                raise ValueError(error_message)
        
            elements = list()
            for x in range(dimension):
                for y in range(dimension):
                    value = float(input("Matrix element at ({x}, {y}):".format(x=x+1, y=y+1)))
                    elements.append(value)
        
            operator = np.matrix(np.resize(elements, (dimension, dimension)))
            output_state = np.dot(operator, input_state)
            return output_state
        
        state = np.matrix([[0], [1]])
        custom_operator(state)
        

        此外,如果您想进行此类门模拟,并且您还不了解它们,请务必查看 all_closereshape。他们会出现很多。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-20
          • 2012-02-25
          • 2017-07-08
          • 2017-03-15
          • 1970-01-01
          • 2018-09-25
          • 1970-01-01
          相关资源
          最近更新 更多