Skip to content

Compatibility problems with sympy 1.8

Hi, I'm new to QSymm and I'm just running through the tutorials from readthedocs. It appears to me that whenever I try to convert a string hamiltonian with J>=1, it returns some error codes. J3_2 This is part of the tutorial code that I downloaded from the readthedocs. I've also tried to use J=1, but it returns a similar error:J1 The details of the error codes are attached below:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: Error from parse_expr with transformed code: "Symbol ('hbar' )**Integer (2 )/(Integer (2 )*Symbol ('m' ))*(k_x **Integer (2 )+k_y **Integer (2 )+k_z **Integer (2 ))*eye (Integer (4 ))+Symbol ('alpha' )*J_x *k_x +Symbol ('alpha' )*J_y *k_y +Symbol ('alpha' )*J_z *k_z "

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-14-b84411f42ce9> in <module>
      4     "alpha * J_x * k_x + alpha * J_y * k_y + alpha * J_z * k_z"
      5 )
----> 6 H_rashba = qsymm.Model(ham_rashba, locals=dict(J_x=J_x, J_y=J_y, J_z=J_z))
      7 H_rashba.tosympy(nsimplify=True)

~/opt/anaconda3/lib/python3.8/site-packages/qsymm/model.py in __init__(self, hamiltonian, locals, momenta, keep, symbol_normalizer, normalize, shape, format)
    205         else:
    206             # Try to parse the input with kwant_continuum.sympify
--> 207             hamiltonian = kwant_continuum.sympify(hamiltonian, locals=locals)
    208             if not isinstance(hamiltonian, MatrixBase):
    209                 hamiltonian = sympy.Matrix([[hamiltonian]])

~/opt/anaconda3/lib/python3.8/site-packages/qsymm/kwant_continuum.py in sympify(expr, locals)
    213         stored_value = converter.pop(list, None)
    214         converter[list] = lambda x: sympy.Matrix(x)
--> 215         hamiltonian = sympy.sympify(expr, locals=locals)
    216         # if input is for example ``[[k_x * A(x) * k_x]]`` after the first
    217         # sympify we are getting list of sympy objects, so we call sympify

~/opt/anaconda3/lib/python3.8/site-packages/sympy/core/sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
    477     try:
    478         a = a.replace('\n', '')
--> 479         expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
    480     except (TokenError, SyntaxError) as exc:
    481         raise SympifyError('could not parse %r' % a, exc)

~/opt/anaconda3/lib/python3.8/site-packages/sympy/parsing/sympy_parser.py in parse_expr(s, local_dict, transformations, global_dict, evaluate)
   1016         return eval_expr(code, local_dict, global_dict)
   1017     except Exception as e:
-> 1018         raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
   1019 
   1020 

~/opt/anaconda3/lib/python3.8/site-packages/sympy/parsing/sympy_parser.py in parse_expr(s, local_dict, transformations, global_dict, evaluate)
   1014 
   1015     try:
-> 1016         return eval_expr(code, local_dict, global_dict)
   1017     except Exception as e:
   1018         raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")

~/opt/anaconda3/lib/python3.8/site-packages/sympy/parsing/sympy_parser.py in eval_expr(code, local_dict, global_dict)
    908     Generally, ``parse_expr`` should be used.
    909     """
--> 910     expr = eval(
    911         code, global_dict, local_dict)  # take local objects in preference
    912     return expr

<string> in <module>

TypeError: unsupported operand type(s) for +: 'MutableDenseMatrix' and 'ImmutableDenseNDimArray'

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: Error from parse_expr with transformed code: "Symbol ('hbar' )**Integer (2 )/(Integer (2 )*Symbol ('m' ))*(k_x **Integer (2 )+k_y **Integer (2 )+k_z **Integer (2 ))*eye (Integer (4 ))+Symbol ('alpha' )*J_x *k_x +Symbol ('alpha' )*J_y *k_y +Symbol ('alpha' )*J_z *k_z "

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-14-b84411f42ce9> in <module>
      4     "alpha * J_x * k_x + alpha * J_y * k_y + alpha * J_z * k_z"
      5 )
----> 6 H_rashba = qsymm.Model(ham_rashba, locals=dict(J_x=J_x, J_y=J_y, J_z=J_z))
      7 H_rashba.tosympy(nsimplify=True)

~/opt/anaconda3/lib/python3.8/site-packages/qsymm/model.py in __init__(self, hamiltonian, locals, momenta, keep, symbol_normalizer, normalize, shape, format)
    205         else:
    206             # Try to parse the input with kwant_continuum.sympify
--> 207             hamiltonian = kwant_continuum.sympify(hamiltonian, locals=locals)
    208             if not isinstance(hamiltonian, MatrixBase):
    209                 hamiltonian = sympy.Matrix([[hamiltonian]])

~/opt/anaconda3/lib/python3.8/site-packages/qsymm/kwant_continuum.py in sympify(expr, locals)
    213         stored_value = converter.pop(list, None)
    214         converter[list] = lambda x: sympy.Matrix(x)
--> 215         hamiltonian = sympy.sympify(expr, locals=locals)
    216         # if input is for example ``[[k_x * A(x) * k_x]]`` after the first
    217         # sympify we are getting list of sympy objects, so we call sympify

~/opt/anaconda3/lib/python3.8/site-packages/sympy/core/sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
    477     try:
    478         a = a.replace('\n', '')
--> 479         expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
    480     except (TokenError, SyntaxError) as exc:
    481         raise SympifyError('could not parse %r' % a, exc)

~/opt/anaconda3/lib/python3.8/site-packages/sympy/parsing/sympy_parser.py in parse_expr(s, local_dict, transformations, global_dict, evaluate)
   1016         return eval_expr(code, local_dict, global_dict)
   1017     except Exception as e:
-> 1018         raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
   1019 
   1020 

~/opt/anaconda3/lib/python3.8/site-packages/sympy/parsing/sympy_parser.py in parse_expr(s, local_dict, transformations, global_dict, evaluate)
   1014 
   1015     try:
-> 1016         return eval_expr(code, local_dict, global_dict)
   1017     except Exception as e:
   1018         raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")

~/opt/anaconda3/lib/python3.8/site-packages/sympy/parsing/sympy_parser.py in eval_expr(code, local_dict, global_dict)
    908     Generally, ``parse_expr`` should be used.
    909     """
--> 910     expr = eval(
    911         code, global_dict, local_dict)  # take local objects in preference
    912     return expr

<string> in <module>

TypeError: unsupported operand type(s) for +: 'MutableDenseMatrix' and 'ImmutableDenseNDimArray'
Edited by Anton Akhmerov