Commit 8ec6507
authored
Soquet to _Soquet, _QVar, Soquet and QVar (#1831)
In Qualtran, we compose quantum operations into larger operations by
connecting the outputs of a predecessor bloq to the input "ports" of a
successor bloq. We've used the `Soquet` frozen attrs class to represent
these nodes in the compute graph, which address the "ports" of a
particular instance of the bloq.
Previously, the immutable `Soquet` object represented both the "quantum
variables" being passed around during bloq building as well as the nodes
of the compute graph. With this PR, these concerns are separated. The
compute graph nodes are frozen dataclasses of type `_Soquet`, and the
quantum variables are *mutable* objects of type `_QVar`. We will put
additional helper attributes and methods onto `_QVar` to assist in bloq
building.
For backwards compatibility, the `Soquet` name is now a
`typing.Protocol` that encapsulates the duck-typing behavior of
`_Soquet` and `_QVar`. Bloqs in the wild should not have to update the
type annotations in `build_composite_bloq`
with this backwards compatibilty typing shim.
`isinstance(..., Soquet)` checks will emit a deprecation warning and
return True for *either* `_Soquet` or `_QVar`.
If you're using isinstance(soq, Soquet) to determine whether an item is
a single object
or an ndarray of those objects, use `BloqBuilder.is_single(x)` or
`BloqBuilder.is_ndarray(x)`. See the documentation in `QVarT` for an
example.
If you're developing library functionality, you can port isinstance
checks to either `_Soquet` or `QVar` as appropriate.
----
*Backwards compatibility note:* Despite the fundamental change to the
data model, this PR is designed to be runtime-backwards compatible with
deprecation warnings. The way I've accomplished this is through some
dark Python incantations, so no guarantees. Please send reports if this
breaks something.
Note that type checkers like `mypy` will likely still complain about
things even if the code runs fine without modification following this
PR.
----
### High-priority deprecations
- Please avoid constructing `Soquet`s yourself. You really shouldn't
have been doing this. `_Soquet` is now "private"
- If you're copying or modifying `CompositeBloq` by iterating over
connections and using `map_soqs`, please urgently upgrade your code to
use the new idiom where you must initialize the mapping with
`bb.initial_soq_map`. The shim I put in is brittle. #17421 parent 80364b6 commit 8ec6507
24 files changed
Lines changed: 601 additions & 308 deletions
File tree
- qualtran
- _infra
- bloqs
- bookkeeping
- mcmt
- cirq_interop
- drawing
- l1
- qref_interop
- serialization
- simulation
- tensor
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
49 | 50 | | |
50 | 51 | | |
| 52 | + | |
| 53 | + | |
51 | 54 | | |
52 | 55 | | |
53 | 56 | | |
| |||
84 | 87 | | |
85 | 88 | | |
86 | 89 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 90 | + | |
95 | 91 | | |
96 | 92 | | |
97 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
22 | 30 | | |
23 | 31 | | |
24 | 32 | | |
25 | 33 | | |
26 | 34 | | |
27 | 35 | | |
28 | 36 | | |
29 | | - | |
| 37 | + | |
30 | 38 | | |
31 | 39 | | |
32 | 40 | | |
33 | 41 | | |
34 | | - | |
| 42 | + | |
35 | 43 | | |
36 | 44 | | |
37 | 45 | | |
| |||
57 | 65 | | |
58 | 66 | | |
59 | 67 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | 68 | | |
64 | 69 | | |
65 | 70 | | |
66 | 71 | | |
67 | 72 | | |
68 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
69 | 77 | | |
70 | 78 | | |
71 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
346 | | - | |
| 346 | + | |
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
| |||
512 | 512 | | |
513 | 513 | | |
514 | 514 | | |
515 | | - | |
516 | | - | |
| 515 | + | |
| 516 | + | |
517 | 517 | | |
518 | 518 | | |
519 | 519 | | |
| |||
0 commit comments