# Usage of Tensorflow/Keras to train Qiskit circuits

In order to explore whether it is possible to train a Qiskit Quantum circuit with tensorflow I built a small toy model. The purpose of this toy model is to find via tensorflow the correct angle to get "zero" output independent of the input.

import numpy as np
import qiskit
from qiskit.circuit import QuantumCircuit, QuantumRegister
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, Layer

def QuantumCircuit(thetas, n_qubits=1):

simulator = qiskit.Aer.get_backend('qasm_simulator')
shots=1024

circuit= qiskit.QuantumCircuit(n_qubits)
circuit.h(0)
circuit.ry(float(thetas),0)
circuit.measure_all()

job = qiskit.execute(circuit,backend=simulator,shots=shots)
result = job.result().get_counts(circuit)

counts = np.array(list(result.values()))
states = np.array(list(result.keys())).astype(float)

# Compute probabilities for each state
probabilities = counts / shots
# Get state expectation
expectation = np.sum(states * probabilities)

return np.array(expectation)

class Linear(Layer):
def __init__(self,units=1,input_dim=1):
super(Linear,self).__init__()

def call(self, inputs, input_dim=1):
if (tf.executing_eagerly()):
return QuantumCircuit(self.w)
return inputs

x_train = np.arange(10)
y_train = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

inputs=Input(shape=(1,))
outputs=Linear()(inputs)
model=tf.keras.models.Model(inputs=inputs,outputs=outputs)
model.summary()
loss=tf.keras.losses.MeanSquaredError())
model.fit(x_train, y_train, epochs=100, batch_size=1, verbose=1)


Unfortunately the toy model doesn't work and I get the following error:

So I tried to calculate the gradient "by myself":

@tf.custom_gradient
def custom_activation(w):
result  = QuantumCircuit(w)

eps=0.0001
result1=QuantumCircuit(w)
result2=QuantumCircuit(w+eps)



as an intermediate step before the Quantum circuit is called. But this works out neither :-(

Does anybody have another idea to plug in Qiskit circuits into tensorflow and to deal with the fact that the automatic differentiation framework of tensorflow does not work in this case ? Thanks a lot !!

Indeed as was pointed out, this problem was figured out a few years ago and we built a software library for it. PennyLane was created to make it easier for users to perform automatic differentiation of hybrid quantum-classical computations.

For the specific use case, using PennyLane you could create a KerasLayer natively, to create a quantum layer. Such a quantum layer can then be used together with classical layers defined using Keras.

Furthermore, PennyLane is hardware and device agnostic. This means that the same quantum circuit could be run on different quantum devices and simulators by a minimal change in code.