x = np.random.random(M) phi_x = np.zeros(M) phi_r = np.zeros(M) np.random.seed(1) # initialize some random vals for the periodic convolution init = np.random.uniform(low=-1, high=1, size=(2)) c = np.zeros(M) c[0] = init[0] c[1] = init[1] # get the fourier transformed values and seperate argument and absolute value complex_vals = np.fft.fft(c) scale_arg = np.angle(complex_vals) scale_abs = np.absolute(complex_vals) # get the required squeeze scaling argument r1 = -np.log(scale_abs) # perform a neural network feed forward using the transformed Fourier matrix, the required phase shift using a rotation gate (phase gate), # the required squeeze scaling and the Fourier matrix circ_res = quantum_circ1(x=x, phi_x=phi_x, U1=F_H, U2=F, r=r1, phi_r=phi_r, phi_rot=scale_arg)
def TraceDistance(A, B): return 0.5 * np.trace(np.absolute(np.add(A, -1*B)))
def natural_gradient(params): """Calculate the natural gradient of the qnode() cost function. The code you write for this challenge should be completely contained within this function between the # QHACK # comment markers. You should evaluate the metric tensor and the gradient of the QNode, and then combine these together using the natural gradient definition. The natural gradient should be returned as a NumPy array. The metric tensor should be evaluated using the equation provided in the problem text. Hint: you will need to define a new QNode that returns the quantum state before measurement. Args: params (np.ndarray): Input parameters, of dimension 6 Returns: np.ndarray: The natural gradient evaluated at the input parameters, of dimension 6 """ natural_grad = np.zeros(6) # QHACK # dcircuit = qml.grad(qnode, argnum=0) normal_grad = dcircuit(params) def normalise(a1): return (np.vdot(a1, a1)) qml.enable_tape() @qml.qnode(dev) def circuit(params): variational_circuit(params) return qml.state() F_matrix = np.zeros([6, 6], dtype=np.float64) initial_prod = circuit(params) s = np.pi / 2 for i in range(6): for j in range(6): params[i] += s params[j] += s a11 = circuit(params) params[j] -= 2 * s a22 = circuit(params) params[i] -= 2 * s a44 = circuit(params) params[j] += 2 * s a33 = circuit(params) params[i] += s params[j] -= s aa1 = np.vdot(a11, initial_prod) a1 = normalise(aa1) aa2 = np.vdot(a22, initial_prod) a2 = normalise(aa2) aa3 = np.vdot(a33, initial_prod) a3 = normalise(aa3) aa4 = np.vdot(a44, initial_prod) a4 = normalise(aa4) #print(str((a2 + a3 - a1 - a4)/8.0)) #print(a1) F_matrix[i][j] = (np.absolute(a2) + np.absolute(a3) - np.absolute(a1) - np.absolute(a4)) / 8.0 #print(circuit(params)) final_F = np.linalg.inv(F_matrix) natural_grad = final_F.dot(normal_grad) # QHACK # return natural_grad
while (certainty < 0.11 and iterations < 5): print("Iteration:", iterations) test_indices = np.random.randint(low=0, high=len(x.T), size=sample_size) samples = x.T[test_indices] plt.scatter(x_0, x_1) plt.scatter(samples[:, 0], samples[:, 1]) plt.ylim([0, 1]) plt.xlim([0, 1]) plt.show() zero_or_not = svm_trained_kernel.predict(samples) # zero or not one_or_not = svm_trained_kernel_1.predict(samples) # zero or not certainty = np.absolute(((np.sum(zero_or_not) + 15) / 30) - ((np.sum(one_or_not) + 15) / 30)) iterations += 1 print("Certainty:", (np.sum(zero_or_not) + 15) / 30, (np.sum(one_or_not) + 15) / 30, certainty) print("Classification:", np.argmax([np.sum(zero_or_not), np.sum(one_or_not)])) sample_size = 15 for i in range(5): current_sample = i #current_test_image = test_X1[current_sample] x_0, x_1 = np.asarray(np.where(test_X1[current_sample] >= 0.95)) / 28 x = np.asarray([x_0, x_1]) certainty = 0. iterations = 0
def norm(x): """ Norm of a quantum state: ||x|| = (<x|x>)^1/2 """ return np.sqrt(np.sum(np.absolute(x)**2))