def test_not_overlapping_barriers(self):
        """ Test two barriers with no overlap are not merged
            (NB in these pictures they look like 1 barrier but they are
                actually 2 distinct barriers, this is just how the text
                drawer draws them)
                     ░                     ░
            q_0: |0>─░─           q_0: |0>─░─
                     ░     ->              ░
            q_1: |0>─░─           q_1: |0>─░─
                     ░                     ░
        """
        qr = QuantumRegister(2, 'q')

        circuit = QuantumCircuit(qr)
        circuit.barrier(qr[0])
        circuit.barrier(qr[1])

        expected = QuantumCircuit(qr)
        expected.barrier(qr[0])
        expected.barrier(qr[1])

        pass_ = MergeAdjacentBarriers()
        result = pass_.run(circuit_to_dag(circuit))

        self.assertEqual(result, circuit_to_dag(expected))
    def test_numerous_identical_barriers(self):
        """ Merges 5 identical barriers in a row into one
                 ░  ░  ░  ░  ░  ░                     ░
        q_0: |0>─░──░──░──░──░──░─    ->     q_0: |0>─░─
                 ░  ░  ░  ░  ░  ░                     ░
        """
        qr = QuantumRegister(1, 'q')

        circuit = QuantumCircuit(qr)
        circuit.barrier(qr)
        circuit.barrier(qr)
        circuit.barrier(qr)
        circuit.barrier(qr)
        circuit.barrier(qr)
        circuit.barrier(qr)

        expected = QuantumCircuit(qr)
        expected.barrier(qr)

        expected = QuantumCircuit(qr)
        expected.barrier(qr)

        pass_ = MergeAdjacentBarriers()
        result = pass_.run(circuit_to_dag(circuit))

        self.assertEqual(result, circuit_to_dag(expected))
    def test_barriers_with_blocking_obstacle_long(self):
        """ Test that barriers don't merge if there is an obstacle that
            is blocking
                 ░ ┌───┐ ░                     ░ ┌───┐ ░
        q_0: |0>─░─┤ H ├─░─           q_0: |0>─░─┤ H ├─░─
                 ░ └───┘ ░     ->              ░ └───┘ ░
        q_1: |0>─────────░─           q_1: |0>─────────░─
                         ░                             ░
        """
        qr = QuantumRegister(2, 'q')

        circuit = QuantumCircuit(qr)
        circuit.barrier(qr[0])
        circuit.h(qr[0])
        circuit.barrier(qr)

        expected = QuantumCircuit(qr)
        expected.barrier(qr[0])
        expected.h(qr[0])
        expected.barrier(qr)

        pass_ = MergeAdjacentBarriers()
        result = pass_.run(circuit_to_dag(circuit))

        self.assertEqual(result, circuit_to_dag(expected))
    def test_barriers_with_blocking_obstacle_twoQ(self):
        """ Test that barriers don't merge if there is an obstacle that
            is blocking

                 ░       ░                     ░       ░
        q_0: |0>─░───────░─           q_0: |0>─░───────░─
                 ░       ░                     ░       ░
        q_1: |0>─░───■─────    ->     q_1: |0>─░───■─────
                 ░ ┌─┴─┐ ░                     ░ ┌─┴─┐ ░
        q_2: |0>───┤ X ├─░─           q_2: |0>───┤ X ├─░─
                   └───┘ ░                       └───┘ ░

        """
        qr = QuantumRegister(3, 'q')

        circuit = QuantumCircuit(qr)
        circuit.barrier(0, 1)
        circuit.cx(1, 2)
        circuit.barrier(0, 2)

        expected = QuantumCircuit(qr)
        expected.barrier(0, 1)
        expected.cx(1, 2)
        expected.barrier(0, 2)

        pass_ = MergeAdjacentBarriers()
        result = pass_.run(circuit_to_dag(circuit))

        self.assertEqual(result, circuit_to_dag(expected))
    def test_two_identical_barriers(self):
        """Merges two barriers that are identical into one
                 ░  ░                  ░
        q_0: |0>─░──░─   ->   q_0: |0>─░─
                 ░  ░                  ░
        """
        qr = QuantumRegister(1, "q")

        circuit = QuantumCircuit(qr)
        circuit.barrier(qr)
        circuit.barrier(qr)

        expected = QuantumCircuit(qr)
        expected.barrier(qr)

        pass_ = MergeAdjacentBarriers()
        result = pass_.run(circuit_to_dag(circuit))

        self.assertEqual(result, circuit_to_dag(expected))
    def test_barriers_of_different_sizes(self):
        """ Test two barriers of different sizes are merged into one
                     ░  ░                     ░
            q_0: |0>─░──░─           q_0: |0>─░─
                     ░  ░     ->              ░
            q_1: |0>────░─           q_1: |0>─░─
                        ░                     ░
        """
        qr = QuantumRegister(2, 'q')

        circuit = QuantumCircuit(qr)
        circuit.barrier(qr[0])
        circuit.barrier(qr)

        expected = QuantumCircuit(qr)
        expected.barrier(qr)

        pass_ = MergeAdjacentBarriers()
        result = pass_.run(circuit_to_dag(circuit))

        self.assertEqual(result, circuit_to_dag(expected))
    def test_barriers_with_obstacle_after(self):
        """ Test with an obstacle after the larger barrier
                 ░   ░                      ░
        q_0: |0>─░───░──           q_0: |0>─░──────
                 ░ ┌───┐    ->              ░ ┌───┐
        q_1: |0>─░─┤ H ├           q_1: |0>─░─┤ H ├
                 ░ └───┘                    ░ └───┘
        """
        qr = QuantumRegister(2, 'q')

        circuit = QuantumCircuit(qr)
        circuit.barrier(qr)
        circuit.barrier(qr[0])
        circuit.h(qr[1])

        expected = QuantumCircuit(qr)
        expected.barrier(qr)
        expected.h(qr[1])

        pass_ = MergeAdjacentBarriers()
        result = pass_.run(circuit_to_dag(circuit))

        self.assertEqual(result, circuit_to_dag(expected))
    def test_barriers_with_obstacle_before(self):
        """ Test with an obstacle before the larger barrier
                  ░   ░                          ░
        q_0: |0>──░───░─           q_0: |0>──────░─
                ┌───┐ ░     ->             ┌───┐ ░
        q_1: |0>┤ H ├─░─           q_1: |0>┤ H ├─░─
                └───┘ ░                    └───┘ ░
        """
        qr = QuantumRegister(2, 'q')

        circuit = QuantumCircuit(qr)
        circuit.barrier(qr[0])
        circuit.h(qr[1])
        circuit.barrier(qr)

        expected = QuantumCircuit(qr)
        expected.h(qr[1])
        expected.barrier(qr)

        pass_ = MergeAdjacentBarriers()
        result = pass_.run(circuit_to_dag(circuit))

        self.assertEqual(result, circuit_to_dag(expected))