![]() |
Make the code shorter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Make the code shorter (/thread-36421.html) |
Make the code shorter - quest - Feb-18-2022 I have 4 matrix which is shape 2*2 and pi,px,py and pz are my coefficient. and according to the input from the use, I am trying to calculate my matrix and these coefficient with all combinations. pi link to matrix1, px link to matrix2 py link to matrix3 pz link to matrix4 For instance, if the input is 1: I am having this formula: pi*M1*pi + px*M2*px +py*M3*py + pz*M4*pzIf the input is 2, this time I will have all (4,2) combinations of coefficient and will multiply wth my matrix: pipi*M1tensorM1*pipi+pipx*M1tensorM2*pipx+pipy*M1tensorM3*pipy+pipz*M1tensorM4*pipz+pxpi*M2tensorM1*pxpi+ pxpx*M2tensorM2*pxpx+pxpy*M2tensorM3*pxpy+pxpz*M2tensorM4*pxpz+ pypi*M3tensorM1*pypi+pypx*M3tensorM2*pypx+pypy*M3tensorM3*pypy+pypz*M3tensorM4*pypz+pzpi*M4tensorM1 *pzpi+pzpx*M4tensorM2*pzpx+pzpy*M4tensorM3*pzpy+pzpz*M4tensorM4*pzpAnd if the input is 3 this time all triple combination of the coefficient and so on... I actually wrote a function and it is working too. But i did not like the structure of the code.. I mean if I would have 10 input I do not want to write the ifstatement till 10. I am looking for more autonomous way to do it. How can I write it a smaller way? def generalization(input_number): qubit_number=3 a = list(product(['p_i','px','py','pz'],repeat = input_number)) result=[] paulis = list(product([i,x,y,z],repeat=input_number)) if input_number==1: for order in range(len(paulis)): element = sympy.Symbol(a[order][0]) matrix = sympy.Matrix(paulis[order][0]) r = element*matrix*element print(element, '*', matrix, '*', element) result.append(r) rho_final = sum(result,sympy.zeros(2**input_number)) if input_number==2: for order in range(len(paulis)): element1 = sympy.Symbol(a[order][0]) element2 = sympy.Symbol(a[order][1]) matrix = TensorProduct(paulis[order][0],paulis[order][1]) r = element1*element2 * matrix * element1*element2 print(element1,'*', element2, '*', matrix, '*', element1, '*', element2) result.append(r) rho_final = sum(result,sympy.zeros(2**input_number)) #rho_final if input_number==3: for order in range(len(paulis)): element1 = sympy.Symbol(a[order][0]) element2 = sympy.Symbol(a[order][1]) element3 = sympy.Symbol(a[order][2]) matrix = TensorProduct(paulis[order][0],paulis[order][1]) matrix = TensorProduct(matrix,paulis[order][2]) r = element1*element2*element3 * matrix * element1*element2*element3 print(element1,'*', element2, '*',element3, '*', matrix, '*', element1, '*', element2,'*',element3) result.append(r) rho_final = sum(result,sympy.zeros(2**input_number)) if input_number==4: for order in range(len(paulis)): element1 = sympy.Symbol(a[order][0]) element2 = sympy.Symbol(a[order][1]) element3 = sympy.Symbol(a[order][2]) element4 = sympy.Symbol(a[order][3]) matrix = TensorProduct(paulis[order][0],paulis[order][1]) matrix = TensorProduct(matrix,paulis[order][2]) matrix = TensorProduct(matrix,paulis[order][3]) r = element1*element2*element3*element4 * matrix * element1*element2*element3*element4 print(element1,'*', element2, '*',element3,'*',element4, '*', matrix, '*', element1, '*', element2,'*',element3,'*',element4) result.append(r) rho_final = sum(result,sympy.zeros(2**input_number)) return rho_finalAs you see if input_number==something, an then I am repeating the code in a very similar way and I did not like it. I am looking for a smarter way to do it RE: Make the code shorter - Oshadha - Mar-14-2022 This is actually a very long code. If you cant shorten it, then create a separate python file to add the function. Then import the function from the file to your main project. Using import
RE: Make the code shorter - deanhystad - Mar-14-2022 Do you have to do this symbolically? There is a short and easy solution if it can be done numerically. |