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

Implement coeffs method for SummedOp #5931

Closed
jlapeyre opened this issue Feb 27, 2021 · 7 comments · Fixed by #6610
Closed

Implement coeffs method for SummedOp #5931

jlapeyre opened this issue Feb 27, 2021 · 7 comments · Fixed by #6610
Assignees
Labels
good first issue Good for newcomers mod: opflow Related to the Opflow module type: enhancement It's working, but needs polishing
Milestone

Comments

@jlapeyre
Copy link
Contributor

It would be useful to have a method SummedOp.coeffs analogous to PauliSumOp.coeffs. This would allow the same code to extract coefficients from both via duck typing (eg in PhaseEstimation)

@jlapeyre jlapeyre added type: enhancement It's working, but needs polishing good first issue Good for newcomers labels Feb 27, 2021
@woodsp-ibm woodsp-ibm added the mod: opflow Related to the Opflow module label Feb 28, 2021
@john-judge
Copy link
Contributor

john-judge commented Mar 9, 2021

Update 6/20/21: Revisited this one and created PR #6610

If I understand the enhancement correctly, we need a method to pull the coefficients for the operators listed for the lazy sum, scaled by the overall SummedOp coefficient.

Does this seem to be starting off on the right path?

+    @property
+    def coeffs(self)-> List[Union[complex, ParameterExpression]]:
+        """Return the coefficients."""
+        return [self.coeff * op.coeff for op in self.oplist]
+

Going off of the discussion of PauliSumOp.coeffs typing in #5547 / #5560 and noting that oplist is taken in as a typing.List, I figure that a List[complex] return type might be appropriate.

Testing this:

from qiskit.opflow.list_ops import SummedOp, ListOp
from qiskit.aqua.operators import X, Y, Z, I
from math import sqrt
 
coefficient = 0.5
 
summ = SummedOp([(0 + 1j) * X,
                (1j) * X,
                (1/sqrt(2) + 1j/sqrt(2)) * Z,
                (1/sqrt(2) - 1j/sqrt(2)) *  Y], coefficient)
 
print(summ)
print(summ.coeffs)
print(type(summ.coeffs))
 
summ = summ.collapse_summands()
 
print(summ.coeffs)
0.5 * SummedOp([
  1j * X,
  1j * X,
  (0.7071067811865475+0.7071067811865475j) * Z,
  (0.7071067811865475-0.7071067811865475j) * Y
])
[0.5j, 0.5j, (0.35355339059327373+0.35355339059327373j), (0.35355339059327373-0.35355339059327373j)]
<class 'list'>
[1j, (0.35355339059327373+0.35355339059327373j), (0.35355339059327373-0.35355339059327373j)]

@john-judge
Copy link
Contributor

Hi @woodsp-ibm,

Any thoughts on what we have here so far? I think this might meet the use case @jlapeyre is suggesting.

However, I'm slightly concerned about how the typing may interact in SummedOp. In particular, it seems coefficients can be ParameterExpression objects, in which case maybe I need to consider the case of calling ParameterExpression._apply_operation, which is documented as:

Base method implementing math operations between Parameters and either a constant or a second ParameterExpression.

Let me know if I should create a pull request to allow others to review/test more easily.

@jlapeyre
Copy link
Contributor Author

jlapeyre commented Apr 7, 2021

Hi @john-judge . Thanks for the contribution. I think this is on the right track. You are correct here:

In particular, it seems coefficients can be ParameterExpression objects

I'm pretty sure that your implementation above already correctly handles ParameterExpression.

Yes, please make a PR, if you like.

@javabster
Copy link
Contributor

Hi @john-judge are you still working on this? If you have any further questions let us know :)

@john-judge
Copy link
Contributor

@javabster I'm not actively working on this at the moment. I'm fine if anyone wants to jump in and finish this. Next step would be to write tests including for the ParameterExpression case to include in a PR. If there are no takers I may pick this back up in about a month when I find some time.

@timsinashok
Copy link

I am a newbie with a desire to learn. I would love to work on this issue.

@john-judge
Copy link
Contributor

@a-freakish Great to hear, please do pick this one up! Let me know if you have any questions and I can try to answer.

john-judge added a commit to john-judge/qiskit-terra that referenced this issue Jun 20, 2021
@mergify mergify bot closed this as completed in #6610 Jul 14, 2021
mergify bot added a commit that referenced this issue Jul 14, 2021
#6610)

* implement coeffs method for SummedOp (Issue #5931), matching coeffs method of PauliSumOp

* Move SummedOp.coeffs method to parent class ListOps; raise exceptions for nested ListOps

* reformatted automatically by black

* Added reno for ListOp.coeffs feature addition

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@kdk kdk added this to the 0.19 milestone Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers mod: opflow Related to the Opflow module type: enhancement It's working, but needs polishing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants