Skip to content

Commit

Permalink
Implement arithmetic on SparseObservable (#13298)
Browse files Browse the repository at this point in the history
* Implement arithmetic on `SparseObservable`

The simple arithmetic operators of addition, subtraction,
multiplication, division and tensor product are in line with other
`quantum_info` objects, including coercion from other objects (though
better behaved on failed coercions, in many cases).

Where appopriate, in-place operations will re-use the existing
allocations.  The tensor product cannot really be done in place, so it
doesn't define a special `__rxor__` operator, but it inherits the
standard Python behaviour of this being derived from `__xor__`.

There are further mathematical operations to add around composition and
evolution, and to apply a `TranspileLayout` to the observable.  All of
these, in the `quantum_info` style either directly deal with a layout,
or take a `qargs` argument that is effectively a sublayout for the
right-hand side of the operation.  These will be added in a follow-up.

* Match `x + x` behaviour to `x += x`

This puts the `self is other` check into the `__add__` and `__sub__`
methods so that the behaviour of `x + x` is consistent with `x += x`,
with regards to the addition being done as a scalar multiplication
instead of concatentation.  Both forms are mathematically correct, but
this makes sure they're aligned.
  • Loading branch information
jakelishman authored Oct 29, 2024
1 parent 01642a8 commit ddf93ab
Show file tree
Hide file tree
Showing 2 changed files with 1,493 additions and 44 deletions.
Loading

0 comments on commit ddf93ab

Please sign in to comment.