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

Aer fails to save MPS if it estimates (incorrectly) that there is insufficient memory #2246

Closed
garrison opened this issue Oct 17, 2024 · 2 comments · Fixed by #2248
Closed
Labels
bug Something isn't working

Comments

@garrison
Copy link
Member

garrison commented Oct 17, 2024

Informations

  • Qiskit Aer version: 0.15.1
  • Python version: 3.11
  • Operating system: Linux

What is the current behavior?

First of all, I find that #2056 is still a problem even though it is marked as fixed.

Secondly, at #2059 it was mentioned that I can use max_memory_mb=-1 to ignore the check of required memory. However, even when that is passed, the save_matrix_product_state instruction will fail to create an MPS if the error would have been triggered. Here's a full example:

from qiskit import QuantumCircuit
from qiskit.circuit.library import PauliEvolutionGate
from qiskit.quantum_info import SparsePauliOp
from qiskit.synthesis import SuzukiTrotter
from qiskit_aer import AerSimulator

L = 100
hamiltonian = SparsePauliOp.from_sparse_list([], num_qubits=L)
for x in range(L - 1):
    hamiltonian += SparsePauliOp.from_sparse_list(
        [
            ("XX", (x, x + 1), 1),
            ("YY", (x, x + 1), 1),
        ],
        num_qubits=L,
    )
sim = AerSimulator(
    method="matrix_product_state",
    matrix_product_state_max_bond_dimension=10,
    max_memory_mb=-1,
)
qc = QuantumCircuit(L)
qc.append(
    PauliEvolutionGate(hamiltonian, synthesis=SuzukiTrotter(reps=100)), qargs=qc.qubits
)
qc = qc.decompose()
qc.save_matrix_product_state("mps")
sim.run(qc).result().data(0)["mps"]

fails with

Traceback (most recent call last):
  File "/var/home/garrison/Qiskit/aer-mps-fail.py", line 19, in <module>
    sim.run(qc).result().data(0)["mps"]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
KeyError: 'mps'

However, if you change it to L = 10, it works (at least on my system; these numbers likely need to be tweaked slightly elsewhere).

Steps to reproduce the problem

Code example above

What is the expected behavior?

It should save the matrix product state, especially if given max_memory_mb=-1. And ideally the memory check will be fixed so that passing max_memory_mb=-1 is not necessary, especially when matrix_product_state_max_bond_dimension already provides an upper bound on the memory that can be used.

Suggested solutions

I have not examined the code yet but may soon.

@garrison garrison added the bug Something isn't working label Oct 17, 2024
@garrison
Copy link
Member Author

I just received a report from another user that who was able to reproduce the bug on both macOS and Linux using the latest Aer release (0.15.1).

@gadial
Copy link
Collaborator

gadial commented Oct 29, 2024

The error results from the Executor<state_t>::get_max_parallel_shots method here. Basically, even when max_memory_mb=-1 the memory check in that method is still performed, and return std::min(circ.shots * circ.num_bind_params, (max_memory_mb_ / mem)); results in the value 0 when mem is too big, meaning the simulator won't run at all.

An easy fix is to change line 431 to

if (mem == 0 || !check_required_memory_)

which returns the memory-agnostic value. This ensures the above code runs successfully so I'll open a PR with this fix, but it does not deal with the wrong memory estimation for the MPS simulator performed here. I'll try taking a look at this next, but feel free to elaborate if you see the problem.

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 a pull request may close this issue.

2 participants