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

CliffordBackend with packed bits #1239

Merged
merged 39 commits into from
Apr 30, 2024
Merged

Conversation

BrunoLiegiBastonLiegi
Copy link
Contributor

@BrunoLiegiBastonLiegi BrunoLiegiBastonLiegi commented Mar 1, 2024

This PR introduces a further optimization for the CliffordBackend. Namely the bits of the symplectic matrix are now packed as uint8 arrays by the np.packbits function.

Checklist:

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

Copy link

codecov bot commented Mar 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.81%. Comparing base (75c026a) to head (267cad3).

❗ Current head 267cad3 differs from pull request most recent head 5544e50. Consider uploading reports for the commit 5544e50 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1239   +/-   ##
=======================================
  Coverage   99.81%   99.81%           
=======================================
  Files          72       72           
  Lines       10490    10517   +27     
=======================================
+ Hits        10471    10498   +27     
  Misses         19       19           
Flag Coverage Δ
unittests 99.81% <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.

@renatomello renatomello added the quantum_info module PRs and issues related to the quantum information module label Mar 4, 2024
@renatomello renatomello added this to the Qibo 0.2.6 milestone Mar 4, 2024
@renatomello renatomello added the enhancement New feature or request label Mar 11, 2024
@scarrazza scarrazza modified the milestones: Qibo 0.2.6, Qibo 0.2.7 Mar 13, 2024
Co-authored-by: Andrea Pasquale <andreapasquale97@gmail.com>
@scarrazza scarrazza modified the milestones: Qibo 0.2.7, Qibo 0.2.8 Apr 5, 2024
@BrunoLiegiBastonLiegi
Copy link
Contributor Author

@alecandido I was following your proposed order in #1231 (comment) for this PR. However, now that in main the qibojit test dependency points to the branch py3.12, I cannot do the same with this PR, right? Because I would need to point to another qibojit branch. One possible solution, I guess, would be to merge my qibojit PR into py3.12.
Maybe, while we wait for qibo-core to be ready, we could set up a tmp branch in each repo for handling this situations.

@alecandido
Copy link
Member

In this case, the solution would be to rebase your PR (qiboteam/qibojit#173) on top of Qibojit's py3.12, and you can point this PR to that branch.

And yes, it's not great, but now PRs are rather coupled. So, to avoid broken tests, we should first merge qiboteam/qibojit#170

Copy link
Member

@alecandido alecandido left a comment

Choose a reason for hiding this comment

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

I didn't really check all the corners thoroughly (i.e. rechecking the meaning of existing functions, and that has been preserved), but I read the whole diff and I only have stylistic suggestions

src/qibo/backends/_clifford_operations.py Outdated Show resolved Hide resolved
Comment on lines +447 to +450
r, x, z = _get_rxz(state, nqubits)
x = _packbits(x, axis=1)
z = _packbits(z, axis=1)
return np.hstack((x, z, r[:, None]))
Copy link
Member

Choose a reason for hiding this comment

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

Why are you packing x and z separately?

Copy link
Member

Choose a reason for hiding this comment

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

I guess the motivation is the _rowsum function above, in which you need to treat separately x, z, and r, but just to get a confirmation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes precisely, because a single row of the matrix contains the encoding for both x, z and r, thus you cannot pack the complete row in one go but pack the different sections of the row separately.

return state


def _clifford_post_execution_reshape(state, nqubits):
def _clifford_post_execution_reshape(state, nqubits: int):
Copy link
Member

Choose a reason for hiding this comment

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

This is asymmetric with respect to the one above, i.e. there is no pack flag (and it is always unpacking).

What's the reason?

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 remember that the pre- function was called both before execution, but also in correspondence of collapsing measurements, where packing is not needed. But I will doublecheck because after I updated the measurement I might have changed this mechanism.

Copy link
Contributor Author

@BrunoLiegiBastonLiegi BrunoLiegiBastonLiegi Apr 30, 2024

Choose a reason for hiding this comment

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

I double checked and indeed the pack arg can be dropped, thanks for spotting this

src/qibo/backends/_clifford_operations.py Outdated Show resolved Hide resolved
@alecandido
Copy link
Member

Of course, whenever you can, merge/rebase on master

@BrunoLiegiBastonLiegi
Copy link
Contributor Author

BrunoLiegiBastonLiegi commented Apr 29, 2024

I believe there is a problem with the qibotn dependency, the py3.12 branch needs to be updated as it still points to the qibo/py3.12 branch (https://github.com/qiboteam/qibotn/blob/3718d5c8bca63f63bfd42234a2679adf9776951e/pyproject.toml#L24C1-L25C1) that doesn't exist anymore (I presume) and this make the poetry lock crash.

@alecandido
Copy link
Member

I believe there is a problem with the qibotn dependency, the py3.12 branch needs to be updated as it still points to the qibo/py3.12 branch (qiboteam/qibotn@3718d5c/pyproject.toml#L24C1-L25C1) that doesn't exist anymore (I presume) and this make the poetry lock crash.

I saw you updated it, thanks.

Simply, no one touched it after the merge.

@BrunoLiegiBastonLiegi
Copy link
Contributor Author

BrunoLiegiBastonLiegi commented Apr 29, 2024

There is a test, test_models_circuit_features::test_repeated_execute_probs_and_freqs, which fails with pytorch but only under macos, due to some problem with the seed most likely.

I fixed it by setting a test target that depends on the platform.system()

@BrunoLiegiBastonLiegi BrunoLiegiBastonLiegi mentioned this pull request Apr 29, 2024
4 tasks
@alecandido
Copy link
Member

alecandido commented Apr 29, 2024

due to some problem with the seed most likely.

Is the seed not fixed explicitly anywhere? Are we relying on some default?

(it could be much more complicate than that, but this is the starting point to investigate)

@BrunoLiegiBastonLiegi
Copy link
Contributor Author

BrunoLiegiBastonLiegi commented Apr 30, 2024

Yes the seed is set

backend.set_seed(1234)

but for some reason pytorch yields different results with macos using the same seed.

I believe the problem exists in main as well, as I see the test failing in #1310 as well. Weirdly, this came out all of a sudden...

(EDIT): indeed I just saw the issue #1313

@alecandido
Copy link
Member

@scarrazza just provided #1317 as a (temporary) solution for the test problem. I'd suggest merging/rebasing on it (or just wait that it will be merged in master)

@BrunoLiegiBastonLiegi BrunoLiegiBastonLiegi added this pull request to the merge queue Apr 30, 2024
Merged via the queue into master with commit 9538523 Apr 30, 2024
18 of 25 checks passed
@scarrazza scarrazza deleted the clifford_simulator_numba branch June 25, 2024 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request quantum_info module PRs and issues related to the quantum information module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants