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 #577

Merged
merged 23 commits into from
May 9, 2022
Merged

Multi-qubit fusion #577

merged 23 commits into from
May 9, 2022

Conversation

stavros11
Copy link
Member

Extends the circuit.fuse() to work with more than two qubits in the fused gates. The maximum number of qubits in a FusedGate is passed as argument in circuit.fuse(max_qubits).

Here are some benchmarks comparing this with the two qubit fusion currently implemented in qibo:

variational - cupy
nqubits no fusion fusion master fusion (max_qubits=2) this branch
20 0.00650024 0.00381665 0.00400567
21 0.013676 0.00941253 0.00939865
22 0.0244068 0.0174996 0.0174672
23 0.0470985 0.0336952 0.0337574
24 0.0919702 0.0676015 0.066224
25 0.183358 0.132579 0.132887
26 0.372621 0.263389 0.264354
27 0.751765 0.53461 0.53431
28 1.53455 1.07948 1.08052
29 3.0932 2.1975 2.17422
30 6.31183 4.39072 4.41419
supremacy - cupy
nqubits no fusion fusion master fusion (max_qubits=2) this branch
20 0.00714312 0.00362139 0.00368185
21 0.0147595 0.00922861 0.00924082
22 0.0276767 0.0176665 0.017413
23 0.0532548 0.0345442 0.0344771
24 0.103385 0.0685107 0.0681491
25 0.208079 0.13674 0.136516
26 0.426755 0.273745 0.272982
27 0.855133 0.550696 0.550875
28 1.74063 1.10813 1.1088
29 3.55886 2.25113 2.24221
30 7.29316 4.56042 4.54837
variational - numba
nqubits no fusion fusion master fusion (max_qubits=2) this branch
20 0.00717251 0.0044237 0.00438499
21 0.0145025 0.0073688 0.00857282
22 0.0245298 0.014758 0.0153122
23 0.0596275 0.0320367 0.0331313
24 0.192632 0.0801779 0.0694064
25 0.510924 0.153383 0.167004
26 1.08237 0.357689 0.351768
27 2.20973 0.721261 0.715018
28 4.70274 1.47233 1.53245
29 9.43148 3.00157 2.90914
30 19.8349 6.3264 6.12652
supremacy - numba
nqubits no fusion fusion master fusion (max_qubits=2) this branch
20 0.00855581 0.00485126 0.00415087
21 0.0166401 0.0092756 0.00953794
22 0.0302078 0.0158449 0.0157746
23 0.0734877 0.0407141 0.0391196
24 0.232868 0.0951609 0.0730411
25 0.63422 0.19142 0.176894
26 1.37733 0.374858 0.37815
27 2.78392 0.760626 0.774159
28 5.74549 1.6226 1.60402
29 11.8313 3.38964 3.31643
30 24.7313 6.58238 6.7799

where we see that the two-qubit fusion performance remains the same as master. Here is a comparison to qiskit using max_qubits>=2:

supremacy - max_qubits = 2
nqubits qibo qiskit
20 0.00424743 0.00561269
21 0.00906237 0.00931303
22 0.0149677 0.0182811
23 0.0361267 0.0290986
24 0.0923665 0.0631892
25 0.191264 0.179972
26 0.377141 0.391424
27 0.76376 0.814479
28 1.6189 1.67025
supremacy - max_qubits = 3
nqubits qibo qiskit
20 0.0038631 0.00545168
21 0.00901151 0.0123674
22 0.0169856 0.0137096
23 0.0338949 0.0283528
24 0.0888384 0.046873
25 0.176907 0.136258
26 0.375311 0.224167
27 0.769534 0.450493
28 1.62279 0.945164
supremacy - max_qubits = 4
nqubits qibo qiskit
20 0.00426801 0.00549873
21 0.00928775 0.00831294
22 0.0149652 0.0233534
23 0.0348389 0.0303541
24 0.078332 0.0461663
25 0.176815 0.122285
26 0.379753 0.235653
27 0.768286 0.454412
28 1.63447 0.94747
supremacy - max_qubits = 5
nqubits qibo qiskit
20 0.00449403 0.012344
21 0.00916354 0.00820939
22 0.0154273 0.018616
23 0.033929 0.0309339
24 0.0851702 0.0657368
25 0.186404 0.116064
26 0.373424 0.232725
27 0.754416 0.44802
28 1.61651 0.954646

where we see that we do not get improvement when using max_qubits > 2, in contrast to other libraries (qiskit, qsim). This is more likely associated to qiboteam/qibojit#51 about multi-qubit custom operators.

Regardless, I believe that this PR is useful to have since it doesn't affect any current feature (two-qubit fusion performance remains the same) and enables higher order fusion, even if it is not really optimized. If we improve the multi-qubit operators in qibojit, hopefully this will work better.

@stavros11 stavros11 mentioned this pull request Apr 21, 2022
2 tasks
Copy link
Contributor

@andrea-pasquale andrea-pasquale left a comment

Choose a reason for hiding this comment

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

Thanks for this implementation!
Everything looks good to me.
Do we need to add something in the doc?

@stavros11
Copy link
Member Author

This is more likely associated to qiboteam/qibojit#51 about multi-qubit custom operators.

I did more extensive benchmarks regarding this point and it seems that the slow performance for max_qubits > 2 is associated to both the multiqubit operators and the fusion algorithm. In particular, I tried extracting the fused circuit from qiskit and translating it to qibo and performance (second column below) is worse than qiskit but better than using qibo fusion:

supremacy - max_qubits = 2
nqubits qiskit qibo (using qiskit algorithm) qibo (using this branch)
20 0.00551739 0.00385623 0.00413577
21 0.0193736 0.00883374 0.0094498
22 0.0199203 0.0160643 0.0153139
23 0.0351699 0.0343719 0.0392432
24 0.0637625 0.0820693 0.0789008
25 0.17972 0.16847 0.179465
26 0.382657 0.380449 0.381843
27 0.805375 0.788001 0.778076
28 1.68317 1.63728 1.55446
29 3.31429 3.22245 3.35224
30 6.87321 6.80787 6.80193
supremacy - max_qubits = 3
nqubits qiskit qibo (using qiskit algorithm) qibo (using this branch)
20 0.0052587 0.00392785 0.00425625
21 0.00835266 0.0113288 0.00850089
22 0.0168691 0.0197931 0.0166647
23 0.0290197 0.0433391 0.0371486
24 0.0440456 0.0872999 0.081575
25 0.123802 0.176683 0.190229
26 0.246532 0.356565 0.377662
27 0.477173 0.687741 0.78859
28 0.994763 1.30436 1.60338
29 1.91694 2.42828 3.42528
30 3.7765 4.75425 6.83358
supremacy - max_qubits = 4
nqubits qiskit qibo (using qiskit algorithm) qibo (using this branch)
20 0.00534439 0.00445962 0.00387534
21 0.00853567 0.0106022 0.00891225
22 0.0244256 0.0132226 0.0160395
23 0.0252513 0.0458574 0.0382089
24 0.0479651 0.0956609 0.0926591
25 0.123212 0.105081 0.185433
26 0.244013 0.36335 0.365644
27 0.47745 0.716064 0.772659
28 0.989011 1.26652 1.65337
29 1.91922 2.48112 3.34416
30 3.76126 4.72298 6.72968
supremacy - max_qubits = 5
nqubits qiskit qibo (using qiskit algorithm) qibo (using this branch)
20 0.00539398 0.0043889 0.0044055
21 0.00831232 0.0105943 0.00748642
22 0.0158529 0.0204207 0.0163794
23 0.0262601 0.0436341 0.0356704
24 0.0452288 0.0922475 0.0911295
25 0.124818 0.173159 0.190342
26 0.248199 0.355191 0.376313
27 0.474587 0.704213 0.805853
28 0.989997 1.16549 1.66056
29 1.92013 2.47111 3.38975
30 3.75612 4.81733 6.78876

@codecov
Copy link

codecov bot commented May 2, 2022

Codecov Report

Merging #577 (af2a7c0) into master (69e91b9) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master      #577   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           86        86           
  Lines        12861     12926   +65     
=========================================
+ Hits         12861     12926   +65     
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 69e91b9...af2a7c0. Read the comment docs.

@scarrazza scarrazza merged commit ec53507 into master May 9, 2022
@stavros11 stavros11 deleted the qfusion branch May 9, 2022 09:29
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.

3 participants