Skip to content

✨ Support if (c) and if (c[k]) as boolean-only conditions #462

Description

@rturrado

πŸ€– AI text below πŸ€–

Problem statement

OpenQASM 3 accepts a classical register or a single classical bit as a boolean condition, with no explicit comparator:

if (c) x q[0];
if (c[0]) x q[0];

The debugger's parser (parseClassicConditionExpression in src/common/parsing/CodePreprocessing.cpp)
requires one of the six comparators (==, !=, <, <=, >, >=) and returns std::nullopt for these bare forms,
so the classic-controlled gate fails to parse.

mqt-core's IfElseOperation already models this shape (a single classical control with an implicit non-zero check),
so the simulation backend is ready. Only the debugger front-end needs to catch up.

Surfaced during the follow-up discussion of #168 (PR #456), which added support for the six non-equality comparators.

Proposed solution

Extend parseClassicConditionExpression to accept a bare register reference (c) or a bare bit reference (c[k]) as a valid condition
and evaluate it as "the value is non-zero".
The existing ClassicCondition and applyComparison(..., qc::Neq) machinery should be enough, so no evaluator change is expected.

Tests should cover both the satisfied and unsatisfied cases, the single-bit form,
and one backward-step case to mirror the coverage added in #456.

Out of scope

  • Compound boolean expressions with &&, ||, !.
    OpenQASM 3 supports them, but mqt-core's parser does not; that work would start in mqt-core.
  • Multi-instruction if bodies, per-statement stepping, register slicing, and other extensions of if.
    Covered by separate follow-up items.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions