Skip to content

Adding dimension property to comparison of XPowGate and ZPowGate #6005

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

Merged
merged 15 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cirq-core/cirq/ops/common_gates.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(cirq.Z**0.789)
[(cirq.Z**0.789), cirq.ZPowGate(exponent=0.789, dimension=3)]