Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make BarrierBeforeFinalMeasurements deterministic #9568

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion qiskit/transpiler/passes/utils/merge_adjacent_barriers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class MergeAdjacentBarriers(TransformationPass):

def run(self, dag):
"""Run the MergeAdjacentBarriers pass on `dag`."""
indices = {qubit: index for index, qubit in enumerate(dag.qubits)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, it strikes again. This also was a likely source of a performance regression in how I implemented #9569 . We really need to implement #9389 and cache this in the dagcircuit.


# sorted to so that they are in the order they appear in the DAG
# so ancestors/descendants makes sense
Expand All @@ -77,7 +78,9 @@ def run(self, dag):
if node in node_to_barrier_qubits:
qubits = node_to_barrier_qubits[node]
# qubits are stored as a set, need to convert to a list
new_dag.apply_operation_back(Barrier(len(qubits)), qargs=list(qubits))
new_dag.apply_operation_back(
Barrier(len(qubits)), qargs=sorted(qubits, key=indices.get)
)
else:
# copy the condition over too
new_dag.apply_operation_back(node.op, qargs=node.qargs, cargs=node.cargs)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
fixes:
- |
The :class:`.BarrierBeforeFinalMeasurements` and :class:`.MergeAdjacentBarriers` transpiler
passes previously had a non-deterministic order of their emitted :class:`.Barrier` instructions.
This did not change the semantics of circuits but could, in limited cases where there were
non-full-width barriers, cause later stochastic transpiler passes to see a different topological
ordering of the circuit and consequently have different outputs for fixed seeds. The passes
have been made deterministic to avoid this.
23 changes: 23 additions & 0 deletions test/python/transpiler/test_adjacent_barriers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

"""Test the MergeAdjacentBarriers pass"""

import random
import unittest
from qiskit.transpiler.passes import MergeAdjacentBarriers
from qiskit.converters import circuit_to_dag
Expand Down Expand Up @@ -271,6 +272,28 @@ def test_barriers_with_blocking_obstacle_twoQ(self):

self.assertEqual(result, circuit_to_dag(expected))

def test_output_deterministic(self):
"""Test that the output barriers have a deterministic ordering (independent of
PYTHONHASHSEED). This is important to guarantee that any subsequent topological iterations
through the circuit are also deterministic; it's in general not possible for all transpiler
passes to produce identical outputs across all valid topological orderings, especially if
those passes have some stochastic element."""
order = list(range(20))
random.Random(2023_02_10).shuffle(order)
circuit = QuantumCircuit(20)
circuit.barrier([5, 2, 3])
circuit.barrier([7, 11, 14, 2, 4])
circuit.barrier(order)

# All the barriers should get merged together.
expected = QuantumCircuit(20)
expected.barrier(range(20))

output = MergeAdjacentBarriers()(circuit)
self.assertEqual(expected, output)
# This assertion is that the ordering of the arguments in the barrier is fixed.
self.assertEqual(list(output.data[0].qubits), list(output.qubits))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

"""Test the BarrierBeforeFinalMeasurements pass"""

import random
import unittest
from qiskit.transpiler.passes import BarrierBeforeFinalMeasurements
from qiskit.converters import circuit_to_dag
Expand Down Expand Up @@ -392,6 +393,29 @@ def test_conditioned_on_single_bit(self):
pass_ = BarrierBeforeFinalMeasurements()
self.assertEqual(expected, pass_(circuit))

def test_output_deterministic(self):
"""Test that the output barriers have a deterministic ordering (independent of
PYTHONHASHSEED). This is important to guarantee that any subsequent topological iterations
through the circuit are also deterministic; it's in general not possible for all transpiler
passes to produce identical outputs across all valid topological orderings, especially if
those passes have some stochastic element."""
measure_order = list(range(20))
random.Random(2023_02_10).shuffle(measure_order)
circuit = QuantumCircuit(20, 20)
circuit.barrier([5, 2, 3])
circuit.barrier([7, 11, 14, 2, 4])
circuit.measure(measure_order, measure_order)

# All the barriers should get merged together.
expected = QuantumCircuit(20, 20)
expected.barrier(range(20))
expected.measure(measure_order, measure_order)

output = BarrierBeforeFinalMeasurements()(circuit)
jakelishman marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(expected, output)
# This assertion is that the ordering of the arguments in the barrier is fixed.
self.assertEqual(list(output.data[0].qubits), list(output.qubits))


if __name__ == "__main__":
unittest.main()