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

Potential workaround for _from_attributes #461

Closed apchytr closed 3 months ago

apchytr commented 3 months ago

Context: Atm calling e.g DGate.to_fock returns a Unitary instead of a DGate due to _from_attributes. Here is a proposed alternative where we attempt to initialize the original class using the ParameterSet and default to _from_attributes if that fails.

Description of the Change: to_fock will attempt to initialize self.__class__ using _getitem_builtin (moved from State) and if it fails with a TypeError will default to _from_attributes. Number and Sauron now populate their parameter sets. ParameterSet now has a to_dict method. Lots of code cleanup/ appeasing codefactor/code coverage.

Benefits: to_fock returns more user facing classes. This helps us avoid the need to overwrite _from_attributes in some cases.

codecov[bot] commented 3 months ago

Codecov Report

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

Project coverage is 89.10%. Comparing base (877d0ea) to head (77e2690). Report is 1 commits behind head on develop.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #461 +/- ## =========================================== + Coverage 88.96% 89.10% +0.14% =========================================== Files 101 101 Lines 7051 7051 =========================================== + Hits 6273 6283 +10 + Misses 778 768 -10 ``` | [Files](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI) | Coverage Δ | | |---|---|---| | [mrmustard/lab/abstract/state.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Flab%2Fabstract%2Fstate.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYi9hYnN0cmFjdC9zdGF0ZS5weQ==) | `92.66% <100.00%> (ø)` | | | [mrmustard/lab/circuit.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Flab%2Fcircuit.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYi9jaXJjdWl0LnB5) | `95.16% <100.00%> (ø)` | | | [mrmustard/lab\_dev/circuit\_components.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fcircuit_components.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvY2lyY3VpdF9jb21wb25lbnRzLnB5) | `100.00% <100.00%> (+2.26%)` | :arrow_up: | | [mrmustard/lab\_dev/states/base.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fbase.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL2Jhc2UucHk=) | `96.91% <100.00%> (+0.72%)` | :arrow_up: | | [mrmustard/lab\_dev/states/number.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fnumber.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL251bWJlci5weQ==) | `100.00% <100.00%> (+3.70%)` | :arrow_up: | | [mrmustard/lab\_dev/states/sauron.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Fstates%2Fsauron.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvc3RhdGVzL3NhdXJvbi5weQ==) | `100.00% <100.00%> (ø)` | | | [mrmustard/lab\_dev/utils.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Flab_dev%2Futils.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvdXRpbHMucHk=) | `95.83% <100.00%> (ø)` | | | [.../strategies/compactFock/singleLeftoverMode\_amps.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Fmath%2Flattice%2Fstrategies%2FcompactFock%2FsingleLeftoverMode_amps.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL21hdGgvbGF0dGljZS9zdHJhdGVnaWVzL2NvbXBhY3RGb2NrL3NpbmdsZUxlZnRvdmVyTW9kZV9hbXBzLnB5) | `29.89% <100.00%> (ø)` | | | [mrmustard/math/lattice/strategies/flat\_indices.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Fmath%2Flattice%2Fstrategies%2Fflat_indices.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL21hdGgvbGF0dGljZS9zdHJhdGVnaWVzL2ZsYXRfaW5kaWNlcy5weQ==) | `100.00% <100.00%> (ø)` | | | [mrmustard/math/parameter\_set.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree&filepath=mrmustard%2Fmath%2Fparameter_set.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL21hdGgvcGFyYW1ldGVyX3NldC5weQ==) | `97.43% <100.00%> (+0.29%)` | :arrow_up: | | ... and [4 more](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI) | | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?dropdown=coverage&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/461?dropdown=coverage&src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). Last update [877d0ea...77e2690](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/461?dropdown=coverage&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).
apchytr commented 3 months ago

I like it! I don't wanna drag this out much so I'll give my approval, but I do have an alternate proposal. Perhaps instead of moving _getitem_builtin up, we should just add the functionality to ParameterSet - something like todict()? then the modes quirk can have its own handling in one place, and the parameter kwargs generation can be in another place. thoughts? if you do choose to make the change, I promise to come back with another quick approval 😇

@timmysilv pushed a to_dict method on ParameterSet. It's mostly just taking the kwargs logic from the previous _getitem_builtin 👍 feel free to take a look!