-
Notifications
You must be signed in to change notification settings - Fork 2
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: Hugr lowering for array indexing and add integration tests #422
Conversation
39717c6
to
1755c5b
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## feat/arrays #422 +/- ##
===============================================
+ Coverage 92.48% 92.63% +0.14%
===============================================
Files 48 48
Lines 5256 5295 +39
===============================================
+ Hits 4861 4905 +44
+ Misses 395 390 -5 ☔ View full report in Codecov by Sentry. |
1755c5b
to
9c390ec
Compare
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.
Just some small nits and questions
args=[arg.to_hugr() for arg in self.type_args], | ||
) | ||
node = self.builder.add_op(op, array, idx, elem) | ||
return CallReturnWires(regular_returns=[], inout_returns=[node]) |
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.
nit: clearer to be explicit
return CallReturnWires(regular_returns=[], inout_returns=[node]) | |
return CallReturnWires(regular_returns=[], inout_returns=[node[0]) |
|
||
@guppy(module) | ||
def main(ss: array[S, 10]) -> array[S, 10]: | ||
# This will panic at runtime :( |
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.
what will the runtime issue be?
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.
See the explanation in #415
|
||
@guppy(module) | ||
def main(qs: array[qubit, 42] @inout, i: int) -> None: | ||
foo(qs[i]) |
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.
both this and the example above would fail if the argument to foo
wasn't inout right?
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 👍
guppylang/compiler/expr_compiler.py
Outdated
if subscript.item.id not in self.dfg.locals: | ||
self.dfg[subscript.item] = self.visit(subscript.item_expr) |
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.
just a side-thought but it is a bit confusing to read that you check for existence of item.id
in dfg.locals
but then just assign to dfg[item]
. Does dfg
need a__contains__
?
This is the feature branch for array indexing. Tracked by #253. The goal is to allow array indexing in `@inout` positions that ignores linearity constraints in the indices: ```python qs: array[qubit, 42] cx(qs[0], qs[1]) # Ok cx(qs[0], qs[0]) # Compiles, but will panic at runtime q = qs[0] # Error: Indexing only allowed in inout position ``` This is achieved by lowering `array[qubit]` to `array[qubit | None]` and making `array.__getitem__` and `array.__setitem__` swap in `None` whenever a qubit is taken out or put back in. The functions `array.__getitem__` and `array.__setitem__` take the array as `@inout`, so it suffices to apply the following desugaring: ```python cx(qs[expr1], qs[expr2]) # Desugars to idx1 = expr1 idx2 = expr2 tmp1 = qs.__getitem__(idx1) tmp2 = qs.__getitem__(idx2) cx(tmp1, tmp2) qs.__setitem__(tmp1, idx1) qs.__setitem__(tmp2, idx2) ``` This also means that arrays containing structs might behave slightly unexpectedly: ```python @guppy.struct class QubitPair: q1: qubit q2: qubit ps: array[QubitPair, 42] cx(ps[0].q1, ps[0].q2) # Panics at runtime :( # Since it desugars to idx1 = 0 idx2 = 0 tmp1 = ps.__getitem__(idx1) tmp2 = ps.__getitem__(idx2) # Panic: Struct at index 0 has already been replaced with None cx(tmp1.q1, tmp2.q2) ... ``` To solve this, we need to change the Hugr lowering of arrays to structually replace any occurrence of `qubit` with `qubit | None`, i.e. instead of doing `array[QubitPair | None]`, we would need to do `array[tuple[qubit | None, qubit | None]]`. I'll leave this to a future PR in case we are interested in that. The following feature PRs target this branch: * #420 * #421 * #422 * #447
🤖 I have created a release *beep* *boop* --- ## [0.10.0](v0.9.0...v0.10.0) (2024-09-11) ### ⚠ BREAKING CHANGES * Bumped the `hugr` dependency to `0.8.0` * `GuppyModule.load` no longer loads the content of modules but instead just brings the name of the module into scope. Use `GuppyModule.load_all` to get the old behaviour. * Removed `guppylang.hugr_builder.hugr.Hugr`, compiling a module returns a `hugr.Package` instead. ### Features * Add `__version__` field to guppylang ([#473](#473)) ([b996c62](b996c62)) * Add angle type ([#449](#449)) ([12e41e0](12e41e0)) * Add array literals ([#446](#446)) ([a255c02](a255c02)) * Add equality test for booleans ([#394](#394)) ([dd702ce](dd702ce)), closes [#363](#363) * Add pi constant ([#451](#451)) ([9d35a78](9d35a78)) * Add qualified imports and make them the default ([#443](#443)) ([553ec51](553ec51)) * Allow calling of methods ([#440](#440)) ([5a59da3](5a59da3)) * Allow imports of function definitions and aliased imports ([#432](#432)) ([e23b666](e23b666)) * Array indexing ([#415](#415)) ([2199b48](2199b48)), closes [#421](#421) [#422](#422) [#447](#447) * Inout arguments ([#311](#311)) ([060649b](060649b)), closes [#315](#315) [#316](#316) [#349](#349) [#344](#344) [#321](#321) [#331](#331) [#350](#350) [#340](#340) [#351](#351) * range() with single-argument ([#452](#452)) ([d05f369](d05f369)) * Skip checking of redefined functions ([#457](#457)) ([7f9ad32](7f9ad32)) * Support `nat`/`int` ↔ `bool` cast operations ([#459](#459)) ([3b778c3](3b778c3)) * Use `hugr-cli` for validation ([#455](#455)) ([1d0667b](1d0667b)) * Use cell name instead of file for notebook errors ([#382](#382)) ([d542601](d542601)) * Use the hugr builder ([536abf9](536abf9)) ### Bug Fixes * Fix and update demo notebook ([#376](#376)) ([23b2a15](23b2a15)) * Fix linearity checking bug ([#441](#441)) ([0b8ea21](0b8ea21)) * Fix struct definitions in notebooks ([#374](#374)) ([b009465](b009465)) ### Documentation * Update readme, `cargo build` instead of `--extra validation` ([#471](#471)) ([c2a4c86](c2a4c86)) ### Miscellaneous Chores * Update hugr to `0.8.0` ([#454](#454)) ([b02e0d0](b02e0d0)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Closes #418. See #415 for context.
Note that
array.__getitem__
andarray.__setitem__
are currently lowered to dummy Hugr ops. The "swapping withNone
" logic explained in #415 will follow in a future PR (see #419)