Skip to content
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

[obs] 4.2 - Basic sampling loop #3855

Merged
merged 5 commits into from
Mar 25, 2021
Merged

Conversation

mpharrigan
Copy link
Collaborator

@mpharrigan mpharrigan commented Feb 25, 2021

Loop through grouped settings, execute, accumulate bitstrings.

The following PRs will add

  • More advanced control of how many samples to take per chunk and when to stop
  • Checkpointing
  • Higher-level APIs that are more convenient to use

Part of #3647
Follows #3792

@mpharrigan mpharrigan requested review from cduck, vtomole and a team as code owners February 25, 2021 00:07
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Feb 25, 2021
Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

Looks good, picked a bunch of nits.

cirq/work/observable_measurement.py Outdated Show resolved Hide resolved
cirq/work/observable_measurement.py Outdated Show resolved Hide resolved
cirq/work/observable_measurement.py Show resolved Hide resolved
cirq/work/observable_measurement_test.py Outdated Show resolved Hide resolved
cirq/work/observable_measurement.py Outdated Show resolved Hide resolved
cirq/work/observable_measurement.py Outdated Show resolved Hide resolved
qubit_to_index = {q: i for i, q in enumerate(qubits)}

needs_init_layer = False
for max_setting in grouped_settings.keys():
Copy link
Contributor

Choose a reason for hiding this comment

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

we could have the whole (for settings as well) "needs init layer" logic extracted into a private method, not just the state part of it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

what do you mean? in any event, I inlined the now-one-line definition of _[determine_]needs_init_layer.

Whether an init layer is required only depends on the init state.

Copy link
Contributor

@balopat balopat Mar 22, 2021

Choose a reason for hiding this comment

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

I meant having _determine_needs_init_layer separately seemed like it could have encompassed these (now) 5 lines of code. This method (measure_grouped_settings) is very long, I was trying to chip away extractable pieces so it reads better.

needs_init_layer = _determine_needs_init_layer(grouped_settings.keys())

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

factored out into a helper function, thanks

if circuit_sweep is None:
circuit_sweep = study.UnitSweep
else:
circuit_sweep = study.to_sweep(circuit_sweep)
Copy link
Contributor

Choose a reason for hiding this comment

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

circuit_sweep = study.UnitSweep if circuit_sweep is None else study.to_sweep(circuit_sweep)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed

]
resolved_params = _to_sweep(resolved_params)
total_samples = repetitions * len(resolved_params)
print("Total samples", total_samples)
Copy link
Contributor

Choose a reason for hiding this comment

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

don't we want to use some proper logging instead of print statements? In that case we can also write tests for the things we print with cirq.testing.assert_logs, but also timestamps, formatting, file specs, etc. can be added on top of this kind of output in general.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do we have an example of user-facing logging in cirq? The python logging module requires the user to configure something to actually get the log messages to appear and the google cloud stuff produces a lot of logging output.

I'm removing this print statement for now. We can add better logging later.

program=measurement_param_circuit, params=resolved_params, repetitions=repetitions
)

assert len(results) == len(flippy_meas_specs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a readable error message here. In case we run into this, it will be a bug - some hints would be useful to see what expectation failed, why do we think this equality should hold.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is just testing that the number of results matches the number of params requested and done defensively because of the zip behavior in the next line. If this equality doesn't hold we have bigger problems. Added an assertion message.

Copy link
Collaborator Author

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

sorry for the delay, PTAL!

cirq/work/observable_measurement.py Outdated Show resolved Hide resolved
cirq/work/observable_measurement.py Outdated Show resolved Hide resolved
cirq/work/observable_measurement.py Show resolved Hide resolved
cirq/work/observable_measurement.py Outdated Show resolved Hide resolved
cirq/work/observable_measurement.py Outdated Show resolved Hide resolved
qubit_to_index = {q: i for i, q in enumerate(qubits)}

needs_init_layer = False
for max_setting in grouped_settings.keys():
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

what do you mean? in any event, I inlined the now-one-line definition of _[determine_]needs_init_layer.

Whether an init layer is required only depends on the init state.

if circuit_sweep is None:
circuit_sweep = study.UnitSweep
else:
circuit_sweep = study.to_sweep(circuit_sweep)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed

]
resolved_params = _to_sweep(resolved_params)
total_samples = repetitions * len(resolved_params)
print("Total samples", total_samples)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do we have an example of user-facing logging in cirq? The python logging module requires the user to configure something to actually get the log messages to appear and the google cloud stuff produces a lot of logging output.

I'm removing this print statement for now. We can add better logging later.

program=measurement_param_circuit, params=resolved_params, repetitions=repetitions
)

assert len(results) == len(flippy_meas_specs)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is just testing that the number of results matches the number of params requested and done defensively because of the zip behavior in the next line. If this equality doesn't hold we have bigger problems. Added an assertion message.

cirq/work/observable_measurement_test.py Outdated Show resolved Hide resolved
@mpharrigan
Copy link
Collaborator Author

CI failure looks scary but unrelated

@balopat
Copy link
Contributor

balopat commented Mar 22, 2021

CI failure looks scary but unrelated

Wow that does look scary. Restarted the jobs to see whether it's a flake. Opened #3951.

Copy link
Collaborator Author

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

anything else @balopat ?

qubit_to_index = {q: i for i, q in enumerate(qubits)}

needs_init_layer = False
for max_setting in grouped_settings.keys():
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

factored out into a helper function, thanks

@mpharrigan
Copy link
Collaborator Author

@95-martin-orion since you've been working on similar things

Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

LGTM

@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Mar 25, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Mar 25, 2021
@CirqBot CirqBot merged commit 24e98fa into quantumlib:master Mar 25, 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 Mar 25, 2021
CirqBot pushed a commit that referenced this pull request Apr 12, 2021
Extend the observable measurement "main sampling loop" to use `StoppingCriteria` instances to provide different ways of stopping. Two are built in: Variance and Number of repetitions. Users can write their own by implementing the abstract interface.

For `measure_grouped_settings`, the caller must provide a `StoppingCriteria`. Forthcoming, higher-level wrappers will provide a more user-friendly way of taking the more traditional path of `RepetitionsStoppingCriteria`. 

cc @balopat @95-martin-orion 

Part of #3647
Follows #3855
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/expectation-value cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants