myQLM / myqlm-issues

4 stars 1 forks source link

`get_variables` method of `get_item` Symbol raises an AttributeError #25

Closed a-corni closed 10 months ago

a-corni commented 1 year ago

Bug Description

When calling get_variables method of a get_item instance, an AttributeError is raised:

File variables.py:392, in qat.core.variables.ArithExpression.get_variables()

AttributeError: 'list' object has no attribute 'get_variables'

To reproduce

In a notebook, I ran:

from qat.core.variables import Variable, get_item
t_variable = Variable("t")
u_variable = Variable("u")
A = get_item([0, u_variable, 1, 3, 5], t_variable)
A.get_variables()

Expected

As A is an ArithExpression, I would have expected ["u", "t"] (u is defined in the list, t is the index).

Screenshot

image

System

ArnaudAtos commented 10 months ago

Dear @a-corni,

A new version of myQLM has been release, this new version should fix your problem. Then, the following sample of code returns the expected result:

from qat.core.variables import Variable, get_item

index = Variable("index")
expr = get_item([0, 1, 3, 5], index)
expr.get_variables()  # Returns ["index"]

Please note that a list used in an arithmetic expression must be composed exclusively of python number. Then, your sample of code will raise an exception:

from qat.core.variables import Variable, get_item

t_variable = Variable("t")
u_variable = Variable("u")

A = get_item([0, u_variable, 1, 3, 5], t_variable)  # Error - the list in invalid

You'll get the following traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "variables.py", line 108, in qat.core.variables.Symbol.__call__
  File "variables.py", line 372, in qat.core.variables.ArithExpression.__init__
ValueError: Could not create the arithmetric expression, one child does not have a valid value (i.e. number or list of numbers or arith expression)