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

Efficient calculation of probabilities #459

Open
1 task done
JacobHast opened this issue Aug 13, 2024 · 2 comments
Open
1 task done

Efficient calculation of probabilities #459

JacobHast opened this issue Aug 13, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@JacobHast
Copy link
Contributor

JacobHast commented Aug 13, 2024

Before posting a feature request

  • I have searched exisisting GitHub issues to make sure the feature request does not already exist.

Feature details

The calculation of probabilities in lab_dev for states in the Fock representation appears relatively slow - at least it can be sped up by a factor 10 using a simple numpy code as shown in this example.

image

Implementation

No response

How important would you say this feature is?

2: Somewhat important. Needed this quarter.

Additional information

While the calculation of the probability of a single state is quite fast, I sometimes need to do it for >100,000 states, at which point this step becomes the numerical bottleneck, so it would be very useful to get this improved.
I have not done the comparison for DM states, only Ket

@JacobHast JacobHast added the enhancement New feature or request label Aug 13, 2024
@JacobHast
Copy link
Contributor Author

This also directly affects the speed of .normalize

@JacobHast
Copy link
Contributor Author

Code also testing .to_fock() before .probability - doesn't improve things

import mrmustard.lab_dev as mm
import numpy as np
from mrmustard import settings
import timeit

settings.AUTOSHAPE_MAX = 1000

def make_state() -> mm.Ket:
    state = mm.Ket.from_fock([0], np.random.random(settings.AUTOSHAPE_MAX))
    return state

STATE = make_state()

def probability_np() -> float:
    state_fock = STATE.fock(settings.AUTOSHAPE_MAX)
    return np.sum(np.abs(state_fock) ** 2)

def probability_buildin() -> float:
    return STATE.probability

def probability_buildin_to_fock() -> float:
    return STATE.to_fock().probability

%timeit probability_np() # 22.4 µs
%timeit probability_buildin() # 218 µs
%timeit probability_buildin_to_fock() # 371 µs


assert np.isclose(probability_np(), probability_buildin())
assert np.isclose(probability_np(), probability_buildin())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant