Skip to content

Commit

Permalink
Adding dimension property to comparison of XPowGate and ZPowGate (#6005)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
joesho112358 and tanujkhattar committed Mar 2, 2023
1 parent a827583 commit f6a92a0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
32 changes: 32 additions & 0 deletions cirq-core/cirq/ops/common_gates.py
Expand Up @@ -120,6 +120,10 @@ def __init__(
super().__init__(exponent=exponent, global_shift=global_shift)
self._dimension = dimension

@property
def dimension(self) -> value.TParamVal:
return self._dimension

def _num_qubits_(self) -> int:
return 1

Expand Down Expand Up @@ -316,6 +320,18 @@ def __repr__(self) -> str:
all_args = ', '.join(args)
return f'cirq.XPowGate({all_args})'

def _json_dict_(self) -> Dict[str, Any]:
d = protocols.obj_to_dict_helper(self, ['exponent', 'global_shift'])
if self.dimension != 2:
d['dimension'] = self.dimension
return d

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)


class Rx(XPowGate):
r"""A gate with matrix $e^{-i X t/2}$ that rotates around the X axis of the Bloch sphere by $t$.
Expand Down Expand Up @@ -608,6 +624,10 @@ def __init__(
super().__init__(exponent=exponent, global_shift=global_shift)
self._dimension = dimension

@property
def dimension(self) -> value.TParamVal:
return self._dimension

def _num_qubits_(self) -> int:
return 1

Expand Down Expand Up @@ -834,6 +854,18 @@ def _commutes_on_qids_(
return NotImplemented
return True

def _json_dict_(self) -> Dict[str, Any]:
d = protocols.obj_to_dict_helper(self, ['exponent', 'global_shift'])
if self.dimension != 2:
d['dimension'] = self.dimension
return d

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)


class Rz(ZPowGate):
r"""A gate with matrix $e^{-i Z t/2}$ that rotates around the Z axis of the Bloch sphere by $t$.
Expand Down
4 changes: 4 additions & 0 deletions cirq-core/cirq/ops/common_gates_test.py
Expand Up @@ -1100,6 +1100,7 @@ def test_approx_eq():

def test_xpow_dim_3():
x = cirq.XPowGate(dimension=3)
assert cirq.X != x
# fmt: off
expected = [
[0, 0, 1],
Expand Down Expand Up @@ -1127,6 +1128,7 @@ def test_xpow_dim_3():

def test_xpow_dim_4():
x = cirq.XPowGate(dimension=4)
assert cirq.X != x
# fmt: off
expected = [
[0, 0, 0, 1],
Expand Down Expand Up @@ -1159,6 +1161,7 @@ def test_zpow_dim_3():
L = np.exp(2 * np.pi * 1j / 3)
L2 = L**2
z = cirq.ZPowGate(dimension=3)
assert cirq.Z != z
# fmt: off
expected = [
[1, 0, 0],
Expand Down Expand Up @@ -1209,6 +1212,7 @@ def test_zpow_dim_3():

def test_zpow_dim_4():
z = cirq.ZPowGate(dimension=4)
assert cirq.Z != z
# fmt: off
expected = [
[1, 0, 0, 0],
Expand Down
18 changes: 13 additions & 5 deletions cirq-core/cirq/protocols/json_test_data/XPowGate.json
@@ -1,5 +1,13 @@
{
"cirq_type": "XPowGate",
"exponent": 0.123,
"global_shift": 0.0
}
[
{
"cirq_type": "XPowGate",
"exponent": 0.123,
"global_shift": 0.0
},
{
"cirq_type": "XPowGate",
"exponent": 0.123,
"global_shift": 0.0,
"dimension": 3
}
]
2 changes: 1 addition & 1 deletion cirq-core/cirq/protocols/json_test_data/XPowGate.repr
@@ -1 +1 @@
(cirq.X**0.123)
[(cirq.X**0.123), cirq.XPowGate(exponent=0.123, dimension=3)]
18 changes: 13 additions & 5 deletions cirq-core/cirq/protocols/json_test_data/ZPowGate.json
@@ -1,5 +1,13 @@
{
"cirq_type": "ZPowGate",
"exponent": 0.789,
"global_shift": 0.0
}
[
{
"cirq_type": "ZPowGate",
"exponent": 0.789,
"global_shift": 0.0
},
{
"cirq_type": "ZPowGate",
"exponent": 0.789,
"global_shift": 0.0,
"dimension": 3
}
]
2 changes: 1 addition & 1 deletion cirq-core/cirq/protocols/json_test_data/ZPowGate.repr
@@ -1 +1 @@
(cirq.Z**0.789)
[(cirq.Z**0.789), cirq.ZPowGate(exponent=0.789, dimension=3)]

0 comments on commit f6a92a0

Please sign in to comment.