diff --git a/CHANGELOG.md b/CHANGELOG.md index dac9966..97b5bbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Types of changes: ### Removed ### Fixed +- Fixed the OpenQASM 2 serializer emitting OpenQASM 3 syntax for classical conditionals while still declaring `OPENQASM 2.0`. `if(m==1) x q[1];` round-tripped as a braced `if (m == 1) { x q[1]; }` block, which QASM 2 parsers reject, so valid feed-forward programs were corrupted in-place. Unrolling made it worse: `unroll()` ravels a register comparison into a nested chain of per-bit tests (`if (m[0] == true) { if (m[1] == false) { ... } }`), which additionally uses creg indexing and a `true` literal that QASM 2 has no syntax for. Conditionals are now emitted as `if (creg == int) `, with the per-bit chain collapsed back into the whole-register comparison it came from and a body that unrolled into several statements expanded to one guarded statement each. Branches QASM 2 genuinely cannot express — `else` blocks, nested `if`s, conditions spanning registers or constraining only part of one, non-equality operators, a bit compared against a non-bit value, or a multi-statement body that writes to the register under test — now raise a `ValidationError` instead of emitting output that is invalid or triggers on different values than the source. ([#337](https://github.com/qBraid/pyqasm/issues/337)) - Fixed `remove_idle_qubits()` raising `KeyError` when the unrolled AST contains operand nodes shared across multiple statements (e.g. the `crz` decomposition) and an idle lower-indexed qubit shifts the register indices. `_remap_qubits` now remaps each operand node exactly once instead of once per statement that references it. ([#332](https://github.com/qBraid/pyqasm/pull/332)) - Fixed `box` duration validation summing `delay` durations across all qubits instead of tracking each qubit's timeline. Delays on disjoint qubits run in parallel, so `box[300ns] { delay[200ns] q[0]; delay[200ns] q[1]; }` was rejected while the identical schedule written as a broadcast delay (`delay[200ns] q;`) was accepted. Delays are now accumulated per qubit and the box is validated against the busiest single timeline; the error message names the offending qubit. Nested boxes now also contribute their declared duration to the enclosing box's timelines (previously the accumulator was reset when an inner box closed, dropping all inner delay accounting). ([#330](https://github.com/qBraid/pyqasm/pull/330)) - Fixed `reset` on a physical qubit rewriting the operand to the internal pulse register, e.g. `reset $2;` unrolled to `reset __PYQASM_QUBITS__[2];`. That names a register the program never declares, so the unrolled output did not round-trip through `dumps()`/`loads()`, and the qubit was never registered (a program whose only operation was `reset $3;` reported `num_qubits == 0`). Physical qubits are now kept as-is in plain QASM programs, matching how gate and measurement operands already treat them; the rename is still applied for OpenPulse programs, where the pulse visitor expects it. ([#325](https://github.com/qBraid/pyqasm/pull/325)) diff --git a/src/pyqasm/modules/qasm2.py b/src/pyqasm/modules/qasm2.py index f4b0de9..407f334 100644 --- a/src/pyqasm/modules/qasm2.py +++ b/src/pyqasm/modules/qasm2.py @@ -16,18 +16,234 @@ Defines a module for handling OpenQASM 2.0 programs. """ +import io import re from copy import deepcopy import openqasm3.ast as qasm3_ast from openqasm3.ast import Include, Program -from openqasm3.printer import dumps +from openqasm3.printer import Printer, PrinterState, dumps from pyqasm.exceptions import ValidationError from pyqasm.modules.base import QasmModule from pyqasm.modules.qasm3 import Qasm3Module +def _qasm3_repr(node: qasm3_ast.QASMNode) -> str: + """Render a node with the stock QASM 3 printer, for use in error messages.""" + out = io.StringIO() + Printer(out).visit(node) + return out.getvalue().strip() + + +def _creg_sizes(program: Program) -> dict[str, int]: + """Map each classical register in the program to its declared size.""" + sizes = {} + for statement in program.statements: + if isinstance(statement, qasm3_ast.ClassicalDeclaration) and isinstance( + statement.type, qasm3_ast.BitType + ): + size = statement.type.size + if size is None: + sizes[statement.identifier.name] = 1 + elif isinstance(size, qasm3_ast.IntegerLiteral): + sizes[statement.identifier.name] = size.value + return sizes + + +def _parse_branch_condition(condition: qasm3_ast.Expression) -> tuple[str, int | None, int]: + """Decompose a branch condition into ``(register name, bit index, value)``. + + ``bit index`` is ``None`` for a whole-register comparison (``c == 2``) and an + integer for the single-bit comparisons that unrolling produces (``c[0] == true``). + """ + if ( + not isinstance(condition, qasm3_ast.BinaryExpression) + or condition.op != qasm3_ast.BinaryOperator["=="] + ): + raise ValidationError( + f"Branch condition '{_qasm3_repr(condition)}' is not supported in QASM 2.0, " + "which only allows 'if (creg == int)'" + ) + + lhs, rhs = condition.lhs, condition.rhs + if not isinstance(rhs, (qasm3_ast.IntegerLiteral, qasm3_ast.BooleanLiteral)): + raise ValidationError( + f"Branch condition '{_qasm3_repr(condition)}' is not supported in QASM 2.0, " + "which only allows comparison against an integer literal" + ) + value = int(rhs.value) + + if isinstance(lhs, qasm3_ast.Identifier): + return lhs.name, None, value + + if ( + isinstance(lhs, qasm3_ast.IndexExpression) + and isinstance(lhs.collection, qasm3_ast.Identifier) + and isinstance(lhs.index, list) + and len(lhs.index) == 1 + and isinstance(lhs.index[0], qasm3_ast.IntegerLiteral) + ): + return lhs.collection.name, lhs.index[0].value, value + + raise ValidationError( + f"Branch condition '{_qasm3_repr(condition)}' is not supported in QASM 2.0, " + "which only allows 'if (creg == int)'" + ) + + +def _add_chain_constraint( + reg_name: str, + bit_index: int | None, + value: int, + reg_value: int | None, + bit_values: dict[int, bool], +) -> int | None: + """Fold one link of a branch chain into the constraints collected so far. + + Every link's constraint has to survive into the single comparison QASM 2 allows, + so a link that contradicts or duplicates an earlier one has nowhere to go and must + not be quietly dropped by the collapse in :func:`_flatten_branch`. + """ + if bit_index is None: + if reg_value is not None: + raise ValidationError( + f"Branch on '{reg_name}' nests another whole-register comparison, " + "which is not supported in QASM 2.0" + ) + return value + + if bit_index in bit_values: + raise ValidationError( + f"Branch on '{reg_name}' tests bit {bit_index} more than once, " + "which is not supported in QASM 2.0" + ) + if value not in (0, 1): + raise ValidationError( + f"Branch on '{reg_name}' compares bit {bit_index} against {value}; " + "a single bit can only be compared against 0 or 1" + ) + bit_values[bit_index] = bool(value) + return reg_value + + +def _flatten_branch( + statement: qasm3_ast.BranchingStatement, creg_sizes: dict[str, int] +) -> tuple[str, int, list[qasm3_ast.Statement]]: + """Reduce a branching statement to the ``if (creg == int) `` form of QASM 2. + + Unrolling rewrites ``if (c == 2)`` into a chain of nested single-bit tests, one per + bit of ``c``; QASM 2 has no bit indexing in conditions and no nested ``if``, so the + chain is walked back into the whole-register comparison it came from. + """ + bit_values: dict[int, bool] = {} + reg_name = None + reg_value = None + current = statement + + while True: + if current.else_block: + raise ValidationError( + "'else' blocks are not supported in QASM 2.0, which only allows " + "'if (creg == int) '" + ) + + name, bit_index, value = _parse_branch_condition(current.condition) + if reg_name is not None and name != reg_name: + raise ValidationError( + f"Nested branches on different registers ('{reg_name}' and '{name}') are not " + "supported in QASM 2.0, which has no nested 'if' statements" + ) + reg_name = name + reg_value = _add_chain_constraint(reg_name, bit_index, value, reg_value, bit_values) + + body = current.if_block + # unrolling nests one branch per bit, so keep walking while the body is + # nothing but the next branch in that chain + if len(body) == 1 and isinstance(body[0], qasm3_ast.BranchingStatement): + current = body[0] + continue + break + + assert reg_name is not None + if any(isinstance(stmt, qasm3_ast.BranchingStatement) for stmt in body): + raise ValidationError( + f"Nested 'if' statements inside the body of a branch on '{reg_name}' are not " + "supported in QASM 2.0" + ) + + if bit_values: + if reg_value is not None: + raise ValidationError( + f"Branch on '{reg_name}' mixes whole-register and single-bit comparisons, " + "which is not supported in QASM 2.0" + ) + size = creg_sizes.get(reg_name) + if size is None: + raise ValidationError( + f"Missing declaration for classical register '{reg_name}' used in a branch" + ) + if set(bit_values) != set(range(size)): + missing = sorted(set(range(size)) - set(bit_values)) + raise ValidationError( + f"Branch on '{reg_name}' constrains only bits {sorted(bit_values)} of a " + f"{size}-bit register (bits {missing} are unconstrained); QASM 2.0 can only " + "compare a classical register against an integer" + ) + # unrolling ravels the comparison value MSB-first, so invert that ordering here + reg_value = sum(1 << (size - 1 - index) for index, set_ in bit_values.items() if set_) + + assert reg_value is not None + + # a multi-statement body is emitted as one guarded statement each, which re-tests the + # register before every statement; that is only faithful while the body leaves it alone + if len(body) > 1 and any(_writes_to_register(stmt, reg_name) for stmt in body): + raise ValidationError( + f"Branch on '{reg_name}' has a body that writes to '{reg_name}' itself. QASM 2.0 " + "guards a single statement per 'if', so the multi-statement body cannot be emitted " + "without re-evaluating the condition mid-body and changing the program's meaning" + ) + + return reg_name, reg_value, body + + +def _writes_to_register(statement: qasm3_ast.Statement, reg_name: str) -> bool: + """Whether ``statement`` assigns to any bit of the classical register ``reg_name``.""" + if isinstance(statement, qasm3_ast.QuantumMeasurementStatement): + target = statement.target + if isinstance(target, qasm3_ast.IndexedIdentifier): + return target.name.name == reg_name + if isinstance(target, qasm3_ast.Identifier): + return target.name == reg_name + return False + + +class Qasm2Printer(Printer): + """``openqasm3`` printer that emits OpenQASM 2.0 branching syntax. + + OpenQASM 2.0 has no braced blocks: a conditional is ``if (creg == int) `` + with exactly one statement and no ``else``. The base printer always emits the QASM 3 + braced form, which downstream QASM 2 parsers reject. + """ + + def __init__(self, *args, creg_sizes: dict[str, int] | None = None, **kwargs): + super().__init__(*args, **kwargs) + self._creg_sizes = creg_sizes or {} + + def visit_BranchingStatement( # pylint: disable=invalid-name + self, node: qasm3_ast.BranchingStatement, context: PrinterState + ) -> None: + reg_name, reg_value, body = _flatten_branch(node, self._creg_sizes) + + # a single QASM 2 conditional guards a single statement, so a body that + # unrolled into several statements becomes one guarded statement each + for statement in body: + self._start_line(context) + self.stream.write(f"if ({reg_name} == {reg_value}) ") + context.skip_next_indent = True + self.visit(statement, context) + + class Qasm2Module(QasmModule): """ A module representing an openqasm2 quantum program. @@ -74,8 +290,9 @@ def _qasm_ast_to_str(self, qasm_ast): """Convert the qasm AST to a string""" # set the version to 2.0 qasm_ast.version = "2.0" - raw_qasm = dumps(qasm_ast, old_measurement=True) - return self._format_declarations(raw_qasm) + stream = io.StringIO() + Qasm2Printer(stream, old_measurement=True, creg_sizes=_creg_sizes(qasm_ast)).visit(qasm_ast) + return self._format_declarations(stream.getvalue()) def to_qasm3(self, as_str: bool = False) -> str | Qasm3Module: """Convert the module to openqasm3 format diff --git a/tests/qasm2/test_branching.py b/tests/qasm2/test_branching.py new file mode 100644 index 0000000..e0d3bb1 --- /dev/null +++ b/tests/qasm2/test_branching.py @@ -0,0 +1,190 @@ +# Copyright 2025 qBraid +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Module containing unit tests for serializing classical conditionals as OpenQASM 2.0 + +""" + +import pytest + +from pyqasm.entrypoint import dumps, loads +from pyqasm.exceptions import ValidationError +from tests.utils import check_unrolled_qasm + + +def test_branch_emits_qasm2_syntax(): + """Test that a conditional round-trips as QASM 2 syntax rather than a braced + QASM 3 block (issue #337)""" + qasm2_string = """OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[1]; + creg c[2]; + h q[0]; + measure q[0] -> m[0]; + if(m==1) x q[1]; + measure q -> c; + """ + expected_qasm2_string = """OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[1]; + creg c[2]; + h q[0]; + measure q[0] -> m[0]; + if (m == 1) x q[1]; + measure q -> c; + """ + check_unrolled_qasm(dumps(loads(qasm2_string)), expected_qasm2_string) + + +def test_branch_body_expands_to_one_conditional_per_statement(): + """Test that a body which unrolls into several statements becomes one guarded + statement each, since QASM 2 has no braced blocks""" + qasm2_string = """OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[1]; + measure q[0] -> m[0]; + if(m==1) h q; + """ + expected_qasm2_string = """OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[1]; + measure q[0] -> m[0]; + if (m == 1) h q[0]; + if (m == 1) h q[1]; + """ + module = loads(qasm2_string) + module.unroll() + check_unrolled_qasm(dumps(module), expected_qasm2_string) + + +@pytest.mark.parametrize("value", [0, 1, 2, 3]) +def test_multibit_branch_survives_unrolling(value): + """Test that the per-bit conditionals unrolling produces for a multi-bit register + collapse back into the whole-register comparison QASM 2 requires""" + qasm2_string = f"""OPENQASM 2.0; + include "qelib1.inc"; + qreg q[3]; + creg m[2]; + measure q[0] -> m[0]; + measure q[1] -> m[1]; + if(m=={value}) x q[2]; + """ + expected_qasm2_string = f"""OPENQASM 2.0; + include "qelib1.inc"; + qreg q[3]; + creg m[2]; + measure q[0] -> m[0]; + measure q[1] -> m[1]; + if (m == {value}) x q[2]; + """ + module = loads(qasm2_string) + module.unroll() + check_unrolled_qasm(dumps(module), expected_qasm2_string) + + +@pytest.mark.parametrize( + "operation, expected", + [ + ("x q[1];", "if (m == 1) x q[1];"), + ("reset q[1];", "if (m == 1) reset q[1];"), + ("measure q[1] -> c[0];", "if (m == 1) measure q[1] -> c[0];"), + ], +) +def test_branch_body_statement_types(operation, expected): + """Test that every operation QASM 2 allows in a conditional body is emitted on the + same line as the condition""" + qasm2_string = f"""OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[1]; + creg c[1]; + measure q[0] -> m[0]; + if(m==1) {operation} + """ + module = loads(qasm2_string) + module.unroll() + unrolled = dumps(module) + assert "{" not in unrolled and "}" not in unrolled + assert expected in unrolled + + +def test_branch_body_writing_tested_register_raises(): + """Test that a multi-statement body assigning to the register under test is rejected + rather than emitted with the condition re-evaluated mid-body""" + qasm2_string = """OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[2]; + measure q[0] -> m[0]; + if(m==1) measure q -> m; + """ + module = loads(qasm2_string) + module.unroll() + with pytest.raises(ValidationError, match="writes to 'm' itself"): + dumps(module) + + +@pytest.mark.parametrize( + "operation, match", + [ + # two whole-register comparisons in a chain: the outer constraint has nowhere + # to go in the single comparison QASM 2 allows + ("if(m==1) if(m==0) x q[0];", "nests another whole-register comparison"), + # the same bit constrained twice, ditto + ("if(m[0]==1) if(m[0]==0) x q[0];", "tests bit 0 more than once"), + # a bit compared against something that is not a bit value + ("if(m[0]==2) x q[0];", "against 2"), + ], +) +def test_conflicting_chain_constraints_raise(operation, match): + """Test that a chain carrying constraints the collapse cannot represent is rejected + rather than silently reduced to whichever constraint happens to be walked last""" + qasm2_string = f"""OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[1]; + measure q[0] -> m[0]; + {operation} + """ + with pytest.raises(ValidationError, match=match): + dumps(loads(qasm2_string)) + + +@pytest.mark.parametrize( + "operation, match", + [ + ("if(m==1) x q[0]; else x q[1];", "'else' blocks are not supported"), + ("if(m==1) if(c==1) x q[0];", "different registers"), + ("if(m[0]==1) x q[0];", "unconstrained"), + ("if(m>=1) x q[0];", "only allows 'if \\(creg == int\\)'"), + ], +) +def test_inexpressible_branches_raise(operation, match): + """Test that each branch shape QASM 2 has no syntax for is rejected. Emitting these + would drop a branch or silently change which values trigger the body.""" + qasm2_string = f"""OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[2]; + creg c[1]; + measure q[0] -> m[0]; + {operation} + """ + with pytest.raises(ValidationError, match=match): + dumps(loads(qasm2_string))