Skip to content

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

Merged
pavoljuhas merged 16 commits into
quantumlib:mainfrom
kmlau:add-device_parameters_diff-proto
May 31, 2024
Merged

Add a new proto DeviceParametersDiff which provides a compact way to bundle multiple DeviceParameters and their values#6583
pavoljuhas merged 16 commits into
quantumlib:mainfrom
kmlau:add-device_parameters_diff-proto

Conversation

@kmlau

@kmlau kmlau commented May 1, 2024

Copy link
Copy Markdown
Collaborator

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 a team, cduck, verult, vtomole and wcourtney as code owners May 1, 2024 23:44
@kmlau

kmlau commented May 1, 2024

Copy link
Copy Markdown
Collaborator Author

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
@codecov

codecov Bot commented May 1, 2024

Copy link
Copy Markdown

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
Copy Markdown
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.

@kmlau kmlau May 2, 2024

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
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
Copy Markdown
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

@senecameeks senecameeks left a comment

Copy link
Copy Markdown
Collaborator

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

kmlau commented May 3, 2024

Copy link
Copy Markdown
Collaborator Author

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
Comment thread cirq-google/cirq_google/api/v2/run_context.py
@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
Copy Markdown
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
Copy Markdown
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"

// The parameters for operations in a program.
repeated ParameterSweep parameter_sweeps = 1;

// optional override of select device parameters before program

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread cirq-google/cirq_google/api/v2/run_context.py
"""
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
Copy Markdown
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
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Comment thread cirq-google/cirq_google/api/v2/run_context.proto
}
message Key {
// directory hosting this key, as index into dirs array.
int32 dir = 1;

Copy link
Copy Markdown
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
Copy Markdown
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?

Comment thread cirq-google/cirq_google/api/v2/run_context.proto
import google.protobuf.text_format as text_format


def test_to_device_parameters_diff() -> None:

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

kmlau commented May 29, 2024

Copy link
Copy Markdown
Collaborator Author

friendly ping @senecameeks and @dstrain115

@dstrain115 dstrain115 left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
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.

Comment thread cirq-google/cirq_google/api/v2/run_context.proto
Comment thread cirq-google/cirq_google/api/v2/run_context.py
Comment thread cirq-google/cirq_google/api/v2/run_context.py
Comment thread cirq-google/cirq_google/api/v2/run_context.py

@senecameeks senecameeks left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@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