question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add scalar multiplication `__mul__` method to `SparseHamiltonian`

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
antalszavacommented, Jul 15, 2022

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

0reactions
antalszavacommented, Nov 22, 2022

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]

Read more comments on GitHub >

github_iconTop Results From Across the Web

A sparse matrix-vector multiplication based algorithm for ...
We present an efficient sparse matrix vector (SpMV) based method to com- pute the density matrix P from a given Hamiltonian in electronic ......
Read more >
Exponential improvement in precision for simulating sparse ...
The first efficient algorithm for sparse Hamiltonian simulation was due to Aharonov and Ta- ... and π by adding the identity matrix and...
Read more >
The Distributed Block-Compressed Sparse Row Library
way, an autotuning library for small matrix multiplications, ... both the density matrix and the Hamiltonian matrix become sparse but the.
Read more >
Source code for quspin.operators.hamiltonian_core
`fun`: function object which multiplies the matrix or operator given in the ... function does the matrix multiplication with the state(s) and Hamiltonian...
Read more >
parallel sparse matrix-matrix multiplication and indexing ...
Our novel approach to. SpRef uses SpGEMM as its key subroutine, which regularizes the computation and data access patterns; conversely, applying SpGEMM to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found