cirKITers / qml-essentials

Python package with commonly used Ansaetze and Data-Reuploading Model
https://cirkiters.github.io/qml-essentials/
MIT License
0 stars 0 forks source link

[BUG] Density simulator runs into numpy problem for n_qubits > 14 #26

Open majafranz opened 3 weeks ago

majafranz commented 3 weeks ago

Describe the bug It seems like the numpy interface for the density simulator has a limit of 14 Qubits.

To Reproduce Code sample that fails:

from qml_essentials.model import Model

n_qubits = 15
output_qubit = list(range(n_qubits))
model = Model(
    n_qubits=n_qubits,
    n_layers=1,
    circuit_type="Hardware_Efficient",
    output_qubit=output_qubit
)

res = model(
        params=model.params,
        inputs=None,
        execution_type="density"
)

changing the number of qubits to 14 or lower works as expected.

Expected behavior Well, would be nice if this would not crash :/

Screenshots See Stacktrace:

Traceback (most recent call last):
  File "<path>/test.py", line 12, in <module>
    res = model(
          ^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/qml_essentials/model.py", line 406, in __call__
    return self._forward(params, inputs, noise_params, cache, execution_type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/qml_essentials/model.py", line 487, in _forward
    result = self.circuit_mixed(
             ^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/workflow/qnode.py", line 1098, in __call__
    res = self._execution_component(args, kwargs, override_shots=override_shots)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/workflow/qnode.py", line 1052, in _execution_component
    res = qml.execute(
          ^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/workflow/execution.py", line 616, in execute
    results = inner_execute(tapes)
              ^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/workflow/execution.py", line 297, in inner_execute
    results = device_execution(transformed_tapes)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/maja/.pyenv/versions/3.11.9/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/_qubit_device.py", line 490, in batch_execute
    res = self.execute(circuit)
          ^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/devices/default_mixed.py", line 767, in execute
    return super().execute(circuit, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/_qubit_device.py", line 281, in execute
    self.apply(
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/devices/default_mixed.py", line 781, in apply
    self._apply_operation(operation)
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/devices/default_mixed.py", line 711, in _apply_operation
    self._apply_channel(matrices, wires)
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/devices/default_mixed.py", line 375, in _apply_channel
    self._state = qnp.einsum(einsum_indices, kraus, self._state, kraus_dagger)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/math/multi_dispatch.py", line 551, in einsum
    return np.einsum(indices, *operands, like=like)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/autoray/autoray.py", line 81, in do
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/pennylane/numpy/wrapper.py", line 117, in _wrapped
    res = obj(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/autograd/tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.11/site-packages/numpy/core/einsumfunc.py", line 1371, in einsum
    return c_einsum(*operands, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: too many subscripts in einsum

Additional context We have problems with scaling anyway, see #23.

majafranz commented 3 weeks ago

maybe using pennylane lightning simulator could fix this