PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.25k stars 584 forks source link

Add scalar multiplication `__mul__` method to `SparseHamiltonian` #2558

Closed albi3ro closed 9 months ago

albi3ro commented 2 years ago

Suppose we create a Sparse Hamiltonian

import pennylane as qml
from scipy import sparse

sparse_mat = sparse.coo_matrix(np.array([[1.0, 2.0], [2.0, 3.0]]))

h = qml.SparseHamiltonian(sparse_mat, wires=[0])

If we multiply this by a float, we get back a Hamiltonian object pairing the SparseHamiltonian with a coefficient:

>>> new_h = -1.0 * h
>>> print(new_h)
(-1.0) [SparseHamiltonian0]

We should instead create a new SparseHamiltonian object with a new matrix. This way we can keep the benefits of having a SparseHamiltonian object.

This would be accomplished by defining the __mul__ and __rmul__ dunder methods in SparseHamiltonian located in pennylane/ops/qubit/observables.py

yass19 commented 2 years ago

Hello, I would like to work on this feature.

albi3ro commented 2 years ago

Sounds great @yass19 . Feel free to open a work-in-progress PR as you go or reach out to us with any questions. I also encourage you to read our guide on submitting a Pull Request.

Just to let you know we will be participating in Unitary Hack from June 3-17. Submission and acceptance of Pull Requests during that time will be eligible for swag and potential bounties.

antalszava commented 2 years ago

Hi @yass19, if interested in bounties as a follow-up to what Christina mentioned, see issues with the unitaryhack-bounty label.

tgag17 commented 2 years ago

Hi! Can I work on this issue?

antalszava commented 2 years ago

Hi @tgag17, sure :slightly_smiling_face: Feel free to reach out for help/when ready for the PR to be reviewed.

tgag17 commented 2 years ago

Hi. I am having some issues in pushing the code. Whenever I try to push the code to the origin its showing me an error that permission has been denied to give access and The requested URL returned error: 403. I understand this issue is due to the personal access token but I am not able to figure out the solution for this. Any kind of help would be great :) @antalszava

antalszava commented 2 years ago

Hi @tgag17, apologies for the very delayed response!

Did you try creating a fork for PennyLane and pushing there?

tgag17 commented 1 year ago

Hi @albi3ro @antalszava ! I am trying to retackle this issue. From the code given in the original post, I am not able to recreate the result. Instead of getting the result the code throws an error "Observable must be a scipy sparse csr_matrix.". Could you please confirm if the code should reproduce the result or not? I am also trying to create a new SparseHamiltonion object but to convert it to the csr format, the function is not accepting the input of (scalar)(SparseHamiltonion). Any suggestions on what can be done? Thanks!

antalszava commented 1 year ago

Hi @tgag17, apologies for the late reply. Changes in the master branch have made the original snippet outdated and also seem to have made this issue be resolved (cc @albi3ro):

import pennylane as qml
from scipy import sparse
import numpy as np

sparse_mat = sparse.csr_matrix(np.array([[1.0, 2.0], [2.0, 3.0]]))

h = qml.SparseHamiltonian(sparse_mat, wires=[0])

new_h = -1.0 * h
print(new_h)

(-1.0) [SparseHamiltonian0]

trbromley commented 9 months ago

@isaacdevlugt @albi3ro can this issue be closed now that #4828 is merged?