M00N KNIGHT
M00N KNIGHT

Reputation: 137

Multiplying A Matrix By A String

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?

Upvotes: 0

Views: 2449

Answers (2)

Paul Panzer
Paul Panzer

Reputation: 53089

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())

Upvotes: 1

ACupofJoe
ACupofJoe

Reputation: 21

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.

Upvotes: 2

Related Questions