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

Fix and speed-up DBI tests #1245

Merged
merged 3 commits into from
Mar 6, 2024
Merged

Fix and speed-up DBI tests #1245

merged 3 commits into from
Mar 6, 2024

Conversation

andrea-pasquale
Copy link
Contributor

@andrea-pasquale andrea-pasquale commented Mar 3, 2024

Now DBI tests are much faster and the implementation is GPU compatible.

Checklist:

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

@andrea-pasquale andrea-pasquale added this to the Qibo 0.2.6 milestone Mar 3, 2024
@andrea-pasquale andrea-pasquale self-assigned this Mar 3, 2024
Copy link

codecov bot commented Mar 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.95%. Comparing base (62c4dfd) to head (054e045).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1245   +/-   ##
=======================================
  Coverage   99.95%   99.95%           
=======================================
  Files          72       72           
  Lines       10452    10454    +2     
=======================================
+ Hits        10447    10449    +2     
  Misses          5        5           
Flag Coverage Δ
unittests 99.95% <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
Copy link
Contributor

renatomello commented Mar 3, 2024

Maybe this is out of the scope of this PR, but the generate_Z_operators and str_to_symbolic in qibo.models.dbi.utils can be replaced by this faster implementation below:

from functools import reduce
from itertools import product

import numpy as np

from qibo import matrices
from qibo.models.dbi.utils import generate_Z_operators

def generate_Z_operators_new(nqubits):
    combination_strings = list(product(["I", "Z"], repeat=nqubits))[1:]

    dictionary = {}
    dictionary["I"*nqubits] = np.eye(2**nqubits, dtype=complex)
    for string in combination_strings:
        matrix = [getattr(matrices, s) for s in string]
        matrix = reduce(np.kron, matrix)
        dictionary["".join(string)] = matrix
        del matrix  # maybe this is needed to control memory usage for nqubits >= 7-ish

    return dictionary

nqubits = 2
%timeit generate_Z_operators(nqubits)
%timeit generate_Z_operators_new(nqubits)

nqubits = 5
%timeit generate_Z_operators(nqubits)
%timeit generate_Z_operators_new(nqubits)

This is in line of how qibo.quantum_info.basis.pauli_basis is implemented, and it might even be added to it using the pauli_order argument.
However, even though this is faster than the current implementation in dbi, it is still inefficient overall for a number of qubits greater than 7-ish. I had a conversation with @BrunoLiegiBastonLiegi about rewriting the pauli_basis function in a way that returns a generator instead of all these big matrices, so this might be even one more reason to do it.

@marekgluza
Copy link
Contributor

Maybe this is out of the scope of this PR, but the generate_Z_operators and str_to_symbolic in qibo.models.dbi.utils can be replaced by this faster implementation below:

from functools import reduce
from itertools import product

import numpy as np

from qibo import matrices
from qibo.models.dbi.utils import generate_Z_operators

def generate_Z_operators_new(nqubits):
    combination_strings = list(product(["I", "Z"], repeat=nqubits))[1:]

    dictionary = {}
    dictionary["I"*nqubits] = np.eye(2**nqubits, dtype=complex)
    for string in combination_strings:
        matrix = [getattr(matrices, s) for s in string]
        matrix = reduce(np.kron, matrix)
        dictionary["".join(string)] = matrix
        del matrix  # maybe this is needed to control memory usage for nqubits >= 7-ish

    return dictionary

nqubits = 2
%timeit generate_Z_operators(nqubits)
%timeit generate_Z_operators_new(nqubits)

nqubits = 5
%timeit generate_Z_operators(nqubits)
%timeit generate_Z_operators_new(nqubits)

This is in line of how qibo.quantum_info.basis.pauli_basis is implemented, and it might even be added to it using the pauli_order argument. However, even though this is faster than the current implementation in dbi, it is still inefficient overall for a number of qubits greater than 7-ish. I had a conversation with @BrunoLiegiBastonLiegi about rewriting the pauli_basis function in a way that returns a generator instead of all these big matrices, so this might be even one more reason to do it.

@Sam-XiaoyueLi please have a look

Thank you @renatomello for the useful suggestion!

@renatomello
Copy link
Contributor

@MatteoRobbiati may I ask you to review this soon because #1202 depends on it? Thanks.

@renatomello renatomello added the bug Something isn't working label Mar 6, 2024
@renatomello renatomello mentioned this pull request Mar 6, 2024
4 tasks
Copy link
Contributor

@MatteoRobbiati MatteoRobbiati left a comment

Choose a reason for hiding this comment

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

Thanks @andrea-pasquale for the fix and @renatomello for the reminder.
There is one test failing in test_quantum_info_metrics.py with the tensorflow backend, but I guess we can face it elsewhere.

@renatomello renatomello added this pull request to the merge queue Mar 6, 2024
Merged via the queue into master with commit d1fe5a9 Mar 6, 2024
21 checks passed
@renatomello renatomello deleted the fix_dbi_gpu branch March 6, 2024 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants