Hi, I am trying to convert the following quantum node to a keras layer
@qml.qnode(dev)
def node(inputs, theta_1, phi_1, varphi_1, r, phi_r, theta_2, phi_2, varphi_2, a, phi_a):
k = np.zeros(n_layers, n_modes) # Kerr parameters
qml.templates.DisplacementEmbedding(inputs, wires=range(n_modes), method='amplitude', c=0.0)
qml.templates.layers.CVNeuralNetLayers(theta_1, phi_1, varphi_1, r, phi_r, theta_2, phi_2, varphi_2, a, phi_a, k, wires=range(n_modes))
return [qml.expval(qml.FockStateProjector(wires=w)) for w in range(n_modes)]
as follows:
if __name__ == "__main__"
q_layer = qml.qnn.KerasLayer(node, weight_shapes, output_dim=int(n_modes))
c_layer_out = tf.keras.layers.Dense(int(2 ** n_modes), activation=tf.keras.activations.softmax)
model = tf.keras.models.Sequential([q_layer, c_layer_out])
opt = tf.keras.optimizers.Adam(learning_rate=0.2)
model.compile(opt, loss="mae", metrics=["accuracy"])
for iteration in range(N_ITER):
X_batch, y_batch = generate_training_data(BATCH_SIZE)
breakpoint()
model.fit(X_batch, y_batch, epochs=5, batch_size=BATCH_SIZE)
print(model.summary())
where the “training data” is generated with
def generate_training_data(n_datapoints):
x_data, y_data = [], []
for _ in range(n_datapoints):
t_data = np.random.choice([0, 1], size=n_modes)
index = int("".join(map(str, t_data)), base=2)
onehot = np.zeros(2 ** n_modes)
onehot[index] = 1
x_data.append(t_data * 2 - 1)
y_data.append(onehot)
return np.array(x_data), np.array(y_data)
However, on doing so, I encounter a cryptic TypeError on calling the model.fit()
method. The traceback for it is as follows:
> /Users/Sayan/Desktop/Projects/coherent-state-discrimination/examples/explore_pennylane.py(85)<module>()
-> model.fit(X_batch, y_batch, epochs=5, batch_size=BATCH_SIZE)
(Pdb) n
Epoch 1/5
TypeError: Exception encountered when calling layer "sequential" (type Sequential).
Dimension value must be integer or None or have an __index__ method, got value '1.0' with type '<class 'float'>'
Call arguments received:
• inputs=tf.Tensor(shape=(100, 2), dtype=int64)
• training=True
• mask=None
> /Users/Sayan/Desktop/Projects/coherent-state-discrimination/examples/explore_pennylane.py(85)<module>()
-> model.fit(X_batch, y_batch, epochs=5, batch_size=BATCH_SIZE)
Could you kindly help me discern this error?
Please check the following comment in the thread for my updated question.
I am on tensorflow 2.7.0rc1, and I have reproduced the same error on 2.6.0 too. In addition, I have pennylane 0.17.0 installed.