1
$\begingroup$

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 !!

$\endgroup$
1
$\begingroup$

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.

Additional direct references that would be helpful:

Disclaimer: I'm one of the developers working on PennyLane.

| improve this answer | |
$\endgroup$
0
$\begingroup$

I would use Pennylane to do that. And Pennylane offers a plugin to Qiskit so you can run your circuit on IBM's hardware.

Although with the new Qiskit release, they provide a gradient frame work within Qiskit if you are interested. Here is the link to the announcement.. And here is the link to their tutorial notebook on gradient framework.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thanks a lot for your hints ! I know the solution via Pennylane and used it. However having Pennylane between Qiskit and Tensorflow makes the calculations terribly slow. At least compared with TensorflowQuantum (which is optimzed I know but anyway). Therefore I'm interested in gaining performance in connecting Qiskit and Tensorflow directly. As I want to combine my Quantum circuit with a classical neural net and train them together the new Qiskit gradient framework does not help I guess.... $\endgroup$ – Dr Gerhard Hellstern Nov 11 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.