Matrix is ​​not added correctly

My code is designed to create a custom matrix that applies to the original state. Since I want it to be able to generate whatever square matrix the user wishes, I have to do some funky things. My main approach was for the user to enter different elements that are all placed on the same list. Based on the position of the items in the list, they are placed on different lines. I do this with help numpy.append(). However this gives an error

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

in response to my line .append(). What am I doing wrong?

To reproduce the error in this particular case of code, enter “2”, enter “0”, enter “1”, enter “1”, enter “0”, enter, although this seems to be any numbers from the last four. Another note - the lines print(current_row)are for debugging links. Same thing with the lines print(matrix).

The code

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)
+4
source share
4 answers

Given the input specified above (size 2 and elements 0, 1, 1, 0), the error comes from the fact that you are trying to add a row of 2 elements to an empty matrix. Your (empty) matrix has the form (1, 0), and current_row has the form (2) if it is turned into np.array.

DYZ, ,

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

, , , , . . , float.

, :

# 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, .

+3

, , , , :

size = 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)

while for, .

+1

, , , .

, @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 not float.

, . . ( , .) , , 2.

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

However, given that you want to apply any gate to your condition, you know what the dimensions of the matrix should be. Allowing the user to indicate that they want to use qutrit-gate for the qubit state will only allow them to enter errors. Thus, an even better version of the code:

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)

Also, if you want to make such a simulation of the gate, and if you did not already know about them, make sure that you look at all_close and reshape . They are going to come up with a lot.

0
source

Source: https://habr.com/ru/post/1683856/


All Articles