Skip to content

Commit

Permalink
Improve performance of barrier() (Qiskit#11345)
Browse files Browse the repository at this point in the history
* Improve performance of barrier()

* black

* fix barrier statement in context

* add test

* black

* fix label argument

* Use `unittest` test method

---------

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
2 people authored and ShellyGarion committed Jan 18, 2024
1 parent b97fb3a commit 8a1fc09
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 8 additions & 6 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3317,13 +3317,15 @@ def barrier(self, *qargs: QubitSpecifier, label=None) -> InstructionSet:
"""
from .barrier import Barrier

qubits = (
if qargs:
# This uses a `dict` not a `set` to guarantee a deterministic order to the arguments.
list({q: None for qarg in qargs for q in self.qbit_argument_conversion(qarg)})
if qargs
else self.qubits.copy()
)
return self.append(Barrier(len(qubits), label=label), qubits, [])
qubits = tuple({q: None for qarg in qargs for q in self.qbit_argument_conversion(qarg)})
return self.append(CircuitInstruction(Barrier(len(qubits), label=label), qubits, ()))
else:
qubits = self.qubits.copy()
return self._current_scope().append(
CircuitInstruction(Barrier(len(qubits), label=label), qubits, ())
)

def delay(
self,
Expand Down
11 changes: 11 additions & 0 deletions test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,17 @@ def test_barrier(self):

self.assertEqual(qc, expected)

def test_barrier_in_context(self):
"""Test barrier statement in context, see gh-11345"""
qc = QuantumCircuit(2, 2)
qc.h(0)
with qc.if_test((qc.clbits[0], False)):
qc.h(0)
qc.barrier()

operation_names = [c.operation.name for c in qc]
self.assertNotIn("barrier", operation_names)

def test_measure_active(self):
"""Test measure_active
Applies measurements only to non-idle qubits. Creates a ClassicalRegister of size equal to
Expand Down

0 comments on commit 8a1fc09

Please sign in to comment.