-
Notifications
You must be signed in to change notification settings - Fork 150
Add support for H-boxes with arbitrary complex labels #400
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
base: master
Are you sure you want to change the base?
Add support for H-boxes with arbitrary complex labels #400
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for H-boxes with arbitrary complex labels, similar to the existing support for Z-boxes with complex labels. Previously, H-boxes could only represent phases as multiples of π. This enhancement allows H-boxes to represent matrices with arbitrary complex entries in the bottom-right position, enabling more general quantum operations.
Changes:
- Added utility functions (
get_h_box_label,set_h_box_label,is_standard_hbox,hbox_has_complex_label) for managing H-box complex labels - Updated tensor computation to handle H-boxes with complex labels
- Enhanced JSON and TikZ serialization to support round-trip conversion of H-box complex labels
- Modified multiple rewrite rules to correctly distinguish between standard Hadamard gates (label=-1) and H-boxes with arbitrary complex labels
- Added comprehensive test coverage for the new functionality
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pyzx/utils.py | Added four new helper functions for managing H-box complex labels, following the same pattern as Z-box labels |
| pyzx/tensor.py | Modified H_to_tensor to accept optional complex label parameter and updated tensorfy_naive to use labels when present |
| pyzx/tikz.py | Enhanced TikZ import/export to parse and serialize complex labels for H-boxes, hiding standard Hadamard labels (-1) |
| pyzx/drawing.py | Updated matplotlib and JSON visualization to display complex labels for H-boxes |
| pyzx/graph/jsonparser.py | Added JSON serialization support for H-box labels in both new and legacy formats |
| pyzx/graph/base.py | Updated documentation for add_vertex to clarify usage of label functions |
| pyzx/rewrite_rules/*.py | Updated 7 rewrite rule files to use is_standard_hbox instead of direct phase checks, ensuring rules only apply to standard Hadamard gates |
| tests/test_tensor.py | Added tests for H_to_tensor with labels and tensorfy with complex-labeled H-boxes |
| tests/test_jsonparser.py | Added comprehensive tests for JSON and TikZ round-trip with H-box labels |
| tests/test_hbox.py | Added extensive tests for helper functions and rewrite rules with complex labels |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
af0c73f to
d1364d0
Compare
| newv = g.add_vertex(g.type(v[j]), qubit=q, row=r) | ||
| g.set_phase(newv, g.phase(v[j])) | ||
| # Copy complex label if H-box has one. | ||
| if g.type(v[j]) == VertexType.H_BOX and hbox_has_complex_label(g, v[j]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v[i] and v[j] are always Z and X spiders here, and there phase I believe is always a Pauli, so this case here never applies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the check on line 121 below? I think it has the same applicability as this check here, so if this one is removed, that one should as well, for consistency.
d1364d0 to
cccbf40
Compare
| newv = g.add_vertex(g.type(v[j]), qubit=q, row=r) | ||
| g.set_phase(newv, g.phase(v[j])) | ||
| # Copy complex label if H-box has one. | ||
| if g.type(v[j]) == VertexType.H_BOX and hbox_has_complex_label(g, v[j]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the check on line 121 below? I think it has the same applicability as this check here, so if this one is removed, that one should as well, for consistency.
| etab[upair(n1,n2)] = [0, 0] | ||
| etab[upair(n1,n2)][0] += 1 | ||
|
|
||
| if g.type(v1) == VertexType.H_BOX or g.type(v2) == VertexType.H_BOX: # x-h bialgebra |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am referring to this check for H_BOX.
Based on the git history, it was added in commit 9dacf64 to editor_actions.py, before this was refactored here in commit ef23f18. In this line there was originally a check for X-H algebra as well.
So the code is inconsistent right now. check_bialgebra only allows Z-X pairs, but unsafe_bialegra (partially) handles H_BOX scalars. Should I remove both references (the existing one and the one I added) to H_BOX in unsafe_bialgebra? Or was the removal of X-H from check_bialgebra during the refactoring changes an accidental oversight?
Fixes #167.