Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

SummedOp updates & optimization converters to use Opflow #1059

Merged
merged 38 commits into from
Jul 6, 2020

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Jun 18, 2020

Summary

#1077 should be merged first.

Update the converters in the optimization module to accept and return OperatorBase objects (WeightedPauliOperator is supported as before). Also adds an equals implementation to SummedOp which was necessary for the tests to work and moves simplify into reduce as suggested in #1000 (comment).

Marked as on hold to give @t-imamichi time for some performance checks.
WIP: Need to fix set comparison if SummedOp contains other primitives than just PauliOp.\

Details and comments

equals has been updated such that expressions such as X + X == 2*X and X + Z == Z + X evaluate to True, unlike before. This calls reduce on the operators and is is slower than the previous equals used in ListOp I removed two lines of add which checked if other is equal to self, as this adds a lot of overhead e.g. if many Pauli terms are summed. Since the opflow focuses on ease-of-use and intuitive syntax close to math, I think it is very important to have X + X = 2X and XZ = ZX equivalencies.

simplify has been renamed to collapse_summands to be more descriptive and such that users have one distinct method they should call to reduce/simplify the operator, namely reduce.

@Cryoris Cryoris added the status: on hold E.g. needs more discussion or more information (preformance, tests, ...) label Jun 18, 2020
@Cryoris Cryoris requested a review from t-imamichi June 18, 2020 12:36
@Cryoris Cryoris added the Changelog: New Feature Include in the Added section of the changelog label Jun 18, 2020
@t-imamichi
Copy link
Contributor

I checked the performance with some Hamiltonians of molecules and confirmed that it works well.
I add a short comment for simplicity.

@Cryoris Cryoris changed the title [WIP] SummedOp updates & optimization converters to use Opflow SummedOp updates & optimization converters to use Opflow Jun 29, 2020
@Cryoris Cryoris merged commit 6f9629d into qiskit-community:master Jul 6, 2020
@Cryoris Cryoris deleted the summed-op-compare branch July 6, 2020 07:42
@adekusar-drl
Copy link
Contributor

@Cryoris seems like this PR causes troubles in the build pipeline.

@Cryoris
Copy link
Contributor Author

Cryoris commented Jul 6, 2020

Indeed there seems the word summands which is marked as error by the spellcheck. The test failures are coming from Qiskit/qiskit#4622, where the type of Instruction.definition has been changed from a list to a circuit. Thus expressions such as if gate.definition == list are failing now.

From the CI log:

if isinstance(gate, IGate) or (type(gate) == Instruction and gate.definition == []):
      File "/home/travis/miniconda/lib/python3.7/site-packages/qiskit/circuit/quantumcircuit.py", line 211, in __eq__
    return circuit_to_dag(self) == circuit_to_dag(other)

Thanks for the info!

@adekusar-drl
Copy link
Contributor

Haven't looked into details but some other PRs(including my own) are failing at

qiskit/aqua/utils/controlled_circuit.py:125:12: E1120: No value for argument 'c' in function call (no-value-for-parameter)

@Cryoris
Copy link
Contributor Author

Cryoris commented Jul 6, 2020

Not sure why that happens, but in #1100 @manoelmarques also had to change the input to controlled_circuit. Looks like the linter wants to have the arguments unrolled 🤔

@adekusar-drl
Copy link
Contributor

Just curious, something in this PR caused this behavior or the problem somewhere else?

@Cryoris
Copy link
Contributor Author

Cryoris commented Jul 6, 2020

Pretty sure this is from somewhere else, as this PR didn't touch controlled_circuit and the failures of the tests in the CI are coming from Qiskit/qiskit#4622. This PR was able to merge because it was built and tested before that terra PR merged and has not been rebuilt since then.

@adekusar-drl
Copy link
Contributor

Thanks for the update, a pretty large PR in terra as I see.

manoelmarques added a commit to manoelmarques/qiskit-aqua that referenced this pull request Jul 28, 2020
…unity#1059)

* simplify and reduce, add equals to SummedOp, update tests

* directly use new opflow, no need to go via WPO

* update comments and docstrings

* directly use opflow

* don't do equality check in add

* directly use opflow

* change order in reduce

* fix qaoa

* add short test on summed op equality

* rm prints

* use set comparison, rename simplify to collapse_summands

* fix expected value, should be sqrt(2), not 2

* cast coeffs to complex

* add reno on equals

* fix mypy

* fix spell

* fix lint

* dont cast coefficient to complex

leads to problems if the coeff is exponentitated and not supposed to be complex

* use sum instead of reduce

* rm unused import

* move __hash__ to primitive op and base on repr

* use != over not ==

* add summed op test for different primitives

* check for opbase, not summedop

* adress changes from review

* explicitly raise an error upon ListOp input
* return identity op instead of the int 0

* fix spell

* add note that equals is not mathematically sound

Co-authored-by: Manoel Marques <[email protected]>
pbark pushed a commit to pbark/qiskit-aqua that referenced this pull request Sep 16, 2020
…unity#1059)

* simplify and reduce, add equals to SummedOp, update tests

* directly use new opflow, no need to go via WPO

* update comments and docstrings

* directly use opflow

* don't do equality check in add

* directly use opflow

* change order in reduce

* fix qaoa

* add short test on summed op equality

* rm prints

* use set comparison, rename simplify to collapse_summands

* fix expected value, should be sqrt(2), not 2

* cast coeffs to complex

* add reno on equals

* fix mypy

* fix spell

* fix lint

* dont cast coefficient to complex

leads to problems if the coeff is exponentitated and not supposed to be complex

* use sum instead of reduce

* rm unused import

* move __hash__ to primitive op and base on repr

* use != over not ==

* add summed op test for different primitives

* check for opbase, not summedop

* adress changes from review

* explicitly raise an error upon ListOp input
* return identity op instead of the int 0

* fix spell

* add note that equals is not mathematically sound

Co-authored-by: Manoel Marques <[email protected]>
mtreinish pushed a commit to mtreinish/qiskit-core that referenced this pull request Nov 20, 2020
…unity/qiskit-aqua#1059)

* simplify and reduce, add equals to SummedOp, update tests

* directly use new opflow, no need to go via WPO

* update comments and docstrings

* directly use opflow

* don't do equality check in add

* directly use opflow

* change order in reduce

* fix qaoa

* add short test on summed op equality

* rm prints

* use set comparison, rename simplify to collapse_summands

* fix expected value, should be sqrt(2), not 2

* cast coeffs to complex

* add reno on equals

* fix mypy

* fix spell

* fix lint

* dont cast coefficient to complex

leads to problems if the coeff is exponentitated and not supposed to be complex

* use sum instead of reduce

* rm unused import

* move __hash__ to primitive op and base on repr

* use != over not ==

* add summed op test for different primitives

* check for opbase, not summedop

* adress changes from review

* explicitly raise an error upon ListOp input
* return identity op instead of the int 0

* fix spell

* add note that equals is not mathematically sound

Co-authored-by: Manoel Marques <[email protected]>
manoelmarques added a commit to manoelmarques/qiskit-terra that referenced this pull request Dec 2, 2020
…unity/qiskit-aqua#1059)

* simplify and reduce, add equals to SummedOp, update tests

* directly use new opflow, no need to go via WPO

* update comments and docstrings

* directly use opflow

* don't do equality check in add

* directly use opflow

* change order in reduce

* fix qaoa

* add short test on summed op equality

* rm prints

* use set comparison, rename simplify to collapse_summands

* fix expected value, should be sqrt(2), not 2

* cast coeffs to complex

* add reno on equals

* fix mypy

* fix spell

* fix lint

* dont cast coefficient to complex

leads to problems if the coeff is exponentitated and not supposed to be complex

* use sum instead of reduce

* rm unused import

* move __hash__ to primitive op and base on repr

* use != over not ==

* add summed op test for different primitives

* check for opbase, not summedop

* adress changes from review

* explicitly raise an error upon ListOp input
* return identity op instead of the int 0

* fix spell

* add note that equals is not mathematically sound

Co-authored-by: Manoel Marques <[email protected]>
manoelmarques added a commit to manoelmarques/qiskit-terra that referenced this pull request Dec 7, 2020
…unity/qiskit-aqua#1059)

* simplify and reduce, add equals to SummedOp, update tests

* directly use new opflow, no need to go via WPO

* update comments and docstrings

* directly use opflow

* don't do equality check in add

* directly use opflow

* change order in reduce

* fix qaoa

* add short test on summed op equality

* rm prints

* use set comparison, rename simplify to collapse_summands

* fix expected value, should be sqrt(2), not 2

* cast coeffs to complex

* add reno on equals

* fix mypy

* fix spell

* fix lint

* dont cast coefficient to complex

leads to problems if the coeff is exponentitated and not supposed to be complex

* use sum instead of reduce

* rm unused import

* move __hash__ to primitive op and base on repr

* use != over not ==

* add summed op test for different primitives

* check for opbase, not summedop

* adress changes from review

* explicitly raise an error upon ListOp input
* return identity op instead of the int 0

* fix spell

* add note that equals is not mathematically sound

Co-authored-by: Manoel Marques <[email protected]>
manoelmarques added a commit to qiskit-community/qiskit-optimization that referenced this pull request Jan 14, 2021
…unity/qiskit-aqua#1059)

* simplify and reduce, add equals to SummedOp, update tests

* directly use new opflow, no need to go via WPO

* update comments and docstrings

* directly use opflow

* don't do equality check in add

* directly use opflow

* change order in reduce

* fix qaoa

* add short test on summed op equality

* rm prints

* use set comparison, rename simplify to collapse_summands

* fix expected value, should be sqrt(2), not 2

* cast coeffs to complex

* add reno on equals

* fix mypy

* fix spell

* fix lint

* dont cast coefficient to complex

leads to problems if the coeff is exponentitated and not supposed to be complex

* use sum instead of reduce

* rm unused import

* move __hash__ to primitive op and base on repr

* use != over not ==

* add summed op test for different primitives

* check for opbase, not summedop

* adress changes from review

* explicitly raise an error upon ListOp input
* return identity op instead of the int 0

* fix spell

* add note that equals is not mathematically sound

Co-authored-by: Manoel Marques <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Changelog: New Feature Include in the Added section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants