Skip to content

Commit

Permalink
Add kron dense dia
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericgig committed Apr 10, 2024
1 parent 10a4c4b commit 1a5a8d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions qutip/core/data/kron.pxd
Expand Up @@ -10,6 +10,8 @@ cpdef CSR kron_csr(CSR left, CSR right)
cpdef Dia kron_dia(Dia left, Dia right)
cpdef CSR kron_dense_csr_csr(Dense left, CSR right)
cpdef CSR kron_csr_dense_csr(CSR left, Dense right)
cpdef Dia kron_dia_dense_dia(Dia left, Dense right)
cpdef Dia kron_dense_dia_dia(Dense left, Dia right)

cpdef Data kron_transpose_data(Data left, Data right)
cpdef Dense kron_transpose_dense(Dense left, Dense right)
19 changes: 19 additions & 0 deletions qutip/core/data/kron.pyx
Expand Up @@ -16,6 +16,7 @@ import numpy
__all__ = [
'kron', 'kron_csr', 'kron_dense', 'kron_dia',
'kron_csr_dense_csr', 'kron_dense_csr_csr',
'kron_dia_dense_dia', 'kron_dense_dia_dia',
'kron_transpose', 'kron_transpose_dense', 'kron_transpose_data',
]

Expand Down Expand Up @@ -176,6 +177,22 @@ cpdef Dia kron_dia(Dia left, Dia right):
return out


cpdef Dia kron_dia_dense_dia(Dia left, Dense right):
# The dispatcher would use kron_dense, but the output is at least as sparse
# as the sparse input. Since the dispatcher does not have precise control
# on which function to use when the signature is missing. We add
# then like this.
return kron_dia(left, _to(Dia, right))


cpdef Dia kron_dense_dia_dia(Dense left, Dia right):
# The dispatcher would use kron_dense, but the output is at least as sparse
# as the sparse input. Since the dispatcher does not have precise control
# on which function to use when the signature is missing. We add
# then like this.
return kron_dia(_to(Dia, left), right)


from .dispatch import Dispatcher as _Dispatcher
import inspect as _inspect

Expand All @@ -200,6 +217,8 @@ kron.add_specialisations([
(Dia, Dia, Dia, kron_dia),
(CSR, Dense, CSR, kron_csr_dense_csr),
(Dense, CSR, CSR, kron_dense_csr_csr),
(Dia, Dense, Dia, kron_dia_dense_dia),
(Dense, Dia, Dia, kron_dense_dia_dia),
], _defer=True)


Expand Down
2 changes: 1 addition & 1 deletion qutip/core/tensor.py
Expand Up @@ -467,4 +467,4 @@ def expand_operator(oper, dims, targets, dtype=None):
for i, ind in enumerate(rest_pos):
new_order[ind] = rest_qubits[i]
id_list = [identity(dims[i]) for i in rest_pos]
return tensor([oper] + id_list).permute(new_order)
return tensor([oper] + id_list).permute(new_order).to(dtype)
2 changes: 2 additions & 0 deletions qutip/tests/core/data/test_mathematics.py
Expand Up @@ -764,6 +764,8 @@ def op_numpy(self, left, right):
pytest.param(data.kron_dia, Dia, Dia, Dia),
pytest.param(data.kron_dense_csr_csr, Dense, CSR, CSR),
pytest.param(data.kron_csr_dense_csr, CSR, Dense, CSR),
pytest.param(data.kron_dense_dia_dia, Dense, Dia, Dia),
pytest.param(data.kron_dia_dense_dia, Dia, Dense, Dia),
]


Expand Down

0 comments on commit 1a5a8d7

Please sign in to comment.