def predict(Xnew, var, X, Y): newinput = Xnew x = np.delete(X, 0, 0) y = np.delete(Y, 0, 0) newinput = np.tile(newinput, (len(x), 1)) # create copies of new input Xdata = np.vstack((newinput, x)) Ydata = np.tile(y, (2, 1)) result1, result2 = circuit(var, Xdata, Ydata) return result2*result1
def test(Xtest, Ytest, X, Y, var): count = 0 x = np.delete(X, 0, 0) y = np.delete(Y, 0, 0) Ydata = np.tile(y, (2, 1)) for i in enumerate(Xtest): idx = i[0] newinput = i[1] newinput = np.tile(newinput, (len(x), 1)) Xdata = np.vstack((newinput, x)) result1, result2 = circuit(var, X=Xdata, Y=Ydata) if np.round(result1*result2)==int(Ytest[idx]): count += 1 accuracy = count/len(Xtest) return accuracy
def cost(var, X, Y): # specifically for dataset = 5 points # 4 data points and 1 test input MSE = 0 for i in enumerate(X): idx = i[0] newinput = i[1] x = np.delete(X, i[0], 0) # remove chosen data point (which is the "new input") if idx == len(x) - 1: break newinput = np.tile(newinput, (len(x), 1)) # create copies of new input Xdatacost = np.vstack((newinput, x)) # stack 4 data points and copies of new input for the circuit ytrue = int(Y[idx]) # select the true y label of the new input y = np.delete(Y, idx, 0) # remove chosen data point's label Ydatacost = np.tile(y, (2, 1)) # Create copy of labels result1, result2 = circuit(var, X=Xdatacost, Y=Ydatacost) ypred = result1 * result2 loss = (ypred - ytrue) ** 2 MSE = MSE + loss return MSE / len(x)
def cost(var, Xdata, Y): MSE = 0 for i in enumerate(X): idx = i[0] newinput = i[1] x = np.delete( Xdata, i[0], 0 ) # remove chosen data point (which is the "new input") so that only the other 4 data points go into the cost if idx == len(x) - 1: break newinput = np.tile(newinput, (len(x), 1)) # create copies of new input Xdata = np.vstack( (newinput, x)) # stack 4 data points and copies of new input for the circuit ytrue = int(Y[idx]) # select the true y label of the new input y = np.delete(Y, idx, 0) # remove chosen data point's label Ydata = np.tile(y, (2, 1)) # Create copy of labels result1, result2 = variational_circ(var=var, Xdata=Xdata, Y=Ydata) ypred = result1 * result2 loss = (ypred - ytrue)**2 MSE = MSE + loss print(MSE) return MSE / len(x)
def embedding_1(X, wires, fill='redundant'): if len(X) < len(wires): r_ = len(wires) // len(X) if fill == 'redundant': large_features = np.tile(X, r_) elif fill == 'pad': large_features = np.pad(X, (0, len(wires)), 'constant', constant_values=0) qml.templates.embeddings.AngleEmbedding( large_features, wires=wires, rotation='Y') # replace with more general embedding else: qml.templates.embeddings.AngleEmbedding( X, wires=wires, rotation='Y') # replace with more general embedding qml.templates.embeddings.AngleEmbedding(X, wires=wires)
def embedding_1(X, wires, fill='redundant'): """ Args: X: wires: fill: (Default value = 'redundant') Returns: """ if len(X) < len(wires): r_ = len(wires) // len(X) if fill == 'redundant': large_features = np.tile(X, r_) elif fill == 'pad': large_features = np.pad(X, (0, len(wires)), 'constant', constant_values=0) qml.templates.embeddings.AngleEmbedding(large_features, wires=wires, rotation='Y') else: qml.templates.embeddings.AngleEmbedding(X, wires=wires, rotation='Y')