Lightning gpu with TensorFlow interface

I can only get the lightning gpu backend working with the Tf interface if I force everything to be a tf.float64 and use the command “tf.keras.backend.set_floatx(‘float64’)”. If I try the same but fixing everything as tf.float32 and using “tf.keras.backend.set_floatx(‘float32’)” I get type errors. If I change the backend away from lightning gpu I can use tf.float32. Is this behaviour expected?


Hi @Bnesh , welcome to the Forum!

Yes, lightning.gpu works together with cuStateVec library and I believe that library only supports 64-bit floats.

Thank you for posting it here though! It’s good to have additional visibility over this.

Thanks for the response. Part of my code takes second order derivatives of circuits, this works as expected using default.qubit however when swapping to lightning.gpu all of my second order derivatives are within machine error of 0. Is this expected? I.e can the lightning.gpu backend only take first order derivatives?

Ahh I see I have to use a different differentiation method like “parameter-shift” or “finite-diff” which are both pretty slow. Do you have any advice for this? I initially moved away from Yao hoping to take advantage of some A100s and cuQuantum but even with gpu acceleration my code runs considerably slower.

Hi @Bnesh,

I’m not sure about the first part of the issue but I can check about this.

About the second part, performance will indeed be slower with “parameter-shift”. Comparing CPU vs GPU performance will largely depend on the number of qubits and the depth of your circuits. You only start seeing a performance improvement when using GPUs on circuits that have over 20 qubits.

This paper has some nice performance comparisons between different devices.

If you’re ok with sharing your code and the performance comparison that you experienced I can try making some suggestions that could help.

Hi @Bnesh ,

I’m coming back regarding your question about your issues with lightning.gpu. In principle there shouldn’t be any issues. However this can depend on what exactly you’re running. Have you set max_diff=2 when instantiating your QNode? You can see an example in this post here, where they calculate second order derivatives in two different ways and then compare them.

I hope this helps you!