-
Notifications
You must be signed in to change notification settings - Fork 983
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
Conversation
There was a problem hiding this 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
qubit_to_index = {q: i for i, q in enumerate(qubits)} | ||
|
||
needs_init_layer = False | ||
for max_setting in grouped_settings.keys(): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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())
There was a problem hiding this comment.
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
cirq/work/observable_measurement.py
Outdated
if circuit_sweep is None: | ||
circuit_sweep = study.UnitSweep | ||
else: | ||
circuit_sweep = study.to_sweep(circuit_sweep) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
cirq/work/observable_measurement.py
Outdated
] | ||
resolved_params = _to_sweep(resolved_params) | ||
total_samples = repetitions * len(resolved_params) | ||
print("Total samples", total_samples) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
cirq/work/observable_measurement.py
Outdated
program=measurement_param_circuit, params=resolved_params, repetitions=repetitions | ||
) | ||
|
||
assert len(results) == len(flippy_meas_specs) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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
qubit_to_index = {q: i for i, q in enumerate(qubits)} | ||
|
||
needs_init_layer = False | ||
for max_setting in grouped_settings.keys(): |
There was a problem hiding this comment.
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.
cirq/work/observable_measurement.py
Outdated
if circuit_sweep is None: | ||
circuit_sweep = study.UnitSweep | ||
else: | ||
circuit_sweep = study.to_sweep(circuit_sweep) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
cirq/work/observable_measurement.py
Outdated
] | ||
resolved_params = _to_sweep(resolved_params) | ||
total_samples = repetitions * len(resolved_params) | ||
print("Total samples", total_samples) |
There was a problem hiding this comment.
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.
cirq/work/observable_measurement.py
Outdated
program=measurement_param_circuit, params=resolved_params, repetitions=repetitions | ||
) | ||
|
||
assert len(results) == len(flippy_meas_specs) |
There was a problem hiding this comment.
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.
CI failure looks scary but unrelated |
Wow that does look scary. Restarted the jobs to see whether it's a flake. Opened #3951. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anything else @balopat ?
cirq/work/observable_measurement.py
Outdated
qubit_to_index = {q: i for i, q in enumerate(qubits)} | ||
|
||
needs_init_layer = False | ||
for max_setting in grouped_settings.keys(): |
There was a problem hiding this comment.
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
@95-martin-orion since you've been working on similar things |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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
Loop through grouped settings, execute, accumulate bitstrings.
The following PRs will add
Part of #3647
Follows #3792