Skip to content

Add a new proto DeviceParametersDiff which provides a compact way to bundle multiple DeviceParameters and their values #6583

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 16 commits into from
May 31, 2024

Conversation

kmlau
Copy link
Collaborator

@kmlau kmlau commented May 1, 2024

The new proto DeviceParametersDiff is for a user to compose a RunJobRequest for invoking RunJob rpc on a Cirq server, in particular to populate the RunJobRequest.run_context field with device parameters overrides to customize the circuit(s) execution with some control on the device's samples data.

This is based on a design reviews to add "device parameters overrides" before executing circuits sweeping.

I renamed some proto type names from similar internal data structure to prevent reference to internal infrastructures.

…ay to bundle multiple DeviceParameters and their values
@kmlau kmlau requested review from wcourtney, vtomole, cduck, verult and a team as code owners May 1, 2024 23:44
@kmlau
Copy link
Collaborator Author

kmlau commented May 1, 2024

cc @dstrain115

@kmlau kmlau changed the title Add a new proto DeviceParametersDiff proto which provides a compact way to bundle multiple DeviceParameters and their values Add a new proto DeviceParametersDiff which provides a compact way to bundle multiple DeviceParameters and their values May 1, 2024
Copy link

codecov bot commented May 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.81%. Comparing base (6709046) to head (8e79f46).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6583   +/-   ##
=======================================
  Coverage   97.81%   97.81%           
=======================================
  Files        1061     1063    +2     
  Lines       91455    91501   +46     
=======================================
+ Hits        89454    89500   +46     
  Misses       2001     2001           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@senecameeks senecameeks self-requested a review May 2, 2024 21:04
// DeviceParametersDiff from a list of (DeviceParameter, ArgValue) pairs.
message DeviceParametersDiff {
message Dir {
int32 parent = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should explain that this is an index into the strs array.
Also should clarify that -1 means root.

Copy link
Collaborator Author

@kmlau kmlau May 2, 2024

Choose a reason for hiding this comment

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

comments added to indicate the various int32 fields as indices into which arrays, among groups and strs.

dirs_seen: set[tuple[int, int]] = set()

for device_param, value in device_params:
parent = -1 # no parent for the 1st path component
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think "-1" should be a constant, as we will likely need it for decoding the diff too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added a constant of -1, with comment.

@kmlau kmlau requested a review from dstrain115 May 2, 2024 23:42
Copy link
Collaborator

@senecameeks senecameeks left a comment

Choose a reason for hiding this comment

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

How will users interact with this new proto? IIUC, the internal diff proto has an abstraction layer that clients can easily interact with for creating a Diff which can then be easily packaged into its proto form. Can a similar well defined abstraction layer e.g class DeviceParameterDiff exist for this proto as well? It doesn't have to include all the APIs as the one linked, but a clear abstraction layer will help me see how this proto will later get integrated into a Job's run context. WDYT?

@kmlau
Copy link
Collaborator Author

kmlau commented May 3, 2024

How will users interact with this new proto? IIUC, the internal diff proto has an abstraction layer that clients can easily interact with for creating a Diff which can then be easily packaged into its proto form. Can a similar well defined abstraction layer e.g class DeviceParameterDiff exist for this proto as well? It doesn't have to include all the APIs as the one linked, but a clear abstraction layer will help me see how this proto will later get integrated into a Job's run context. WDYT?

thanks for the comment.

the PR includes a helper function to construct a DeviceParametersDiff proto out of a list of DeviceParameter and their values. please see the module run_context.py in this PR. Suppose you want to override some readout parameters on 2 qubits, and build a RunContext proto with such overrides, you may use the following code


import run_context

device_params_override = run_context.to_device_parameters_diff([
    (
        run_context_pb2.DeviceParameter(
            path=["q1_2", "readout_default", "readoutDemodDelay"], units="ns"
        ),
        program_pb2.ArgValue(float_value=5.0),
    ),
    (
        run_context_pb2.DeviceParameter(
            path=["q3_4", "readout_default", "readoutFidelities"]),
        program_pb2.ArgValue(
            double_values=program_pb2.RepeatedDouble(values=[0.991, 0.993])
        ),
    ),
])

my_rc = run_context_pb2.RunContext(
    device_parameters_override=device_params_override)

@kmlau kmlau requested a review from senecameeks May 3, 2024 16:06
@kmlau kmlau requested a review from senecameeks May 3, 2024 18:41
@CirqBot CirqBot added the size: L 250< lines changed <1000 label May 5, 2024
// See run_context.py for utility function to construct a
// DeviceParametersDiff from a list of (DeviceParameter, ArgValue) pairs.
message DeviceParametersDiff {
message Dir {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Avoid abbreviation, here and below: go/pystyle#naming

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

rename from "Dir" to "ResourceGroup"

@@ -10,6 +12,10 @@ option java_multiple_files = true;
message RunContext {
// The parameters for operations in a program.
repeated ParameterSweep parameter_sweeps = 1;

// optional override of select device parameters before program
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it valid for both the parameter_sweeps and device_parameter_override to specify the same parameter(s)? The ordering of these two is ambiguous, which only makes sense if they're disjoint, but either way it should be documented.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch. yes the proto definition allows such overlap. however the override will have no effect since it is applied before the circuit sweep is executed.
I have put in proto field comment to document such case.

"""
diff = run_context_pb2.DeviceParametersDiff()

# Maps a string to its token id. A string is a component of a device parameter's path
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: add punctuation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

}
message Key {
// directory hosting this key, as index into dirs array.
int32 dir = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does Key reference the dir index rather than just being a sub-message?

int32 name = 2;
ArgValue value = 3;
}
message Del {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could deletions be incorporated into the Dir as a list of strings indicating keys that should be deleted?

import google.protobuf.text_format as text_format


def test_to_device_parameters_diff() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Test names should summarize the behavior being tested and its expected outcome.

go/unit-testing-practices#naming

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

renamed the test to call out the behavior to be tested.

strs: "phase_i_rad"
strs: "q5_6"
"""
assert text_format.Parse(expected_diff_pb_text, run_context_pb2.DeviceParametersDiff()) == diff
Copy link
Collaborator

Choose a reason for hiding this comment

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

Test behaviors, not methods.

Behaviors should be tested independently.

go/unit-testing-practices#behavior-testing-examples

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

@kmlau kmlau requested review from NoureldinYosri and wcourtney May 28, 2024 22:59
@kmlau
Copy link
Collaborator Author

kmlau commented May 29, 2024

friendly ping @senecameeks and @dstrain115

Copy link
Collaborator

@dstrain115 dstrain115 left a comment

Choose a reason for hiding this comment

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

Some final comments, then LGTM

// Optional override of select device parameters before program
// execution. Note it is permissible to specify the same device parameter
// here and in a parameter_sweeps, as sweep.single_sweep.parameter.
// However the override will have no effect since it is applied before
Copy link
Collaborator

Choose a reason for hiding this comment

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

This wording is confusing. Can we clarify it? Maybe something like "If the same parameter is supplied in both places, then XYZ will happen"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

reworded the docstring here.

Copy link
Collaborator

@senecameeks senecameeks left a comment

Choose a reason for hiding this comment

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

A few nits but overall LGTM

Thanks, Kim-Ming!

// The main use case is to set those parameters with the
// values from this bundle before executing a circuit sweep.
// Note multiple device parameters may have common ancestor paths
// and/or share the same parameter names. A DeviceParamaetersDiff
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: fix spelling

// Note multiple device parameters may have common ancestor paths
// and/or share the same parameter names. A DeviceParamaetersDiff
// stores the resource groups hierarchy extracted from the DeviceParameters'
// paths and maintains a string tables; thereby storing ancestor resource
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: should this be maintains a table of strings

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated per suggested.

def to_device_parameters_diff(
device_params: Sequence[tuple[run_context_pb2.DeviceParameter, program_pb2.ArgValue]]
) -> run_context_pb2.DeviceParametersDiff:
"""Constructs a DeviceParametersDiff from multiple DeviceParameters and values
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: add punctuation

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.


def resource_path_id(path: tuple[str, ...]) -> int:
"""Computes the index of a path in diff.groups."""
idx = resource_groups_index.get(path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: prefer explicitly setting a default resource_groups_index.get(path, None)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

@pavoljuhas pavoljuhas merged commit d077532 into quantumlib:main May 31, 2024
34 checks passed
@kmlau kmlau deleted the add-device_parameters_diff-proto branch May 31, 2024 20:44
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…bundle multiple DeviceParameters and their values (quantumlib#6583)

The new proto `DeviceParametersDiff` is for a user to compose a RunJobRequest
for invoking RunJob rpc on a Cirq server, in particular to populate the
`RunJobRequest.run_context` field with device parameters overrides to 
customize the circuit(s) execution with some control on the device's samples data.

This is based on a design reviews to add "device parameters overrides"
before executing circuits sweeping.

I renamed some proto type names from similar internal data structure
to prevent reference to internal infrastructures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants