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

Speed-up of AbelianGrouper.group_subops #923

Merged
merged 40 commits into from
May 15, 2020

Conversation

t-imamichi
Copy link
Contributor

@t-imamichi t-imamichi commented Apr 29, 2020

Summary

Faster version of AbelianGrouper.group_subops with numpy.
As a PoC, I implemented AbelianGrouper.group_subops_fast.
I know it is necessary to use commutes method, but it takes so long time to check commutes between all pairs of operators.

Details and comments

TPB based grouping can be implemented with numpy as tpb_grouped_weighted_pauli_operator.py does.
I implemented the same technique as AbelianGrouper.group_subops_fast.
I also tested a trick to reduce the overhead of Graph.add_edges_from by directly inserting elements to Graph._adj.

I compared them using pauli_grouping.py.
I used NH3JW.txt of https://arxiv.org/src/1701.08213v1/anc/TaperingPaperHamiltonians.rar
'original' is the result of AbelianGrouper.group_subops, 'fast1' is that of group_subops_fast without the networkx trick, and 'fast2' is that of group_subops_fast with the networkx trick.
The resulting groups are identical and 'fast2' is the fastest.

$ python pauli_grouping.py -i TaperingPaperHamiltonians/NH3/NH3JW.txt
$ diff3 original.txt fast1.txt fast2.txt
====
1:1c
  Elasped time: 190.92967315999977
2:1c
  Elasped time: 39.291700249000314
3:1c
  Elasped time: 21.408162898999763

@t-imamichi
Copy link
Contributor Author

I don't think this build error is not caused by my change.

=========================
Failures during discovery
=========================
--- import errors ---
Failed to import test module: test.aqua.operators.test_pauli_expectation
Traceback (most recent call last):
  File "/opt/python/3.8.0/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/opt/python/3.8.0/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/travis/build/Qiskit/qiskit-aqua/test/aqua/operators/test_pauli_expectation.py", line 28, in <module>
    from qiskit import BasicAer, IBMQ
ImportError: cannot import name 'IBMQ' from 'qiskit' (/home/travis/virtualenv/python3.8.0/lib/python3.8/site-packages/qiskit/__init__.py)
Failed to import test module: test.aqua.test_vqe
Traceback (most recent call last):
  File "/opt/python/3.8.0/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/opt/python/3.8.0/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/travis/build/Qiskit/qiskit-aqua/test/aqua/test_vqe.py", line 21, in <module>
    from qiskit import BasicAer, QuantumCircuit, IBMQ
ImportError: cannot import name 'IBMQ' from 'qiskit' (/home/travis/virtualenv/python3.8.0/lib/python3.8/site-packages/qiskit/__init__.py)

@dongreenberg
Copy link
Contributor

This is awesome!! Maybe we should check inside convert to see whether the summedop only contains PauliOps, and if that's the case, use the fast method, and otherwise use the .commutes method?

Alternatively, is there an elegant way to add the numpy speed directly into the PauliOp's .commutes method?

@t-imamichi
Copy link
Contributor Author

t-imamichi commented Apr 30, 2020

@dongreenberg Thank you for your comments. It is a good idea to call the fast version only if the summedop only contains PualiOps. I will write a code to realize it and add unit tests.
It may be also possible to implement PualiOp's .commutes with numpy. I will try it too.

@t-imamichi
Copy link
Contributor Author

t-imamichi commented Apr 30, 2020

PauliOp.commutes already uses numpy. The problem is that it computes the ndarray every time commutes is called; but the ndarrays do not change during all pair comparison. So the ndarray generation is the bottleneck.
https://github.com/Qiskit/qiskit-aqua/blob/master/qiskit/aqua/operators/primitive_ops/pauli_op.py#L248

So, I think a special pass for ListOp consisting of only PauliOp is the best approach.

@t-imamichi
Copy link
Contributor Author

I added an option to control the pass (generic or fast) and update the test script

The result of H2O JW:

$ python pauli_grouping.py -i TaperingPaperHamiltonians/H2O/H2OJW.txt
$ diff3 original.txt fast.txt fast-nxtrick.txt
====
1:1c
  Elasped time: 21.976199786999132
2:1c
  Elasped time: 4.463234193000972
3:1c
  Elasped time: 2.1695586320001894

Unit tests are not completed yet.

@t-imamichi
Copy link
Contributor Author

Thank you for your comments. I changed the default options and moved the unit tests to test_pauli_expectaion.

@mtreinish
Copy link
Contributor

Fwiw, please feel free to open an issue against retworkx to add the coloring support. The first priority for it when I started was the terra's usage for the DAGCircuit and the transpiler. So outside of that it doesn't have many features yet. But, now that it is in place expanding retworkx to support other needs from a graph library is totally fair and definitely worth it. I've got an in-progress PR for undirected graphs open now Qiskit/rustworkx#68. Once that's merged adding coloring algorithms (especially the largest_first which is just a list of nodes sorted by degree) should be pretty straightforward.

@t-imamichi
Copy link
Contributor Author

Thanks! It's a good news. When undirected graphs are merged, I want to try it.

@t-imamichi t-imamichi requested a review from dongreenberg May 13, 2020 14:00
dongreenberg
dongreenberg previously approved these changes May 13, 2020
@dongreenberg
Copy link
Contributor

Sorry - I think I may have been unclear about something. I liked that you pull the AbelianGrouper tests into a separate file. Can we just move test_abelian_grouper1 2 and 3 into the new test_abelian_grouper file and out of test_pauli_expectation?

@t-imamichi
Copy link
Contributor Author

Sorry. I will move the tests to a separate file.

@t-imamichi
Copy link
Contributor Author

I moved abelian grouper tests to a separate file test_abelian_grouper.py and added a unit test with random pauli operators.

Copy link
Contributor

@dongreenberg dongreenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks Imamichi-san!!!!

@t-imamichi
Copy link
Contributor Author

Thank you for fixing lint.

@dongreenberg
Copy link
Contributor

@woodsp-ibm should I hold off on merging this until after the patch to stable today?

@woodsp-ibm
Copy link
Member

@dongreenberg Yes, if we can hold off on this until we do the release, thanks.

@dongreenberg dongreenberg merged commit 65f222e into qiskit-community:master May 15, 2020
@t-imamichi t-imamichi deleted the fast-grouping branch May 15, 2020 05:25
mtreinish pushed a commit to mtreinish/qiskit-core that referenced this pull request Nov 20, 2020
…#923)

* fast operator grouping with numpy

* add a trick for networkx

* remove unnecessary "[]"

* fix comments

* (wip) add test_abelian_grouper.py

* implement a fast pass of grouping

* fix lint

* implement a numpy-based coloring

* move abelian groupter tests

* simplify

* move abelian groupter tests to a separete file

* replace assert with assertTrue

* Fix lint

Co-authored-by: Manoel Marques <manoel@us.ibm.com>
Co-authored-by: Julien Gacon <jules.gacon@googlemail.com>
Co-authored-by: Donny Greenberg <dongreenberg2@gmail.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
manoelmarques added a commit to manoelmarques/qiskit-terra that referenced this pull request Dec 2, 2020
…#923)

* fast operator grouping with numpy

* add a trick for networkx

* remove unnecessary "[]"

* fix comments

* (wip) add test_abelian_grouper.py

* implement a fast pass of grouping

* fix lint

* implement a numpy-based coloring

* move abelian groupter tests

* simplify

* move abelian groupter tests to a separete file

* replace assert with assertTrue

* Fix lint

Co-authored-by: Manoel Marques <manoel@us.ibm.com>
Co-authored-by: Julien Gacon <jules.gacon@googlemail.com>
Co-authored-by: Donny Greenberg <dongreenberg2@gmail.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
manoelmarques added a commit to manoelmarques/qiskit-terra that referenced this pull request Dec 7, 2020
…#923)

* fast operator grouping with numpy

* add a trick for networkx

* remove unnecessary "[]"

* fix comments

* (wip) add test_abelian_grouper.py

* implement a fast pass of grouping

* fix lint

* implement a numpy-based coloring

* move abelian groupter tests

* simplify

* move abelian groupter tests to a separete file

* replace assert with assertTrue

* Fix lint

Co-authored-by: Manoel Marques <manoel@us.ibm.com>
Co-authored-by: Julien Gacon <jules.gacon@googlemail.com>
Co-authored-by: Donny Greenberg <dongreenberg2@gmail.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants