BrunoRosendo / master-thesis

Source code for my master's thesis, in the topic "Quantum algorithms for optimizing urban transportation"
MIT License
5 stars 0 forks source link

Value error with more than 32 variables #76

Closed BrunoRosendo closed 4 months ago

BrunoRosendo commented 4 months ago

The error is the following:

Traceback (most recent call last):
  File "/home/brosendo/.local/share/JetBrains/Toolbox/apps/pycharm-professional/plugins/python/helpers/pydev/pydevd.py", line 1534, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/brosendo/.local/share/JetBrains/Toolbox/apps/pycharm-professional/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/brosendo/Documents/Thesis/src/main.py", line 40, in <module>
    result = cvrp.solve()
  File "/home/brosendo/Documents/Thesis/src/solver/VRPSolver.py", line 92, in solve
    result = self._solve_cvrp()
  File "/home/brosendo/Documents/Thesis/src/solver/qubo/DWaveSolver.py", line 54, in _solve_cvrp
    result = self.sampler.sample_cqm(cqm, time_limit=5)
  File "/home/brosendo/.local/lib/python3.10/site-packages/dimod/reference/samplers/exact_solver.py", line 245, in sample_cqm
    cases = _all_cases_cqm(cqm)
  File "/home/brosendo/.local/lib/python3.10/site-packages/dimod/reference/samplers/exact_solver.py", line 293, in _all_cases_cqm
    c1 = np.array(np.meshgrid(*cases))
  File "<__array_function__ internals>", line 180, in meshgrid
  File "/home/brosendo/.local/lib/python3.10/site-packages/numpy/lib/function_base.py", line 4977, in meshgrid
    output = [np.asanyarray(x).reshape(s0[:i] + (-1,) + s0[i + 1:])
  File "/home/brosendo/.local/lib/python3.10/site-packages/numpy/lib/function_base.py", line 4977, in <listcomp>
    output = [np.asanyarray(x).reshape(s0[:i] + (-1,) + s0[i + 1:])
ValueError: maximum supported dimension for an ndarray is 32, found 510

A possible input:

cvrp = DWaveSolver(
        2,
        None,
        [
            (456, 320),
            (228, 0),
            (912, 0),
            (0, 80),
            (114, 80),
            (570, 160),
            (798, 160),
            (342, 240),
            (684, 240),
            (570, 400),
            (912, 400),
            (114, 480),
            (228, 480),
            (342, 560),
            (684, 560),
            (0, 640),
            (798, 640),
        ],
        [
            (1, 6, 5),
            (2, 10, 6),
            (4, 3, 4),
            (5, 9, 2),
            (7, 8, 7),
            (15, 11, 4),
            (13, 12, 6),
            (16, 14, 4),
        ],
        True,
        True,
        # sampler=LeapHybridCQMSampler(),
    )
BrunoRosendo commented 4 months ago

A similar thing happens with cplex:

Traceback (most recent call last):
  File "/home/brosendo/.local/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 35, in wrapper
    return func(*args, **kwargs)
  File "/home/brosendo/.local/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 278, in _minimize_cobyla
    sf = _prepare_scalar_function(fun, x0, args=args, jac=_jac)
  File "/home/brosendo/.local/lib/python3.10/site-packages/scipy/optimize/_optimize.py", line 383, in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,
  File "/home/brosendo/.local/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 158, in __init__
    self._update_fun()
  File "/home/brosendo/.local/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 251, in _update_fun
    self._update_fun_impl()
  File "/home/brosendo/.local/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 155, in update_fun
    self.f = fun_wrapped(self.x)
  File "/home/brosendo/.local/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 137, in fun_wrapped
    fx = fun(np.copy(x), *args)
  File "/home/brosendo/.local/lib/python3.10/site-packages/qiskit_algorithms/minimum_eigensolvers/sampling_vqe.py", line 309, in evaluate_energy
    ).result()
  File "/home/brosendo/.local/lib/python3.10/site-packages/qiskit/primitives/primitive_job.py", line 55, in result
    return self._future.result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/usr/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/brosendo/.local/lib/python3.10/site-packages/qiskit_algorithms/minimum_eigensolvers/diagonal_estimator.py", line 118, in _call
    sampler_result = job.result()
  File "/home/brosendo/.local/lib/python3.10/site-packages/qiskit/primitives/primitive_job.py", line 55, in result
    return self._future.result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 451, in result
    return self.__get_result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/usr/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/brosendo/.local/lib/python3.10/site-packages/qiskit/primitives/sampler.py", line 99, in _call
    probabilities = [
  File "/home/brosendo/.local/lib/python3.10/site-packages/qiskit/primitives/sampler.py", line 100, in <listcomp>
    Statevector(bound_circuit_to_instruction(circ)).probabilities_dict(
  File "/home/brosendo/.local/lib/python3.10/site-packages/qiskit/quantum_info/states/statevector.py", line 91, in __init__
    self._data = Statevector.from_instruction(data).data
  File "/home/brosendo/.local/lib/python3.10/site-packages/qiskit/quantum_info/states/statevector.py", line 765, in from_instruction
    init = np.zeros(2**instruction.num_qubits, dtype=complex)
ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size.

input:

cvrp = CplexSolver(
        2,
        None,
        [
            (456, 320),
            (228, 0),
            (912, 0),
            (0, 80),
            (114, 80),
            # (570, 160),
            # (798, 160),
            # (342, 240),
            # (684, 240),
            # (570, 400),
            # (912, 400),
            # (114, 480),
            # (228, 480),
            # (342, 560),
            # (684, 560),
            # (0, 640),
            # (798, 640),
        ],
        [
            (2, 1, 6),
            (3, 0, 5),
            (4, 1, 2),
        ],
        True,
        False,
    )
BrunoRosendo commented 4 months ago

The dwave error is not a problem, it only happens with the exact solver which is not made for large problems

BrunoRosendo commented 4 months ago

As for IBM, it happens only with the local sampler (not the classical optimizer), so I think it might be a similar case. I'm now waiting to test this in a real machine. I might use too many minutes but I think it's worth it to discard this problem

BrunoRosendo commented 4 months ago

After running an iteration in a real IBM machine, I conclude this is in fact not a problem (free stress :D )