Skip to content

Support for user gate definitions - #18

Open
thierry-martinez wants to merge 1 commit into
TeamGraphix:mainfrom
thierry-martinez:user_gate_definitions
Open

Support for user gate definitions#18
thierry-martinez wants to merge 1 commit into
TeamGraphix:mainfrom
thierry-martinez:user_gate_definitions

Conversation

@thierry-martinez

Copy link
Copy Markdown
Collaborator

No description provided.

@matulni matulni left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread tests/test_parser.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 ?

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.

2 participants