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

Fix conditional import problem and refactor #187

Closed

Conversation

EarlMilktea
Copy link
Contributor

Resolves #171 .

@EarlMilktea EarlMilktea self-assigned this Jul 28, 2024
@EarlMilktea EarlMilktea linked an issue Jul 28, 2024 that may be closed by this pull request
Copy link

codecov bot commented Jul 28, 2024

Codecov Report

Attention: Patch coverage is 85.36585% with 12 lines in your changes missing coverage. Please review.

Project coverage is 75.42%. Comparing base (364be19) to head (011aecc).
Report is 13 commits behind head on master.

Files with missing lines Patch % Lines
graphix/graphsim/rxgraphstate.py 84.00% 4 Missing ⚠️
graphix/graphsim/utils.py 76.92% 3 Missing ⚠️
graphix/extraction.py 60.00% 2 Missing ⚠️
graphix/graphsim/rxgraphviews.py 91.30% 2 Missing ⚠️
graphix/graphsim/graphstate.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #187      +/-   ##
==========================================
- Coverage   75.52%   75.42%   -0.10%     
==========================================
  Files          34       34              
  Lines        5569     5551      -18     
==========================================
- Hits         4206     4187      -19     
- Misses       1363     1364       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EarlMilktea EarlMilktea marked this pull request as ready for review July 28, 2024 01:18
@EarlMilktea EarlMilktea force-pushed the 171-bug-type-hint-is-dynamically-initialized branch from 2e92467 to a233c93 Compare July 28, 2024 11:09
Copy link
Contributor

@thierry-martinez thierry-martinez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, except for some remarks regarding type annotations and use_rustworkx default.

Comment on lines -62 to +34
def nodes(self) -> NodesObject:
def nodes(self) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can be more precise in these type annotations: NodesObject, EdgesObject and GraphObject were not intended to be union types; they depend on whether the subclass is NXGraphState or RXGraphState. Therefore, they are type parameters of the generic class BaseGraphState, which NXGraphState and RXGraphState instantiate with the types they actually use. I propose a fix here: thierry-martinez@f2ef4b1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this! Let me use your suggestion.

Comment on lines +53 to +54
def try_use_rustworkx() -> bool:
return util.find_spec("rustworkx") is not None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it would be better to let GraphState use rustworkx by default if available, not specifically for tests but in any use cases (except for some tests such as test_pauli_measurement, where we specifically want to check that both networkx and rustworkx code paths work correctly). I propose moving rustworkx probing at GraphState level: thierry-martinez@f7e503c

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shinich1 In the first place, I'm not sure why rustworkx was made optional. Do you remember why?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EarlMilktea actually that's a good point! When we first added rustworkx did not support some combinations of OS/version, thus this impl. If rustworkx is much more mature now, we can just drop networkx version. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shinich1

It would greatly simplify the code. I'm for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also would like to do thorough refactor.

Copy link
Contributor Author

@EarlMilktea EarlMilktea Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thierry-martinez @shinich1
I'm not opposed to keep networkx, but at the same time I'm strongly tempted to drop rxgraphviews.
This module is forcibly making rustworkx behave like networkx by translating its internal representation.
This can never be the right way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, let me point out that networkx has very poor type annotation support: we somehow need to isolate/wrap it to prevent Any or type warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thierry-martinez

for portability

fastflow will anyway introduce similar portability constraints as rustworkx, but it would not be a problem because for such environments even numpy is not binary-distributed in the first place.
Also see https://github.com/TeamGraphix/fastflow/blob/2-implement-flow-algorithm/.github/workflows/wheel.yml .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

being able to target multiple (two) graph back-ends help to have the right abstractions in BaseGraphState.

@thierry-martinez do you have stim integration in mind regarding above (which you mentioned are ongoing on Paris side)?
I am tempted to drop networkx, as doing so should make it easier to maintain. If stim or other stabilizer simulator may come in to this, let us keep the abstract interface. what do you think? @EarlMilktea @thierry-martinez

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we have to keep networkx for stim, but I believe that having the networkx back-end may have several advantages: beside keeping a pure Python implementation, it can be helpful for instance to catch bugs in other backends (see #206 ).

from rustworkx import PyGraph
else:
PyGraph = None
def try_to_networkx(g: BaseGraphState) -> nx.Graph:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the prefix try_ should be preferred for functions that gracefully return in any case, even if they failed to accomplish their goal (for instance, by returning None). This function converts g into networkx graph, and raises an exception if it is not possible; to_networkx seems to be a good name for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Anyway we may need to share naming styles...

graphix/graphsim/rxgraphviews.py Outdated Show resolved Hide resolved
graphix/graphsim/rxgraphviews.py Outdated Show resolved Hide resolved
graphix/graphsim/rxgraphviews.py Outdated Show resolved Hide resolved
from graphix.graphsim.rxgraphstate import RXGraphState

use_rustworkx = isinstance(graph, RXGraphState)
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModuleNotFoundError would be better, but I propose to let GraphState do rustworkx probing itself: thierry-martinez@f7e503c

EarlMilktea and others added 4 commits July 29, 2024 08:09
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
@EarlMilktea EarlMilktea changed the title Fix conditional import problem Fix conditional import problem and refactor Jul 28, 2024
@EarlMilktea EarlMilktea marked this pull request as draft August 8, 2024 04:20
@EarlMilktea
Copy link
Contributor Author

Let me close this PR as graphsim module is subject to change in the near future.

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

Successfully merging this pull request may close these issues.

[Bug]: Type hint is dynamically initialized
3 participants