ngnrsaa / qflex

Flexible Quantum Circuit Simulator (qFlex) implements an efficient tensor network, CPU-based simulator of large quantum circuits.
Apache License 2.0
97 stars 24 forks source link

Add --memory flag to prevent OOM failures. #236

Closed 95-martin-orion closed 4 years ago

95-martin-orion commented 4 years ago

Determine the required space before attempting to allocate it, and cancel the simulation if required space exceeds a user-specified limit (default 1GB). Try it out:

$ src/qflex.x config/circuits/bristlecone_48_1-24-1_0.txt config/ordering/bristlecone_48.txt config/grid/bristlecone_48.txt -m 20000000

# should return:
ERROR (contraction_utils.cpp: Initialize:145) --> Required space (106.799 MB) exceeds memory limit (19.0735 MB). Cancelling simulation.

Also replaced long unsigned int with uint64_t, since on some systems long will only use 32 bits.

This partially addresses #141, but does not fully resolve it (tensors larger than rank 32 will still cause issues until #225 is submitted).

s-mandra commented 4 years ago

Looks good to me! I'll fix uint64_t --> std::size_t once we merge #225.

95-martin-orion commented 4 years ago

There were a couple of unneeded allocated_space += {...} lines, so I went ahead and changed to std::size_t along with that change.