0

I am trying to multiply a matrix by constants I have defined as the following:

E1 = "E1"
E2 = "E2"
J = "J"

However then when I try to multiply them by the matrices defined below I end up with an error:

# Defining the sigma functions and Identity matrices:
sigx = np.array([[0,1],[1,0]])
sigy = np.array([[0,-1j],[1j,0]])
sigz = np.array([[1,0],[0,-1]])
I1 = np.array([[1,0],[0,1]])
I2 = I1

enter image description here

# Trying to structure a Hamiltonian
H1 = np.kron(((E1)*(sigz)),I2)
H2 = np.kron((E2*(I1)),(sigz))
H3 = J*((np.kron((sigx),(sigx)))+(np.kron((sigy),(sigy))))
print(H1)
print(H2)
print(H3)
# Overall Hamiltonian Matrix 
Hs = H1 + H2 + H3
print(Hs)

enter image description here

The script works if I do not define E1, E2 or J beforehand as shown above but it only works because python automatically assigns them a value of 1,2,3. How can I get it so E1,E2 and J appear in the matrix operation outputs?

M00N KNIGHT
  • 137
  • 1
  • 11
  • Numpy can't do symbolic math, which is what it looks like you're trying to do. If you're trying to do something that requires symbolic math, you can use sympy instead. Otherwise, if you know the value of `J` in terms of numbers, you should set the variable `J` to that instead of a string. – amiller27 Sep 17 '18 at 21:48
  • What exactly is this calculation supposed to accomplish (i.e. what are you trying to do with the hamiltonian once you define it)? – amiller27 Sep 17 '18 at 21:50
  • Ok I'll try rewrite it with sympy. Does that library have the same packages as numpy like kron? – M00N KNIGHT Sep 17 '18 at 21:52
  • I am trying to get the Hamiltonian and from there I can get the eigenvalues and eigenvectors – M00N KNIGHT Sep 17 '18 at 21:53
  • You want the eigenvalues and eigenstates for specific values of E1, E2, and J, or you want a symbolic expression for the eigenvalues and eigenstates of the hamiltonian in terms of those variables? – amiller27 Sep 17 '18 at 21:54
  • The second way you said it. Apologies for not being more clear – M00N KNIGHT Sep 17 '18 at 21:56
  • No problem. In that case you want a symbolic math package (if you're sticking with python, that'd be sympy). Sympy actually has a quantum mechanics package, although I've never used the quantum mechanics package and I'm not sure what's in there – amiller27 Sep 17 '18 at 21:57
  • Perfect I'll look into it thanks a mil – M00N KNIGHT Sep 17 '18 at 21:58

2 Answers2

2

First time posting here.

I hope I'm not stating the obvious, but it looks like you are multiplying a matrix of integers by strings. I think you have to define the constants as integers (or doubles or floats possibly). You are doing matrix multiplication using the Kronecker product but you are multiplying integers by strings causing the dtype(U11) error.

https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

Here you can see that the 'U11' error means an little-Endian 11 Character Unicode String.

ACupofJoe
  • 21
  • 2
  • That's fair I get that man but I'm trying to understand how else to go about it if that's the case. Appreciate the input – M00N KNIGHT Sep 17 '18 at 21:59
  • You might find this useful. https://stackoverflow.com/questions/9958506/element-wise-string-concatenation-in-numpy https://stackoverflow.com/questions/31468188/how-to-combine-np-string-array-with-float-array-python These might point you in a direction you could go. They are about concatenating string arrays. You could also try MATLAB. – ACupofJoe Sep 18 '18 at 07:59
1

To get you started here is your code translated to sympy:

import sympy as sp

I1 = I2 = sp.eye(2)

# Pauli matrices
# (there is sympy.physics.paulialgebra but I do not know how to use it)
sigx = sp.Matrix([[0, 1], [1, 0]])
sigz = sp.diag(1, -1)
sigy = sigx * sigz * sp.I

# symbolic constants
E1, E2, J = sp.symbols('E1 E2 J')

kp = sp.kronecker_product

H1 = kp(E1 * sigz, I2)
H2 = kp(E2 * I1, sigz)
H3 = J * (kp(sigx, sigx) + kp(sigy, sigy))

Hs = H1 + H2 + H3

# now we can for example compute eigen decomposition
evals, mults, evecs = zip(*Hs.eigenvects())
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99