rigetti / quil-rs

Quil Parser & Program Builder
https://rigetti.github.io/quil-rs/
Apache License 2.0
20 stars 9 forks source link

Expand calibrations with source mapping #366

Closed kalzoo closed 3 weeks ago

kalzoo commented 6 months ago

Currently, calibration expansion happens somewhat opaquely: a Vec<Instruction> goes in, and another Vec<Instruction> comes out. In complex programs, it can be hard for a user to discern which post-calibration instructions originated from which pre-calibration instructions.

This can be fixed with a source map. Perhaps it can be structured like this:

struct SourceMap<SourceIndex, TargetIndex> {
    entries: Vec<SourceMapEntry<SourceIndex, TargetIndex>>
}

struct SourceMapEntry<Label, SourceIndex, TargetIndex> {
    /// A description of the mapping, such the calibration used to perform the expansion
    label: Label,

    source_location: Vec<SourceIndex>,
    target_location: Vec<TargetIndex>,
}

these are generic over *Index in order to be re-used for other mappings, such as in parsing a program (mapping nom::locate information such as line and column span). Label, likewise, would be a different type in these situations:

This type would be made available for other libraries to use as well, such as compilers which consume Quil.

Finally, SourceMaps should be able to be folded, such that a program can undergo multiple transformations in series and emit a single mapping from the original text down to the final artifact.