Error creating a keras model with a quantum Circuit as a layer

Hello.
I am having some problems creating a keras model with a quantum layer. I obtain the following Warning when I try to train my model:

WARNING:tensorflow:You are casting an input of type complex128 to an incompatible dtype float32. This will discard the imaginary part and may not be what you intended.

My code is the following one:

import pennylane as qml
import sklearn.datasets
from qiskit_machine_learning import datasets
import qiskit_machine_learning as qiskitml
from sklearn.model_selection import train_test_split

n_qubits = 5
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(inputs, weights):
    qml.templates.AngleEmbedding(np.pi*inputs, wires=range(n_qubits))
    qml.templates.StronglyEntanglingLayers(weights, wires=range(n_qubits))
    
    return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]

shape = qml.StronglyEntanglingLayers.shape(n_layers=1, n_wires=5)
weight_shapes = {"weights": shape}

inputs = np.random.rand(n_qubits).astype(comp_dtype)
weights = np.random.rand(2, n_qubits, 3).astype(comp_dtype)

X_train, X_test, Y_train, Y_test =train_test_split(x, y,test_size=0.15, random_state=0)
X = tf.constant(X_train,dtype=comp_dtype)
Y = tf.constant(Y_train,dtype=comp_dtype)
X_test= tf.constant(X_test,dtype=comp_dtype)
Y_test= tf.constant(Y_test,dtype=comp_dtype)
print(qnode(X_train[0],weights))

q_layer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits, dtype=comp_dtype)
q_layer.build(2)

q_model = tf.keras.models.Sequential()
q_model.add(tf.keras.layers.Dense(n_qubits, activation='sigmoid', input_dim=5))
q_model.add(q_layer)
q_model.add(tf.keras.layers.Dense(1, activation='softmax'))

q_model.summary()

opt = tf.keras.optimizers.Adam(learning_rate=0.05)
q_model.compile(loss='huber', optimizer=opt, metrics=["accuracy"])

q_model.fit(X, Y, epochs=8, batch_size=5, verbose=1,  validation_data=(X_test, Y_test))

And my versions of tensorflow and pennylane are the following ones:
tensorflow == Version: 2.12.0
Pennylane == Version: 0.29.1

The problem is that this warning appears many times, so if someone can help me solving it or just ignoring it would be perfect.
Thanks in advance.

1 Like

Hey @Guillermo_Valverde!

Your code is incomplete and I cannot run it or replicate your issue; there are some imports and variable definitions missing and I’m getting different errors than your are. That said, if you’re okay with what the warning message is saying to you (i.e., “discarding the imaginary part”) and you just want to get rid of the warning message, you can try this:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

If you aren’t okay with discarding the imaginary part, then I’ll need you to copy-paste 100% self-contained code so that I can run it :slight_smile:.

The following is a simpler code with the same warning. The solution you gave me it didn’t solve the warning problem, here I attach an screenshot.

My code

import pennylane as qml
import tensorflow as tf
import sklearn.datasets
from qiskit_machine_learning import datasets
import qiskit_machine_learning as qiskitml
n_qubits = 2
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(inputs, weights):
    qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
    qml.templates.StronglyEntanglingLayers(weights, wires=range(n_qubits))
    return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))

weight_shapes = {"weights": (3, n_qubits, 3)}

qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)
clayer1 = tf.keras.layers.Dense(2)
clayer2 = tf.keras.layers.Dense(2, activation="softmax")
model = tf.keras.models.Sequential([clayer1, qlayer, clayer2])

train_feats, train_labels, test_feats, test_labels =qiskitml.datasets.breast_cancer(80, 30, n=2, plot_data=False,one_hot=False)
X = tf.constant(train_feats)
Y = tf.one_hot(train_labels, depth=2)
X_test= tf.constant(test_feats)
Y_test= tf.one_hot(test_labels,depth=2)

opt = tf.keras.optimizers.SGD(learning_rate=0.5)
model.compile(opt, loss='mae', metrics=['accuracy'])

import warnings
warnings.filterwarnings('ignore') # To ignore warnings.
model.fit(X, Y, epochs=8, batch_size=5, verbose=1,  validation_data=(X_test, Y_test))

The warning

Ah! This one was tricky to spot. Interestingly, the demo on turning QNodes into KerasLayers also outputs the same warnings :thinking:. It’s unclear at this stage whether or not this is a PennyLane issue or a TensorFlow issue. It might be something related to what can happen in PyTorch (see here: Casting real parameter to complex during forward produces warning on backward · Issue #68331 · pytorch/pytorch · GitHub).

TLDR: This sometimes arises with backprop, because a workflow that has float → complex → float could accidentally trigger this warning in reverse.

What I suggest to you is to simply suppress the warning messages. I did some digging on best practices here and there’s a package for silencing Tensorflow warnings :laughing: : silence-tensorflow · PyPI

You can pip install silence-tensorflow, then just put

from silence_tensorflow import silence_tensorflow
silence_tensorflow()

at the top of your code :slight_smile: .

Hello, I get a TensorFlow error in ’ Learning to learn with quantum neural networks’ and ’ Turning quantum nodes into Keras Layers’, which makes Is there a version that works better than others? Silencing partially works.

"WARNING:tensorflow:You are casting an input of type complex128 to an incompatible dtype float64.  This will discard the imaginary part and may not be what you intended.",

'https://github.com/kevinkawchak/Medical-Quantum-Machine-Learning/blob/main/Code/All%20PennyLane%20QML%20Demos/16%20Keras%20Layers%200.18%20Loss%20kkawchak.ipynb

Hey @kevinkawchak, thanks for reporting this! I don’t think we have a work-around at this time. If you’re interested, though, you can try to restructure the code to make it work with Pytorch instead of TensorFlow :slight_smile:. Pytorch is a little less fussy!

Happy Holidays. This version of TensorFlow in Colab removes the input warnings for the following demos.

!pip install tensorflow==2.8.1

'Learning to learn with quantum neural networks | PennyLane Demos
'Turning quantum nodes into Keras Layers | PennyLane Demos

2 Likes

Hi @kevinkawchak, thank you for posting this solution!