# Differentiable Programming of Quantum Computers

Recently, I have tried PennyLane and TensorFlow Quantum. These platforms are said to provide differentiable programming of quantum computers but I can't understand it clearly though.

I have searched about differentiable programming and found out that it allows for gradient-based optimization of parameters in the program. Does differentiable programming of quantum computers supplied us with a faster way to do gradient descent such as parameter shift rules, natural gradient, ...

Except for PennyLane and TensorFlow Quantum, Can we do differentiable programming of quantum computers on other platforms like Qiskit, Cirq, ...

Yes, it is possible to do this with Qiskit and Cirq. For Qiskit, you can read up this tutorial page here.

There was an answer by Josh Izaac from Pennylane here awhile back about parameter shift rule and finite difference to do gradient on quantum circuit here: https://quantumcomputing.stackexchange.com/a/15445/9858

For any quantum software platform, you can do this. The gradient frame-work might not be built-in like Pennylane or Qiskit but you can write your own program to evaluate the gradient. If you already know how to evaluate your cost function then tweaking it a bit to evaluate the gradient is not too hard. So if you have the cost function $$C(\theta) = \langle\psi|U^\dagger(\theta) HU(\theta)|\psi\rangle$$ and if $$U(\theta)$$ is generated from a gate that can be written as the exponential of Pauli operator, for instance $$U(\theta) = e^{-i\theta/2 X} = R_X(\theta)$$, then to evaluate the derivative you can just evaluate the cost function with $$\theta$$ shift by $$+\pi/2$$ and $$-\pi/2$$, then take the difference. So in the case you $$R_X(\theta)$$ you have to perform the two operations/circuits:

Forward shift: $$R_X(\theta + \pi/2) |\psi \rangle$$

Backward shift: $$R_X(\theta - \pi/2) |\psi \rangle$$

Evaluate the cost for these two circuits, then take their difference to get the gradient. Note that here $$|\psi\rangle$$ is just some initial state, you can take it to be $$|0\rangle$$ if you want.

• So the differentiable programming of quantum computers just means that it pushes the speed when doing gradient descent right? – BẢO BẠCH GIA May 26 at 4:27
• Hi @BẢOBẠCHGIA, I am not sure what do you mean by "pushes the speed". It's true that for certain quantum circuit, if we want to simulate it classically then it would be very expensive but that really doesn't have to do anything with evaluate gradient directly. To evaluate the gradient, you must perform your parametrized quantum circuit with the parameter of interest shift forward and shift backward so that you can extract a forward shift value and a backward shift value. Using these two values, you can calculate the gradient. Just like how you would do classically. – KAJ226 May 26 at 5:36
• This is a question that bothered me a lot recently. This shift rule just allows you to find the gradient wrt one parameter exactly (as opposed to approximately), right? It is not as powerful as backprop in NN where the gradient wrt to all parameters can be evaluated simultaneously from the activation values at the previous layers? – Weather Report May 26 at 6:54
• Yeah, I have the same idea about the shift rules so why many techs company try to do that way, it is less efficient than in classical isn't it? What is its potential that Xanadu, Google, and IBM are trying to do it, perhaps it is the only way to use Quantum Machine Learning – BẢO BẠCH GIA May 26 at 8:33
• @WeatherReport Yes, at least in the naive and straight forward way, you evaluate one parameter at a time. There are ways to do backprop in quantum setting as well, see here pennylane.ai/qml/demos/tutorial_backprop.html#backpropagation but this requires you to store intermediate state which is not feasible on current and even near term hardware (in my opinion). Depend on what you application is, the hope is that the number of parameter in your circuit is not exponential scaling in term of the number of qubits... maybe something like linear would be ideal. – KAJ226 May 26 at 15:19