Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make the code shorter
#1
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
Reply
#2
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
Reply
#3
Do you have to do this symbolically? There is a short and easy solution if it can be done numerically.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 801 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 699 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Cleaning my code to make it more efficient BSDevo 13 1,381 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  how to make bot that sends instagram auto password reset code kraixx 2 1,391 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 1,144 Dec-03-2022, 10:07 PM
Last Post: Extra
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,820 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Pyspark - my code works but I want to make it better Kevin 1 1,799 Dec-01-2021, 05:04 AM
Last Post: Kevin
  List index out of range error when attempting to make a basic shift code djwilson0495 4 3,009 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Cannot Make the python Code work ErnestTBass 4 2,690 Apr-23-2020, 02:42 PM
Last Post: snippsat
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,767 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020