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