XanaduAI / MrMustard

A differentiable bridge between phase space and Fock space
https://mrmustard.readthedocs.io/
Apache License 2.0
78 stars 27 forks source link

Fix `math.Categorical` #342

Closed SamFerracin closed 9 months ago

SamFerracin commented 9 months ago

Context:

probs = np.array([1e-6 for _ in range(300)])
results = [math.Categorical(probs, "") for _ in range(100)]
assert len(set(results)) > 1

When using the numpy backend, the code above fails: results contains 100 identical values. This is because math.Categorical uses np.random.multinomial, which assumes that the probabilities sum up to 1 (if not, the last probability is increased such that the resulting probabilities sum up to 1)

codecov[bot] commented 9 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (db1f4d5) 84.14% compared to head (a26185c) 84.14%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #342 +/- ## ======================================== Coverage 84.14% 84.14% ======================================== Files 65 65 Lines 4775 4775 ======================================== Hits 4018 4018 Misses 757 757 ``` | [Files](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/342?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI) | Coverage Δ | | |---|---|---| | [mrmustard/math/backend\_manager.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/342?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL21hdGgvYmFja2VuZF9tYW5hZ2VyLnB5) | `98.08% <ø> (ø)` | | | [mrmustard/math/backend\_numpy.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/342?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL21hdGgvYmFja2VuZF9udW1weS5weQ==) | `100.00% <ø> (ø)` | | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/342?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/342?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). Last update [db1f4d5...a26185c](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/342?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI).