qiskit-community / qiskit-dynamics

Tools for building and solving models of quantum systems in Qiskit
https://qiskit-community.github.io/qiskit-dynamics/
Apache License 2.0
103 stars 61 forks source link

Handling of operator input types to models #22

Closed DanPuzzuoli closed 3 years ago

DanPuzzuoli commented 3 years ago

What is the expected behavior?

There are a few type-handling issues that we need to sort out. Currently, the model classes support operation in both dense and sparse modes, however, regardless of the chosen mode of operation, at instantiation, everything is passed through the to_array function, which converts everything to a dense array. If a sparse mode is selected, it will then convert these arrays to sparse. We should set things up so that if a user specifies things in a sparse format then it never gets converted to a dense format.

To do:

DanPuzzuoli commented 3 years ago

Possible code suggestion from @chriseclectic for detecting/conversion from qutip without importing qutip.

def isinstance_qutip_qobj(obj):
    if (type(obj).__name__ == 'Qobj'
            and hasattr(obj, '_data')
            and type(obj._data).__name__ == 'fast_csr_matrix'):
        return True
    return False

import qutip
op = qutip.Qobj([[0, 1], [1, 0]])

if isinstance_qutip_qobj(op):
    op = op._data  # csr matrix