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

SparsePauliOp fails when initialized with Parameter in list #8894

Closed
kevinsung opened this issue Oct 13, 2022 · 5 comments
Closed

SparsePauliOp fails when initialized with Parameter in list #8894

kevinsung opened this issue Oct 13, 2022 · 5 comments
Labels
bug Something isn't working documentation Something is not clear or an error documentation
Milestone

Comments

@kevinsung
Copy link
Contributor

Environment

  • Qiskit Terra version: 0.22.0rc1
  • Python version: 3.10.7
  • Operating system: Arch Linux

What is happening?

See title.

How can we reproduce the issue?

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

labels = ["I", "X", "Y", "-Z", "iZ", "-iX"]
paulis = PauliList(labels)
coeffs = [1, 2, 3, 4, 5, Parameter("a")]
spp_op = SparsePauliOp(paulis, coeffs)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/projects/qiskit-terra/qiskit/circuit/parameterexpression.py:449, in ParameterExpression.__complex__(self)
    448 try:
--> 449     return complex(self._symbol_expr)
    450 # TypeError is for sympy, RuntimeError for symengine

File symengine_wrapper.pyx:1163, in symengine.lib.symengine_wrapper.Basic.__complex__()

File symengine_wrapper.pyx:976, in symengine.lib.symengine_wrapper.Basic.n()

File symengine_wrapper.pyx:4346, in symengine.lib.symengine_wrapper.evalf()

RuntimeError: Symbol cannot be evaluated.

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
Cell In [23], line 7
      5 paulis = PauliList(labels)
      6 coeffs = [1, 2, 3, 4, 5, Parameter("a")]
----> 7 spp_op = SparsePauliOp(paulis, coeffs)

File ~/projects/qiskit-terra/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py:119, in SparsePauliOp.__init__(self, data, coeffs, ignore_pauli_phase, copy)
    117     coeffs = np.ones(pauli_list.size, dtype=dtype)
    118 else:
--> 119     coeffs = np.array(coeffs, copy=copy, dtype=dtype)
    121 if ignore_pauli_phase:
    122     # Fast path used in copy operations, where the phase of the PauliList is already known
    123     # to be zero.  This path only works if the input data is compatible with the internal
    124     # ZX-phase convention.
    125     self._pauli_list = pauli_list

File ~/projects/qiskit-terra/qiskit/circuit/parameterexpression.py:452, in ParameterExpression.__complex__(self)
    450 # TypeError is for sympy, RuntimeError for symengine
    451 except (TypeError, RuntimeError) as exc:
--> 452     raise TypeError(
    453         "ParameterExpression with unbound parameters ({}) "
    454         "cannot be cast to a complex.".format(self.parameters)
    455     ) from exc

TypeError: ParameterExpression with unbound parameters ({Parameter(a)}) cannot be cast to a complex.

What should happen?

It should work (??)

Any suggestions?

This can be fixed by checking the type of the coefficients in the list. However, this incurs some overhead. So I'm actually not sure whether this behavior is intentional. @ikko

@kevinsung kevinsung added the bug Something isn't working label Oct 13, 2022
@ikkoham
Copy link
Contributor

ikkoham commented Oct 13, 2022

This is a spec. The default dtype is complex, so if you want to use other dtypes, you need to set it explicitly.

coeffs = np.array([1, 2, 3, 4, 5, Parameter("a")])

works.
It should be documented.

@ikkoham ikkoham added the documentation Something is not clear or an error documentation label Oct 13, 2022
@Cryoris
Copy link
Contributor

Cryoris commented Oct 14, 2022

I would actually also expect @kevinsung's example to work, it seems unintuitive if we have to wrap the coefficients in an array -- and that wouldn't work if I used from_list or from_sparse_list, right?

Is there an overhead to allowing Parameters directly or could we add that efficiently?

@kevinsung
Copy link
Contributor Author

The overhead would be iterating through the list and checking the type of the elements.

An alternative to making the user wrap the coefficients in an array would be to allow them to specify the dtype. That may be slightly easier to discover.

@Cryoris Cryoris added this to the 0.24.0 milestone Jan 10, 2023
@Cryoris
Copy link
Contributor

Cryoris commented Jan 10, 2023

Linking #8896 where this was also discussed. Personally, I would be in favor of supporting Parameters as they pretty much native numeric types in Qiskit and it would make life of users easier if one doesn't have to remember to explicitly set dtype=object.

@Cryoris
Copy link
Contributor

Cryoris commented Apr 7, 2023

This works since #8896.

@Cryoris Cryoris closed this as completed Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Something is not clear or an error documentation
Projects
None yet
Development

No branches or pull requests

3 participants