Skip to content
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 Place union and lower AST to it during checking #289

Merged
merged 16 commits into from
Jul 23, 2024

Conversation

mark-koch
Copy link
Collaborator

@mark-koch mark-koch commented Jul 8, 2024

See #295 for context.

The tests are at #293

@mark-koch mark-koch marked this pull request as ready for review July 8, 2024 16:32
@mark-koch mark-koch requested a review from a team as a code owner July 8, 2024 16:32
@mark-koch mark-koch requested review from aborgna-q and ss2165 and removed request for a team and aborgna-q July 8, 2024 16:32
from guppylang.definition.struct import StructField


#: A "place" is a description for a storage location of a local value that users
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#: A "place" is a description for a storage location of a local value that users
#: A "place" is a description for a storage location of a local value that users

# For now we complain if someone tries to assign to something that
# is not a place (e.g. `f().a = 4`)
raise GuppyError(
"Assigning to this expression is not supported yet", value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth more details on why not, otherwise I don't necessarily know how to fix it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant more details in the error message

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops yeah I misunderstood. I added a fix suggestion to the error message

"Mutation of classical fields is not supported yet", lhs
)
place = FieldAccess(value.place, struct_ty.field_dict[attr], lhs)
return with_loc(lhs, PlaceNode(place=place))

# The only other thing we support right now are tuples
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slightly outdated comment

@mark-koch mark-koch requested a review from ss2165 July 10, 2024 08:34
Copy link
Member

@ss2165 ss2165 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More detail in the error message please!

@mark-koch mark-koch requested a review from ss2165 July 15, 2024 08:21
Base automatically changed from structs/generic-analysis to feat/struct-fields July 23, 2024 08:29
@mark-koch mark-koch merged commit acf1242 into feat/struct-fields Jul 23, 2024
@mark-koch mark-koch deleted the structs/checking branch July 23, 2024 08:32
github-merge-queue bot pushed a commit that referenced this pull request Jul 24, 2024
This is the feature branch for enabling struct field access and mutation
of linear fields.

Closes #276, closes #280, and closes #156. 

The difficult bit is that we want to track linearity of individual
struct fields separately (akin to Rust's partial moves):

```python
@guppy.struct
class MyStruct:
   q1: qubit
   q2: qubit
   x: int

def main(s: MyStruct):
   q = h(s.q1)
   t = h(s.q2)    # Using s.q2 is fine, only s.q1 has been used
   y = s.x + s.x  # Classical fields can be used multiple times
   use(s)         # Error: Linear fields of s have already been used
   ...
```

This is the plan:

* We introduce a new notion called `Place`: A place is a description for
a storage location of a local value that users can refer to in their
program. Roughly, these are values that can be lowered to a static wire
within the Hugr and are tracked separately when checking linearity.
* For the purposes of this PR, a place is either a local variable or a
field projection of another place that holds a struct. I.e. places are
paths `a.b.c.d` of zero or more nested struct accesses. In the future,
indexing into an array etc will also become a place.
* During type checking, we figure out which AST nodes correspond to
places and annotate them as such
* For linearity checking, we run a liveness analysis pass that tracks
usage and assignment of places across the CFG. This way, linearity of
different struct fields is tracked individually.
* When compiling to Hugr, we keep track of a mapping from places to
wires/ports


Tracked PRs:

* #288: Precursor PR to generalise our program analysis framework to run
on places in the future.
* #289: Adds the `Place` type and implements the type checking logic to
turn `ast.Name` and `ast.Attribute` nodes into places.
* #290: Update linearity checker to operate on places instead of
variables
* #291: Lower places to Hugr
* #292: Some missing pieces to handle nested functions correctlt
* #293
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants