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

Unwinding Symbolic formulas and parameters? #1930

Closed dabacon closed 5 years ago

dabacon commented 5 years ago

Quite accidentally I ran an optimization pass which ended up working correctly but changing a parameter to a formula in sympy, like Symbol('a') + 1.23.

It occurred to me that if we allow this, then we could support a flow where you a) do compilations that support formulas in parameters b) create parameter sweeps c) unwind the sweeps and then calculate the values of the formulas in the parameters. I think this could have a problem where you have different formulas for the same parameter or parameters entering multiple times, but I don't see a fundamental problem with this. d) now you have some parameters and some parameterized circuits which you can send off to a simulator or a quantum computer!

cduck commented 5 years ago

I did the same thing while decomposing a gate into e.g. [X**(gate_param+0.5), ...]. This would be useful.

It turns out that many gate's _circuit_diagram_info_ crash if the gate parameter is a formula.

cduck commented 5 years ago

@dabacon Am I missing any features not in the referencing PRs?

dabacon commented 5 years ago

I think this is great! The one thing I don't see in the ParamFlattener is the case where teh formula is the same and a small helper to do this all in one pass.

cduck commented 5 years ago

What do you mean by "the case where the formula is the same"?

dabacon commented 5 years ago

The case where Symbol('a') + 1 appears in multiple gates.

cduck commented 5 years ago

Yes. That is already handled in #1963.

I just found that this doesn't work as expected when two gates have a+1 and a+1.0 (#1997).