Python Forum

Full Version: Make the code shorter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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*pz
If 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*pzp
And 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
if 
statement 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_final
As 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
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
Do you have to do this symbolically? There is a short and easy solution if it can be done numerically.