Skip to content

Commit

Permalink
Handle pre-existing adjacent barriers
Browse files Browse the repository at this point in the history
In the case when a label is set to trigger the removal of the labelled
barrier the merge step would lose the context around which barrier
instruction was transpiler inserted and which was user provided. To
address this issue this commit skips the MergeAdjacentBarrier step so
that the transpiler barrier is kept separate from any user inserted
barriers which need to be preserved.
  • Loading branch information
mtreinish committed Nov 21, 2023
1 parent b12cc3b commit 29c1217
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ def run(self, dag):

dag.compose(barrier_layer)

# Merge the new barrier into any other barriers
adjacent_pass = MergeAdjacentBarriers()
return adjacent_pass.run(dag)
if self.label is None:
# Merge the new barrier into any other barriers
adjacent_pass = MergeAdjacentBarriers()
return adjacent_pass.run(dag)
else:
return dag
19 changes: 19 additions & 0 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,25 @@ def test_barrier_not_output(self, opt_level):
)
self.assertNotIn("barrier", tqc.count_ops())

@data(0, 1, 2, 3)
def test_barrier_not_output_input_preservered(self, opt_level):
"""Test that barriers added as part internal transpiler operations do not leak out."""
qc = QuantumCircuit(2, 2)
qc.cx(0, 1)
qc.measure_all()
tqc = transpile(
qc,
initial_layout=[1, 4],
coupling_map=[[0, 1], [1, 2], [2, 3], [3, 4]],
optimization_level=opt_level,
)
op_counts = tqc.count_ops()
self.assertEqual(op_counts["barrier"], 1)
for inst in tqc.data:
if inst.operation.name == "barrier":
self.assertEqual(len(inst.qubits), 2)


@combine(opt_level=[0, 1, 2, 3])
def test_transpile_annotated_ops(self, opt_level):
"""Test transpilation of circuits with annotated operations."""
Expand Down
2 changes: 1 addition & 1 deletion test/python/transpiler/test_sabre_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def test_layout_many_search_trials(self):
self.assertIsInstance(res, QuantumCircuit)
layout = res._layout.initial_layout
self.assertEqual(
[layout[q] for q in qc.qubits], [22, 7, 2, 12, 1, 5, 14, 4, 11, 0, 16, 15, 3, 10]
[layout[q] for q in qc.qubits], [10, 16, 8, 4, 11, 20, 15, 19, 18, 7, 12, 1, 2, 0]
)


Expand Down

0 comments on commit 29c1217

Please sign in to comment.