replace ground-based Reset with symbolic boolean paradigm#430
replace ground-based Reset with symbolic boolean paradigm#430dlyongemallo wants to merge 1 commit intozxcalc:masterfrom
Reset with symbolic boolean paradigm#430Conversation
There was a problem hiding this comment.
Pull request overview
Fixes an issue in PyZX’s copy rewrite matching that affected hybrid (grounded) graphs produced by circuits with mid-circuit reset, preventing copy_simp/full_reduce from incorrectly rewriting away measurement outcome phases.
Changes:
- Updated
check_copyto explicitly exclude ground vertices from matching the copy rule. - Added a regression test based on a Steane X-stabiliser measurement circuit with mid-circuit resets to ensure symbolic measurement phases survive
full_reduce. - Documented the fix in the changelog.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
pyzx/rewrite_rules/copy_rule.py |
Prevents check_copy from matching degree-1 ground spiders created by reset/discard semantics. |
tests/test_simplify.py |
Adds regression coverage ensuring ground nodes don’t match check_copy and that full_reduce preserves measurement symbolic phases. |
CHANGELOG.md |
Notes the bugfix in the Unreleased “Fixed” section. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
It would be safe to disallow this copying, but it is semantically sound to copy through a ground, as long as the ground copies to both spiders that are produced. |
|
The problem I'm trying to understand is the cause of the difference in the post-reduction graphs in tqec/tqec#528 (comment). The You're right, this (excluding ground nodes from |
copy rule|
I think there is more a problem about mixing two different approaches to representing mixed quantum-classical processes in ZX-diagrams. We can either use grounds to make a distinction between classical and quantum information, or we can not use grounds at all but use parameters to represent measurement outcomes. Not using the parameter for the measurement outcome is then the same as discarding, because then we would have to sum over the two outcomes, which is semantically equal to discarding. |
|
@jvdwetering IIUC the problem is that the library has two approaches to representing ground and it would be better to stick to just one. Following up on the conversation in #402 (comment), would it make sense to just remove |
f27e012 to
ccf5521
Compare
|
I think the |
|
I agree its confusing. There are some benefits to still having the grounds, as they have special rewrites (a ground eats Hadamards, phases and if you have two grounds after a CNOT, the CNOT gets removed), but in principle we could mimick this behaviour when we have parameters by labelling certain parameters as being 'summed over', which effectively make them the same as grounds. |
Reset with symbolic boolean paradigm
ccf5521 to
5b7d866
Compare
|
I've carried out the suggested "summed-over" change for The |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
pyzx/circuit/graphparser.py:195
graph_to_circuitnow calls_poly_phase_to_conditional_gate(phase, t, ...)for any vertex with aPolyphase (includingVertexType.X), but_poly_phase_to_conditional_gatestill raisesValueErrorfor non-Zvertex types. This will crash graph extraction for conditional X/NOT gates (which are represented as X spiders with booleanPolyphases). Update_poly_phase_to_conditional_gateto handleVertexType.X(mapping phases to X-rotations/NOT analogously to the Z case), or restore the previous guard so X-typePolyphases fall back to emitting anXPhasegate instead of raising.
# Conditional gate (both Z and X types are unambiguous
# now that measurement outcomes are leaves).
cgate = _poly_phase_to_conditional_gate(phase, t, int(q))
if cgate is not None:
c.add_gate(cgate)
elif phase != 0:
gate_name = "ZPhase" if t == VertexType.Z else "XPhase"
c.add_gate(gate_name, q, phase=phase)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
5b7d866 to
7fb0126
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
51b42f6 to
53ce3c8
Compare
|
I still don't really understand what is going on here and why it is needed. I don't understand what all the logic is about creating new boundary spiders. A Reset should not have anything to do with boundary wires. |
Let's start with the "why it is needed": currently, mid-circuit The existing As for what's going on: this change moves the symbolic phase to a leaf hanging off the wire. The consequence is that The boundary spider is not identifying the reset. It's just how the " The downside is that the disconnected boundary vertex is not in |
|
I'm going to rework this to tag the reset using the |
69552a6 to
ae9769d
Compare
|
This is now in a state which satisfies the requirements of tqec/tqec#528. This is a fairly large change. If necessary, I could try to break it up into 3 or 4 smaller PRs. |
ae9769d to
2743dbc
Compare
Description
Replaces the ground-based representation of
Resetwith a symbolic-boolean representation.Previously,
Resetproduced aZ(0)spider withground=True(trace-out) followed by a fresh|0>boundary, which mixed two paradigms. This causedcopy_simp(infull_reduce) to copy a measurement's symbolic-phaseXleaf through the adjacent reset ground, destroying the measurement outcome.After this change,
Resetwill produce aZ(0)spider with anX(_rN)leaf hanging off of it (where_rNis a fresh boolean variable), with anoutcome_type='reset_discard'tagged in vertex data (distinct fromoutcome_type='measurement'), followed by a disconnected|0>boundary. Since_rNis not referenced elsewhere, summing over it is equivalent to discarding the measurement result. This implements the trace-out semantics without grounds.Related issue
tqec/tqec#528
Checklist
pyzx.extract.extract_circuit).