Discretizer doesn't work with numpy arrays in locals
Here is a minimal example, where discretizer fails with a numpy array in locals
. This is relevant for more complicated cases, where the matrix coefficient can't be easily expressed in terms of Pauli matrices, like higher spin matrices.
sz = np.diag([1, -1])
subs={'sz': sz}
ham="""
Bz * sz
"""
kwant.continuum.discretize(ham, locals=subs, coords=['x', 'y'])
The problem is, it calls sympy.sympify
on every value in locals
which returns an ImmutableDenseNDimArray
which doesn't have the same interface as other sympy
expressions. A solution is to pass sympy.Matrix(sz)
instead. This is inconvenient and not clear from the docs or the error message why this error occurs.
The solution is simple, one needs to test for numpy arrays and cast them to sympy matrices separately. I implemented it in this qsymm
MR which uses a copy of kwant.continuum.common
, so porting that solution here would be straightforward.