Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize apply_matrix_to_slices when sparse #5902

Closed
wants to merge 2 commits into from

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Oct 2, 2022

Improves the perf of apply_matrix_to_slices when the operator matrix has ones and zeroes. For example for ResetChannel on a large density matrix, this results in about a 3x speedup.

cirq.DensityMatrixSimulator(split_untangled_states=False).simulate(
    cirq.Circuit([cirq.reset(cirq.LineQubit(0))] * 100), qubit_order=cirq.LineQubit.range(11)
)

@daxfohl daxfohl requested review from a team, vtomole and cduck as code owners October 2, 2022 23:00
@daxfohl daxfohl requested a review from tanujkhattar October 2, 2022 23:01
@CirqBot CirqBot added the size: S 10< lines changed <50 label Oct 2, 2022
Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

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

What is the performance impact of this change for dense matrices?

My hunch is that introducing branching to special-case multiplication by one and by zero is actually a bad idea for performance in the general case. Especially, if the branching is done in python rather than in C within numpy (though I doubt it would be helpful even in the latter case on the grounds that performance impact of branch misprediction is usually rather severe).

A better way to exploit matrix sparsity in python is to use a package such as scipy.sparse. However, in my view this is still not ideal since it makes cirq inconsistent in its approach to sparse operators. Instead, I think we should do for channels what we've done for unitary gates which is to special-case the handling of sparse operators in magic methods such as _apply_unitary_ and _apply_channel_. See here for an example.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Oct 7, 2022

Good callout. I'll look at the perf impact on various cases when I get a chance. It didn't have a noticeable effect on the unit tests in cirq/sim in either direction, but I'll look more explicitly.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Oct 8, 2022

Yes the perf impact does seem to be significant (about 20%) up to about 2^13 element input tensors, beyond which the numpy dominates.

I figured an if check would be negligible compared to the numpy operation at any size. Learn something new every day.

@daxfohl daxfohl closed this Oct 8, 2022
@daxfohl daxfohl deleted the speedup-linalg branch October 8, 2022 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants