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.
Before submitting
Context:
Some speedups for sampling from a GBS distribution.
Description of the Change:
The current generate_hafnian_sample function calculates hafnians of matrices larger than (twice) the sampled number of photons in each mode. This can be avoided by first sampling a random number and calculating the cumulative probability for increasing photon numbers and stopping once the cumulative probability has reached the sampled number. The loops have also been reduced to return -1 sooner if the sampling fails and the final mode can use pure_state_amplitude if the covariance matrix is pure. Finally the absolute function in the approximate case has been removed so that in the case of a non-positive matrix it throws an error rather than giving a sample from the wrong distribution.
Benefits:
Faster sampling than the current method.
There is another pull request that replaces this function with the quadratically faster algorithm. For higher numbers of photons in the sample that function will be faster. However, for lower numbers of photons this suggested method may be faster (depending on the cutoff supplied in the quadratic speedup version) as it does not need to calculate larger matrices than the number of photons sampled regardless of the cutoff chosen. This method is also exact up to a global cutoff whereas the quadratic speedup algorithm has some error due to the cutoff in each mode. In order to sample exactly, the cutoff in each mode needs to be the same as the global cutoff and so the cutoff kwarg is set to None which becomes the total number of photons +1.
Possible Drawbacks:
None
Related GitHub Issues:
None