quantumlib / Cirq

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

Add a generic Optimizer that makes use of GateTabulation #4059

Open wjhuggins opened 3 years ago

wjhuggins commented 3 years ago

We should write an Optimizer that can use the GateTabulation code to recompile a circuit for an arbitrary two-qubit gate.

Is your feature request related to a use case or problem? Please describe. Yes. This would make it easy to compile a circuit to a specific device using a two-qubit gate which we don't have a full set of analytical tools for.

In the future, we could also use this kind of functionality to do better refitting based on calibration data (at the expense of generating many GateTabulation objects).

Describe the solution you'd like We should adapt the existing infrastructure we have to provide an optimizer which uses the GateTabulation code to compile a circuit with a user specific base_gate.

[optional] Describe alternatives/workarounds you've considered I've hacked this in to the code myself, but it isn't pretty. Analytical compilation isn't always possible and it definitely isn't always straightforward to implement, even for gates we support well.

P1 or P2 depending maybe.

balopat commented 3 years ago

Thanks for opening @wjhuggins! I'm trying to gauge the size/complexity of this do you mind sharing your not-so-pretty code in a gist as a prototype and what specific concerns you have regarding analytical compilation? That would help contributors / ourselves get started.

If I understand correctly the task would be to write an optimizer that approximates any two-qubit gates to single-qubit gates using GateTabulation, which currently only produces single qubit unitaries but not actual gates. We would have to convert these single qubit unitaries to PhasedXZPow gates? Or something else?

wjhuggins commented 3 years ago

I'd be happy to. I'll spend a bit of time on it next week.

The main thing is that the existing infrastructure around the GateTabulation code is only useful for compiling to a Sycamore gate + single-qubit gates. But the same building blocks could be used to write an optimizer that compiles to an arbitrary two-qubit gate + single-qubit gates.

mpharrigan commented 3 years ago

Discussed in cirq cync: possible connection to gatesets cc @tanujkhattar

A generic interface for compiling to various gates should exist. It should go to analytic compilation routines if we have them, but can fall back to tabulation as well. Marking "accepted"

wjhuggins commented 3 years ago

Here's a gist that has the diff of the changes I made:

https://gist.github.com/wjhuggins/05e54da80a9e02b96c700569273f6c92

wjhuggins commented 3 years ago

I basically did two things.

  1. Modified MergeInteractions to optionally return a circuit with two-qubit MatrixGates.
  2. Modified the code in convert_to_sycamore_gates to accept and properly use a GateTabulation built with two-qubit gates other than Sycamore gates.

A real version of the code should definitely be organized differently. I wasn't sure how to do this so I just left it alone in its hack-y state. I'm open to working on this later after I wrap up the experiment that I've been using it for and move on to producing some tutorials about it for public consumption.

95-martin-orion commented 2 years ago

Marked pre-1.0 as a component of Transformers.

dstrain115 commented 2 years ago

@tanujkhattar Do we really need this before 1.0?

tanujkhattar commented 2 years ago

Nope, this should be marked after 1.0. Changed the label.