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

Add sparse_encoder to models.encodings #1591

Open
wants to merge 64 commits into
base: master
Choose a base branch
from
Open

Conversation

renatomello
Copy link
Contributor

@renatomello renatomello commented Feb 17, 2025

This is based on Section IV of https://arxiv.org/abs/2405.20408

Depends on qiboteam/qibojit#220

Checklist:

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

@renatomello renatomello added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 17, 2025
@renatomello renatomello added this to the Qibo 0.2.17 milestone Feb 17, 2025
@renatomello renatomello self-assigned this Feb 17, 2025
@renatomello renatomello linked an issue Feb 17, 2025 that may be closed by this pull request
Copy link

codecov bot commented Feb 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.11%. Comparing base (c818eba) to head (2505333).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1591   +/-   ##
=======================================
  Coverage   99.11%   99.11%           
=======================================
  Files          77       77           
  Lines       11475    11571   +96     
=======================================
+ Hits        11373    11469   +96     
  Misses        102      102           
Flag Coverage Δ
unittests 99.11% <100.00%> (+<0.01%) ⬆️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@scarrazza scarrazza modified the milestones: Qibo 0.2.17, Qibo 0.2.18 Mar 19, 2025
@renatomello
Copy link
Contributor Author

Ah ok, so that one should be rebased to this one, I guess. I would prefer not to merge something that is failing in main, this will make all the other PR fail.

Can I merge that one into this one and make it just one PR then?

@renatomello
Copy link
Contributor Author

renatomello commented Apr 10, 2025

@BrunoLiegiBastonLiegi @AlejandroSopena all tests in models.encodings are passing for GPUs now. I'm not going to fix all the other tests unrelated to that module in this PR to not make it even more bloated than it already is.

@renatomello
Copy link
Contributor Author

@BrunoLiegiBastonLiegi @AlejandroSopena all tests in models.encodings are passing for GPUs now. I'm not going to fix all the other tests unrelated to that module in this PR to not make it even more bloated than it already is.

I lied. It's fixed now.

Copy link

Run on QPU sim completed! :atom:

You can download the coverage report as an artifact, from the workflow summary page:
https://github.com/qiboteam/qibo/actions/runs/14379519102

@renatomello renatomello linked an issue Apr 10, 2025 that may be closed by this pull request
Comment on lines +183 to +186
_data_test = data[0][1]
_data_test = (
_data_test.dtype if "array" in str(type(_data_test)) else type(_data_test)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_data_test = data[0][1]
_data_test = (
_data_test.dtype if "array" in str(type(_data_test)) else type(_data_test)
)
_data_test = (
_data_test.dtype if "array" in str(type(data[0][1])) else type(data[0][1])
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't want to do that to not call data[0][1] three times in the same line

Comment on lines +173 to +177
# TODO: Fix this mess with qibo native dtypes
try:
type_test = bool("int" in str(data[0][0].dtype))
except AttributeError:
type_test = bool("int" in str(type(data[0][0])))
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah indeed I think it's time we address this because it's extremely tedious.

Copy link
Contributor Author

@renatomello renatomello Apr 14, 2025

Choose a reason for hiding this comment

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

Maybe this is a good issue for the unitaryHACK @alecandido @AlejandroSopena

thetas = _generate_rbs_angles(
_data_sorted, architecture="diagonal", backend=backend
)
phis = backend.np.zeros(len(thetas) + 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

This may be failing with CupyBackend as backend.np is numpy and not cupy. Or better, it may not fail but it most likely won't use cupy arrays, and thus GPU.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fix, in my opinion, is to be done outside of this PR. I have been thinking of refactoring this part of the backends as something like backend.engine or backend.platform (or even backend.math) and drop backend.np entirely from every backend.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah yeah I agree, just pointing out here, if you want to simply patch it here you could just do a backend.cast even though it's not the cleanest solution...

for qubit, bit in enumerate(bitstrings_sorted[0]):
if bit == 1:
touched_qubits.append(qubit)
touched_qubits.append(int(qubit))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you actually need the explicit cast to int, enumerate should produce an int in any case I believe. In any case, if bitstring_sorted is a int8 np array you could probably just do

touched_qubits = (bitstrings_sorted[0] == 1).nonzero()[0]

Copy link
Contributor Author

@renatomello renatomello Apr 14, 2025

Choose a reason for hiding this comment

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

Yes, the cast to int needs to be explicit because of the GPU backends.

list(backend.np.argsort(b_0)[-hw_0:]),
list(backend.np.argsort(b_1)[-hw_1:]),
)
ones, new_ones = {int(elem) for elem in ones}, {int(elem) for elem in new_ones}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't you just do

Suggested change
ones, new_ones = {int(elem) for elem in ones}, {int(elem) for elem in new_ones}
ones, new_ones = set(ones), set(new_ones)

?

Copy link
Contributor Author

@renatomello renatomello Apr 14, 2025

Choose a reason for hiding this comment

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

This doesn't work again because of GPU backends. The elements are unhashable without the explicit casting to int.

if qubit not in touched_qubits:
touched_qubits.append(qubit)
touched_qubits.append(int(qubit))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
touched_qubits.append(int(qubit))
touched_qubits.append(qubit)

qubit should already be an int, as you just casted it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They need to be actual ints instead of cp.int8 otherwise some other errors are raised down the line.

Copy link

Run on QPU sim completed! :atom:

You can download the coverage report as an artifact, from the workflow summary page:
https://github.com/qiboteam/qibo/actions/runs/14444165235

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 enhancement New feature or request run-on-sim
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test failing on GPU for both cupy and cuquantum
4 participants