1
\$\begingroup\$

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.append(x)

V = array(V)

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

print TP1
print TP2

Thanks for any assistance.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Code not working as intended is not ready for review on CodeReview@SE. That said, try printing V. \$\endgroup\$ Commented Dec 6, 2019 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\$ Commented Dec 6, 2019 at 3:44
  • 2
    \$\begingroup\$ @JohnDoe try TP2 = TensorProduct(*V); let me know if that works or if I'm misunderstanding something \$\endgroup\$ Commented Dec 6, 2019 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\$ Commented Dec 6, 2019 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\$ Commented Dec 6, 2019 at 4:01

1 Answer 1

2
\$\begingroup\$

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

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.