Can anyone advise on how to use the SymPy TensorProduct function without having to explicitly indicate the full sequence of objects to take the product of. Below (for the simple case N=3) you can see one of my attempts. But the tensor products are still not coinciding, hence I am still having to write out the full sequence to get the correct answer.

import scipy
from numpy import*
import math
from sympy import I, Matrix, symbols
from sympy.physics.quantum import TensorProduct

N = 3

x = array([1/(math.sqrt(2)),1/(math.sqrt(2))])

V =[]
for i in range(1,N+1):

V = array(V)

TP1 = TensorProduct(V[0],V[1],V[2])
TP2 = TensorProduct(V[:])

print TP1
print TP2

Thanks for any assistance.

  • 1
    \$\begingroup\$ Code not working as intended is not ready for review on CodeReview@SE. That said, try printing V. \$\endgroup\$ – greybeard Dec 6 '19 at 3:37
  • \$\begingroup\$ @greybeard The code runs as is above and it prints $V$. The problem is that is does not give the correct tensor product unless I explicitly write out each element of the sequence. \$\endgroup\$ – John Doe Dec 6 '19 at 3:44
  • 2
    \$\begingroup\$ @JohnDoe try TP2 = TensorProduct(*V); let me know if that works or if I'm misunderstanding something \$\endgroup\$ – alexyorke Dec 6 '19 at 3:56
  • \$\begingroup\$ @alexyorke That seems to works thanks! I'm not familiar with the '*v' syntax, I'm relatively new to Python. Where can I read up on this? \$\endgroup\$ – John Doe Dec 6 '19 at 4:00
  • \$\begingroup\$ @JohnDoe Here is an article: stackoverflow.com/questions/36620025/… . I will post my comment as the answer which you can accept to let others know how you solved the problem \$\endgroup\$ – alexyorke Dec 6 '19 at 4:01

To pass a list of array arguments as individual parameters to a Python function, use the * operator:

TP1 = TensorProduct(*V)

This is equivalent to TensorProduct(V[0], V[1], V[2],...,V[n])


Not the answer you're looking for? Browse other questions tagged or ask your own question.