Skip to content

durationof({...}) is unsupported #400

Description

@TheGupta2012

Limitation

Duration and stretch types defines
durationof(), which takes a block of quantum instructions and returns its duration. It is
how a program expresses a delay relative to the length of some other operation rather than as
an absolute time.

pyqasm does not evaluate it.

Example QASM failure

OPENQASM 3.0;
include "stdgates.inc";
qubit[1] q;
duration a = durationof({x q[0];});
ValidationError: Invalid initialization value for variable 'a'

Plain duration declarations, unit arithmetic, delay[...] and box[...] all work today, so
durationof is the isolated gap in the timing surface.

Change Requested

  1. Evaluate durationof({ ... }) to a duration value usable anywhere a duration literal is,
    in particular in delay[...] and box[...].
  2. Where the enclosed block's length is known — because the module was loaded with
    device_cycle_time / calibration information, or the block contains only explicit delay
    instructions — return the concrete value.
  3. Where it is not known, keep it symbolic and emit durationof({ ... }) unchanged through
    dumps(). Rejecting a valid program because a backend duration is unknown would be worse
    than passing it through.
  4. Validate the block itself: the enclosed statements must be legal quantum instructions, and
    any qubit they reference must be declared.

Implementation Details

  • openqasm3 parses this as a DurationOf expression whose target is a statement list.
    Qasm3ExprEvaluator (src/pyqasm/expressions.py) has no branch for it, which is why the
    value reaches the declaration check as unresolved.
  • pyqasm already tracks per-qubit depth via QubitDepthNode in src/pyqasm/elements.py. The
    duration of a block is the same walk over a smaller statement list, so the existing
    bookkeeping is largely reusable.
  • loads() already accepts device_cycle_time, and dt units are handled in
    DURATION_UNITS (src/pyqasm/maps/expressions.py). A cycle-count result converts to a
    concrete duration through that path when a cycle time is available.
  • This composes with the symbolic representation proposed in the stretch arithmetic issue —
    both need a duration value that is not necessarily a concrete number. Landing a single
    representation that serves both is preferable to two partial ones.
  • The block must be visited in a child scope so that any temporary state it creates does not
    leak, and its instructions must not be emitted into the unrolled output — durationof
    measures the block, it does not execute it.
  • Tests: tests/qasm3/test_timing.pydurationof of a single gate, of a multi-gate block,
    of a block containing an explicit delay, its use inside delay[...], a dumps()
    round-trip of the symbolic form, and an undeclared qubit inside the block raising a
    ValidationError.

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 requestllm-assistedUsed LLMs to fine tune issue description.qasm3Related to openqasm3qasm3-coverageAdding support for qasm3 constructs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions