Skip to content

Add kron specialisation for sparse and dense mixes.#2391

Merged
Ericgig merged 4 commits into
qutip:masterfrom
Ericgig:misc.kron_dense_csr
Feb 4, 2025
Merged

Add kron specialisation for sparse and dense mixes.#2391
Ericgig merged 4 commits into
qutip:masterfrom
Ericgig:misc.kron_dense_csr

Conversation

@Ericgig

@Ericgig Ericgig commented Apr 10, 2024

Copy link
Copy Markdown
Member

Description
In general, operation between sparse and dense return dense. This is not ideal for the kron product which output would be sparser than the sparse input. The dispatcher does not have fine control for these cases, (Jake plan to add it but never had the time to get to it), so I added wrapper specialisation for these cases.

I had to patch expand_operator since it used dense priority to work.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 86.048%. remained the same
when pulling 1a5a8d7 on Ericgig:misc.kron_dense_csr
into 8035590 on qutip:master.

Comment thread qutip/core/data/kron.pyx Outdated
# 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a suggestion here; how about "Since the dispatcher does not have precise control on which function to use when the signature is missing, we specify the output to be the sparse format of the input." or something like that (the original is a bit opaque)

Comment thread qutip/core/data/kron.pyx Outdated
# 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Comment thread qutip/core/tensor.py
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one question; is there a reason the dtype defaults to CSR if its not specified or set in settings? why not default to the dtype of oper?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what we want to avoid.
If oper is dense, once extended, the output will be quite sparse:

oper = rand_herm(2, dtype="Dense")
expand_operator(oper, [2, 2, 2, 2, 2], [2])

The output will be a 32*32 matrix with 4 non-zeros elements...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, that's a bit clearer, thanks!

@coveralls

coveralls commented Feb 4, 2025

Copy link
Copy Markdown

Coverage Status

coverage: 87.729%. remained the same
when pulling 19cc09d on Ericgig:misc.kron_dense_csr
into eb3e5c4 on qutip:master.

@Ericgig Ericgig merged commit 2f73d01 into qutip:master Feb 4, 2025
@Ericgig Ericgig deleted the misc.kron_dense_csr branch February 4, 2025 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants