import tensorflow as tf
import pennylane as qml
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_moons
# Set random seeds
np.random.seed(42)
tf.random.set_seed(42)
from tensorflow import keras
from tensorflow.keras import layers
from sklearn import datasets
from sklearn.decomposition import PCA
# import some data to play with
iris = datasets.load_iris()
X = iris.data # we only take the first two features.
y = iris.target
from sklearn.preprocessing import normalize
X=normalize(X,axis=0)
randomize = np.arange(len(X))
np.random.shuffle(randomize)
X = X[randomize]
y = y[randomize]
X_train=X[:120]
X_test=X[120:]
y_train=y[:120]
y_test=y[120:]
n_qubits = 4
dev = qml.device("strawberryfields.fock", wires=4, cutoff_dim=4)
def layer(theta, phi, wires):
M = len(wires)
phi_nonlinear = np.pi / 2
qml.templates.Interferometer(
theta, phi, np.zeros(M), wires=wires, mesh="triangular", beamsplitter="pennylane"
)
for i in wires:
qml.Kerr(phi_nonlinear, wires=i)
@qml.qnode(dev)
def quantum_neural_net(inputs, weights):
wires = list(range(len(inputs)))
for i in wires:
qml.FockState(inputs[i], wires=i)
# "layer" subcircuits
for i, v in enumerate(weights):
layer(v[: len(v) // 2], v[len(v) // 2 :], wires)
return [qml.expval(qml.NumberOperator(w)) for w in wires]
num_layers =4
M = len(X[0])
print(M)
num_variables_per_layer = M * (M - 1)
weight_shapes = {"weights": (num_layers, num_variables_per_layer)}
weight_shapes
qlayer = qml.qnn.KerasLayer(quantum_neural_net, weight_shapes, output_dim=4)
clayer_2 = tf.keras.layers.Dense(3, activation="softmax")
model = tf.keras.models.Sequential([qlayer, clayer_2])
opt = keras.optimizers.SGD()
model.compile(opt, loss = 'categorical_crossentropy', metrics =[tf.keras.metrics.CategoricalAccuracy()])
fitting = model.fit(X_train,y_train,validation_data=(X_test,y_test),batch_size=8,epochs=10,verbose=1)
hi, i tried different hyper parameters, optimizer. but accuracy remains same. Not able to make progress in it. can you please help in this.