Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.05k stars 2.33k forks source link

Support sparse matrix handling in `qiskit.primitives` #10014

Open Cryoris opened 1 year ago

Cryoris commented 1 year ago

What should we add?

It would be great to support sparse matrices in the primitives and derived algorithms (like the gradients). Since they are built to take quantum_info objects as inputs, one option would be to add a SparseOperator that acts like the quantum_info.Operator, but is based on sparse matrices instead of dense operators.

The SparsePauliOp does not satisfy this need, as (1) the conversion to a sparse matrix representation is still expensive and (2) computing expectation values based on the matrix representation can be more efficient than on each of the paulis individually.

ikkoham commented 1 year ago

Need a general Sparse matrix? If you need a diagonal matrix that includes the projection for example, then I agree.

Cryoris commented 1 year ago

The idea would be to support general sparse matrices, not just diagonal ones. As Hamiltonians coming from a quantum mechanical systems are more mostly sparse, that would be widely applicable 🙂 but projectors would of course also be included.

ikkoham commented 1 year ago

True, but most of them are linear sums of Paulis (So SparsePauliOp is enough). I think it may be difficult to get the measurement basis from the general sparse matrix. I am concerned that this would be a bottleneck in the calculation.

Cryoris commented 1 year ago

But even for a linear sum of Paulis the evaluation can be terribly slow, especially for VQE or gradient calculations (or Statevector.expectation_value I would expect). I could also do a quick benchmark to be sure 🙂

What would be the issue of getting the measurement basis? Note that this class would be designated for working with simulators, not with backends (of course it should be compatible, but it's not the main goal).