diff --git a/tests/test_transpilers_blocks.py b/tests/test_transpilers_blocks.py index c5722b4cf..26929fe19 100644 --- a/tests/test_transpilers_blocks.py +++ b/tests/test_transpilers_blocks.py @@ -7,7 +7,6 @@ _find_previous_gates, _find_successive_gates, block_decomposition, - count_2q_gates, count_multi_qubit_gates, gates_on_qubit, initial_block_decomposition, @@ -15,6 +14,13 @@ ) +def assert_gates_equality(gates_1: list, gates_2: list): + """Check that the gates are the same.""" + for g_1, g_2 in zip(gates_1, gates_2): + assert g_1.qubits == g_2.qubits + assert g_1.__class__ == g_2.__class__ + + def test_count_2q_gates(): block = Block(qubits=(0, 1), gates=[gates.CZ(0, 1), gates.CZ(0, 1), gates.H(0)]) assert block.count_2q_gates() == 2 @@ -44,9 +50,7 @@ def test_fuse_blocks(): block_1 = Block(qubits=(0, 1), gates=[gates.CZ(0, 1)]) block_2 = Block(qubits=(0, 1), gates=[gates.H(0)]) fused = block_1.fuse(block_2) - assert fused.qubits == (0, 1) - assert fused.entangled == True - assert fused.count_2q_gates() == 1 + assert_gates_equality(fused.gates, block_1.gates + block_2.gates) def test_fuse_blocks_error(): @@ -76,29 +80,29 @@ def test_count_multi_qubit_gates(): def test_gates_on_qubit(): gatelist = [gates.H(0), gates.H(1), gates.H(2), gates.H(0)] - assert len(gates_on_qubit(gatelist, 0)) == 2 - assert len(gates_on_qubit(gatelist, 1)) == 1 + assert_gates_equality(gates_on_qubit(gatelist, 0), [gatelist[0], gatelist[-1]]) + assert_gates_equality(gates_on_qubit(gatelist, 1), [gatelist[1]]) + assert_gates_equality(gates_on_qubit(gatelist, 2), [gatelist[2]]) def test_remove_gates(): gatelist = [gates.H(0), gates.CZ(0, 1), gates.H(2), gates.CZ(0, 2)] + remaining = [gates.CZ(0, 1), gates.H(2)] delete_list = [gatelist[0], gatelist[3]] remove_gates(gatelist, delete_list) - assert len(gatelist) == 2 - assert count_2q_gates(gatelist) == 1 + assert_gates_equality(gatelist, remaining) def test_find_previous_gates(): gatelist = [gates.H(0), gates.H(1), gates.H(2)] previous_gates = _find_previous_gates(gatelist, (0, 1)) - assert len(previous_gates) == 2 + assert_gates_equality(previous_gates, gatelist[:2]) def test_find_successive_gates(): gatelist = [gates.H(0), gates.CZ(2, 3), gates.H(1), gates.H(2), gates.CZ(2, 1)] - previous_gates = _find_successive_gates(gatelist, (0, 1)) - assert len(previous_gates) == 2 - assert count_2q_gates(previous_gates) == 0 + successive_gates = _find_successive_gates(gatelist, (0, 1)) + assert_gates_equality(successive_gates, [gatelist[0], gatelist[2]]) def test_initial_block_decomposition(): @@ -111,6 +115,7 @@ def test_initial_block_decomposition(): circ.add(gates.H(3)) circ.add(gates.H(4)) blocks = initial_block_decomposition(circ) + assert_gates_equality(blocks[0].gates, [gates.H(1), gates.H(0), gates.CZ(0, 1)]) assert len(blocks) == 4 assert len(blocks[0].gates) == 3 assert len(blocks[1].gates) == 1 @@ -144,6 +149,7 @@ def test_block_decomposition_no_fuse(): circ.add(gates.H(1)) circ.add(gates.H(3)) blocks = block_decomposition(circ, fuse=False) + assert_gates_equality(blocks[0].gates, [gates.H(1), gates.H(0), gates.CZ(0, 1), gates.H(0)]) assert len(blocks) == 4 assert len(blocks[0].gates) == 4 assert len(blocks[1].gates) == 1 @@ -167,6 +173,7 @@ def test_block_decomposition(): circ.add(gates.CZ(2, 3)) # 4 block circ.add(gates.CZ(0, 1)) # 3 block blocks = block_decomposition(circ) + assert_gates_equality(blocks[0].gates, [gates.H(1), gates.H(0), gates.CZ(0, 1), gates.H(0), gates.CZ(0, 1)]) assert len(blocks) == 4 assert blocks[0].count_2q_gates() == 2 assert len(blocks[0].gates) == 5