Skip to content
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

Fix expressions with duration literals #38

Merged

Conversation

anuragm
Copy link
Contributor

@anuragm anuragm commented May 20, 2023

Duration literals are missing a type which causes errors when trying to build expressions with them. For example, this code

import oqpy

port = oqpy.PortVar("myport")
frame = oqpy.FrameVar(port, 1e9, name="myframe")
delay_time = oqpy.make_duration(50e-9) # 50 ns
delay_repetition = 10

program = oqpy.Program()
program.delay(delay_repetition * delay_time, frame)

errors out and print

AttributeError                            Traceback (most recent call last)
Cell In[2], line 9
      6 delay_repetition = 10
      8 program = oqpy.Program()
----> 9 program.delay(delay_repetition * delay_time, frame)

File ~/Work/oqpy/oqpy/base.py:96, in OQPyExpression.__rmul__(self, other)
     95 def __rmul__(self, other: AstConvertible) -> OQPyBinaryExpression:
---> 96     return self._to_binary("*", other, self)

File ~/Work/oqpy/oqpy/base.py:61, in OQPyExpression._to_binary(op_name, first, second)
     56 @staticmethod
     57 def _to_binary(
     58     op_name: str, first: AstConvertible, second: AstConvertible
     59 ) -> OQPyBinaryExpression:
     60     """Helper method to produce a binary expression."""
---> 61     return OQPyBinaryExpression(ast.BinaryOperator[op_name], first, second)

File ~/Work/oqpy/oqpy/base.py:237, in OQPyBinaryExpression.__init__(self, op, lhs, rhs)
    235     self.type = lhs.type
    236 elif isinstance(rhs, OQPyExpression):
--> 237     self.type = rhs.type
    238 else:
    239     raise TypeError("Neither lhs nor rhs is an expression?")

AttributeError: 'OQDurationLiteral' object has no attribute 'type'

Adding the proper type fixes the issue. I have also added a few tests.

anuragm added 2 commits May 20, 2023 15:21

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@anuragm anuragm requested a review from jcjaskula-aws May 22, 2023 17:30
@jcjaskula-aws jcjaskula-aws merged commit 1266bc4 into openqasm:main May 22, 2023
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.

None yet

2 participants