def test_biRNN_bprop(backend_default, fargs, deltas_buffer): # basic sanity check with 0 weights random inputs seq_len, input_size, hidden_size, batch_size = fargs in_shape = (input_size, seq_len) NervanaObject.be.bsz = batch_size # setup the bi-directional rnn init_glorot = GlorotUniform() birnn = BiRNN(hidden_size, activation=Rectlinclip(slope=0), init=init_glorot) birnn.configure(in_shape) birnn.prev_layer = True birnn.allocate() birnn.allocate_deltas(deltas_buffer) deltas_buffer.allocate_buffers() birnn.set_deltas(deltas_buffer) # same weight for bi-rnn backward and rnn weights birnn.W_input_b[:] = birnn.W_input_f birnn.W_recur_b[:] = birnn.W_recur_f birnn.b_b[:] = birnn.b_f birnn.dW[:] = 0 # same weight for bi-directional rnn init_glorot = GlorotUniform() rnn = Recurrent(hidden_size, activation=Rectlinclip(slope=0), init=init_glorot) rnn.configure(in_shape) rnn.prev_layer = True rnn.allocate() rnn.allocate_deltas(deltas_buffer) deltas_buffer.allocate_buffers() rnn.set_deltas(deltas_buffer) # inputs and views lr = np.random.random((input_size, seq_len * batch_size)) lr_rev = list(reversed(get_steps(lr.copy(), in_shape))) rl = con(lr_rev, axis=1) # allocate gpu buffers inp_lr = birnn.be.array(lr) inp_rl = birnn.be.array(rl) # outputs out_lr_g = birnn.fprop(inp_lr) del_lr = birnn.bprop(out_lr_g).get().copy() birnn.h_buffer[:] = 0 out_rl_g = birnn.fprop(inp_rl) del_rl = birnn.bprop(out_rl_g).get().copy() del_lr_s = get_steps(del_lr, in_shape) del_rl_s = get_steps(del_rl, in_shape) for (x, y) in zip(del_lr_s, reversed(del_rl_s)): assert np.allclose(x, y, rtol=0.0, atol=1.0e-5)
def test_biLSTM_bprop(backend_default, fargs): # basic sanity check with 0 weights random inputs seq_len, input_size, hidden_size, batch_size = fargs in_shape = (input_size, seq_len) out_shape = (hidden_size, seq_len) NervanaObject.be.bsz = batch_size # setup the bi-directional rnn init_glorot = GlorotUniform() bilstm = BiLSTM(hidden_size, gate_activation=Logistic(), activation=Tanh(), init=init_glorot, reset_cells=True) bilstm.configure(in_shape) bilstm.prev_layer = True bilstm.allocate() bilstm.set_deltas([bilstm.be.iobuf(bilstm.in_shape)]) # same weight for bi-rnn backward and rnn weights nout = hidden_size bilstm.W_input_b[:] = bilstm.W_input_f bilstm.W_recur_b[:] = bilstm.W_recur_f bilstm.b_b[:] = bilstm.b_f bilstm.dW[:] = 0 # inputs and views lr = np.random.random((input_size, seq_len * batch_size)) lr_rev = list(reversed(get_steps(lr.copy(), in_shape))) rl = con(lr_rev, axis=1) # allocate gpu buffers inp_lr = bilstm.be.array(lr) inp_rl = bilstm.be.array(rl) # outputs out_lr_g = bilstm.fprop(inp_lr) out_lr = out_lr_g.get().copy() del_lr = bilstm.bprop(out_lr_g).get().copy() bilstm.h_buffer[:] = 0 out_rl_g = bilstm.fprop(inp_rl) out_rl = out_rl_g.get().copy() del_rl = bilstm.bprop(out_rl_g).get().copy() # views out_lr_f_s = get_steps(out_lr[:nout], out_shape) out_lr_b_s = get_steps(out_lr[nout:], out_shape) out_rl_f_s = get_steps(out_rl[:nout], out_shape) out_rl_b_s = get_steps(out_rl[nout:], out_shape) # asserts for x_f, x_b, y_f, y_b in zip(out_lr_f_s, out_lr_b_s, reversed(out_rl_f_s), reversed(out_rl_b_s)): assert np.allclose(x_f, y_b, rtol=0.0, atol=1.0e-5) assert np.allclose(x_b, y_f, rtol=0.0, atol=1.0e-5) del_lr_s = get_steps(del_lr, in_shape) del_rl_s = get_steps(del_rl, in_shape) for (x, y) in zip(del_lr_s, reversed(del_rl_s)): assert np.allclose(x, y, rtol=0.0, atol=1.0e-5)
def test_biLSTM_fprop(backend_default, fargs): # basic sanity check with 0 weights random inputs seq_len, input_size, hidden_size, batch_size = fargs in_shape = (input_size, seq_len) out_shape = (hidden_size, seq_len) NervanaObject.be.bsz = batch_size # setup the bi-directional rnn init_glorot = GlorotUniform() bilstm = BiLSTM(hidden_size, gate_activation=Logistic(), init=init_glorot, activation=Tanh(), reset_cells=True) bilstm.configure(in_shape) bilstm.prev_layer = True bilstm.allocate() # same weight nout = hidden_size bilstm.W_input_b[:] = bilstm.W_input_f bilstm.W_recur_b[:] = bilstm.W_recur_f bilstm.b_b[:] = bilstm.b_f bilstm.dW[:] = 0 # inputs - random and flipped left-to-right inputs lr = np.random.random((input_size, seq_len * batch_size)) lr_rev = list(reversed(get_steps(lr.copy(), in_shape))) rl = con(lr_rev, axis=1) inp_lr = bilstm.be.array(lr) inp_rl = bilstm.be.array(rl) # outputs out_lr = bilstm.fprop(inp_lr).get().copy() bilstm.h_buffer[:] = 0 out_rl = bilstm.fprop(inp_rl).get().copy() # views out_lr_f_s = get_steps(out_lr[:nout], out_shape) out_lr_b_s = get_steps(out_lr[nout:], out_shape) out_rl_f_s = get_steps(out_rl[:nout], out_shape) out_rl_b_s = get_steps(out_rl[nout:], out_shape) # asserts for x_f, x_b, y_f, y_b in zip(out_lr_f_s, out_lr_b_s, reversed(out_rl_f_s), reversed(out_rl_b_s)): assert allclose_with_out(x_f, y_b, rtol=0.0, atol=1.0e-5) assert allclose_with_out(x_b, y_f, rtol=0.0, atol=1.0e-5)
def test_biRNN_fprop(backend_default, fargs): # basic sanity check with 0 weights random inputs seq_len, input_size, hidden_size, batch_size = fargs in_shape = (input_size, seq_len) out_shape = (hidden_size, seq_len) NervanaObject.be.bsz = batch_size # setup the bi-directional rnn init_glorot = GlorotUniform() birnn = BiRNN(hidden_size, activation=Rectlinclip(slope=0), init=init_glorot) birnn.configure(in_shape) birnn.prev_layer = True birnn.allocate() birnn.set_deltas([birnn.be.iobuf(birnn.in_shape)]) # same weight nout = hidden_size birnn.W_input_b[:] = birnn.W_input_f birnn.W_recur_b[:] = birnn.W_recur_f birnn.b_b[:] = birnn.b_f birnn.dW[:] = 0 # inputs - random and flipped left-to-right inputs lr = np.random.random((input_size, seq_len * batch_size)) lr_rev = list(reversed(get_steps(lr.copy(), in_shape))) rl = con(lr_rev, axis=1) inp_lr = birnn.be.array(lr) inp_rl = birnn.be.array(rl) # outputs out_lr = birnn.fprop(inp_lr).get().copy() birnn.h_buffer[:] = 0 out_rl = birnn.fprop(inp_rl).get().copy() # views out_lr_f_s = get_steps(out_lr[:nout], out_shape) out_lr_b_s = get_steps(out_lr[nout:], out_shape) out_rl_f_s = get_steps(out_rl[:nout], out_shape) out_rl_b_s = get_steps(out_rl[nout:], out_shape) # asserts for x_f, x_b, y_f, y_b in zip(out_lr_f_s, out_lr_b_s, reversed(out_rl_f_s), reversed(out_rl_b_s)): assert np.allclose(x_f, y_b, rtol=0.0, atol=1.0e-5) assert np.allclose(x_b, y_f, rtol=0.0, atol=1.0e-5)
import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 16 * (np.pi), 0.1) y1 = np.sin(x) y2 = np.con(x) plt.plot(x, y1, label="sin") plt.plot(x, y2, label="cos") plt.legend()
import numpy as np x = 1.0 #define a float y = 2.0 #define another float #trigonometry print(np.sin(x)) print(np.con(x)) print(np.tan(x)) print(np.arcsin(x)) print(np.arccos(x)) print(np.arctan(x)) print(np.arctan2(x, y)) print(np.rad2deg(x)) print(' ') #hyperbolic functions print(np.sinh(x)) pront(np.cosh(x)) print(np.tanh(x)) print(np.arcsinh(x)) print(np.arccosh(x)) print(np.arctanh(x)) #Will return warning bc inf.
def two_tomography(self): bell = self.bell initial = self.initial state_list = self.state_list mk_rho = 0 # state = ["I", "x", "y", "z"] t_rho = 0 measurement_basis = basis.table_mb for i in initial.values(): # iterate for each bell pair probability = [] Stokes_parameters = [] rho = 0 # calculate probability of each state. for measure in measurement_basis: prob = np.vdot(np.dot(con(bell[i[0]]), con(measure)), np.dot(measure, bell[i[0]])) # con is the conjugate operation probability.append(prob * i[1]) probability = np.array(probability).reshape(6, 6) # print(probability) # calculate stokes parameters S00 = (probability[0][0] + probability[0][1] + probability[1][0] + probability[1][1]) Stokes_parameters.append(S00) for c in range(0, 5, 2): Stokes_parameters.append(probability[c][c] - probability[c][c + 1] + probability[c + 1][c] - probability[c + 1][c + 1]) for col in range(0, 5, 2): for low in range(0, 5, 2): Stokes_parameters.append(probability[col][low] + probability[col + 1][low + 1] - probability[col][low + 1] - probability[col + 1][low]) S10 = (probability[0][0] - probability[0][1] + probability[1][0] - probability[1][1]) S20 = (probability[2][2] - probability[2][3] + probability[3][2] - probability[3][3]) S30 = (probability[4][4] - probability[4][5] + probability[5][4] - probability[5][5]) Stokes_parameters.insert(4, S10) Stokes_parameters.insert(8, S20) Stokes_parameters.insert(12, S30) # TODO can more efficient # finished calculattion of Stokes_parameters # stat = [str(k) + str(c) for k in state for c in state] # making density matrix from stokes parameters krons = [kron(ul, rl) for ul in state_list for rl in state_list] # print(krons) for tl in range(len(Stokes_parameters)): rho = rho + Stokes_parameters[tl] * krons[tl] rho = 1 / 4 * rho # print(rho) mk_rho = mk_rho + rho # print(Stokes_parameters) for t in initial.values(): t_rho = t_rho + t[1] * kron(bell[t[0]], con(bell[t[0]])) t_rho = np.array(t_rho, dtype="complex") # self.return_table(probability) # FIXME return value is not list but matrix. must repair # print("========== measure density matrix ==========") # pprint.pprint(mk_rho) # print("============ true density matrix ===========") # pprint.pprint(t_rho.reshape(4, 4)) self.fidelity(t_rho, mk_rho) return mk_rho, t_rho, probability
def tomograph_density(self, iter): state_list = self.state_list initial = self.initial bell = self.bell to_rho = 0 measurement_basis = basis.table_mb for j in initial.values(): probability = [] st = [] # stokes parameter rho = 0 tomography = np.array( ((0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0)), dtype='complex') # FIXME zeros does not work result = np.array( ((0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0)), dtype='complex') sum = 0 tomography.reshape(6, 6) for n in range(iter): ran = random.randint(0, 1) ran2 = random.randint(0, 1) for nc in range(0, 5, 2): for nl in range(0, 5, 2): tomography[nc + ran][nl + ran2] = \ tomography[nc + ran][nl + ran2] + 1 # pprint.pprint(tomography) for measure in measurement_basis: prob = np.vdot(np.dot(con(bell[j[0]]), con(measure)), np.dot(measure, bell[j[0]])) # con is the conjugate operation probability.append(prob) probability = np.array(probability).reshape(6, 6) for cols in range(6): for lows in range(6): result[cols][lows] = (probability[cols][lows] * tomography[cols][lows]) sum = result[0][0] + result[0][1] + result[1][0] + result[1][1] for cols in range(6): for lows in range(6): if sum == 0: result[cols][lows] = result[cols][lows] / 1 else: result[cols][lows] = result[cols][lows] * j[1] / sum S00 = result[0][0] + result[0][1] + result[1][0] + result[1][1] st.append(S00) for c in range(0, 5, 2): st.append(result[c][c] - result[c][c + 1] + result[c + 1][c] - result[c + 1][c + 1]) for col in range(0, 5, 2): for low in range(0, 5, 2): # print(col, low) # print(re[col][low] + re[col + 1][low + 1] + # re[col][low + 1] + re[col + 1][low]) st.append(result[col][low] + result[col + 1][low + 1] - result[col][low + 1] - result[col + 1][low]) S10 = result[0][0] - result[0][1] + result[1][0] - result[1][1] S20 = result[2][2] - result[2][3] + result[3][2] - result[3][3] S30 = result[4][4] - result[4][5] + result[5][4] - result[5][5] st.insert(4, S10) st.insert(8, S20) st.insert(12, S30) # making density matrix from stokes parameters krons = [kron(ul, rl) for ul in state_list for rl in state_list] for tl in range(len(st)): rho = rho + st[tl] * krons[tl] rho = 1 / 4 * rho to_rho = to_rho + rho # print("========== measure density matrix ==========") # pprint.pprint(to_rho) return to_rho