Skip to content

[XEB] Optimize/characterize by pair #3795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2021

Conversation

mpharrigan
Copy link
Collaborator

Add a new method to perform optimization of angles (aka characterization) by pair. This is essential for "parallel" XEB. At it's heart, this is simply a "groupby" operation on the dataframe before calling the characterize function. There are additional code changes:

  • Lots of boilerplate for making a "closure class" so we can optimize by pair in parallel. This is an embarrassingly parallel operation, and it's important when doing very "wide" (i.e. lots of pairs) calibrations.
  • The least-squares estimation of individual (one pair, one cycle depth) fidelities involved using pandas apply, which turned out to be very slow! I profiled the benchmark function on a test workflow. It was taking 7.87 seconds, of which 5.3s were cumulative inside the apply function. The rest of it involved simulating the many circuits. benchmark_... is called during the optimization's objective function so it's performance critical. With the change, the call was a total of 2.5s and the least-squares estimation no longer appeared in the top many cumulative-time profiling calls.
  • Bug fix if you tried to do parallel XEB on a device that couldn't do all four layers. With added test.
  • The result of the characterize_.. function has been beefed up to be a dataclass with more fields. Not only do you get the optimization results, but a nicer dictionary of angles and a dataframe of the refit fidelities. This simplifies one of the older notebooks. This data is also per-pair so you can do parallel xeb effectively
  • An exponential decay fitting function, taken from experiments.cross_entropy_benchmarking, which will be deprecated, [XEB] Plan to deprecate duplicate functionality #3775
  • Some helpful pandas magic wrapped with nice function names to simplify plotting

@mpharrigan mpharrigan requested a review from mrwojtek February 12, 2021 02:59
@mpharrigan mpharrigan requested review from cduck, vtomole and a team as code owners February 12, 2021 02:59
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Feb 12, 2021
Copy link
Collaborator

@mrwojtek mrwojtek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing which is beyond this PR but might be a concern is that it's very hard to review if the code operating on DataFrames is correct. The dataframes have a lot of context which is almost impossible to learn from the code itself.

One possibility to ease this might be having more verbose/explicit tests with hard-coded input and output dataframes.

assert len(row['fidelities']) == len(cycle_depths)


def test_benchmark_2q_xeb_fidelities_vectorized():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to have a test with hard-coded expected values of all the intermediate variables instead of realying on two different implementations of the same logic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that seems more fragile, no? having two independent implementations is the best possible unit test!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you test if the other implementation doesn't have a bug then?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are other tests that test the whole function, which the previous and current one is passing. This is an additional test to make sure the two implementations are identical.

@mrwojtek
Copy link
Collaborator

I consider the comments minor and I'll LGTM it now.

@mpharrigan
Copy link
Collaborator Author

Re dataframes and testing experimental/workflow code in general: I think the notebooks (which thanks to @balopat are now run against cirq master on each PR!) are super helpful for making sure the whole flow works as expected. You can already see in the "unit" tests that I have to basically mock out a whole experiment to test a given part. This is expected from the more "workflow-type" functionality in this suite of PRs. The unit behavior is simple and tested elsewhere (generating random circuits, simulating them, executing them ... this is all covered well in cirq!)

@mpharrigan
Copy link
Collaborator Author

PTAL (I know you already approved, but I'll give you a chance to respond to my responses anyways)

Copy link
Collaborator

@mrwojtek mrwojtek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the variable name the lower case a is better but I still feel the code which implements some algorithm should be structured differently than the mathematical description of that algorithm, for readability reasons.

assert len(row['fidelities']) == len(cycle_depths)


def test_benchmark_2q_xeb_fidelities_vectorized():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you test if the other implementation doesn't have a bug then?

@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Feb 18, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Feb 18, 2021
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CODEOWNERS approval for docs/ files, for which @mrwojtek is not an owner.

@CirqBot CirqBot merged commit 9ab0f6d into quantumlib:master Feb 18, 2021
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Feb 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/xeb cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants