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

Multi-qubit fusion #505

Closed
wants to merge 24 commits into from
Closed

Multi-qubit fusion #505

wants to merge 24 commits into from

Conversation

stavros11
Copy link
Member

Generalizes the gate fusion algorithm to work with arbitrary number of max_qubits which is now possible since the qibojit backend supports multi-qubit gates. The maximum number of qubits for fusion can be set when calling the circuit.fuse() method. The current default is 2, but we may change it to a different number (for example 5, if we follow qiskit) depending on benchmark results.

Below is some comparison of this implementation to qiskit. In each column we have the ration of qibo simulation time / qiskit simulation time for different choices of fusion max_qubits from 2 to 8. Simulation, not dry run, times are used.

qft
nqubits max_qubits=2 max_qubits=3 max_qubits=4 max_qubits=5 max_qubits=6 max_qubits=7 max_qubits=8
3 0.20610 0.08418 0.09645 0.09511 0.09376 0.09415 0.09574
4 0.27322 0.20666 0.09970 0.12229 0.12664 0.11661 0.13105
5 0.40971 0.32253 0.24150 0.11827 0.11290 0.11423 0.10994
6 0.51495 0.38617 0.28347 0.24539 0.12872 0.17979 0.18191
7 0.56359 0.36434 0.34203 0.25258 0.41455 0.11300 0.17466
8 0.65333 0.44113 0.36942 0.27612 0.45016 0.55353 0.19977
9 0.67696 0.59501 0.35652 0.38223 0.45030 0.55807 0.90887
10 0.68283 0.60097 0.44080 0.42769 0.53450 0.54176 0.93219
11 0.64424 0.55535 0.41726 0.40138 0.53167 0.41922 0.85935
12 0.56822 0.44630 0.32520 0.30930 0.47301 0.56328 0.61369
13 0.44925 0.38391 0.28012 0.28160 0.46229 0.49167 0.75810
14 0.26829 0.25044 0.21624 0.26319 0.40924 0.43875 0.52741
15 0.20808 0.31032 0.36157 0.44405 0.68985 0.58737 0.37335
16 0.21493 0.34252 0.43177 0.54268 1.09735 0.84952 0.58443
17 0.18826 0.33735 0.52843 0.71947 1.47227 1.25480 0.89972
18 0.20630 0.42695 0.79086 1.24113 2.63374 2.35916 1.93672
19 0.25208 0.57239 1.30136 1.84342 4.54642 4.05777 2.62571
20 0.30024 0.67886 1.92696 2.85587 5.96119 5.44896 4.59788
21 0.34968 0.90667 2.55980 5.94465 10.37865 7.21071 8.71386
22 0.37725 1.00783 2.67319 7.85334 11.91473 14.12240 15.20113
23 0.39422 1.10585 2.95582 5.94947 17.05321 15.46743 15.71536
24 0.51315 1.29089 3.25359 8.20007 16.26323 17.77953 23.17776
25 0.30063 0.60797 2.34548 4.83730 11.16344 12.30776 19.13217
26 0.39397 0.78282 2.06717 4.41566 11.38827 13.81669 20.93561
27 0.40393 0.78935 2.09944 4.64882 11.07182 14.17314 22.99675
28 0.40670 0.75732 2.17397 5.12565 10.67715 14.91259 24.52161
29 0.39942 0.76523 2.29478 5.24375 11.53743 14.19988 24.47468
30 0.39481 0.78706 2.29914 5.18139 11.30671 15.04154 24.41561
variational
nqubits max_qubits=2 max_qubits=3 max_qubits=4 max_qubits=5 max_qubits=6 max_qubits=7 max_qubits=8
3 0.12918 0.09290 0.08594 0.09214 0.08508 0.09395 0.08972
4 0.18967 0.15728 0.12452 0.12445 0.12743 0.11498 0.12063
5 0.18312 0.12131 0.11739 0.10515 0.11403 0.11452 0.11861
6 0.21155 0.19054 0.13579 0.13514 0.14059 0.13832 0.13581
7 0.15907 0.19177 0.13408 0.07640 0.12939 0.13033 0.13017
8 0.25479 0.23260 0.15107 0.09917 0.15304 0.15601 0.16794
9 0.22643 0.22130 0.11500 0.14922 0.14968 0.14661 0.14751
10 0.25891 0.23123 0.16976 0.16812 0.15939 0.13411 0.16588
11 0.22037 0.21210 0.13780 0.13355 0.13947 0.14536 0.14594
12 0.21001 0.21413 0.12321 0.14195 0.14099 0.12954 0.14125
13 0.17822 0.16634 0.12994 0.13138 0.12488 0.12003 0.12780
14 0.14624 0.13456 0.10552 0.10943 0.10540 0.10389 0.10276
15 0.16319 0.19073 0.21613 0.15735 0.19986 0.16632 0.20181
16 0.23841 0.24610 0.27855 0.16029 0.27180 0.26562 0.23745
17 0.30894 0.31406 0.31256 0.36604 0.30217 0.34898 0.35583
18 0.38848 0.42418 0.46097 0.33544 0.50544 0.58581 0.39120
19 0.52558 0.63272 0.81654 0.82585 0.90594 0.89172 0.90314
20 0.69194 0.73752 1.60976 1.70846 1.55462 1.62007 1.65468
21 0.91445 1.01003 1.81093 1.78431 1.85583 2.00530 1.46395
22 0.85691 0.98048 3.18528 2.97109 2.72203 2.47227 2.90535
23 0.89021 1.04287 3.12754 3.69202 3.18779 3.14014 3.11378
24 1.01610 1.44653 3.03373 3.77333 3.70526 4.04854 3.82562
25 0.88843 1.11728 2.68815 2.57932 2.58797 2.63095 2.74291
26 0.93132 1.13162 2.16047 2.22403 1.96750 1.96645 2.21386
27 0.95482 1.08215 2.42035 2.42800 2.39451 2.27586 2.43179
28 0.95274 1.08452 2.08856 1.82675 1.92931 2.12992 1.90313
29 0.89881 1.06306 2.20685 2.10730 2.11960 2.16078 2.20331
30 0.89803 1.09436 1.79687 2.04702 2.01804 2.00824 1.91996
bv
nqubits max_qubits=2 max_qubits=3 max_qubits=4 max_qubits=5 max_qubits=6 max_qubits=7 max_qubits=8
3 0.14046 0.09433 0.09977 0.09303 0.09360 0.09551 0.09758
4 0.15804 0.12529 0.08938 0.08877 0.08911 0.07797 0.08929
5 0.18130 0.07570 0.12299 0.06886 0.09190 0.08925 0.08764
6 0.19775 0.14274 0.09069 0.11481 0.13008 1.40166 0.35120
7 0.21799 0.13275 0.11079 0.10585 0.37708 0.15507 0.14809
8 0.25241 0.10077 0.08963 0.10498 0.16405 0.19358 0.26652
9 0.24323 0.15083 0.12333 0.09043 0.12245 0.14011 0.26402
10 0.25635 0.12911 0.11816 0.11939 0.13918 0.18002 2.24682
11 0.24559 0.15046 0.13610 0.11116 0.81163 0.19060 0.25346
12 0.22763 0.15529 0.11538 0.09731 0.16024 0.17813 0.27084
13 0.20325 0.12372 0.10788 0.10755 0.16054 0.20719 0.23125
14 0.15441 0.09457 0.09682 0.09443 0.13800 0.21770 0.19311
15 0.20877 0.16530 0.12949 0.12885 0.70206 0.36953 0.48522
16 0.25112 0.17574 0.16176 0.14049 0.40476 0.42186 0.59605
17 0.26999 0.25599 0.26621 0.31260 0.67065 0.72790 1.07239
18 0.44030 0.42344 0.44516 0.53626 0.99000 1.38535 1.81131
19 0.56456 0.57529 0.67419 0.68721 1.25608 2.03198 2.61977
20 0.73946 0.81816 0.68421 1.21963 2.50002 3.38620 4.82185
21 1.01244 1.14231 0.99468 1.75723 3.53384 3.88030 5.53282
22 1.04583 1.22304 1.36087 1.93512 3.90660 4.42056 7.75701
23 1.16903 0.85674 1.49083 2.34718 4.46741 3.81819 7.72695
24 1.22028 1.37409 1.68582 2.55020 4.37161 5.22531 7.66486
25 0.90181 1.17182 0.84762 2.06576 3.52038 3.88060 4.89322
26 0.92277 1.16616 1.25146 1.52189 3.82825 3.99823 4.90486
27 0.93529 1.15219 1.24572 1.65897 3.67626 3.69007 5.24249
28 0.89754 0.87682 1.13180 1.57908 3.31576 3.36319 4.48362
29 0.89685 1.01265 0.85730 1.24307 3.37984 3.15174 4.97647
30 0.93569 0.90918 0.98798 1.44764 3.24347 3.23044 4.65742
qv
nqubits max_qubits=2 max_qubits=3 max_qubits=4 max_qubits=5 max_qubits=6 max_qubits=7 max_qubits=8
3 0.07972 0.08622 0.08548 0.09056 0.08614 0.07924 0.08409
4 0.09611 0.09486 0.09618 0.09040 0.09326 0.09768 0.07663
5 0.07130 0.09411 0.07661 0.09970 0.09812 0.10103 0.09753
6 0.10242 0.10223 0.07555 0.09979 0.08906 0.09955 0.08913
7 0.10127 0.09349 0.10022 0.09771 0.10146 0.10033 0.10205
8 0.08205 0.10600 0.10717 0.10601 0.10192 0.10154 0.08370
9 0.10548 0.10217 0.10361 0.09952 0.10319 0.11312 0.10619
10 0.10175 0.10480 0.10218 0.10309 0.06639 0.10195 0.10490
11 0.09763 0.09813 0.10887 0.09700 0.09699 0.09347 0.08686
12 0.08248 0.08634 0.08728 0.08543 0.08669 0.08458 0.08298
13 0.07463 0.06815 0.07507 0.07472 0.07574 0.06587 0.08072
14 0.05429 0.05585 0.05454 0.05713 0.05731 0.05610 0.05676
15 0.06211 0.07913 0.10723 0.08873 0.08217 0.07110 0.10935
16 0.13401 0.10590 0.09858 0.12210 0.12991 0.11383 0.11997
17 0.14661 0.15896 0.11433 0.13356 0.14244 0.16005 0.15299
18 0.26181 0.15049 0.20880 0.27519 0.29655 0.27144 0.26564
19 0.37015 0.34550 0.35441 0.35916 0.24331 0.32887 0.35295
20 0.47275 0.38712 0.44274 0.43299 0.37914 0.38492 0.43410
21 0.66819 0.45920 0.73919 0.69654 0.73265 0.71700 0.70866
22 0.90577 0.89172 0.81400 0.75514 0.81090 0.85714 0.67504
23 0.99608 0.97397 1.03261 1.04973 1.01202 0.83660 1.02322
24 1.25176 1.16575 1.34000 1.38282 1.29723 1.31792 1.41917
25 0.84464 0.83522 1.25010 1.27026 1.23742 1.24251 1.23506
26 0.85442 0.92253 1.29354 1.33722 1.33915 1.34331 1.37436
27 0.91009 0.91450 1.26692 1.38259 1.26216 1.38267 1.37792
28 0.85104 0.84939 1.49258 1.41522 1.47279 1.37034 1.35312
29 0.87187 0.94674 1.38148 1.43085 1.37936 1.45328 1.37813
30 0.86447 0.86306 1.38349 1.41236 1.39658 1.37901 1.37201

Clearly the fusion algorithm is not optimal for max_qubits > 3.

TODO:

  • Improve the algorithm to get competitive performance. Potential ways to fix this is to compare the fused circuits for various examples with the ones from qiskit or others, or even check the fusion code of other libraries.
  • Add tests for fusion with max_qubits > 2.

@stavros11 stavros11 marked this pull request as draft November 7, 2021 10:54
@scarrazza scarrazza changed the base branch from rmbackends to master November 7, 2021 22:16
@codecov
Copy link

codecov bot commented Nov 19, 2021

Codecov Report

Merging #505 (0ac873f) into master (fe91300) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #505   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           83        83           
  Lines        11917     11935   +18     
=========================================
+ Hits         11917     11935   +18     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/qibo/abstractions/abstract_gates.py 100.00% <100.00%> (ø)
src/qibo/abstractions/circuit.py 100.00% <100.00%> (ø)
src/qibo/abstractions/gates.py 100.00% <100.00%> (ø)
src/qibo/core/circuit.py 100.00% <100.00%> (ø)
src/qibo/core/gates.py 100.00% <100.00%> (ø)
src/qibo/tests/test_abstract_gates.py 100.00% <100.00%> (ø)
src/qibo/tests/test_core_fusion.py 100.00% <100.00%> (ø)
src/qibo/tests/test_core_gates.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fe91300...0ac873f. Read the comment docs.

@stavros11
Copy link
Member Author

Closing in favor of a different implementation in #577.

@stavros11 stavros11 closed this Apr 21, 2022
@stavros11 stavros11 deleted the fusion branch July 1, 2022 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant