vprusso / toqito

|toqito> (Theory of Quantum Information Toolkit) in Python :snake:
https://toqito.readthedocs.io/en/latest/
MIT License
155 stars 61 forks source link

Enhancement: Parallelize classical value of nonlocal game #12

Open vprusso opened 4 years ago

vprusso commented 4 years ago

The following loop inside of the nonlocal_game.py file under the classical_value function should be parallelized for large nonlocal games.

        for i in range(num_alice_outputs ** num_bob_inputs):
           ...
           tgval = np.sum(np.amax(pred_alice, axis=0))
            p_win = max(p_win, tgval)
        return p_win

This parallelization is also performed in QETLAB where indeed the approach to calculating the classical value of a nonlocal game has been inspired from.

Shivansh20128 commented 1 week ago

Hi @vprusso , this looks interesting. However, it will require some more work than just following what they have done in QETLAB, as there they have used a parfor loop, which is a parallel for loop which we is not available in Python. So we need to look for alternatives, like joblibs or concurrent.futures. But we might also need to add some dependencies to this project as joblib needs an installation too. Please let me know how we should approach this, if this enhancement is still needed. Thank You

vprusso commented 1 week ago

Hey @Shivansh20128 ,

True, there are multiple approaches one could take for this issue. As you mentioned joblib is one. Another might be to use something like multiprocessing:

import multiprocessing

def my_function(x):
    # Just a dummy function, this would be where we would invoke the NLG function.
    return x * x

if __name__ == "__main__":
    data = [1, 2, 3, 4, 5]
    pool = multiprocessing.Pool()  # Create a pool of processes

    result = pool.map(my_function, data)  # This is similar to MATLAB's parfor

    pool.close()  # Close the pool to new tasks
    pool.join()   # Wait for all worker processes to complete

    print(result)

I've had varying experiences with being able to write effective multiprocessing code, but it might be one of the possible approaches to try here. Happy to hear if you have any specific ways of handling this that you think might be particularly useful or elegant! Thanks again for your interest and help, @Shivansh20128 !

Shivansh20128 commented 1 week ago

I have added a PR. Can you also assign this to me? Thank you

vprusso commented 1 week ago

Done! :)