Skip to content

Conversation

@dlyongemallo
Copy link
Contributor

Fixes #167.

Copilot AI review requested due to automatic review settings February 2, 2026 07:01
@dlyongemallo dlyongemallo marked this pull request as draft February 2, 2026 07:05
Copy link

Copilot AI left a 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.

@dlyongemallo dlyongemallo force-pushed the 167-H-box_arbitrary_complex_labels branch from af0c73f to d1364d0 Compare February 2, 2026 08:40
@dlyongemallo dlyongemallo marked this pull request as ready for review February 2, 2026 08:41
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]):
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@dlyongemallo dlyongemallo force-pushed the 167-H-box_arbitrary_complex_labels branch from d1364d0 to cccbf40 Compare February 5, 2026 14:31
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]):
Copy link
Contributor Author

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
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 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?

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.

Support H boxes with arbitrary complex labels

2 participants