<jupyter_start><jupyter_text>We follow <NAME> et al 2018 New J. Phys. 20 123030.Let's consider the noisy evolution of a two-level system. We consider the Hamiltonian $$H= \omega\sigma_z $$ and a jump operator $\sigma_{-}$ with time dependent decay rate$$\gamma(t) = 2\gamma_0\lambda\frac{\sinh(\eta t/2)}{\eta \cosh(\eta t/2) + \lambda \sinh(\eta t/2)}$$Let's assume that we observe $\{\sigma_x,\sigma_y,\sigma_z\}$ for different initial states over time and we try to predict that for an unseen quantum state. We follow <NAME>anchi et al 2018 New J. Phys. 20 123030.Let's consider the noisy evolution of a two-level system. We consider the Hamiltonian $$H= \omega\sigma_z $$ and a jump operator $\sigma_{-}$ with time dependent decay rate$$\gamma(t) = at$$Let's assume that we observe $\{\sigma_x,\sigma_y,\sigma_z\}$ for different initial states over time and we try to predict that for an unseen quantum state.<jupyter_code>### Parameters 
ω = 1
#λ = 2
#γ0 = 0.5
### Operators and initial states
ham = ω*sigmaz()
cop = destroy(2)
d0=1
d1=1
d2=1
print(cop)
def gamma_t(t,args):
    return d1*t
cop_all = [[cop,gamma_t]]
t_list = np.arange(0,10,0.1)
#t_list_long = np.arange(0,30,0.1)
print(t_list.shape)
n_train = 33
n_test = n_train // 3
n_test_long = 2*n_test
### Time evolution - Training
def train(s):
    global d0,d1,d2
    d1=s[0]
    exp_train = []
    exp_test = []    
    exp_test_long = []
    for i in tqdm(range(n_train)):
        ψ0 = rand_ket(2)
        resTI = mesolve(ham,ψ0,t_list,cop_all,[sigmax(),sigmay(),sigmaz()])
        exp_train.append(resTI.expect)
    return exp_train<jupyter_output><empty_output><jupyter_text>Printing train data  Training data visulization<jupyter_code>for i in  np.random.random((10,1)):
    print(i)
alpha=1
def X_train(n):
    l=np.random.random((n,alpha))
    s=[]
    for i in l:
        i=i.tolist()
        s.append(train(i))
    return l, s


l,s=X_train(200)
def y(l):
    g=[]
    for i in l:
        for j in range(n_train):
            g.append(i)
    return np.asarray(g)<jupyter_output><empty_output><jupyter_text>RNN and window functions  Initializing LSTM models<jupyter_code>from pandas import DataFrame
from pandas import concat
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM,Flatten

from keras import regularizers
s=np.asarray(s)
print(s.shape)
s=s.reshape(200*33,3,t_list.shape[0])
np.transpose(s, (0, 2, 1)).shape
X1_train=np.asarray(s).reshape(6600,t_list.shape[0],3)
Y_train=y(l).reshape(-1,alpha)




print(X1_train.shape)
print(Y_train.shape)


#print(X1_test.shape)
#print(Y_test.shape)
from sklearn.utils import shuffle

X1_train, Y_train = shuffle(X1_train, Y_train)
#X1_test, Y_test = shuffle(X1_test, Y_test)<jupyter_output><empty_output><jupyter_text>batch_shape = (N of time series in a batch, the length of time series, N of features)Input shape 3D tensor with shape (batch_size, timesteps, input_dim).<jupyter_code># Define train LSTM model
nb_compon = 1
model = Sequential()
model.add(LSTM(50,  stateful = False,input_shape = (t_list.shape[0], 3)))#input_shape=(None,nb_compon),
model.add(Dense(30, kernel_regularizer=regularizers.l2(0.001),
               activity_regularizer=regularizers.l2(0.001),
               bias_regularizer=regularizers.l2(0.001)))
model.add(Dense(alpha))
model.compile("adam", loss="mae")
history = model.fit(X1_train, Y_train ,batch_size=256 ,validation_split=0.25,epochs=40)
#tf.keras.metrics.mean_absolute_error(model.predict(X1_test).reshape(-1,),Y_test).numpy()


plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model train vs validation loss')
plt.ylabel('loss')
plt.xlabel('epoch')

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model train vs validation loss')
plt.ylabel('loss')
plt.xlabel('epoch')
import tensorflow as tf 
def X_test(n):
    l=np.random.random((n,alpha))
    s=[]
    for i in l:
        i=i.tolist()
        s.append(train(i))
    return l, s

l_test,s_test=X_test(2)

X1_test=np.transpose(np.asarray(s_test).reshape(-1,3,t_list.shape[0]), (0, 2, 1))
Y_test=y(l_test).reshape(-1,)


X1_test.shape
rr=35
plt.plot(t_list, np.array(X1_test)[rr][:,0],label=r'$\sigma_x$',color='r')
plt.plot(t_list, np.array(X1_test)[rr][:,1],label=r'$\sigma_y$',color='b')
plt.plot(t_list, np.array(X1_test)[rr][:,2],label=r'$\sigma_z$',color='c')
plt.legend()
plt.xlabel('t')<jupyter_output><empty_output><jupyter_text>one example demonstration<jupyter_code>exp_train=train([0.874408])
plt.plot(t_list, np.array(exp_train)[0][0],label=r'$\sigma_x$',color='r')
plt.plot(t_list, np.array(exp_train)[0][1],label=r'$\sigma_y$',color='b')
plt.plot(t_list, np.array(exp_train)[0][2],label=r'$\sigma_z$',color='c')

s=np.array(exp_train)[0].reshape(1,t_list.shape[0],3)
print(model.predict(s))

exp_train=train([0.874408])
plt.plot(t_list, np.array(exp_train)[0][0],label=r'$\sigma_x$',color='r')
plt.plot(t_list, np.array(exp_train)[0][1],label=r'$\sigma_y$',color='b')
plt.plot(t_list, np.array(exp_train)[0][2],label=r'$\sigma_z$',color='c')

s=np.array(exp_train)[0].reshape(1,t_list.shape[0],3)
print(model.predict(s))

plt.legend()



plt.xlabel('t')

number=40
q=[]
u=np.random.random(number)
for i in u:
    exp_train=train([i])
    s=np.array(exp_train)[0].reshape(1,t_list.shape[0],3)
    q.append(model.predict(s))
    
plt.scatter(np.arange(0,number),q)
plt.scatter(np.arange(0,number),q,label='True')    
plt.scatter(np.arange(0,number),u, label='Prediction')

plt.xlabel("num of examples")
plt.ylabel('a')
plt.legend()



plt.scatter(np.arange(0,number),q)    

np.power(2, 3)<jupyter_output><empty_output><jupyter_text>non trivial example<jupyter_code>### Parameters 
exp_train = []
ω = 1
λ = 2
γ0 = 0.5
### Operators and initial states
ham = ω*sigmaz()
cop = destroy(2)
def gamma_t(t,args):
    η = np.sqrt(λ**2-2*γ0*λ)
    return 2*γ0*λ*np.sinh(η*t/2)/(η*np.cosh(η*t/2)+λ*np.sinh(η*t/2))
cop_all = [[cop,gamma_t]]

for i in tqdm(range(n_train)):
    ψ0 = rand_ket(2)
    resTI = mesolve(ham,ψ0,t_list,cop_all,[sigmax(),sigmay(),sigmaz()])
    exp_train.append(resTI.expect)
    
    
plt.plot(t_list, np.array(exp_train)[0][0],label=r'$\sigma_x$',color='r')
plt.plot(t_list, np.array(exp_train)[0][1],label=r'$\sigma_y$',color='b')
plt.plot(t_list, np.array(exp_train)[0][2],label=r'$\sigma_z$',color='c')
plt.legend()
plt.xlabel('t')

s=np.array(exp_train[0]).reshape(1,t_list.shape[0],3)
model.predict(s)
model.save('1106_con_positive_suc.h')<jupyter_output><empty_output><jupyter_text>Evaluating LSTM model   Define test LSTM modelnb_compon = 3model_test = Sequential()model_test.add(LSTM(50, input_shape=(None,nb_compon), stateful = True, batch_size=1))model_test.add(Dense(30, kernel_regularizer=regularizers.l2(0.001),               activity_regularizer=regularizers.l2(0.001),               bias_regularizer=regularizers.l2(0.001)))model_test.add(Dense(nb_compon))<jupyter_code>ψ0 = rand_ket(2)
### Time evolution - Training
def extrain(s,ψ0):
    global d0,d1,d2
    d1=s[0]
    exp_train = []
    exp_test = []    
    exp_test_long = []
    for i in tqdm(range(n_train)):
        resTI = mesolve(ham,ψ0,t_list,cop_all,[sigmax(),sigmay(),sigmaz()])
        exp_train.append(resTI.expect)
    return exp_train
### Parameters 
exp_train = []
ω = 1
λ = 2
γ0 = 0.5
### Operators and initial states
ham = ω*sigmaz()
cop = destroy(2)
def gamma_t(t,args):
    η = np.sqrt(λ**2-2*γ0*λ)
    return 2*γ0*λ*np.sinh(η*t/2)/(η*np.cosh(η*t/2)+λ*np.sinh(η*t/2))
cop_all = [[cop,gamma_t]]

for i in tqdm(range(n_train)):
    ψ0 = rand_ket(2)
    resTI = mesolve(ham,ψ0,t_list,cop_all,[sigmax(),sigmay(),sigmaz()])
    exp_train.append(resTI.expect)
    
    
plt.plot(t_list, np.array(exp_train)[0][0],label=r'$\sigma_x$',color='r')
plt.plot(t_list, np.array(exp_train)[0][1],label=r'$\sigma_y$',color='b')
plt.plot(t_list, np.array(exp_train)[0][2],label=r'$\sigma_z$',color='c')
plt.legend()
plt.xlabel('t')

s=np.array(exp_train[0]).reshape(1,t_list.shape[0],3)
model.predict(s)






def gamma(t):
    η = np.sqrt(λ**2-2*γ0*λ)
    return 2*γ0*λ*np.sinh(η*t/2)/(η*np.cosh(η*t/2)+λ*np.sinh(η*t/2))

def fit_t(s,t):
    d1=s[0]
    return d1*t

t=t_list
plt.plot(t, gamma(t))
#plt.plot(t, fit_t(model.predict(s).reshape(-1),t))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18376171030506847, 'fraction_numerical': 0.03519096805188566, 'mean_word_length': 5.270331325301205, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '248795', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 3656, 'n_tokens_neox': 3454, 'n_words': 561, 'path': 'xx/small_systems_con_positive.ipynb', 'repo_name': 'ericntunctu/ericntunctu', 'size': '8339'}