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

Adding dimension property to comparison of XPowGate and ZPowGate #6005

Merged
merged 15 commits into from
Mar 2, 2023
Merged

Adding dimension property to comparison of XPowGate and ZPowGate #6005

merged 15 commits into from
Mar 2, 2023

Conversation

joesho112358
Copy link
Contributor

Attempting to solve issue #5995

@joesho112358 joesho112358 requested review from a team, vtomole and cduck as code owners February 13, 2023 09:52
@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 13, 2023
@joesho112358
Copy link
Contributor Author

will get to the code style stuff soon, but it only occurred to me just now to ask: should the dimension be added only when it is not 2? changing a lot of json test files...

Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

Thanks for opening the PR Joe! I agree that we should include dimension in the json serialization only when dimension != 2.

As next steps, please do the following:

  1. Update the json_dict method to include dimension only when it's != 2. You can do this via
        d = protocols.obj_to_dict_helper(self, ['exponent', 'global_shift'])
        if self.dimension != 2:
            d['dimension'] = self.dimension
        return d
  1. Revert the changes to all the json test data files.
  2. Add a new json test data for XPowGate(dimension=3) and ZPowGate(dimension=3) and test that the roundtrip works. Instead of adding test_json(), you should directly update the cirq-core/cirq/protocols/json_test_data/XPowGate.json and cirq-core/cirq/protocols/json_test_data/XPowGate.repr files with adding the output from cirq.to_json(x3) and repr(x3).

@joesho112358
Copy link
Contributor Author

@tanujkhattar Hi! I made the updates and added the test file you suggested. I am not sure if I completely finished the tests correctly though. I need more explanation on this part:

you should directly update the cirq-core/cirq/protocols/json_test_data/XPowGate.json and cirq-core/cirq/protocols/json_test_data/XPowGate.repr files with adding the output from cirq.to_json(x3) and repr(x3).

Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

Left a comment with an explanation of how to update the json/repr files.

cirq-core/cirq/protocols/json_test_data/XPowGate3Dim.json Outdated Show resolved Hide resolved
Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

Looking good. One last comment and then we can merge.

Comment on lines 329 to 330
def _value_equality_values_(self):
return self._canonical_exponent, self._global_shift, self._dimension
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 it's better to call the _value_equality_values_ of parent class and append it the tuple with self.dimension. Also, we need to update approx equality as well (and add a test for the same). So,

Suggested change
def _value_equality_values_(self):
return self._canonical_exponent, self._global_shift, self._dimension
def _value_equality_values_(self):
return (*super()._value_equality_values_(), self._dimension)
def _value_equality_approximate_values_(self):
return (*super()._value_equality_approximate_values_(), self._dimension)

Ditto for ZPowGate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be pushed now. I also pushed up removing test_json() because I forgot to remove it in the previous round

@tanujkhattar tanujkhattar merged commit f6a92a0 into quantumlib:master Mar 2, 2023
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…ntumlib#6005)

* adding methods so that dimension is included in _value_equality_values_ and _json_dict_

* quick fix to use canonical exponent in added methods

* needed to update the json for unit tests as well

* fix code style

* updating last jsons to include dimension

* Revert "updating last jsons to include dimension"

This reverts commit 7387710.

* Revert "needed to update the json for unit tests as well"

This reverts commit 473c99e.

* adding dimension to json dict only if dimension is not the default

* adding json and repr files to test XPowGate and ZPowGate dimension

* updating json and repr files to test XPowGate and ZPowGate dimension and removing individual files

* updating _value_equality_values_ function after review and adding _value_equality_approximate_values_ For X and Z PowGates

* removing redundant test for read_json

---------

Co-authored-by: Tanuj Khattar <tanujkhattar@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants