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__()
self.w = self.add_weight(shape=(input_dim,units),initializer='random_uniform', trainable=True)
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()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),
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:
optimizer_v2.py:1219 _filter_grads ([v.name for _, v in grads_and_vars],)) ValueError: No gradients provided for any variable: ['Variable:0'].
So I tried to calculate the gradient "by myself":
@tf.custom_gradient
def custom_activation(w):
result = QuantumCircuit(w)
def grad(dy):
eps=0.0001
result1=QuantumCircuit(w)
result2=QuantumCircuit(w+eps)
grad=(result2-result1)/eps
return dy * [grad]
return result, grad
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 !!