For some reason I don’t know, this code is ignoring the complex part of the graph being that I need it. Could someone help me?

Asked

Viewed 42 times

-1

from qutip import*    
import numpy as np  
import matplotlib.pyplot as plt    
import cmath

#Definitions    
omega=4    
gamma=omega/4

#parameters
E1=-1/2    
E2=5/2    
E3=-1/2    
w=1    
psi0=basis(3,2)
d1=basis(3,0).dag()*qeye(3) 
d2=qeye(3)*basis(3,1)    
d3=basis(3,2).dag()*qeye(3)


#Hamiltonians' terms    
ET=E1 * d1.dag() * d1+E2*d2.dag() * d2+E3*d3.dag()*d3    
AO=omega*d1.dag()*d3 
SC=gamma*d1.dag()*d2.dag()  
ETC=E1 * d1.dag() * d1+E2*d2.dag() * d2+E3*d3.dag()*d3    
AOC=omega*d3.dag()*d1  
SCC=gamma*d2*d1

#Hamiltonians
H=ET+AO+SC+ETC+AOC+SCC

#X axis
times=np.linspace(0,20,100000)

#Solution    
result=mesolve(H, psi0, times, [], [AO,AOC])

#graph    
fig, ax= plt.subplots()
ax.plot(result.times, result.expect[0]);   
ax.plot(result.times, result.expect[1]); 
ax.set_xlabel('Time');
ax.set_ylabel('Expectation values');    
plt.show(fig)
  • has error message?

  • complexWarning: Casting Complex values to real discards the Imaginary part Return array(a, dtype, copy=False, order=order)

  • 1

    puts this on the topic, and preferably the complete message

1 answer

0


In your case result is an instance of the class Result that stores the result of the simulations, this class has the attribute expect which is a lista/array, as you have an array of complex numbers you can access the real and imaginary parts with .real and .imag

plt.subplot(2,1,1)
plt.plot(result.times, result.expect[1].real )
plt.xlabel('t')
plt.ylabel('Re x(t)')
plt.title(r'Real part')

plt.subplot(2,1,2)
plt.plot(result.times, result.expect[1].imag)
plt.xlabel('t')
plt.ylabel('Im x(t)')
plt.title(r'Imaginary part')
plt.tight_layout()

plt.show()

Which will result in

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.