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

Make cirq.FREDKIN gate self-inverse #6135

Merged
merged 4 commits into from Jun 11, 2023

Conversation

tanujkhattar
Copy link
Collaborator

The CSWAP / FREDKIN gate is self inverse but currently cirq.FREDKIN ** -1 returns a _InverseCompositeGate(cirq.FREDKIN). This PR adds a __pow__ method to CSwapGate so that cirq.FREDKIN ** -1 returns a cirq.FREDKIN.

@tanujkhattar tanujkhattar requested review from a team, vtomole and cduck as code owners June 11, 2023 04:51
@@ -110,6 +110,11 @@ def test_unitary():
),
atol=1e-8,
)
np.testing.assert_allclose(
cirq.unitary(cirq.FREDKIN**-1),
cirq.linalg.map_eigenvalues(cirq.unitary(cirq.FREDKIN), lambda b: b**-1),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we need the call to map_eigevalues:

np.testing.assert_allclose(cirq.unitary(cirq.FREDKIN**-1), cirq.unitary(cirq.FREDKIN), 1e-8)

Mathematical reason is that the eigenvalues of FREDKIN are all $\pm 1$, so $\lambda=\lambda^{-1}$. Pythonic reason is that AFAICT we should have equality cirq.FREDKIN**-1 == cirq.FREDKIN, before we even call cirq.unitary. Perhaps that is a better (or additional) thing to assert (though perhaps not inside test_unitary, not sure what a better suited test is). Any potential extra assert is up to you.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I agree we should add a assert cirq.FREDKIN ** -1 == cirq.FREDKIN. The reason I used this approach for the test here is to assert that the unitary matrix itself is self inverse, and not the object. Self inverse nature of the unitary matrix can be figured out using the math, but here the goal was to test that math.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The simplest way to do this is

u = cirq.unitary(cirq.FREDKIN)
np.testing.assert_allclose(u @ u, np.eye(8))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Simplified the test for self-inverse as recommended. Also, note that assert cirq.FREDKIN ** -1 == cirq.FREDKIN is already covered in my original PR under the equality test eq.add_equality_group(cirq.CSWAP(a, b, c), cirq.FREDKIN(a, b, c), cirq.FREDKIN(a, b, c) ** -1). Merging now.

@tanujkhattar tanujkhattar enabled auto-merge (squash) June 11, 2023 06:14
@tanujkhattar tanujkhattar merged commit 9a1609c into quantumlib:master Jun 11, 2023
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants