def test_wrongly_mapped(self):
        """ Needs [0]-[1] in a [0]--[2]--[1]
         qr0:--(+)--
                |
         qr1:---.---

         CouplingMap map: [0]->[2]->[1]
        """
        qr = QuantumRegister(2, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[0], qr[1])
        coupling = CouplingMap([[0, 2], [2, 1]])
        dag = circuit_to_dag(circuit)

        pass_ = CheckCnotDirection(coupling)
        pass_.run(dag)

        self.assertFalse(pass_.property_set['is_direction_mapped'])
    def test_trivial_map(self):
        """ Trivial map in a circuit without entanglement
         qr0:---[H]---

         qr1:---[H]---

         qr2:---[H]---

         CouplingMap map: None
        """
        qr = QuantumRegister(3, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.h(qr)
        coupling = CouplingMap()
        dag = circuit_to_dag(circuit)
        pass_ = CheckCnotDirection(coupling)
        pass_.run(dag)
        self.assertTrue(pass_.property_set['is_direction_mapped'])
    def test_2q_barrier(self):
        """ A 2q barrier should be ignored
         qr0:--|--
               |
         qr1:--|--

         CouplingMap map: None
        """
        qr = QuantumRegister(2, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.barrier(qr[0], qr[1])
        coupling = CouplingMap()
        dag = circuit_to_dag(circuit)

        pass_ = CheckCnotDirection(coupling)
        pass_.run(dag)

        self.assertTrue(pass_.property_set['is_direction_mapped'])
    def test_true_direction(self):
        """ Mapped is easy to check
         qr0:---.--[H]--.--
                |       |
         qr1:--(+)------|--
                        |
         qr2:----------(+)-

         CouplingMap map: [1]<-[0]->[2]
        """
        qr = QuantumRegister(3, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[0], qr[1])
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[2])
        coupling = CouplingMap([[0, 1], [0, 2]])
        dag = circuit_to_dag(circuit)

        pass_ = CheckCnotDirection(coupling)
        pass_.run(dag)

        self.assertTrue(pass_.property_set['is_direction_mapped'])
    def test_true_direction_in_same_layer(self):
        """ Two CXs distance_qubits 1 to each other, in the same layer
         qr0:--(+)--
                |
         qr1:---.---

         qr2:--(+)--
                |
         qr3:---.---

         CouplingMap map: [0]->[1]->[2]->[3]
        """
        qr = QuantumRegister(4, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[0], qr[1])
        circuit.cx(qr[2], qr[3])
        coupling = CouplingMap([[0, 1], [1, 2], [2, 3]])
        dag = circuit_to_dag(circuit)

        pass_ = CheckCnotDirection(coupling)
        pass_.run(dag)

        self.assertTrue(pass_.property_set['is_direction_mapped'])
    def test_false_direction_in_same_layer_undirected(self):
        """ Two CXs in the same layer, but one is wrongly directed
         qr0:--(+)--
                |
         qr1:---.---

         qr2:---.---
                |
         qr3:--(+)--

         CouplingMap map: [0]->[1]->[2]->[3]
        """
        qr = QuantumRegister(4, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[0], qr[1])
        circuit.cx(qr[3], qr[2])
        coupling = CouplingMap([[0, 1], [1, 2], [2, 3]])
        dag = circuit_to_dag(circuit)

        pass_ = CheckCnotDirection(coupling)
        pass_.run(dag)

        self.assertFalse(pass_.property_set['is_direction_mapped'])