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

Support multiplication of SparsePauliOp with Parameter #10185

Closed
kevinsung opened this issue May 31, 2023 · 4 comments
Closed

Support multiplication of SparsePauliOp with Parameter #10185

kevinsung opened this issue May 31, 2023 · 4 comments
Labels
mod: quantum info Related to the Quantum Info module (States & Operators) type: feature request New feature or request

Comments

@kevinsung
Copy link
Contributor

What should we add?

from qiskit.circuit import Parameter
from qiskit.quantum_info import SparsePauliOp

param = Parameter("a")
op = SparsePauliOp("X")
param * op
---------------------------------------------------------------------------
QiskitError                               Traceback (most recent call last)
Cell In[5], line 6
      4 param = Parameter("a")
      5 op = SparsePauliOp("X")
----> 6 param * op

File ~/projects/qiskit-terra/qiskit/quantum_info/operators/mixins/multiply.py:36, in MultiplyMixin.__rmul__(self, other)
     35 def __rmul__(self, other):
---> 36     return self._multiply(other)

File ~/projects/qiskit-terra/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py:370, in SparsePauliOp._multiply(self, other)
    368 def _multiply(self, other):
    369     if not isinstance(other, Number):
--> 370         raise QiskitError("other is not a number")
    371     if other == 0:
    372         # Check edge case that we deleted all Paulis
    373         # In this case we return an identity Pauli with a zero coefficient
    374         paulis = PauliList.from_symplectic(
    375             np.zeros((1, self.num_qubits), dtype=bool),
    376             np.zeros((1, self.num_qubits), dtype=bool),
    377         )

QiskitError: 'other is not a number'
@kevinsung kevinsung added the type: feature request New feature or request label May 31, 2023
@woodsp-ibm woodsp-ibm added the mod: quantum info Related to the Quantum Info module (States & Operators) label May 31, 2023
@SimoneGasperini
Copy link
Contributor

SimoneGasperini commented Jun 4, 2023

Hi @kevinsung! I think that the development of this feature and, more in general, the support for symbolic computation within the qiskit.quantum_info module could be very interesting. However, I already had a short but quite exhaustive conversation with @jakelishman on thread #9513 about this and the conclusion was that enabling symbolic computation in Qiskit would mean being able to properly extend all kind of operations already supported for complex/float numbers to symbolic expressions as well: this could very complicated and it goes beyond qiskit-terra core features (e.g. quantum circuit construction, manipulation, and transpilation).
For example, in this specific case, allowing multiplication between SparsePauliOp and Parameter instances could look as simple as adding to _multiply(self, other) something like:

if isinstance(other, ParameterExpression):
    self.coeffs = other * self.coeffs

This works but it converts coeffs from a numpy array of complex to a numpy array of object where each element is actually a ParameterExpression instance. As you can easily understand, this means that all the operations supported for SparsePauliOp (and between it and all the other module classes) have to be basically reimplemented to handle symbolic math.

By the way, I started working on symbolic computation in Qiskit (inspired by #4751) to allow something similar to what you are proposing here and I recently released the package qiskit-symb to leverage sympy for symbolic computation on quantum statevectors, density matrices, and operators as the ones defined in qiskit.quantum_info. The package has also already been published to the Qiskit Ecosystem and I will show a quick demo on June 15th for the Qiskit DemoDay. If interested, check it out and feel free to contact me via Slack.

@kevinsung
Copy link
Contributor Author

This works but it converts coeffs from a numpy array of complex to a numpy array of object where each element is actually a ParameterExpression instance. As you can easily understand, this means that all the operations supported for SparsePauliOp (and between it and all the other module classes) have to be basically reimplemented to handle symbolic math.

The stored coefficients can already have data type object. See #8620.

@SimoneGasperini
Copy link
Contributor

@kevinsung you are right, sorry for the confusion! It also looks that the SparsePauliOp class already implements the features to support unbound parameters: this means that, taking care of everything that might be needed, multiplication could be supported as well. I will take a look at this possibly in the next days.

@jakelishman
Copy link
Member

Closed by #10264.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mod: quantum info Related to the Quantum Info module (States & Operators) type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants