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

Refactoring to support multigraph backend #217

Merged
merged 27 commits into from
May 17, 2024
Merged

Refactoring to support multigraph backend #217

merged 27 commits into from
May 17, 2024

Conversation

akissinger
Copy link
Member

I needed to do some non-trivial refactoring to get PyZX ready for multigraph support (cf. issue #123).

The multigraph backend is still not completely ready, but I think now might be a good time to commit my refactoring to minimise conflicts going forward, since this touches almost every file. The main issue is to revert ET to being an abstract type representing the identity of an edge, rather than an alias to Tuple[VT,VT], because the latter no longer refers to a unique edge in the graph.

Major changes are:

  • ET is now an opaque type, not an alias for Tuple[VT,VT]. If I have an e of type ET, I should use the relevant graph methods to extract it's source and target, rather than simply calling e[0] and e[1].
  • add_edge and add_edge_list should take arguments of type Tuple[VT,VT], not ET
  • add_edge in GraphS is now always "smart", i.e. it will try to handle parallel edges or throw an error. The old "dumb" version, where parallel edges get silently overwritten is gone.
  • add_edge_table just repeated calls add_edge, so all the parallel edge logic has been shifted to add_edge
  • there is a new function upair defined in BaseGraph that returns a given pair of vertices as an "unordered pair", i.e. in a canonical order. This is what GraphS.edge does, but we should no longer assume all backends will do this.

Note that some temporary hacks to get H and W nodes to work with parallel edges have not survived the refactor. I think it would be better instead to get rid of these hacks and assume with a working with multigraphs whenever we need parallel edges and non-ZX nodes. Unfortunately, this might temporarily break some ZXlive stuff.

@akissinger akissinger requested a review from jvdwetering May 3, 2024 14:51
@akissinger
Copy link
Member Author

This passes all tests and typechecker, and at least AllFeatures.ipynb still runs as it is supposed to. Should I go ahead and merge into master? Maybe worth getting one more person to have a look in case I seriously broke something, e.g. @dlyongemallo?

The current status is multigraph support via Multigraph backend is mostly done, but still needs some tests etc, but in principle nothing else should be broken if you stick the GraphS backend, except possibly the temporary hacks to support parallel edges between W & H-boxes on GraphS. This might have a knock-on effect to ZXLive, FYI @RazinShaikh

@RazinShaikh
Copy link
Contributor

Exciting to see that multigraph is finally done. I can help in integrating it with ZXLive after the PR is merged.

@dlyongemallo
Copy link
Contributor

Is there any way to break this down into smaller, logically independent pieces, or is everything tied together in such a way that it would be impossible to separate?

Can you add tests, especially where the multigraph backend behaves differently than the existing simple-graph one (ideally testing both backends to show the difference)? This is especially useful for catching edge cases.

@akissinger
Copy link
Member Author

re: breaking in to chunks: not really. I just wanted to flag this up with a few other people to do a rough sanity check before I merge. This is a major change, so if some stuff breaks, we can deal with it as and when we find it.

re: multigraph tests, those should be written eventually, but I think it's a good idea to go ahead and merge sooner rather than later because this touches so many files. Maybe it's a good idea for the multigraph backend to issue some kind of warning to use at your own risk until it has been tested a bit more extensively.

else:
if g2.edge_type(e) != EdgeType.HADAMARD: # We take Hadamard edges to be the default
self.changed_edge_types[e] = g2.edge_type(e)
# for e in new_edges:
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this just be deleted? I noticed that there are a couple of places in pyzx where blocks of code are commented out without explanation. If these should be kept, it would be helpful to readers to have a preceding comment along the lines of "Alternate implementation of ..." or "Uncomment this block if you want to...". Otherwise, for someone new to a codebase who encounters blocks of code which are commented out, it's just puzzling why they're there.

for _ in range(e.s): yield (s, t, EdgeType.SIMPLE)
for _ in range(e.h): yield (s, t, EdgeType.HADAMARD)

# def edges_in_range(self, start, end, safe=False):
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

return (s,t) if s < t else (t,s)
def edge_set(self):
return set(self.edges())
def edge_st(self, edge):
Copy link
Contributor

Choose a reason for hiding this comment

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

edge_set and edge_st are dangerously close to "minor typo leads to frustrating-to-find bug" territory, though fortunately they have different signatures.

@dlyongemallo
Copy link
Contributor

I think it's a good idea to go ahead and merge sooner rather than later because this touches so many files.

I don't see any major issues with the changes, and rather than block this on a more in-depth review, I don't object to merging this now. (I have some minor issues with places where more type hints would be helpful, or where blocks of code are commented out without explanation, but the existing codebase is already like this, and these can be fixed en masse later.)

One thing I would strongly urge though is to change the commit message to be something like "[BREAKING CHANGE] Refactor to support multigraph backend" (per my comment zxcalc/zxlive#229 (comment)).

@akissinger akissinger merged commit efd400c into master May 17, 2024
3 checks passed
@akissinger
Copy link
Member Author

Cool, done. I added [BREAKING CHANGES] to the commit msg for the merge commit.

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.

3 participants