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

Create a way to encode general functions #6788

Merged
merged 24 commits into from
Nov 14, 2024

Conversation

NoureldinYosri
Copy link
Collaborator

@NoureldinYosri NoureldinYosri commented Nov 8, 2024

No description provided.

@NoureldinYosri NoureldinYosri marked this pull request as ready for review November 8, 2024 20:10
@NoureldinYosri NoureldinYosri changed the title Create a way to encode general 1d functions Create a way to encode general functions Nov 8, 2024
cirq-google/cirq_google/api/v2/program.proto Outdated Show resolved Hide resolved
repeated float x = 1 [packed = true]; // The independent variable.
repeated float y = 2 [packed = true]; // The dependent variable.

// The interpolation method.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you provide documentation for what an "interpolation method" does and how a client should evaluate the options for values?

repeated float y = 2 [packed = true]; // The dependent variable.

// The interpolation method.
// Currently only "interp" (i.e numpy.interp) is supported.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add documentation for the expected behavior of "interp"? We should not depend on a python library (numpy) as the description so that we are not bound to its implementation.

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

repeated float y = 2 [packed = true]; // The dependent variable.

// The interpolation method.
// Currently only "interp" (i.e numpy.interp) is supported.
Copy link
Collaborator

Choose a reason for hiding this comment

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

If there's only one possible value, should we omit this field until there are other options?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ommitted

cirq-google/cirq_google/api/v2/program.proto Outdated Show resolved Hide resolved
cirq-google/cirq_google/api/v2/program.proto Outdated Show resolved Hide resolved
@@ -431,11 +431,31 @@ message ArgMapping {
repeated ArgEntry entries = 1;
}

message FunctionInterpolationData {
// If the function encoded has dimension `d`, then |x| = d |y|. Where |x| the length of `x`.
repeated float x = 1 [packed = true]; // The independent variable.
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIUC, these fields are meant to evoke y = f(x)?

Rather than calling a field x and adding a comment to document that it's "the independent variable", WDYT of calling the field something like independent_variables to be self-documenting? Note that the suggestion is pluralized to follow the style guidance for repeated fields: https://protobuf.dev/programming-guides/style/#repeated-fields

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


// Custom args are arguments that need require special processing during
// deserialization.
map<string, CustomArg> custom_args = 5;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please document what the string keys in this map represent.

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 InternalGate{
string name = 1; // Gate name.
string module = 2; // Gate module.
int32 num_qubits = 3; // Number of qubits. Required during deserialization.
map<string, Arg> gate_args = 4; // Gate args.

// Custom args are arguments that need require special processing during
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 gate_args differ from custom_args? i.e. could FunctionInterpolationData be added to the existing Arg to support use of the existing gate_args? If not, is there a clear guidance (and documentation) for whether a new argument type should be an Arg vs a CustomArg?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How do gate_args differ from custom_args?

Arg are values that we can use as is. That's after deserialization we can use that value internally. CustomArgs are args that we need to perform extra processing on before we can use.

could FunctionInterpolationData be added to the existing Arg to support use of the existing gate_args?
no


I think if we just add an extra possiblitiy to Arg we will just increase the complexity of (de)serialization with no obvious gain.

@CirqBot CirqBot added the size: L 250< lines changed <1000 label Nov 11, 2024
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.85%. Comparing base (fd95547) to head (e5c21d6).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6788   +/-   ##
=======================================
  Coverage   97.85%   97.85%           
=======================================
  Files        1084     1084           
  Lines       93590    93651   +61     
=======================================
+ Hits        91580    91642   +62     
+ Misses       2010     2009    -1     

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

@NoureldinYosri
Copy link
Collaborator Author

@wcourtney ptal

Comment on lines 128 to 129
- `x` is 1D and not sorted in increasing order.
- `method` is not supported.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a check if x and y have matching shapes?

Suggested change
- `x` is 1D and not sorted in increasing order.
- `method` is not supported.
- `x` is not sorted or has 2 or more dimensions.
- `y` has a different shape than `x`

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed docstring.

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

Please use assignment instead of extend() to set message arrays.

Comment on lines 447 to 450
// The independent_var must be sorted in ascending order.
// The independent_var and dependent_var must be of the same length.
repeated float independent_var = 1 [packed = true]; // The independent variable.
repeated float dependent_var = 2 [packed = true]; // The dependent variable.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Apologies @NoureldinYosri, @wcourtney about bike-shedding, but calling these fields x_values, y_values, would perhaps make it a bit easier to parse what these are; it would also follow the recommended pluralization guideline.

How about

Suggested change
// The independent_var must be sorted in ascending order.
// The independent_var and dependent_var must be of the same length.
repeated float independent_var = 1 [packed = true]; // The independent variable.
repeated float dependent_var = 2 [packed = true]; // The dependent variable.
// The x_values must be unique and sorted in ascending order.
// The y_values must be of the same length as x_values.
repeated float x_values = 1 [packed = true]; // The x values where the function is sampled.
repeated float y_values = 2 [packed = true]; // The function values at the corresponding x_values.

cirq-google/cirq_google/api/v2/program.proto Outdated Show resolved Hide resolved
@@ -78,3 +101,55 @@ def _value_equality_values_(self):
self._num_qubits,
frozenset(self.gate_args.items()) if hashable else self.gate_args,
)


def encode_function(
Copy link
Collaborator

Choose a reason for hiding this comment

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

The name may suggest the function itself, such as np.sin, should be passed.
How about renaming to function_values_to_proto?

If we have this one, consider adding a convenience counterpart function_values_from_proto to extract x, y arrays.

cirq-google/cirq_google/ops/internal_gate.py Outdated Show resolved Hide resolved
cirq-google/cirq_google/ops/internal_gate.py Outdated Show resolved Hide resolved
cirq-google/cirq_google/ops/internal_gate.py Outdated Show resolved Hide resolved
cirq-google/cirq_google/ops/internal_gate.py Outdated Show resolved Hide resolved
cirq-google/cirq_google/ops/internal_gate.py Outdated Show resolved Hide resolved
cirq-google/cirq_google/ops/internal_gate_test.py Outdated Show resolved Hide resolved
cirq-google/cirq_google/ops/internal_gate_test.py Outdated Show resolved Hide resolved
@NoureldinYosri NoureldinYosri merged commit 30f5b48 into quantumlib:main Nov 14, 2024
37 checks passed
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.

4 participants