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

Matrix product state simulator aborts with wrong error message #1920

Closed
smiserman opened this issue Aug 29, 2023 · 3 comments
Closed

Matrix product state simulator aborts with wrong error message #1920

smiserman opened this issue Aug 29, 2023 · 3 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@smiserman
Copy link

smiserman commented Aug 29, 2023

Informations

  • Qiskit Aer version: 0.12.1:
  • Python version: 3.10.9:
  • Operating system: Win11, 16 GB RAM:

What is the current behavior?

The AerSimulator(method='matrix_product_state') aborts with an error message if the number of qubits of the circuit is greater than the maximum memory / 32 MB, regardless of the size of a generated MPS. With 16 GB RAM the number of qubits is limited to 504, which is in any case not necessary with a moderate bond dimension.

Steps to reproduce the problem

num_qubits = 505
circ = QuantumCircuit(num_qubits)
circ.h(0)
for i in range (0, num_qubits-1):
    circ.cx(i, i+1)
circ.measure_all()

simulator = AerSimulator(method='matrix_product_state')
res = simulator.run(circ).result()

gives
image

What is the expected behavior?

The simulation of the circuit above works fine in 1-2 sec for 504 qubits. So it should not abort with 505 qubits. Furthermore the required memory for the MPS is only about 0.3 MB and not $505 \cdot 32 = 16160$ MB!

Suggested solutions

You should keep track of the size of the generated MPS, which is bounded above by approximately $N (32 d^2 + 8 d)$ bytes, where $N$ is the number of qubits and $d$ is the maximum bond dimension. There is some overhead for list, tuple, and Ndarray structures that can be neglected for larger $d$. In my own simulation, run in Numpy, I get $N (32 d^2 + 8 d + 440)$ and which, interestingly, is several seconds faster than the qiskit simulation for larger $d.$ So one should be able to comfortably simulate 1000 qubit circuits with d = 64 (correspond to 6 entanglement blocks in a VQE ansatz) and even more so with d=2 on a 16 GB RAM device.

Another remark
You should adapt your matrix product state simulation method tutorial in qiskit: We can handle more qubits than this, but execution may take a few minutes. Try running a similar circuit with 500 qubits! Or maybe even 1000 (you can get a cup of coffee while waiting).
This is definitly not true! With constant bond dimension the matrix product state algorithm scales linearly with the number of qubits and the number of shots (as long shots $\gg d$). So if it takes about 0.1 seconds for 50 qubits, it takes about one second for 500 qubits and two seconds for 1000 qubits. That's not a few minutes and certainly not time to drink a coffee! And this is not only theoretical, I checked the linearity practically on my as well as on Aer Simulation.

@smiserman smiserman added the bug Something isn't working label Aug 29, 2023
@smiserman smiserman changed the title Matrix product Simulator aborts with wrong error messa Matrix product Simulator aborts with wrong error message Aug 29, 2023
@smiserman smiserman changed the title Matrix product Simulator aborts with wrong error message Matrix product state simulator aborts with wrong error message Aug 29, 2023
@doichanj
Copy link
Collaborator

size_t State::required_memory_mb(uint_t num_qubits,
const std::vector<Operations::Op> &ops) const {
// for each qubit we have a tensor structure.
// Initially, each tensor contains 2 matrices with a single complex double
// Depending on the number of 2-qubit gates,
// these matrices may double their size
// for now - compute only initial size
// later - FIXME
size_t mem_mb = 16 * 2 * num_qubits;
return mem_mb;
}

I think this function returns required memory size in byte not in MB, but by dividing this by 1000000 it returns 0.
Comment in this code says FIXME, so this should be fixed

@doichanj doichanj added this to the Aer 0.13.0 milestone Aug 30, 2023
@smiserman
Copy link
Author

Ok, but if you just divide this by 1000000, this is a very optimistic assumption without any information ;-). And in my opinion, the size of the MPS is not determined by the number of 2-qubit gates, but by the number of entanglement blocks (you can add any number of CNOT gates for the above EPR state without changing the bond dimension from 2) . However, you don't know this in advance, so you have to keep the maximum bond dimension tracked while calculating the MPS from the circuit, as I suggested.

@doichanj
Copy link
Collaborator

I think this should be fixed by PR #1933

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

No branches or pull requests

2 participants