dwavesystems / dwavesystems/dimod
BQM.as_vartype and SampleSet.as_vartype or similar
- Dominant language
- Python
- Stars
- 143
- Forks
- 91
- Avg merge
- 1h 24m
- Merged PRs (30d)
- 3
Description
`BQM.change_vartype` and `SampleSet.change_vartype` either always copy or always act in place. It would be useful to be able to cast the BQM/SampleSet to an appropriate vartype, _copying only in the case that they don't match_. It would be similar behavior to
```
if bqm.vartype is dimod.SPIN:
spin_bqm = bqm
else:
spin_bqm = bqm.change_vartype(dimod.SPIN, inplace=False)
```
This could be accomplished with a new method
```
bqm = bqm.as_vartype(dimod.SPIN, copy=False)
```
as inspired by [ndarray.astype](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html#numpy.ndarray.astype). But this is fairly redundant with the existing method and it might be confused with the existing [as_vartype](https://github.com/dwavesystems/dimod/blob/e6b0ea7e8bf614bc532bb85337aeb0060215fc78/dimod/vartypes.py#L80) function.
An alternative would be adding a new keyword argument to `.change_vartype`, e.g.
```
bqm = bqm.change_vartype(dimod.SPIN, inplace=False, copy=False)
```
though I think `copy` is a pretty confusing name.
Contributor guide
Assessment
This issue has not been assessed yet.