Skip to content

Commit

Permalink
Make cirq.FREDKIN gate self-inverse (#6135)
Browse files Browse the repository at this point in the history
* Make FREDKIN gate self-inverse

* Fix formatting

* Simplify test to check CSWAP matrix is self inverse
  • Loading branch information
tanujkhattar committed Jun 11, 2023
1 parent c08649f commit 9a1609c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cirq-core/cirq/ops/three_qubit_gates.py
Expand Up @@ -661,6 +661,11 @@ def _qasm_(self, args: 'cirq.QasmArgs', qubits: Tuple['cirq.Qid', ...]) -> Optio
def _value_equality_values_(self):
return ()

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

def __str__(self) -> str:
return 'FREDKIN'

Expand Down
6 changes: 4 additions & 2 deletions cirq-core/cirq/ops/three_qubit_gates_test.py
Expand Up @@ -94,8 +94,9 @@ def test_unitary():
)

assert cirq.has_unitary(cirq.CSWAP)
u = cirq.unitary(cirq.CSWAP)
np.testing.assert_allclose(
cirq.unitary(cirq.CSWAP),
u,
np.array(
[
[1, 0, 0, 0, 0, 0, 0, 0],
Expand All @@ -110,6 +111,7 @@ def test_unitary():
),
atol=1e-8,
)
np.testing.assert_allclose(u @ u, np.eye(8))

diagonal_angles = [2, 3, 5, 7, 11, 13, 17, 19]
assert cirq.has_unitary(cirq.ThreeQubitDiagonalGate(diagonal_angles))
Expand Down Expand Up @@ -156,7 +158,7 @@ def test_eq():
eq.add_equality_group(cirq.TOFFOLI(a, b, c), cirq.CCX(a, b, c))
eq.add_equality_group(cirq.TOFFOLI(a, c, b), cirq.TOFFOLI(c, a, b))
eq.add_equality_group(cirq.TOFFOLI(a, b, d))
eq.add_equality_group(cirq.CSWAP(a, b, c), cirq.FREDKIN(a, b, c))
eq.add_equality_group(cirq.CSWAP(a, b, c), cirq.FREDKIN(a, b, c), cirq.FREDKIN(a, b, c) ** -1)
eq.add_equality_group(cirq.CSWAP(b, a, c), cirq.CSWAP(b, c, a))


Expand Down

0 comments on commit 9a1609c

Please sign in to comment.