Support for user gate definitions - #18
Open
thierry-martinez wants to merge 1 commit into
Open
Conversation
matulni
reviewed
Sep 10, 2026
matulni
left a comment
There was a problem hiding this comment.
Thanks for the PR! I made some comments.
Comment on lines
35
to
-54
| @@ -51,7 +54,7 @@ def CZ(_q0: int, _q1: int) -> None: # noqa: N802 | |||
| # See https://github.com/TeamGraphix/graphix/pull/399 | |||
| ANGLE_PI = math.pi | |||
|
|
|||
| def rad_to_angle(angle: float) -> float: | |||
There was a problem hiding this comment.
I suppose that this won't be necessary after #15 ?
| self.inside_gate_definition = False | ||
|
|
||
| def check_not_inside_gate_definition(self, ctx: ParserRuleContext) -> None: # type: ignore[valid-type] | ||
| if self.inside_gate_definition: |
There was a problem hiding this comment.
I'm a bit confused by this variable: it's initialized to False and I don't see anywhere in the code where it could take a True value. I don't know how to test if it's a bug or not...
There was a problem hiding this comment.
I think there seems to be a bug when trying to define custom gates without parameters. The following test
def test_gate_definition_without_arguments() -> None:
"""Test gate definition."""
s = """
qubit[2] q;
gate fancyid a {
h a;
h a; }
fancyid q[0];
"""
parser = OpenQASMParser()
circuit = parser.parse_str(s)
assert circuit.width == 2
assert circuit.instruction == [H(0), H(0)]raises
@override
def visitGateStatement(self, ctx: qasm3Parser.GateStatementContext) -> None:
self.check_not_inside_gate_definition(ctx)
name = ctx.Identifier().getText() # type: ignore[no-untyped-call]
param_identifiers = ctx.identifierList(0).Identifier()
> qubit_identifiers = ctx.identifierList(1).Identifier()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E AttributeError: 'NoneType' object has no attribute 'Identifier'
graphix_qasm_parser/parser.py:532: AttributeError
whereas the test
def test_gate_definition_with_arguments() -> None:
"""Test gate definition."""
s = """
qubit[2] q;
gate fancyid(x) a {
h a;
h a; }
fancyid(0.2) q[0];
"""
parser = OpenQASMParser()
circuit = parser.parse_str(s)
assert circuit.width == 2
assert circuit.instruction == [H(0), H(0)]passes.
OpenQASM supports custom gates without arguments, do you think it can be fixed in this PR ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.