Skip to content

Fix initiating duration arrays with numpy array #39

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

Merged
merged 2 commits into from
May 23, 2023

Conversation

anuragm
Copy link
Contributor

@anuragm anuragm commented May 22, 2023

Numpy does not supports using a non-empty array as a truth value, causing the following example to fail:

import numpy as np
import oqpy

npduration = oqpy.ArrayVar(
    name="npinit",
    init_expression=np.arange(11) * 1e-9,
    dimensions=[11],
    base_type=oqpy.DurationVar,
)

with stack trace

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 4
      1 import numpy as np
      2 import oqpy
----> 4 npduration = oqpy.ArrayVar(
      5     name="npinit",
      6     init_expression=np.arange(11) * 1e-9,
      7     dimensions=[11],
      8     base_type=oqpy.DurationVar,
      9 )

File ~/Work/oqpy/oqpy/classical_types.py:377, in ArrayVar.__init__(self, dimensions, base_type, *args, **kwargs)
    374     array_base_type = base_type_instance.type_cls()
    376 # Automatically handle Duration array.
--> 377 if base_type is DurationVar and kwargs["init_expression"]:
    378     kwargs["init_expression"] = (make_duration(i) for i in kwargs["init_expression"])
    380 super().__init__(
    381     *args,
    382     **kwargs,
    383     dimensions=[ast.IntegerLiteral(dimension) for dimension in dimensions],
    384     base_type=array_base_type,
    385 )

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Changing the comparison condition fixes this issue.

Copy link
Collaborator

@jcjaskula-aws jcjaskula-aws left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

@jcjaskula-aws jcjaskula-aws merged commit b0a8ae3 into openqasm:main May 23, 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.

2 participants