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

Improved docs of Hamiltonian and Gradients modules #1428

Merged
merged 3 commits into from
Aug 30, 2024
Merged

Conversation

renatomello
Copy link
Contributor

@renatomello renatomello commented Aug 26, 2024

Improved documentation of the modules mentioned in the title + a faster implementation of the multikron helper function defined inside the Hamiltonian module.

Checklist:

  • Reviewers confirm new code works as expected.
  • Tests are passing.
  • Coverage does not decrease.
  • Documentation is updated.

@renatomello renatomello added this to the Qibo 0.2.13 milestone Aug 26, 2024
@renatomello renatomello self-assigned this Aug 26, 2024
@renatomello renatomello modified the milestones: Qibo 0.2.13, Qibo 0.2.12 Aug 26, 2024
src/qibo/derivative.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Aug 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.94%. Comparing base (386fe25) to head (5b26fe6).
Report is 16 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1428   +/-   ##
=======================================
  Coverage   99.94%   99.94%           
=======================================
  Files          78       78           
  Lines       11309    11307    -2     
=======================================
- Hits        11303    11301    -2     
  Misses          6        6           
Flag Coverage Δ
unittests 99.94% <100.00%> (ø)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@renatomello renatomello added documentation Improvements or additions to documentation enhancement New feature or request and removed enhancement New feature or request labels Aug 27, 2024
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 @renatomello, LGTM.
Also the speed-up on _multikron makes sense, thanks for spotting it.

@alecandido
Copy link
Member

alecandido commented Aug 28, 2024

Unfortunately, while reduce() is definitely convenient and optimal style (because of its expressivity), I believe you won't get any performance boost, since it should be a very similar for loop under the hood (possibly a recursion, but that would be worse in Python... - unless it is tail-call optimized individually, which I really doubt...).

However, the other good reason to encourage this usage is that for other functions, you could really get a speed-up, by using the built-in reduce of NumPy universal functions, e.g. np.add.reduce(array) (in which case multikron would just be an alias).
Unfortunately, np.kron is not a universal function (and it can't be, because it's not acting element-wise - so you would really need multikron to be implemented in NumPy, otherwise this is the best you can do).

@renatomello
Copy link
Contributor Author

Unfortunately, while reduce() is definitely convenient and optimal style (because of its expressivity), I believe you won't get any performance boost, since it should be a very similar for loop under the hood (possibly a recursion, but that would be worse in Python... - unless it is tail-call optimized individually, which I really doubt...).

However, the other good reason to encourage this usage is that for other functions, you could really get a speed-up, by using the built-in reduce of NumPy universal functions, e.g. np.add.reduce(array) (in which case multikron would just be an alias). Unfortunately, np.kron is not a universal function (and it can't be, because it's not acting element-wise - so you would really need multikron to be implemented in NumPy, otherwise this is the best you can do).

In [11]: from functools import reduce
    ...: from itertools import product
    ...: 
    ...: import numpy as np
    ...: 
    ...: from qibo import matrices
    ...: 
    ...: 
    ...: def multikron(matrix_list):
    ...:     h = 1
    ...:     for m in matrix_list:
    ...:         h = np.kron(h, m)
    ...:     return h
    ...: 
    ...: def new_multikron(matrix_list):
    ...:     return reduce(np.kron, matrix_list)
    ...: 
    ...: single_paulis = [matrices.I, matrices.X, matrices.Y, matrices.Z]
    ...: 
    ...: for nqubits in [2, 3, 4, 5]:
    ...:     for func in [multikron, new_multikron]:
    ...:         print(f"nqubits: {nqubits}, func: {func.__name__}")
    ...: 
    ...:         matrix_list = np.asarray(list(product(single_paulis, repeat=nqubits)))
    ...:         indexes = np.random.choice(range(4**nqubits), size=4, replace=False)
    ...:         for row in matrix_list[indexes]:
    ...:             %timeit func(row)
    ...:         print()
    ...: 
nqubits: 2, func: multikron
36.5 μs ± 270 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
36.5 μs ± 57.4 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
36.4 μs ± 76.8 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
36.5 μs ± 76.3 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

nqubits: 2, func: new_multikron
18.4 μs ± 65.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
19.6 μs ± 405 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
19.9 μs ± 884 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
19.1 μs ± 1.06 μs per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

nqubits: 3, func: multikron
55 μs ± 427 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
54.4 μs ± 158 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
54.4 μs ± 167 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
54.7 μs ± 191 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

nqubits: 3, func: new_multikron
35.9 μs ± 434 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
36.4 μs ± 858 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
35.6 μs ± 535 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
36.3 μs ± 389 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

nqubits: 4, func: multikron
80 μs ± 5.24 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
76.3 μs ± 5.42 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
77.4 μs ± 2.96 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
76.9 μs ± 2.89 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

nqubits: 4, func: new_multikron
55.9 μs ± 1.44 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
54.2 μs ± 890 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
54.5 μs ± 423 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
55 μs ± 530 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

nqubits: 5, func: multikron
95.9 μs ± 1.27 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
99.7 μs ± 4.35 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
101 μs ± 3.67 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
99.1 μs ± 1.87 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

nqubits: 5, func: new_multikron
78.8 μs ± 694 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
78 μs ± 1.09 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
76.9 μs ± 943 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
78.5 μs ± 2.82 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

@alecandido
Copy link
Member

alecandido commented Aug 29, 2024

Testing on small samples might be deceptive.

time-kron

time-kron.zip

@renatomello
Copy link
Contributor Author

renatomello commented Aug 30, 2024

Testing on small samples might be deceptive.

time-kron

time-kron.zip

So it is faster for smaller $n$ and the same scaling for bigger $n$ and somehow that's a bad thing?

@renatomello renatomello added this pull request to the merge queue Aug 30, 2024
Merged via the queue into master with commit f20f433 Aug 30, 2024
30 checks passed
@renatomello renatomello deleted the doc_hamiltonian branch August 30, 2024 05:12
@alecandido
Copy link
Member

alecandido commented Aug 30, 2024

So it is faster for smaller n and the same scaling for bigger n and somehow that's a bad thing?

@renatomello I've never said it's a bad thing. Please, do not make inference about missing statements.
It's just marginal, since it's only relevant up to 4-5 qubits (where the difference is ~20%, on a small operation).

If you want to find a "bad" part (i.e. whatever I was trying to make more precise), it is:

  • a faster implementation of the multikron helper function

which is quite a misleading description. The core of the operation is an array product, which is unchanged. The same optimization you gained here it could be obtained in many different places in Qibo - especially saving less redundant information, and using fewer classes - since it's just related to a Python overhead.

However, it's not worth investing, because it is not giving any significant advantage for most use cases - the overhead is only significant for operations with ~<5 qubits. Already, for ~10 qubits, the array operations are always more relevant.

Nevertheless, as I said in the first place, it is good to improve readability and maintainability of the code. Using more expressive syntax is part of the game, and that's the main advantage of the new multikron - which I acknowledged immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants