Add type-safe VecMap for slices indexed by newtypes#14910
Add type-safe VecMap for slices indexed by newtypes#14910alexanderivrii merged 2 commits intoQiskit:mainfrom
VecMap for slices indexed by newtypes#14910Conversation
|
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 16969142707Details
💛 - Coveralls |
0fefe86 to
1c4ccc7
Compare
ef46cd4 to
27e782b
Compare
27e782b to
a72f0d9
Compare
|
Sasha: this is next (with #15897). I think, after this, I have to look again in more detail at the next two PRs because it seems like one of them introduces a non-determinism or a platform dependency. This one is fine, though (without it, I for sure would have made mistakes in writing #14911). There'll be a minor merge conflict with #15897 - the solution will be to take the hunk from #15897 and ignore this PR. |
There is an edge case in release-valve handling where, if the shortest path is a single swap (but the heuristics have been chosen such that it is unselectable normally), it is possible for the release valve to cause _two_ gates to be routed. The previous handling of this path was written in quite a complicated manner, likely to be able to use `closest_node` by name from its discovery earlier in the release-valve process. Instead, it is easier to recognise that, given a single swap: * the two qubits cannot be part of the same gate, or the gate would have been routable _without_ the swap; * the `closest_node` must touch one of these qubits because of the the Dijkstra search; * therefore we can simply add any routable gate that touches _either_ qubit without risk of duplication or forgetting `closest_swap`.
a72f0d9 to
4d73b63
Compare
There was a problem hiding this comment.
This is a cool PR!
So basically previously we could write code like
my_data: Vec<f64> // should be indexed by physical qubits
v: VirtualQubit
my_data[v.index()] = ... and it would compile, but now we would write
my_data: VecMap<PhysicalQubit, f64>
v: VirtualQubit
my_data[v] = ... and it would not compile (preventing the error).
My only question is whether the new VecMap struct should be exposed to more of Qiskit and not local to sabre?
It could be. If you prefer, I can move it into the root of |
We don't need to move this for this PR at all, but would this be a natural fit for the new utils crate that you have added? |
|
The awkward bit about putting it in |
Sabre uses several objects that are logically maps from an index-like newtype (like
NodeIndexorPhysicalQubit) to some value, and are implemented as fixed-sliceVecs for lookup efficiency. The newtype provides type safety while it's in use, but we have to cast it away to index, which makes it easy to index slices with the wrong object.This introduces a
VecMapobject, which provides a (minimal) slice-like interface, but indexes using the relevant newtype.The current implementation of Sabre does not use this too much, but a refactoring of the layer structures will have them store one slice indexed by
PhysicalQubitand one byVirtualQubit, which are trivially easy to get switched (a frequent mistake that is the base reason those new types were introduced in the first place).