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

fixing issue #234 of ZXLive #234

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyzx/graph/jsonparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def string_to_phase(string: str, g: Union[BaseGraph,'GraphDiff']) -> Union[Fract
return Fraction(0)
try:
s = string.lower().replace(' ', '')
s = s.replace('*', '')
s = re.sub(r'\\?(pi|\u03c0)', '', s)
if s == '': return Fraction(1)
if s == '-': return Fraction(-1)
Expand All @@ -54,7 +55,7 @@ def string_to_phase(string: str, g: Union[BaseGraph,'GraphDiff']) -> Union[Fract
elif '/' in s:
a, b = s.split("/", 2)
if not a:
return Fraction(1, int(b))
return Fraction(int(1), int(b))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this just for visual consistency (to aid readers of the code), or does this change actually have an effect?

Copy link
Contributor Author

@lia-approves lia-approves Jun 18, 2024

Choose a reason for hiding this comment

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

This change from 1 to int(1) actually has an effect. For some reason I don't understand, I presume some obscure typing convention that could be platform dependent, there is a bug that setting the phase of a node in ZXLive to "2a+b" automatically converts to "0a+b". Oddly, changing this line here fixes this so that it actually stays as "2a+b". This was the case on at least two devices. I don't know where if there is some typechecking somewhere in the codebase or elsewhere to see if something is of type int, or why python doesn't by default have 1 be of type int, or maybe there's different int types,...

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, wow. This warrants a comment in the code with this explanation so that it isn't accidentally removed by a person or tool later which mistakenly thinks this is a noop.

if a == '-':
a = '-1'
return Fraction(int(a), int(b))
Expand Down
10 changes: 6 additions & 4 deletions pyzx/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,10 @@ def match_phase_gadgets(g: BaseGraph[VT,ET],vertexf:Optional[Callable[[VT],bool]
outputs = g.outputs()
# First we find all the phase-gadgets, and the list of vertices they act on
for v in candidates:
non_clifford = phases[v] != 0 and getattr(phases[v], 'denominator', 1) > 2
if isinstance(phases[v], Poly): non_clifford = True
if isinstance(phases[v], Poly):
non_clifford = True
else:
non_clifford = phases[v] != 0 and getattr(phases[v], 'denominator', 1) > 2
if non_clifford and len(list(g.neighbors(v)))==1:
n = list(g.neighbors(v))[0]
if phases[n] not in (0,1): continue # Not a real phase gadget (happens for scalar diagrams)
Expand Down Expand Up @@ -894,7 +896,7 @@ def match_supplementarity(g: BaseGraph[VT,ET], vertexf:Optional[Callable[[VT],bo
# First we find all the non-Clifford vertices and their list of neighbors
while len(candidates) > 0:
v = candidates.pop()
if phases[v] == 0 or phases[v].denominator <= 2: continue # Skip Clifford vertices
if phases[v] == 0 or (not isinstance(phases[v], Poly) and phases[v].denominator <= 2): continue # Skip Clifford vertices
neigh = set(g.neighbors(v))
if not neigh.isdisjoint(taken): continue
par = frozenset(neigh)
Expand All @@ -910,7 +912,7 @@ def match_supplementarity(g: BaseGraph[VT,ET], vertexf:Optional[Callable[[VT],bo
if v in taken: continue
else: parities[par] = [v]
for w in neigh:
if phases[w] == 0 or phases[w].denominator <= 2 or w in taken: continue
if phases[w] == 0 or (not isinstance(phases[w], Poly) and phases[w].denominator <= 2) or w in taken: continue
diff = neigh.symmetric_difference(g.neighbors(w))
if len(diff) == 2: # Perfect overlap
if (phases[v] + phases[w]) % 2 == 0 or (phases[v] - phases[w]) % 2 == 1:
Expand Down
Loading