Search code examples
sympyqutip

QuTip: How to multiply symbol with matrix


I am trying to multiply a symbol with a matrix which is defined by QuTip quantum object, but I got this error:

TypeError: Incompatible object for multiplication

I used:

from qutip import *
import sympy as sp
w0 = sp.Symbol('\omega_{0}')
w0*destroy(4)

Did I miss something?


Solution

  • The object destroy(4) contains a lot more information than just the matrix representation of the annihilation operator, so its represented as the type Qobj in qutip. The type Qobj currently doesn't support multiplication with the type sympy.Symbol. Note that you can look under the __rmul__ method of Qobj to find which types are supported for multiplying with Qobj on the right.

    If you're happy working with the matrix representations of these operators you could do the following to multiply a symbol with the matrix corresponding to destroy(4). The following will work:

    w0 * destroy(4).data.todense()
    

    This will be a numpy matrix containing symbols, and you can multiply it with the matrices corresponding to other operators in your calculation (at a great loss of efficiency!).

    Otherwise this might be worth posting an issue on their github. An implementation might be possible based on how __rmul__ is dispatched to numbers.Number here.