Skip to content

Commit

Permalink
improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed Oct 4, 2023
1 parent 84071fa commit d5abf08
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions tests/test_transpilers_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
_find_previous_gates,
_find_successive_gates,
block_decomposition,
count_2q_gates,
count_multi_qubit_gates,
gates_on_qubit,
initial_block_decomposition,
remove_gates,
)


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
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d5abf08

Please sign in to comment.