When using numpy or tensorflow in Python, we can simply write

C = A @ B

for matrix multiplication C = np.matmul(A,B). I wonder if there is a shorthand for tensor product (Kronecker product), $C=A\otimes B$ . Now I can only do this by using np.kron(A, B) or using qutip qutip.tensor(A, B)


1 Answer 1


Python currently doesn't support an operator for Kronecker products. Note how the @ symbol works: when you write the statement A @ B, Python$^1$ checks the objects A and B for a __matmul__ method and then returns A.__matmul__(B). But there's no built-in operator that corresponds to something like a __kron__ method.

If you really want this functionality, one way might be to change how the * operator works by redefining __mul__ (e.g. see here or here) to perform a kronecker product, so that calling A * B would return np.kron(A, B). However this could end up being very confusing for yourself and others reading your code.

$^1$ as of Python 3.5, see PEP 465: https://www.python.org/dev/peps/pep-0465/


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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