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

[cirqflow] KeyValueExecutableSpec should provide a to_dict method / override __getitem__ #4734

Closed
tanujkhattar opened this issue Dec 7, 2021 · 7 comments · Fixed by #5072
Labels
area/google area/workflow kind/feature-request Describes new functionality triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@tanujkhattar
Copy link
Collaborator

tanujkhattar commented Dec 7, 2021

Is your feature request related to a use case or problem? Please describe.
cg.KeyValueExecutableSpec provides a nice from_dict() method to convert a dict into a Tuple[Tuple[str, Any], ...] which is hashable. This is useful when constructing the executable spec. However, using the executable spec during analysis of the results forces one to use the stored tuples, which is cumbersome.

Describe the solution you'd like
The class should provide a similar to_dict method which can convert the stored key_value_pairs to a dictionary and return -- which are much easier to work with. Though the method would be a simple return dict(self.key_value_pairs), there might be some value in explicitly having it on the class. We can also consider providing a custom __getitem__ method.

What is the urgency from your perspective for this issue? Is it blocking important work?
P1 - I need this no later than the next release (end of quarter)

cc @mpharrigan

@tanujkhattar tanujkhattar added kind/feature-request Describes new functionality area/google labels Dec 7, 2021
@mpharrigan
Copy link
Collaborator

sounds like a good idea to me

@viathor viathor added the triage/discuss Needs decision / discussion, bring these up during Cirq Cynque label Dec 14, 2021
@viathor
Copy link
Collaborator

viathor commented Dec 14, 2021

I agree with @mpharrigan. Added triage/discuss to decide which options to go with.

@95-martin-orion 95-martin-orion added triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Dec 15, 2021
@tanujkhattar
Copy link
Collaborator Author

Discussed in cirq sync: We should have a to_dict method. Probably also investigate whether the class needs to be immutable since cirq json serialization can handle dict to sorted tuples conversion.

Another heads up here is that dict -> tuple conversion should sort tuples to guarantee stability. In this class, we do not currently do the sorting. cc @mpharrigan

@mpharrigan
Copy link
Collaborator

dictionaries are ordered in modern python. it's not clear to me whether we should be sorting.

@tanujkhattar
Copy link
Collaborator Author

They are insertion ordered, not sorted based on keys. One counter intuitive example due to this is:

> d1 = dict.fromkeys(['a', 'b'])
> d2 = dict.fromkeys(['b', 'a'])
> assert d1 == d2
> k1 = cg.KeyValueExecutableSpec.from_dict(d1, executable_family='test')
> k2 = cg.KeyValueExecutableSpec.from_dict(d2, executable_family='test')
> assert k1 != k2  # k1 == k2 should ideally be True. 

@mpharrigan
Copy link
Collaborator

currently keyvaluespec has these semantics

KVSpec(stuff=(('a', 1), ('b', 2))) != KVSpec(stuff=(('b', 2), ('a', 1)))

so we should "fix" this as well.

@tonybruguier
Copy link
Contributor

The proposed PR above is only for the new function. I have read about the key order issue, but it seems a tiny bit more dangerous, so I was planning to have a separate PR. My preference is weak though.

CirqBot pushed a commit that referenced this issue Mar 17, 2022
CirqBot pushed a commit that referenced this issue Mar 17, 2022
…cutableSpec (#5073)

For issue #4734. It was discussed there that the order of the keys should not matter.
tonybruguier added a commit to tonybruguier/Cirq that referenced this issue Apr 14, 2022
tonybruguier added a commit to tonybruguier/Cirq that referenced this issue Apr 14, 2022
…cutableSpec (quantumlib#5073)

For issue quantumlib#4734. It was discussed there that the order of the keys should not matter.
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…cutableSpec (quantumlib#5073)

For issue quantumlib#4734. It was discussed there that the order of the keys should not matter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/google area/workflow kind/feature-request Describes new functionality triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants