Skip to content

Add ZipLongest to cirq_google#6074

Merged
dstrain115 merged 4 commits intoquantumlib:masterfrom
dstrain115:zip_longest
Apr 25, 2023
Merged

Add ZipLongest to cirq_google#6074
dstrain115 merged 4 commits intoquantumlib:masterfrom
dstrain115:zip_longest

Conversation

@dstrain115
Copy link
Copy Markdown
Collaborator

  • This class is similar to cirq.Zip but repeats the last value if the combined sweeps are not the same length.

- This class is similar to cirq.Zip but repeats the
last value if the combined sweeps are not the same
length.
@dstrain115 dstrain115 requested review from a team, cduck, verult, vtomole and wcourtney as code owners April 21, 2023 19:58
@dstrain115 dstrain115 requested review from maffoo and zchen088 April 21, 2023 20:16
@maffoo
Copy link
Copy Markdown
Contributor

maffoo commented Apr 21, 2023

Can this go into cirq.study? Seems generally useful and not specific to cirq_google

Comment thread cirq-google/cirq_google/study/zip_longest.py Outdated
Comment thread cirq-google/cirq_google/study/zip_longest.py Outdated
Comment thread cirq-google/cirq_google/study/zip_longest.py Outdated
Comment thread cirq-google/cirq_google/study/zip_longest.py Outdated
Comment thread cirq-google/cirq_google/study/zip_longest_test.py Outdated
Copy link
Copy Markdown
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 see the inline comments

@dstrain115 dstrain115 requested a review from pavoljuhas April 24, 2023 19:52
Comment thread cirq-core/cirq/study/sweeps_test.py Outdated
sweep1 = cirq.ZipLongest(cirq.Points('a', [1, 2, 3]), cirq.Points('b', [4, 5, 6, 7]))
sweep2 = cirq.ZipLongest(cirq.Points('a', [1, 2, 3]), cirq.Points('b', [4, 5, 6, 7]))
sweep3 = cirq.ZipLongest(cirq.Points('a', [1, 2]), cirq.Points('b', [4, 5, 6, 7]))
sweep4 = cirq.Zip(cirq.Points('a', [1, 2]), cirq.Points('b', [4, 5, 6, 7]))
Copy link
Copy Markdown
Collaborator

@pavoljuhas pavoljuhas Apr 24, 2023

Choose a reason for hiding this comment

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

As it is, both sweep3 == sweep4 and hash(sweep3) == hash(sweep4) evaluate to True.
Perhaps we need to use a strict type(other) is Zip in the Zip.__eq__ function

def __eq__(self, other):
if not isinstance(other, Zip):
return NotImplemented
return self.sweeps == other.sweeps

and insert ZipLongest or a similar type-marker to the hashed tuple at

def __hash__(self) -> int:
return hash(tuple(self.sweeps))

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.

Changed to use 'is not Zip' and also changed the test to use Cirq's equality tester.

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

Looks good, but I noticed yet another corner case we should clean up.

@dstrain115 dstrain115 requested a review from pavoljuhas April 24, 2023 23:10
@dstrain115 dstrain115 added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Apr 25, 2023
@dstrain115 dstrain115 merged commit c1d6451 into quantumlib:master Apr 25, 2023
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
* Add ZipLongest to cirq_google

- This class is similar to cirq.Zip but repeats the
last value if the combined sweeps are not the same
length.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Tells CirqBot to sync and merge this PR. (If it's running.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants