diff --git a/CHANGELOG.md b/CHANGELOG.md index bcc32f0..59a4228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ Types of changes: - Fixed `unroll(consolidate_qubits=True)` raising `AttributeError: 'str' object has no attribute 'name'` for any gate applied to a physical qubit, e.g. `h $1;`. Consolidation assumed every gate operand was an `IndexedIdentifier`, but a physical qubit survives unrolling as `Identifier("$1")`. Physical qubits are absolute hardware indices belonging to no declared register, so they are now left as written — matching how `measure`, `reset` and `barrier` already treat them. ([#344](https://github.com/qBraid/pyqasm/pull/344)) - Fixed `unroll()` and `rebase()` emitting statements that share operand AST nodes: gate decompositions passed the same `IndexedIdentifier` objects into every statement they emitted, so transformations that rewrite qubit indices in place mutated a shared node once per referencing statement. This crashed `reverse_qubit_order()` (`KeyError: -1`) and `remove_idle_qubits()` (`KeyError`, [#331](https://github.com/qBraid/pyqasm/issues/331)) on any decomposed gate (e.g. `crz`) whenever the remap was not the identity. Statement constructors in `maps/gates.py` and `Decomposer` now copy their qubit operands so every emitted statement owns its nodes. ([#333](https://github.com/qBraid/pyqasm/issues/333)) - Fixed statements that OpenQASM 2 cannot condition being accepted as the body of a classical conditional. The QASM 2 grammar admits only a `` — a gate application, a measurement or a reset — as the body of an `if`. `barrier` is a separate production, and `delay`/`box` have no QASM 2 syntax at all, yet `if(m==1) barrier q;`, `if(m==1) delay[10ns] q;` and `if(m==1) box {...}` all validated and were emitted unchanged, producing output that QASM 2 parsers reject. Conditional bodies are now filtered against the `` whitelist at any nesting depth, raising a `ValidationError` that names the offending keyword and its source span. ([#339](https://github.com/qBraid/pyqasm/pull/339)) +- 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, a register-level measurement reassembled from the per-bit statements unrolling expanded it into, and any remaining multi-statement body expanded to one guarded statement each. Branch shapes QASM 2 has no syntax for — `else` blocks, nested `if`s, conditions spanning registers or indexing a single bit, and non-equality operators — are now reported by `validate()` rather than only at serialization time, and the diagnostic names the operator the user wrote. ([#337](https://github.com/qBraid/pyqasm/issues/337)) - Fixed `remove_idle_qubits(in_place=False)` updating the qubit count on the wrong module: the original module's `num_qubits` was decremented while the returned copy kept the stale pre-removal count. The copy's AST was already correct; only the counters were swapped. ([#336](https://github.com/qBraid/pyqasm/pull/336)) - 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)) diff --git a/src/pyqasm/modules/qasm2.py b/src/pyqasm/modules/qasm2.py index bba821e..5384bb9 100644 --- a/src/pyqasm/modules/qasm2.py +++ b/src/pyqasm/modules/qasm2.py @@ -16,12 +16,13 @@ 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, raise_qasm3_error from pyqasm.modules.base import QasmModule @@ -44,6 +45,315 @@ } +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 _qreg_sizes(program: Program) -> dict[str, int]: + """Map each quantum register in the program to its declared size.""" + sizes = {} + for statement in program.statements: + if isinstance(statement, qasm3_ast.QubitDeclaration): + size = statement.size + sizes[statement.qubit.name] = ( + size.value if isinstance(size, qasm3_ast.IntegerLiteral) else 1 + ) + return sizes + + +def _single_index(operand: qasm3_ast.QASMNode | None) -> tuple[str, int] | None: + """Decompose ``reg[i]`` into ``(reg, i)``, or ``None`` for any other operand shape.""" + if ( + isinstance(operand, qasm3_ast.IndexedIdentifier) + and len(operand.indices) == 1 + and isinstance(operand.indices[0], list) + and len(operand.indices[0]) == 1 + and isinstance(operand.indices[0][0], qasm3_ast.IntegerLiteral) + ): + return operand.name.name, operand.indices[0][0].value + return None + + +def _collapse_broadcast_measurement( + body: list[qasm3_ast.Statement], creg_sizes: dict[str, int], qreg_sizes: dict[str, int] +) -> qasm3_ast.QuantumMeasurementStatement | None: + """Rebuild ``measure qreg -> creg`` from the per-bit statements unrolling expanded it into. + + A register-level measurement is one QASM 2.0 statement, so a branch guarding it is + expressible as written. Emitting the expansion instead would need one ``if`` per bit, + re-testing the register the body writes; see the hazard check in :func:`_flatten_branch`. + Returns ``None`` unless the body is exactly a full, in-order broadcast. + """ + pairs = [] + for statement in body: + if not isinstance(statement, qasm3_ast.QuantumMeasurementStatement): + return None + qubit = _single_index(statement.measure.qubit) + target = _single_index(statement.target) + if qubit is None or target is None: + return None + pairs.append((qubit, target)) + + (qreg, _), (creg, _) = pairs[0] + size = len(pairs) + if qreg_sizes.get(qreg) != size or creg_sizes.get(creg) != size: + return None + if pairs != [((qreg, index), (creg, index)) for index in range(size)]: + return None + + return qasm3_ast.QuantumMeasurementStatement( + measure=qasm3_ast.QuantumMeasurement(qubit=qasm3_ast.Identifier(name=qreg)), + target=qasm3_ast.Identifier(name=creg), + ) + + +def _unsupported_condition_message(condition: qasm3_ast.Expression) -> str: + """Describe why ``condition`` has no QASM 2.0 form, naming the operator when there is one. + + Only reachable before unrolling: the unroller ravels ``m >= 1`` into a chain of + equality tests, so by then the operator the user wrote is gone. + """ + operator = ( + f", which uses '{condition.op.name}'," + if isinstance(condition, qasm3_ast.BinaryExpression) + and condition.op != qasm3_ast.BinaryOperator["=="] + else "" + ) + return ( + f"Branch condition '{_qasm3_repr(condition)}'{operator} is not supported in QASM 2.0, " + "which only allows 'if (creg == int)'" + ) + + +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(_unsupported_condition_message(condition)) + + 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], + qreg_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] = {} + body: list[qasm3_ast.Statement] = [] + 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 + + if len(body) > 1: + # a register-level measurement survives unrolling as one statement per bit; put it + # back together so the branch is emitted as the single statement it was written as + collapsed = _collapse_broadcast_measurement(body, creg_sizes, qreg_sizes) + if collapsed is not None: + body = [collapsed] + + # 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 of {len(body)} statements, at least one of " + f"which writes to '{reg_name}' -- the register the branch tests. QASM 2.0 guards a " + "single statement per 'if', so emitting one guard per statement would re-evaluate " + "the condition mid-body and change 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, + qreg_sizes: dict[str, int] | None = None, + **kwargs, + ): + super().__init__(*args, **kwargs) + self._creg_sizes = creg_sizes or {} + self._qreg_sizes = qreg_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, self._qreg_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: + # `_start_line` and `skip_next_indent` are openqasm3 printer internals, so an + # upstream change could alter emission silently; the brace assertions in + # tests/qasm2/test_branching.py are what turn that into a test failure + 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. @@ -78,6 +388,7 @@ def _filter_statements(self): raise ValidationError(f"Statement of type {stmt_type} not supported in QASM 2.0") if isinstance(stmt, qasm3_ast.BranchingStatement): self._filter_branch_body(stmt) + self._filter_branch(stmt) # TODO: add more filtering here if needed def _filter_branch_body(self, statement: qasm3_ast.BranchingStatement): @@ -114,6 +425,43 @@ def _filter_branch_body(self, statement: qasm3_ast.BranchingStatement): span=inner_stmt.span, ) + def _filter_branch(self, statement: qasm3_ast.BranchingStatement) -> None: + """Reject the conditional shapes QASM 2.0 has no syntax for, before unrolling. + + These are properties of the source program, so they are caught here rather than + at serialization: unrolling rewrites a register comparison into a chain of + per-bit tests, and by then the operator and nesting the user wrote are gone. + :class:`Qasm2Printer` re-checks what survives into the unrolled AST, since + callers may serialize a module they never validated. + """ + if statement.else_block: + raise_qasm3_error( + "'else' blocks are not supported in QASM 2.0, which only allows " + "'if (creg == int) '", + error_node=statement, + span=statement.span, + ) + for inner_stmt in statement.if_block: + if isinstance(inner_stmt, qasm3_ast.BranchingStatement): + raise_qasm3_error( + "Nested 'if' statements are not supported in QASM 2.0, which guards a " + "single statement per conditional", + error_node=inner_stmt, + span=inner_stmt.span, + ) + condition = statement.condition + if ( + not isinstance(condition, qasm3_ast.BinaryExpression) + or condition.op != qasm3_ast.BinaryOperator["=="] + or not isinstance(condition.lhs, qasm3_ast.Identifier) + or not isinstance(condition.rhs, qasm3_ast.IntegerLiteral) + ): + raise_qasm3_error( + _unsupported_condition_message(condition), + error_node=statement, + span=condition.span, + ) + def _format_declarations(self, qasm_str): """Format the unrolled qasm for declarations in openqasm 2.0 format""" for declaration_type, replacement_type in [("qubit", "qreg"), ("bit", "creg")]: @@ -123,11 +471,23 @@ def _format_declarations(self, qasm_str): return qasm_str def _qasm_ast_to_str(self, qasm_ast): - """Convert the qasm AST to a string""" + """Convert the qasm AST to a string + + Raises: + ValidationError: If the program contains a conditional QASM 2.0 has no syntax + for. :meth:`validate` rejects these shapes as written, but a caller may + serialize a module it never validated, or one whose AST it built directly. + """ # 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), + qreg_sizes=_qreg_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..95bf4c7 --- /dev/null +++ b/tests/qasm2/test_branching.py @@ -0,0 +1,249 @@ +# 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; + """ + unrolled = dumps(loads(qasm2_string)) + assert "{" not in unrolled and "}" not in unrolled + check_unrolled_qasm(unrolled, 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() + unrolled = dumps(module) + assert "{" not in unrolled and "}" not in unrolled + check_unrolled_qasm(unrolled, 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() + unrolled = dumps(module) + assert "{" not in unrolled and "}" not in unrolled + check_unrolled_qasm(unrolled, 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_on_register_level_measurement_round_trips(): + """Test that a branch guarding a whole-register measurement is emitted as written, + whether or not unrolling expanded it into one statement per bit""" + 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; + """ + expected_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() + check_unrolled_qasm(dumps(module), expected_qasm2_string) + # the non-unrolled path never expanded the measurement, so the two must agree + check_unrolled_qasm(dumps(loads(qasm2_string)), expected_qasm2_string) + + +def test_branch_body_writing_tested_register_raises(): + """Test that a genuinely 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) { x q[0]; measure q[0] -> m[0]; } + """ + module = loads(qasm2_string) + module.unroll() + with pytest.raises(ValidationError, match="writes to 'm' -- the register the branch tests"): + 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)) + + +@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];", "Nested 'if' statements are not supported"), + ("if(m==1) if(m==0) x q[0];", "Nested 'if' statements are not supported"), + ("if(m[0]==1) x q[0];", "only allows 'if \\(creg == int\\)'"), + # the operator has to be named here: unrolling ravels '>=' into a chain of + # equality tests, so by serialization time there is nothing left to name + ("if(m>=1) x q[0];", "which uses '>='"), + ("if(m<2) x q[0];", "which uses '<'"), + ], +) +def test_inexpressible_branches_rejected_by_validate(operation, match): + """Test that branch shapes with no QASM 2 form are reported by validate(), not left + for serialization to raise on a program already reported as valid""" + qasm2_string = f"""OPENQASM 2.0; + include "qelib1.inc"; + qreg q[2]; + creg m[2]; + creg c[1]; + measure q[0] -> m[0]; + {operation} + """ + module = loads(qasm2_string) + with pytest.raises(ValidationError, match=match): + module.validate()