# Source code for qiskit.circuit.library.standard_gates.dcx

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2020.
#
# obtain a copy of this license in the LICENSE.txt file in the root directory
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Double-CNOT gate."""

import numpy as np
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister

[docs]class DCXGate(Gate):
r"""Double-CNOT gate.

A 2-qubit Clifford gate consisting of two back-to-back
CNOTs with alternate controls.

.. parsed-literal::
┌───┐
q_0: ──■──┤ X ├
┌─┴─┐└─┬─┘
q_1: ┤ X ├──■──
└───┘

This is a classical logic gate, equivalent to a CNOT-SWAP (CNS) sequence,
and locally equivalent to an iSWAP.

.. math::

DCX\ q_0, q_1 =
\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0
\end{pmatrix}
"""

def __init__(self):
"""Create new DCX gate."""
super().__init__("dcx", 2, [])

def _define(self):
"""
gate dcx a, b { cx a, b; cx a, b; }
"""
# pylint: disable=cyclic-import
from qiskit.circuit.quantumcircuit import QuantumCircuit
from .x import CXGate

q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [(CXGate(), [q[0], q[1]], []), (CXGate(), [q[1], q[0]], [])]
for instr, qargs, cargs in rules:
qc._append(instr, qargs, cargs)

self.definition = qc

def __array__(self, dtype=None):
"""Return a numpy.array for the DCX gate."""
return np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0]], dtype=dtype)