-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add source mapping for calibration expansion #370
Changes from 1 commit
1a1e8a6
3a319f1
e13abf7
917ba23
bf246b3
277105d
b35f8fe
1bec330
ec90415
2ecf658
8446b61
feaa61c
00119a6
1965440
8906a76
645e1a2
cafe117
3216ef3
ac63749
4b61fc8
07ae1d3
78df471
25bd576
f64aa8a
d3abce6
c97dcca
8ffb15a
2ca8eea
8f91186
56476ca
5d9f759
59daee1
7a5972c
5d31908
de94d05
5c05993
a879779
a298437
9a3b453
022900a
f72ae40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,70 @@ | ||
""" | ||
The `quil.program` module contains classes for constructing and representing a Quil program. | ||
|
||
# Examples | ||
|
||
## Source Mapping for Calibration Expansion | ||
|
||
```py | ||
import inspect | ||
antalsz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
program_text = inspect.cleandoc( | ||
\"\"\" | ||
DEFCAL X 0: | ||
Y 0 | ||
|
||
DEFCAL Y 0: | ||
Z 0 | ||
|
||
X 0 # This instruction is index 0 | ||
Y 0 # This instruction is index 1 | ||
\"\"\" | ||
) | ||
|
||
# First, we parse the program and expand its calibrations | ||
program = Program.parse(program_text) | ||
expansion = program.expand_calibrations_with_source_map() | ||
source_map = expansion.source_map() | ||
|
||
# This is what we expect the expanded program to be. X and Y have been replaced by Z | ||
expected_program_text = inspect.cleandoc( | ||
\"\"\" | ||
DEFCAL X 0: | ||
Y 0 | ||
|
||
DEFCAL Y 0: | ||
Z 0 | ||
|
||
Z 0 # This instruction is index 0 | ||
Z 0 # This instruction is index 1 | ||
\"\"\" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this ought to have an assert. That raises questions of the precise whitespace, I know. Could we parse them both to Quil and assert the parsed programs are equal? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the test did, I left it out here for readability but have restored it now |
||
|
||
# In order to discover _which_ calibration led to each Z in the resulting program, we | ||
# can interrogate the expansion source mapping: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this section a lot, but I found the specific comments a bit confusing. Perhaps we could revise this first comment to something like "…can interrogate the expansion source mapping. For instance, The X at index 0 should have been replaced with a Z at index 0. Here's how we could confirm that: First, we…". As it stands, the comment on line 46 made me think that we were also going to check the Y at index 1. (I think adding that would probably be redundant.) Another possibility would be to drop the ellipses on lines 51 and 63; they're not following on from a previous sentence. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, fixed |
||
|
||
# The X at index 0 should have been replaced with a Z at index 0: | ||
|
||
# ...first, we list the calibration expansion targets for that first instruction... | ||
targets = source_map.list_targets_for_source_index(0) | ||
|
||
# ...then we extract the expanded instruction. | ||
# If the instruction had _not_ been expanded (i.e. there was no matching calibration), then `as_expanded()` would return `None`. | ||
expanded = targets[0].as_expanded() | ||
|
||
# ...This line shows how that `X 0` was expanded into instruction index 0 (only) within the expanded program. | ||
# The end of the range is exclusive. | ||
assert expanded.range() == range(0, 1) | ||
|
||
# We can also map instructions in reverse: given an instruction index in the expanded program, we can find the source index. | ||
# This is useful for understanding the provenance of instructions in the expanded program. | ||
sources = source_map.list_sources_for_target_index(1) | ||
|
||
# ... in this case, the instruction was expanded from the source program at index 1. | ||
assert sources == [1] | ||
``` | ||
""" | ||
|
||
from typing import Callable, Dict, FrozenSet, List, Optional, Sequence, Set, Union, final | ||
|
||
import numpy as np | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this snippet run and tested? It would be nice if it were but I don't know enough about Python to tell if that's happening/how easy it would be to have that happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it's a test case, although not tested in situ unfortunately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a pointer to the test case it corresponds to?