quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.23k stars 1.01k forks source link

Gradients of expectation values wrt circuit symbols #2092

Closed trevormccrt closed 4 years ago

trevormccrt commented 5 years ago

Is this a module you all would be interested in adding to cirq? I suggest this because there are several non-trivial methods of doing this at this point that i would like to implement, and I want to minimize future re-inventing of the wheel by putting it in a place accessible to everyone.

some examples of different methods usable for computing gradients of expectation values:

harrow - https://arxiv.org/abs/1901.05374 parameter shift - https://arxiv.org/abs/1905.13311 finitie difference - https://en.wikipedia.org/wiki/Finite_difference

gecrooks commented 5 years ago

I, for one, would like to see this functionality added.

In the last section of the second paper above I also attempt to explain the "middle out" method. Middle out is faster than parameter shift, but works only for simulation, and not for an actual quantum computer.

Other approaches include using TensorFlow (again, simulation only), and evolution strategies (https://www.nature.com/articles/s41534-019-0174-7).

There is prototype code for parameter shift and middle out gradients here: https://github.com/gecrooks/quantumflow-dev/blob/master/quantumflow/gradients.py

vtomole commented 5 years ago

From the abstract of the first paper you linked, this is pretty NISQY so i think it's fine to add it.

Other approaches include using TensorFlow

Fortunately, we already have Tensorflow as a dependency in contrib.

@trevormccrt @gecrooks Please add this module you want to contrib (https://github.com/quantumlib/Cirq/tree/master/cirq/contrib) and open a pull request.

gecrooks commented 5 years ago

If gates with sympy parameters returned parameterized unitaries, as suggested in issue #2069, then converting a Cirq circuit to TensorFlow would be fairly easy.

trevormccrt commented 5 years ago

i'd say integration with tf should be out of scope here - i was more thinking adding some simple differentiation methods that stay relatively close to the hardware

gillverd commented 5 years ago

This is overlapping too much with another project that both the above Googlers know about, gradients should be in that project, not Cirq.

gillverd commented 5 years ago

After discussion with other Google engineers and the PM for Cirq we've decided to hold off on the gradient feature as this is an area of constant change in terms of research.

In the meantime, there is currently work going on at X for a Cirq JAX-based execution backend, which would allow for gradients.

dabacon commented 4 years ago

There is currently functionality for this in TensorFlow Quantum http://tensorflow.org/quantum (it's sort of one of its main purposes).

I don't think we should put this into Cirq core right now. However it make sense to figure out how the flow from TensorFlow quantum to actually running against the hardware works, and whether there are ways to get the experiments that it runs to easily be exported over to Cirq.

Alternatively there could be a separate library that contains common code for this (this is an area of active research so perhaps we could start one in ReCirq).

Closing for now.