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

Implement the inverse of IonQ native gates #5889

Merged
merged 15 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions cirq-ionq/cirq_ionq/ionq_native_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def _circuit_diagram_info_(
) -> Union[str, 'protocols.CircuitDiagramInfo']:
return protocols.CircuitDiagramInfo(wire_symbols=(f'GPI({self.phase!r})',))

def __pow__(self, power):
if power == 1:
return self

if power == -1:
return self

return NotImplemented


GPI = GPIGate(phi=0)
document(
Expand Down Expand Up @@ -137,6 +146,15 @@ def _json_dict_(self) -> Dict[str, Any]:
def _value_equality_values_(self) -> Any:
return self.phi

def __pow__(self, power):
if power == 1:
return self

if power == -1:
return GPI2Gate(phi=self.phi + 0.5)

return NotImplemented


GPI2 = GPI2Gate(phi=0)
document(
Expand Down Expand Up @@ -219,6 +237,15 @@ def _json_dict_(self) -> Dict[str, Any]:
def _value_equality_values_(self) -> Any:
return (self.phi0, self.phi1)

def __pow__(self, power):
if power == 1:
return self

if power == -1:
return MSGate(phi0=self.phi0 + 0.5, phi1=self.phi1)

return NotImplemented


MS = MSGate(phi0=0, phi1=0)
document(
Expand Down
71 changes: 71 additions & 0 deletions cirq-ionq/cirq_ionq/ionq_native_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,74 @@ def test_ms_unitary(phases):

mat = cirq.protocols.unitary(gate)
numpy.testing.assert_array_almost_equal(mat.dot(mat.conj().T), numpy.identity(4))


@pytest.mark.parametrize(
"gate,target,phases",
[
(ionq.GPIGate, numpy.identity(2), {"phi": p})
for p in [0, 0.1, 0.4, math.pi / 2, math.pi, 2 * math.pi]
]
+ [
(ionq.GPI2Gate, numpy.identity(2), {"phi": p}) # type: ignore
for p in [0, 0.1, 0.4, math.pi / 2, math.pi, 2 * math.pi]
]
+ [
(ionq.MSGate, numpy.identity(4), {"phi0": p0, "phi1": p1}) # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

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

What would it take not to ignore type checks? (Applies throughout.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looked into how other contributors handled the same issue. Fixed now, and the ignores are removed.

for p0, p1 in [
(0, 1),
(0.1, 1),
(0.4, 1),
(math.pi / 2, 0),
(0, math.pi),
(0.1, 2 * math.pi),
]
],
)
def test_gate_inverse(gate, target, phases):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since all targets are identity, you should be able to compute the appropriate matrix inside the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

"""Tests that the inverse of natives gate are correct."""
gate_bound = gate(**phases)
mat = cirq.protocols.unitary(gate_bound)
mat_inverse = cirq.protocols.unitary(gate_bound**-1)

numpy.testing.assert_array_almost_equal(mat.dot(mat_inverse), target)


@pytest.mark.parametrize(
"gate,phases",
[(ionq.GPIGate, {"phi": p}) for p in [0, 0.1, 0.4, math.pi / 2, math.pi, 2 * math.pi]]
+ [
(ionq.GPI2Gate, {"phi": p}) # type: ignore
for p in [0, 0.1, 0.4, math.pi / 2, math.pi, 2 * math.pi]
]
+ [
(ionq.MSGate, {"phi0": p0, "phi1": p1}) # type: ignore
for p0, p1 in [
(0, 1),
(0.1, 1),
(0.4, 1),
(math.pi / 2, 0),
(0, math.pi),
(0.1, 2 * math.pi),
]
],
)
def test_gate_power1(gate, phases):
"""Tests that power=1 for native gates are correct."""
gate_bound = gate(**phases)
mat = cirq.protocols.unitary(gate_bound)
mat_power1 = cirq.protocols.unitary(gate_bound**1)

numpy.testing.assert_array_almost_equal(mat, mat_power1)


@pytest.mark.parametrize(
"gate,power",
[(ionq.GPIGate(phi=0.1), power) for power in [-2, -0.5, 0, 0.5, 2]]
+ [(ionq.GPI2Gate(phi=0.1), power) for power in [-2, -0.5, 0, 0.5, 2]] # type: ignore
+ [(ionq.MSGate(phi0=0.1, phi1=0.2), power) for power in [-2, -0.5, 0, 0.5, 2]], # type: ignore
)
def test_gate_power_not_implemented(gate, power):
"""Tests that any power other than 1 and -1 is not implemented."""
with pytest.raises(TypeError):
_ = gate**power