-
Notifications
You must be signed in to change notification settings - Fork 30
Fix the creation of Permutation symbols #377
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
Open
arcondello
wants to merge
1
commit into
dwavesystems:main
Choose a base branch
from
arcondello:fix/Permutation-symbol-construction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -663,10 +663,6 @@ cdef class ArgSort(ArraySymbol): | |
| _register(ArgSort, typeid(cppArgSortNode)) | ||
|
|
||
|
|
||
| cdef bool _empty_slice(object slice_) noexcept: | ||
| return slice_.start is None and slice_.stop is None and slice_.step is None | ||
|
|
||
|
|
||
| cdef class AdvancedIndexing(ArraySymbol): | ||
| """Advanced indexing. | ||
|
|
||
|
|
@@ -711,35 +707,50 @@ cdef class AdvancedIndexing(ArraySymbol): | |
|
|
||
| array = next(self.iter_predecessors()) | ||
|
|
||
| if ( | ||
| isinstance(array, Constant) | ||
| and array.ndim() == 2 | ||
| and array.shape()[0] == array.shape()[1] # square matrix | ||
| and self.ptr.indices().size() == 2 | ||
| and isinstance(index, tuple) | ||
| and len(index) == 2 | ||
| ): | ||
| i0, i1 = index | ||
| if (perm := self._check_permutation(index)) is not None: | ||
| return perm | ||
|
|
||
| # check the [x, :][:, x] case | ||
| if (isinstance(i0, slice) and _empty_slice(i0) and | ||
| isinstance(i1, ArraySymbol) and | ||
| holds_alternative[cppArrayNodePtr](self.ptr.indices()[0]) and | ||
| get[cppArrayNodePtr](self.ptr.indices()[0]) == (<ArraySymbol>i1).array_ptr and | ||
| holds_alternative[cppSlice](self.ptr.indices()[1])): | ||
| return super().__getitem__(index) | ||
|
|
||
| return Permutation(array, i1) | ||
| cdef object _check_permutation(self, index): | ||
| """Return a Permutation symbol if the indexing the symbol results in | ||
| a permutation, otherwise return None. | ||
| """ | ||
|
|
||
| # check the [:, x][x, :] case | ||
| if (isinstance(i1, slice) and _empty_slice(i1) and | ||
| isinstance(i0, ArraySymbol) and | ||
| holds_alternative[cppArrayNodePtr](self.ptr.indices()[1]) and | ||
| get[cppArrayNodePtr](self.ptr.indices()[1]) == (<ArraySymbol>i0).array_ptr and | ||
| holds_alternative[cppSlice](self.ptr.indices()[0])): | ||
| # The indexed array must be a Constant square matrix | ||
| array = next(self.iter_predecessors()) | ||
| if not isinstance(array, Constant): | ||
| return None | ||
| if array.ndim() != 2 or array.shape()[0] != array.shape()[1]: | ||
| return None | ||
|
|
||
| return Permutation(array, i0) | ||
| # The total operation must of the form A[i0, i1][i2, i3] | ||
|
|
||
| i0, i1 = self._iter_indices() | ||
| i2, i3 = index if isinstance(index, tuple) else (index, slice(None, None, None)) | ||
|
|
||
| # It also must be of the form A[outer, inner][inner, outer] | ||
|
|
||
| if ( | ||
| isinstance(i0, slice) and isinstance(i3, slice) # outer is a slice | ||
| and i0 == i3 and i0 == slice(None) # and those slices are empty | ||
| and isinstance(i1, ArraySymbol) and isinstance(i2, ArraySymbol) # inner is an array | ||
| and i1.id() == i2.id() # and those arrays are the same array | ||
| and i1.shape() == (array.shape()[0],) # and the shape of the array is correct | ||
| ): | ||
| return Permutation(array, i1) | ||
|
|
||
| if ( | ||
| isinstance(i1, slice) and isinstance(i2, slice) # inner is a slice | ||
| and i1 == i2 and i1 == slice(None) # and those slices are empty | ||
| and isinstance(i0, ArraySymbol) and isinstance(i3, ArraySymbol) # outer is an array | ||
| and i0.id() == i3.id() # and those arrays are the same array | ||
| and i0.shape() == (array.shape()[0],) # and the shape of the array is correct | ||
| ): | ||
| return Permutation(array, i0) | ||
|
|
||
| return None | ||
|
|
||
| return super().__getitem__(index) | ||
|
|
||
| @classmethod | ||
| def _from_symbol(cls, Symbol symbol): | ||
|
|
@@ -791,6 +802,17 @@ cdef class AdvancedIndexing(ArraySymbol): | |
|
|
||
| zf.writestr(directory + "indices.json", encoder.encode(indices)) | ||
|
|
||
| def _iter_indices(self): | ||
| for variant in self.ptr.indices(): | ||
| if holds_alternative[cppSlice](variant): | ||
| cppslice = <cppSlice>get[cppSlice](variant) | ||
| yield slice(None) | ||
| elif holds_alternative[cppArrayNodePtr](variant): | ||
| array_ptr = <cppArrayNodePtr>get[cppArrayNodePtr](variant) | ||
| yield symbol_from_ptr(self.model, array_ptr) | ||
| else: | ||
| raise RuntimeError("unexpected variant contents") | ||
|
|
||
| cdef cppAdvancedIndexingNode* ptr | ||
|
|
||
| _register(AdvancedIndexing, typeid(cppAdvancedIndexingNode)) | ||
|
|
@@ -3474,10 +3496,7 @@ cdef class Permutation(ArraySymbol): | |
| >>> type(p) | ||
| <class 'dwave.optimization.symbols.Permutation'> | ||
| """ | ||
| def __init__(self, Constant array, ListVariable x): | ||
| # todo: Loosen the types accepted. But this Cython code doesn't yet have | ||
| # the type heirarchy needed so for how we specify explicitly | ||
|
|
||
| def __init__(self, Constant array, ArraySymbol x): | ||
|
Member
Author
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. This is the main fix, but there were a few other edge cases as well |
||
| if array.model is not x.model: | ||
| raise ValueError("array and x do not share the same underlying model") | ||
|
|
||
|
|
||
5 changes: 5 additions & 0 deletions
5
releasenotes/notes/fix-Permutation-symbol-construction-c80264f228458179.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| fixes: | ||
| - | | ||
| Fix indexing operations sometimes raising an error when it should | ||
| create a ``Permutation`` symbol. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Assuming there are only two indices here breaks the case of
A[:, x][(x,)]and makes a somewhat more confusing error forA[:, x][x, :, :].