diff --git a/src/qibo/backends/npmatrices.py b/src/qibo/backends/npmatrices.py index efca1b5992..cbd0e9a478 100644 --- a/src/qibo/backends/npmatrices.py +++ b/src/qibo/backends/npmatrices.py @@ -257,6 +257,25 @@ def SWAP(self): [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]], dtype=self.dtype ) + @cached_property + def CSWAP(self): + return self._cast( + [ + [1, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0], + [0, 0, 0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 1], + ], + dtype=self.dtype, + ) + # return self._cast( + # [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]], dtype=self.dtype + # ) + @cached_property def iSWAP(self): return self._cast( diff --git a/src/qibo/gates/gates.py b/src/qibo/gates/gates.py index 56a84b2201..7990a12b88 100644 --- a/src/qibo/gates/gates.py +++ b/src/qibo/gates/gates.py @@ -1686,6 +1686,51 @@ def qasm_label(self) -> str: return "swap" +class CSWAP(Gate): + """The controlled-SWAP gate. + + Corresponds to the following unitary matrix + + .. math:: + \\begin{pmatrix} + 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ + 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ + 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\ + 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\ + 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\ + 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\\\ + 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\ + 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\ + \\end{pmatrix} + + Args: + q0 (int): the control qubit id number. + q1 (int): the first target qubit id number. + q2 (int): the second target qubit id number. + """ + + def __init__(self, q0: int, q1: int, q2: int): + super().__init__() + self.name = "cswap" + self.draw_label = "x" + self.control_qubits = (q0,) + self.target_qubits = (q1, q2) + self.init_args = [q0, q1, q2] + self.unitary = True + + @property + def clifford(self) -> bool: + return False + + @property + def hamming_weight(self) -> bool: + return True + + @property + def qasm_label(self) -> str: + return "cswap" + + class iSWAP(Gate): """The iSWAP gate.