Add kron specialisation for sparse and dense mixes.#2391
Merged
Conversation
nwlambert
approved these changes
Feb 4, 2025
| # 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. |
Member
There was a problem hiding this comment.
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)
| # 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. |
| 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) |
Member
There was a problem hiding this comment.
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?
Member
Author
There was a problem hiding this comment.
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...
Member
There was a problem hiding this comment.
ah ok, that's a bit clearer, thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_operatorsince it useddensepriority to work.