Skip to content
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

State that Evolution has matrix if target is Hamiltonian #4768

Merged
merged 4 commits into from
Nov 1, 2023

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Nov 1, 2023

We discovered a difference between default.qubit and default.qubit.legacy over their treatment of the evolution of hamiltonians:

dev = qml.device('default.qubit', wires=3)
# dev = qml.device('default.qubit.legacy', wires=3)  # this works!coeffs = [0.1, 0.2]
operators = [qml.PauliZ(0) @ qml.PauliZ(1), qml.PauliZ(0) @ qml.PauliZ(2)]
hamiltonian = qml.Hamiltonian(coeffs, operators)
​
@qml.qnode(dev)
def f():
    qml.evolve(hamiltonian, 0.5)
    return qml.density_matrix([0])
​
f()

Previously qml.evolve(hamiltonain, t) worked because Evolution was hardcoded as an accepted observable. This meant it became an accepted operation as well, even though it stated it didn't have a matrix.

In ScalarSymbolicOp, the parent for Evolution, we added a special logical pathway for the matrix if the target is a Hamiltonian, but we didn't include that special logical pathway for the has_matrix property.

This PR updates the has_matrix property for and allows the evolution of a hamiltonian to execute on default.qubit.

Warning:

This matrix is not backprop compatible.

dev = qml.device('default.qubit.legacy', wires=3)

coeffs = [0.1, 0.2]
operators = [qml.PauliZ(0) @ qml.PauliZ(1), qml.PauliZ(0) @ qml.PauliZ(2)]

@qml.qnode(dev)
def f(x):
    hamiltonian = qml.Hamiltonian(x, operators)
    qml.evolve(hamiltonian, 0.5)
    return qml.density_matrix([0])

qml.grad(f)(qml.numpy.array(coeffs))

Gives

AttributeError: 'ArrayBox' object has no attribute 'tocsr'

qml.dot and operator arithemetic does not have these problems.

Copy link

codecov bot commented Nov 1, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (48b26c0) 99.64% compared to head (48c550a) 99.64%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4768      +/-   ##
==========================================
- Coverage   99.64%   99.64%   -0.01%     
==========================================
  Files         381      381              
  Lines       34305    34048     -257     
==========================================
- Hits        34184    33926     -258     
- Misses        121      122       +1     
Files Coverage Δ
pennylane/ops/op_math/symbolicop.py 100.00% <100.00%> (ø)

... and 42 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@albi3ro albi3ro requested a review from a team November 1, 2023 19:20
Copy link
Contributor

@lillian542 lillian542 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! 🎉

tests/ops/op_math/test_symbolic_op.py Outdated Show resolved Hide resolved
albi3ro and others added 2 commits November 1, 2023 15:48
@albi3ro albi3ro enabled auto-merge (squash) November 1, 2023 19:49
@albi3ro albi3ro merged commit 8aee680 into master Nov 1, 2023
33 checks passed
@albi3ro albi3ro deleted the fix-scalar-symbolic-op-has-matrix branch November 1, 2023 20:04
@trbromley trbromley added the bug 🐛 Something isn't working label Nov 6, 2023
timmysilv pushed a commit that referenced this pull request Nov 8, 2023
We discovered a difference between `default.qubit` and
`default.qubit.legacy` over their treatment of the evolution of
hamiltonians:

```python
dev = qml.device('default.qubit', wires=3)
​
coeffs = [0.1, 0.2]
operators = [qml.PauliZ(0) @ qml.PauliZ(1), qml.PauliZ(0) @ qml.PauliZ(2)]
hamiltonian = qml.Hamiltonian(coeffs, operators)
​
@qml.qnode(dev)
def f():
    qml.evolve(hamiltonian, 0.5)
    return qml.density_matrix([0])
​
f()
```

Previously `qml.evolve(hamiltonain, t)` worked because `Evolution` was
hardcoded as an accepted observable. This meant it became an accepted
operation as well, even though it stated it didn't have a matrix.

In `ScalarSymbolicOp`, the parent for `Evolution`, we added a special
logical pathway for the matrix if the target is a Hamiltonian, but we
didn't include that special logical pathway for the `has_matrix`
property.

This PR updates the `has_matrix` property for and allows the evolution
of a hamiltonian to execute on `default.qubit`.

**Warning:**

This matrix is not backprop compatible.

```
dev = qml.device('default.qubit.legacy', wires=3)

coeffs = [0.1, 0.2]
operators = [qml.PauliZ(0) @ qml.PauliZ(1), qml.PauliZ(0) @ qml.PauliZ(2)]

@qml.qnode(dev)
def f(x):
    hamiltonian = qml.Hamiltonian(x, operators)
    qml.evolve(hamiltonian, 0.5)
    return qml.density_matrix([0])

qml.grad(f)(qml.numpy.array(coeffs))
```
Gives
```
AttributeError: 'ArrayBox' object has no attribute 'tocsr'
```

`qml.dot` and operator arithemetic does not have these problems.

---------

Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
timmysilv added a commit that referenced this pull request Nov 8, 2023
**Context:**
These three bugs were identified as good to be released in a bugfix.

**Description of the Change:**
Cherry-pick the 3 commits to master onto this branch for the 0.33.1
bugfix release

**Benefits:**
Fixed bugs pushed out!

**Possible Drawbacks:**
Extra work in a critical time.

**Related GitHub Issues:**
#4768, #4781, #4787

---------

Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
@albi3ro
Copy link
Contributor Author

albi3ro commented Nov 24, 2023

[sc-50818]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants