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

Using Method Resolution Order (MRO) to return objects of better types. #363

Closed SamFerracin closed 8 months ago

SamFerracin commented 8 months ago

Context: In lab_dev, doing Vacuum(modes=[0]).to_fock_component() returns an object of type CircuitComponent. We want an object of type Ket instead.

Description of the Change: The from_attributes method is modified so that if needed, it scans through the MRO of the class of the given object, and it returns a Ket, DM, Unitary, or Channel if possible, a CircuitComponent otherwise.

For performance reasons, this function does not validate the inputs and must therefore be used with care. To avoid bugs on the user's side, it is turned into a private method.

codecov[bot] commented 8 months ago

Codecov Report

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

Project coverage is 86.27%. Comparing base (2c8e366) to head (c4eb218).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #363 +/- ## =========================================== + Coverage 86.26% 86.27% +0.01% =========================================== Files 78 78 Lines 5488 5495 +7 =========================================== + Hits 4734 4741 +7 Misses 754 754 ``` | [Files](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/363?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI) | Coverage Δ | | |---|---|---| | [mrmustard/lab\_dev/circuit\_components.py](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/363?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI#diff-bXJtdXN0YXJkL2xhYl9kZXYvY2lyY3VpdF9jb21wb25lbnRzLnB5) | `95.83% <100.00%> (+0.25%)` | :arrow_up: | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/363?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/363?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XanaduAI). Last update [2c8e366...c4eb218](https://app.codecov.io/gh/XanaduAI/MrMustard/pull/363?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).