TensorProduct of sparse matrices failing

18 views
Skip to first unread message

Edward Dahl

unread,
Apr 28, 2020, 2:16:03 PM4/28/20
to sympy
I'd like to form some tensor products of sparse matrices, but this does not seem to work:

from sympy import diag
from sympy.physics.quantum import TensorProduct
from sympy.matrices import SparseMatrix

spm
= SparseMatrix(diag(1,0))

tp
= TensorProduct(spm,spm)

Here's the failure:

Traceback (most recent call last):
  File "/Users/ddahl/anaconda3/envs/qiskit/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-9-88417d1a4157>", line 1, in <module>
    tp = TensorProduct(spm,spm)
  File "/Users/ddahl/anaconda3/envs/qiskit/lib/python3.8/site-packages/sympy/physics/quantum/tensorproduct.py", line 123, in __new__
    c_part, new_args = cls.flatten(sympify(args))
  File "/Users/ddahl/anaconda3/envs/qiskit/lib/python3.8/site-packages/sympy/physics/quantum/tensorproduct.py", line 139, in flatten
    cp, ncp = arg.args_cnc()
  File "/Users/ddahl/anaconda3/envs/qiskit/lib/python3.8/site-packages/sympy/matrices/matrices.py", line 2339, in __getattr__
    raise AttributeError(
AttributeError: ImmutableSparseMatrix has no attribute args_cnc.

When I look in sympy/physics/quantum/tensorproduct.py it looks to me like there is an attempted test to determine
whether the first argument to the TensorProduct(...) function is a sparse SciPy matrix:

def __new__(cls, *args):
   
if isinstance(args[0], (Matrix, numpy_ndarray, scipy_sparse_matrix)):
       
return matrix_tensor_product(*args)
    c_part
, new_args = cls.flatten(sympify(args))
    c_part
= Mul(*c_part)
   
if len(new_args) == 0:
       
return c_part
   
elif len(new_args) == 1:
       
return c_part * new_args[0]
   
else:
        tp
= Expr.__new__(cls, *new_args)
       
return c_part * tp

But according to my debugger, the isinstance(...) call in the above code block returns false.
Looks to me like that sparse matrix should be of type scipy_sparse_matrix.

My environment is a freshly built Anaconda from two weeks ago and I'm running on macOS.

What am I missing?

Thanks,
-Denny


Aaron Meurer

unread,
Apr 28, 2020, 2:18:19 PM4/28/20
to sympy
This looks like a bug. Can you open an issue for it? I think it should
be using isinstance(MatrixBase) instead of isinstance(Matrix).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/478ff24e-d488-442d-927a-1865b2565850%40googlegroups.com.

Edward Dahl

unread,
Apr 29, 2020, 5:34:25 PM4/29/20
to sympy
Sure thing, Aaron.  I opened an issue - it's #19227.

-Denny


On Tuesday, April 28, 2020 at 12:18:19 PM UTC-6, Aaron Meurer wrote:
This looks like a bug. Can you open an issue for it? I think it should
be using isinstance(MatrixBase) instead of isinstance(Matrix).

Aaron Meurer

> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages