Skip to content

qasm3.dumps_experimental gets some delay units wrong #16097

@ihincks

Description

@ihincks

Environment

  • Qiskit version: 2.4.1
  • Python version:
  • Operating system:

This issue was a collaboration between me and Claude 4.6


What is happening?

qasm3.dumps_experimental produces wrong output for Delay gates with unit='ms' or unit='ps'.
There are two independent bugs:

  1. ms label is emitted as us — a copy-paste error in ast.rs means DurationUnit::Millisecond is serialized as "us" instead of "ms". The numeric value is unchanged, so the emitted duration is 1000× too small.

  2. ps → ns conversion factor is invertedexporter.rs converts picoseconds to nanoseconds by multiplying by 1000 instead of dividing. Since 1 ps = 0.001 ns, Delay(1, 'ps') is emitted as delay[1000ns]10⁶× too large.


How can we reproduce the issue?

from qiskit import QuantumCircuit
from qiskit import qasm3
import warnings

def emit(dur, unit):
    qc = QuantumCircuit(1)
    qc.delay(dur, 0, unit=unit)
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        out = qasm3.dumps_experimental(qc)
    line = next(l for l in out.splitlines() if "delay" in l)
    print(f"Delay({dur}, '{unit}') => {line!r}")

emit(1, "ms")   # expect delay[1ms],    got delay[1us]
emit(1, "ps")   # expect delay[0.001ns], got delay[1000ns]

Output:

Delay(1, 'ms') => '  delay[1us] q[0];'
Delay(1, 'ps') => '  delay[1000ns] q[0];'

What should happen?

  • Delay(1, 'ms') should emit delay[1ms] (not delay[1us]).
  • Delay(1, 'ps') should emit delay[0.001ns] (not delay[1000ns]).

More generally, every Delay unit should round-trip through dumps_experimental with the correct label and a numerically correct value.


Any suggestions?

There are two one-line fixes:

Bug 1crates/qasm3/src/ast.rs (around line 141):

// Before (wrong):
DurationUnit::Millisecond => "us",

// After:
DurationUnit::Millisecond => "ms",

Bug 2crates/qasm3/src/exporter.rs (around line 1216).
The ps → ns conversion currently multiplies by 1000; it should divide:

// Before (wrong — 1 ps = 0.001 ns, not 1000 ns):
value: duration * 1000.0, unit: Nanosecond

// After:
value: duration / 1000.0, unit: Nanosecond

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomersmod: qasm3Related to OpenQASM 3 import or export

    Type

    No type

    Projects

    Status

    Tagged but unassigned

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions