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

Add global barrier in qasm3 dump #13508

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions qiskit/qasm3/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,15 @@ def __init__(self, indexIdentifierList: List[Identifier]):
self.indexIdentifierList = indexIdentifierList


class QuantumGlobalBarrier:
"""
quantumBarrier
: 'barrier'
"""

__slots__ = ()


class QuantumReset(QuantumInstruction):
"""A built-in ``reset q0;`` statement."""

Expand Down
4 changes: 4 additions & 0 deletions qiskit/qasm3/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ class ExperimentalFeatures(enum.Flag):
}
}
"""
GLOBAL_BARRIER = enum.auto()
"""Support exporting using golobal barrier as proposed in
https://github.com/Qiskit/qiskit/issues/13485
"""
10 changes: 8 additions & 2 deletions qiskit/qasm3/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,14 @@ def build_current_scope(self) -> List[ast.Statement]:
if isinstance(instruction.operation, Gate):
nodes = [self.build_gate_call(instruction)]
elif isinstance(instruction.operation, Barrier):
operands = [self._lookup_bit(operand) for operand in instruction.qubits]
nodes = [ast.QuantumBarrier(operands)]
if ExperimentalFeatures.GLOBAL_BARRIER in self.experimental and len(
instruction.qubits
) == len(self.scope.circuit.qubits):
operands = [self._lookup_bit(operand) for operand in instruction.qubits]
nodes = [ast.QuantumGlobalBarrier()]
else:
operands = [self._lookup_bit(operand) for operand in instruction.qubits]
nodes = [ast.QuantumBarrier(operands)]
elif isinstance(instruction.operation, Measure):
measurement = ast.QuantumMeasurement(
[self._lookup_bit(operand) for operand in instruction.qubits]
Expand Down
3 changes: 3 additions & 0 deletions qiskit/qasm3/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ def _visit_QuantumBarrier(self, node: ast.QuantumBarrier) -> None:
self._visit_sequence(node.indexIdentifierList, separator=", ")
self._end_statement()

def _visit_QuantumGlobalBarrier(self, _node: ast.QuantumGlobalBarrier) -> None:
self._write_statement("barrier")

def _visit_ProgramBlock(self, node: ast.ProgramBlock) -> None:
self.stream.write("{\n")
self._current_indent += 1
Expand Down
34 changes: 34 additions & 0 deletions test/python/qasm3/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,40 @@ def test_switch_v1_expr_target(self):
test = dumps(qc, experimental=ExperimentalFeatures.SWITCH_CASE_V1)
self.assertEqual(test, expected)

def test_global_barrier_all_qubits(self):
"""Simple test that the global barrir is created"""
qc = QuantumCircuit(3)
qc.x(0)
qc.barrier()
qc.x(1)
expected = """\
OPENQASM 3.0;
include "stdgates.inc";
qubit[3] q;
x q[0];
barrier;
x q[1];
"""
test = dumps(qc, experimental=ExperimentalFeatures.GLOBAL_BARRIER)
self.assertEqual(test, expected)

def test_global_barrier_partial_qubits(self):
"""Simple test that the global barrir is created"""
qc = QuantumCircuit(3)
qc.x(0)
qc.barrier(0, 1)
qc.x(1)
expected = """\
OPENQASM 3.0;
include "stdgates.inc";
qubit[3] q;
x q[0];
barrier q[0], q[1];
x q[1];
"""
test = dumps(qc, experimental=ExperimentalFeatures.GLOBAL_BARRIER)
self.assertEqual(test, expected)


@ddt
class TestQASM3ExporterFailurePaths(QiskitTestCase):
Expand Down