Getting TypeError when trying to use KerasLayer to wrap a quantum node

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.

Hi, I fixed the issue. My constants were being computed as follows:

N_ITER = 1_000
BATCH_SIZE = 100

cutoff = 5
n_modes = 2
n_layers = 1
k = n_modes * (n_modes - 1) / 2

weight_shapes = {
    "theta_1" : (n_layers, k),
    "phi_1": (n_layers, k),
    "varphi_1": (n_layers, n_modes),
    "r": (n_layers, n_modes),
    "phi_r": (n_layers, n_modes),
    "theta_2" : (n_layers, k),
    "phi_2": (n_layers, k),
    "varphi_2": (n_layers, n_modes),
    "a": (n_layers, n_modes),
    "phi_a": (n_layers, n_modes),
}

instead, I had to alter k to

k = int(n_modes * (n_modes - 1) / 2)

but, this leads me to another issue: now I get this error traceback

(Pdb) n
Epoch 1/5
TypeError: Exception encountered when calling layer "keras_layer" (type KerasLayer).

Cannot interpret '2' as a data type

Call arguments received:
  • inputs=tf.Tensor(shape=(100, 2), dtype=int64)
> /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)

The model has the following parameters for a batch size of 100:

(Pdb) model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 keras_layer (KerasLayer)    (100, 2)                  16        
                                                                 
 dense (Dense)               (100, 4)                  12        
                                                                 
=================================================================
Total params: 28
Trainable params: 28
Non-trainable params: 0
_________________________________________________________________

Could you kindly help with this instead?

Hi @say4n!

Have you tried this tutorial? If you try it, do you get the same error?

I’m thinking you may be missing a line such as tf.keras.backend.set_floatx('float64')

Why don’t you try the tutorial and let us know how it goes?

1 Like

Hi Catalina,

I am in fact following the tutorial that you linked to earlier but I have made my own modifications too. I did add the tf.keras.backend.set_floatx('float64') line, I had overlooked it earlier.

But sadly I still get the same error! :sob:

Here (3.5 KB) is the full script that I am executing if it helps.

Also, thanks a ton for the prompt reply, as always!

Hi @say4n, your problem is that within the qnode definition you have k = np.zeros(n_layers, n_modes) . However, the shape is only the first argument of np.zeros so you need to add an extra set of parenthesis like this k = np.zeros((n_layers, n_modes)).

Also, I recommend that you use from pennylane import numpy as np. In this case it’s not a problem but in the future it may be.

You still have some error with the FockStateProjector but I hope this can help you move forward.

Let me know how it goes!

1 Like

Oh wow, thanks Catalina!

Thanks for pointing out the errors with the FockStateProjector too. :smile:

No problem @say4n! I’m glad I could help.

1 Like