diff --git a/check_cirq_8054/get-total-memory b/check_cirq_8054/get-total-memory new file mode 100755 index 000000000..aa9cce0e7 --- /dev/null +++ b/check_cirq_8054/get-total-memory @@ -0,0 +1,14 @@ +#!/usr/bin/gawk -f + +BEGIN { FS = "="; } + +/^snapshot=[0-9]+$/ {inrec = 1} +inrec && /^time=[0-9]+/ {t = $2 * 1e-9} +inrec && /^mem_heap_B/ {mem_heap_B = $2 + 0} +inrec && /^mem_heap_extra_B/ {mem_heap_extra_B = $2 + 0} + +inrec && /^heap_tree/ { + print t, mem_heap_B + mem_heap_extra_B; + t = mem_heap_B = mem_heap_extra_B = ""; + inrec = 0; +} diff --git a/check_cirq_8054/oom_qsim_simulation.py b/check_cirq_8054/oom_qsim_simulation.py new file mode 100644 index 000000000..87b4aa12a --- /dev/null +++ b/check_cirq_8054/oom_qsim_simulation.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +"""Script to check for maximum number of qubits where QSimSimulator segfaults.""" + +import gc +import sys + +import cirq.testing + +import qsimcirq + +assert __name__ == "__main__" +if len(sys.argv) != 2: + print(f"usage: {__file__} num_qubits") + sys.exit(0) + +nqubits = int(sys.argv[1]) + +q = cirq.LineQubit.range(nqubits) +m1 = cirq.Moment(cirq.H.on_each(q)) +m2 = cirq.Moment(cirq.CX(qi, qj) for qi, qj in zip(q[0::2], q[1::2])) +m3 = cirq.Moment(cirq.measure(*q)) +c = cirq.Circuit(10 * [m1, m2, m3]) + +my_sim = qsimcirq.QSimSimulator() + +gc.disable() +gc.collect() + +state_vector = my_sim.simulate(program=c).state_vector() +print(state_vector.shape, state_vector) diff --git a/check_cirq_8054/qsim_simulation.py b/check_cirq_8054/qsim_simulation.py new file mode 100644 index 000000000..3c620f090 --- /dev/null +++ b/check_cirq_8054/qsim_simulation.py @@ -0,0 +1,36 @@ +import gc + +import cirq.testing +import numpy as np + +import qsimcirq + +nqubits = 20 +# nqubits = 24 + +q = cirq.LineQubit.range(nqubits) +m1 = cirq.Moment(cirq.H.on_each(q)) +m2 = cirq.Moment(cirq.CX(qi, qj) for qi, qj in zip(q[0::2], q[1::2])) +m3 = cirq.Moment(cirq.measure(*q)) +c = cirq.Circuit(10 * [m1, m2, m3]) + +# make reproducible initial_state +rs = cirq.value.parse_random_state(8054) +initial_state = cirq.testing.random_superposition(2**nqubits, random_state=rs).astype( + np.complex64 +) +my_sim = qsimcirq.QSimSimulator() + +gc.disable() +gc.collect() + +# burn CPU cycles for a few seconds +for _ in range(50_000_000): + pass + +state_vector = my_sim.simulate(program=c, initial_state=initial_state).state_vector() +print(state_vector.shape, state_vector) + +# burn CPU cycles for a few seconds +for _ in range(50_000_000): + pass diff --git a/check_cirq_8054/results/README.md b/check_cirq_8054/results/README.md new file mode 100644 index 000000000..7ac763813 --- /dev/null +++ b/check_cirq_8054/results/README.md @@ -0,0 +1,42 @@ +# Check of memory in qsim simulation before and after cirq PR 8054 + +2026-05-27 + +Smallest number of qubits causing OOM crash, checked with +`oom_qsim_simulation.py` on a Debian box with 65 GB of memory. + +- baseline qsim c5bc02394a and cirq f4c35b02090b - OOM for 34 qubits +- patched qsim 7d05583a00e0 and cirq #8054 (bca9c5c617de) - OOM for 34 qubits + +# 2026-05-27 - RUN LIST ------------------------------------------------------ + +Memory use during `qsim_simulation.py` was collected using +``` +valgrind --tool=massif python qsim_simulation.py +``` +The output massif.out.NNNNNN files were converted to memtotal.NNNNNN.dat +using `get-total-memory` script. + +## baseline qsim and cirq ---------------------------------------------------- + +### nqubits=20 (repeated to check reproducibility) + +- memtotal.733604.dat +- memtotal.736557.dat + +### nqubits=24 (repeated to check reproducibility) + +- memtotal.737548.dat +- memtotal.738662.dat + +## patched qsim and cirq ----------------------------------------------------- + +### nqubits=20 (repeated to check reproducibility) + +- memtotal.742794.dat +- memtotal.743107.dat + +### nqubits=24 (repeated to check reproducibility) + +- memtotal.743990.dat +- memtotal.745873.dat diff --git a/check_cirq_8054/results/memtotal.733604.dat b/check_cirq_8054/results/memtotal.733604.dat new file mode 100644 index 000000000..02df9e3f2 --- /dev/null +++ b/check_cirq_8054/results/memtotal.733604.dat @@ -0,0 +1,57 @@ +# time(s) total(B) +0 0 +0.318674 1829576 +0.800413 1830656 +1.08539 1833280 +1.49621 3965600 +2.04109 11420768 +2.30147 13350360 +2.71264 17712696 +3.18717 19979776 +3.47974 23178960 +3.94386 23966192 +4.24774 22427040 +4.73715 26087592 +5.09345 21787608 +5.64853 34344200 +6.0532 23333832 +6.67339 23801272 +7.04263 24054344 +7.33646 26933312 +7.77634 26572128 +8.36293 31020344 +8.72959 27231152 +9.24349 30566016 +9.75678 29527336 +10.0368 31484856 +10.4515 32861584 +10.8521 34065888 +11.196 35819400 +11.5052 36376264 +12.1185 38817856 +12.5623 40904920 +12.8479 41389864 +13.4064 45973416 +13.7793 47525304 +14.251 46322464 +14.8548 46146888 +15.342 46148840 +15.9343 48427936 +16.4012 51152048 +16.9931 51333688 +17.316 54121000 +17.6824 54812296 +17.942 55830936 +18.3247 97230944 +20.4203 80317544 +44.9443 63538632 +45.2405 73033952 +45.7872 73034656 +46.152 73035184 +46.7043 73035976 +46.9648 73036416 +47.272 73036768 +47.5777 73037208 +47.8414 73037648 +48.1475 73038000 +48.4202 54819616 diff --git a/check_cirq_8054/results/memtotal.736557.dat b/check_cirq_8054/results/memtotal.736557.dat new file mode 100644 index 000000000..011da7740 --- /dev/null +++ b/check_cirq_8054/results/memtotal.736557.dat @@ -0,0 +1,68 @@ +# time(s) total(B) +0 0 +0.236307 1829576 +0.647684 1830656 +0.901119 1831376 +1.16279 1832096 +1.62568 4855864 +2.04185 9461272 +2.42849 14207000 +2.81795 18432504 +3.1332 26787080 +3.35759 19030624 +3.67212 19919880 +3.88087 21932792 +4.32047 19697448 +4.59238 23449192 +4.92702 21260224 +5.38009 20740552 +5.6907 34344104 +5.93708 22432224 +6.32605 25918408 +6.56992 23932776 +6.91045 23969744 +7.34829 25118072 +7.78408 26126952 +8.07375 30650456 +8.4586 27037128 +8.84373 27238472 +9.32651 31547392 +9.80775 30132232 +10.225 31882384 +10.7115 33497720 +11.0001 34280976 +11.3337 36002080 +11.5094 36332816 +11.8853 39148072 +12.3438 38551040 +12.7539 40231696 +13.0397 40740888 +13.4487 45972408 +13.7677 45479008 +13.9993 45689144 +14.2996 46235768 +14.6829 46144856 +14.9561 46146808 +15.3351 46853200 +15.6352 48174776 +16.0247 49640680 +16.2574 51150992 +16.5575 50554072 +17.0139 53032160 +17.0688 53563424 +17.1723 54119576 +17.2426 55082016 +17.4184 55005768 +17.5942 55047960 +17.7699 55154488 +17.7983 55829528 +18.01 80314912 +18.0148 80314912 +18.1804 97228312 +23.2847 80314912 +48.5587 63536000 +48.7394 72163936 +48.9182 72164200 +49.0973 72164464 +49.2916 80326656 +49.4717 50396696 diff --git a/check_cirq_8054/results/memtotal.737548.dat b/check_cirq_8054/results/memtotal.737548.dat new file mode 100644 index 000000000..bc739df6d --- /dev/null +++ b/check_cirq_8054/results/memtotal.737548.dat @@ -0,0 +1,99 @@ +# time(s) total(B) +0 0 +0.24384 1829216 +0.563657 1829936 +0.813955 1831016 +1.0374 1833280 +1.30225 1839064 +1.70561 5389248 +1.99261 8696200 +2.35874 13350360 +2.64822 17147600 +2.86035 17388056 +3.14753 26787064 +3.44511 19185968 +3.9095 22048144 +4.13106 19868360 +4.49651 23122080 +4.84353 20408176 +5.04188 25635608 +5.38938 20717440 +5.7061 34344200 +6.00614 22499632 +6.30378 24823696 +6.60223 23307216 +6.80059 23870800 +7.12277 24077696 +7.44424 25003520 +7.60505 26300240 +7.92652 27032448 +8.32849 27879688 +8.56962 32657912 +8.89114 27523216 +9.29605 30264464 +9.6176 28986624 +9.86073 29245704 +10.1969 31991528 +10.5187 32886640 +10.8423 33753192 +11.0642 35095552 +11.4298 36270768 +11.7401 37901992 +12.1969 39385016 +12.6205 40904808 +13.0497 40608224 +13.2163 43593096 +13.6085 45439440 +13.8653 49127576 +14.1522 45892904 +14.5152 46145520 +14.9225 46146984 +15.1357 46147960 +15.3823 46178624 +15.5432 47147704 +15.704 47950408 +15.8649 48425552 +16.0267 49284560 +16.1023 49641512 +16.2521 50137968 +16.3066 50643288 +16.335 51151840 +16.496 50422248 +16.6568 50625192 +16.8176 51594632 +16.933 51679648 +16.9482 52507944 +17.0914 53033376 +17.1463 53564384 +17.2499 54120472 +17.3203 55082912 +17.4811 54936168 +17.6419 54878632 +17.8029 55084632 +17.8763 55830184 +20.4396 457805056 +20.5151 457805056 +23.0232 457806120 +23.1632 726376696 +31.0781 457805056 +58.5807 189367920 +59.6161 323858160 +59.8141 323858280 +60.7914 323858280 +60.9876 323858384 +61.9649 323858384 +62.1366 323858488 +63.1139 323858488 +64.1868 323858592 +65.3186 323858696 +65.5134 323858800 +66.4906 323858800 +67.5553 323858904 +67.7211 323859008 +68.6984 323859008 +68.9144 323859112 +69.8917 323859112 +70.074 323859056 +70.3125 592035208 +71.1325 457813440 +71.3126 50398104 diff --git a/check_cirq_8054/results/memtotal.738662.dat b/check_cirq_8054/results/memtotal.738662.dat new file mode 100644 index 000000000..741d16b65 --- /dev/null +++ b/check_cirq_8054/results/memtotal.738662.dat @@ -0,0 +1,93 @@ +# time(s) total(B) +0 0 +0.241858 1829216 +0.536904 1829936 +0.894681 1831376 +1.27455 1839064 +1.52452 3959456 +1.85926 7373144 +2.07045 11420768 +2.33075 13350360 +2.62021 17147600 +3.02276 20972656 +3.26981 20011800 +3.60782 19686568 +3.9994 19947304 +4.43126 20900776 +4.7444 26079016 +5.00099 24878544 +5.30861 21661584 +5.51445 21796776 +5.83214 22214208 +6.09067 23660496 +6.50097 23262976 +6.75739 24200024 +7.06669 24054440 +7.47673 26595568 +7.80473 26572208 +8.0507 30377440 +8.37866 30944776 +8.78879 27357744 +9.03809 26869824 +9.36646 27613280 +9.61248 29491760 +9.94047 30378864 +10.3773 32756360 +10.7873 33739224 +11.2248 35818840 +11.5271 36351816 +11.7112 37901320 +12.0073 37163416 +12.2594 37482200 +12.5126 39146056 +12.7175 40066816 +13.0033 40273224 +13.4134 45563792 +13.8357 49126728 +14.0878 46245352 +14.4878 46145632 +14.8216 46146120 +15.041 46146608 +15.2749 46147584 +15.5079 46177760 +15.6922 47256600 +15.8763 48176712 +16.0605 48587120 +16.2278 49641032 +16.3791 50137528 +16.432 50642744 +16.4604 51151328 +16.6446 50560216 +16.8288 51104448 +17.013 51498128 +17.0585 51678992 +17.0737 52507288 +17.2169 53032368 +17.2718 53563488 +17.3753 54119592 +17.4456 55082048 +17.6298 54613072 +17.8141 55200336 +17.9984 55664752 +18.0014 55829448 +20.5648 457804288 +20.6403 457804288 +23.1484 457805352 +23.2883 726375928 +74.4273 457804288 +99.78 189367136 +100.815 323857024 +101.966 323857144 +103.039 323857264 +104.164 323857384 +104.362 323857504 +105.34 323857504 +106.412 323857624 +107.522 323857744 +108.655 323857864 +109.754 323857984 +110.857 323858104 +111.05 323858064 +111.288 592034424 +112.108 457816688 +112.292 49454784 diff --git a/check_cirq_8054/results/memtotal.742794.dat b/check_cirq_8054/results/memtotal.742794.dat new file mode 100644 index 000000000..e052249e8 --- /dev/null +++ b/check_cirq_8054/results/memtotal.742794.dat @@ -0,0 +1,69 @@ +# time(s) total(B) +0 0 +0.247474 1829216 +0.540653 1829576 +0.786293 1830296 +1.06812 1831016 +1.55855 1839064 +1.92407 5965288 +2.24833 8696200 +2.61428 13350360 +2.90372 17147600 +3.4021 26787080 +3.72933 19672352 +4.08687 19494432 +4.29122 20214904 +4.72049 21273272 +5.22741 21488824 +5.56274 20457632 +5.96042 34344472 +6.29555 23498400 +6.51888 24248912 +6.85399 23578808 +7.13292 23859128 +7.41281 24236024 +7.80368 28179984 +8.02702 26961608 +8.36199 31016240 +8.80847 31750752 +9.25537 27998880 +9.59189 31490616 +9.81499 28774480 +10.1498 29713064 +10.4965 31882976 +10.8315 33037432 +11.3178 35095856 +11.683 36271008 +12.1628 39555792 +12.4179 38161304 +12.7047 38339768 +13.0165 39919040 +13.3021 40700312 +13.7166 45973656 +14.0356 45480320 +14.5473 46336720 +14.9329 46146776 +15.3997 46148728 +15.8492 46198344 +16.1366 48496040 +16.4234 48727888 +16.8018 51153208 +17.232 50724896 +17.3998 51681080 +17.4149 52509376 +17.558 53034648 +17.6129 53565976 +17.7165 54122128 +17.7868 55084616 +17.9912 54900216 +18.1956 55112600 +18.3425 55832128 +18.5543 80317512 +18.559 80317512 +18.7246 97230912 +22.0952 80317512 +46.6192 63538616 +46.838 72166488 +47.0868 72166840 +47.2923 72167032 +47.5067 50402640 diff --git a/check_cirq_8054/results/memtotal.743107.dat b/check_cirq_8054/results/memtotal.743107.dat new file mode 100644 index 000000000..a9399c502 --- /dev/null +++ b/check_cirq_8054/results/memtotal.743107.dat @@ -0,0 +1,71 @@ +# time(s) total(B) +0 0 +0.274028 1829216 +0.85666 1831736 +1.20887 3650104 +1.62037 6885544 +1.95504 11949024 +2.37666 15808880 +2.81034 19454232 +3.11697 19797304 +3.50564 21092144 +3.89009 19605104 +4.21061 20099848 +4.53129 24973016 +4.94826 22676624 +5.25464 21229344 +5.48879 34344328 +5.79565 22544168 +6.10185 25778448 +6.40787 24768792 +6.71409 24296000 +6.95899 24743464 +7.38776 26356992 +7.63286 27613680 +7.8777 30548656 +8.25409 26850752 +8.53638 26515824 +8.91253 26874120 +9.38279 29034024 +9.75916 30615904 +10.0412 31967688 +10.512 33501808 +10.8464 35095392 +11.036 35819112 +11.4006 36497632 +11.6924 39555280 +11.9747 39309864 +12.2281 38919960 +12.5292 40089600 +12.8149 40273592 +13.2464 45972856 +13.6468 49127096 +13.8997 46254920 +14.3068 46145848 +14.5857 46147312 +14.8137 46148288 +15.1551 46284696 +15.5345 48212176 +15.7877 49278360 +16.0995 51153016 +16.4791 50680112 +16.6688 50874200 +16.6977 51680568 +16.7128 52508864 +16.8559 53034016 +16.9107 53565672 +17.0143 54121824 +17.0845 55084312 +17.2742 54681088 +17.4637 54932808 +17.6402 55832080 +17.8519 80317480 +17.8566 80317480 +18.0222 97230880 +32.5202 80317480 +57.7942 63538568 +58.012 72166680 +58.2058 72167032 +58.4462 72167296 +58.6358 50521248 +58.8254 9808016 diff --git a/check_cirq_8054/results/memtotal.743990.dat b/check_cirq_8054/results/memtotal.743990.dat new file mode 100644 index 000000000..8651eeb5f --- /dev/null +++ b/check_cirq_8054/results/memtotal.743990.dat @@ -0,0 +1,69 @@ +# time(s) total(B) +0 0 +0.323637 1829216 +0.627409 1830296 +1.0176 1831376 +1.34847 1832168 +1.63833 4506032 +2.05161 8743216 +2.34779 13008424 +2.66035 15706504 +2.95015 17670600 +3.42407 19143208 +3.88359 19792864 +4.41276 20929296 +4.92469 21975456 +5.28553 25116952 +5.75221 34344104 +6.16192 23648688 +6.44582 24049640 +6.93258 24337368 +7.21655 24392344 +7.62215 26014504 +8.02125 28631192 +8.25852 28289824 +8.73276 29854328 +9.09061 26869240 +9.44617 27938304 +9.68321 29380320 +10.039 30871032 +10.2877 31882384 +10.6433 33236744 +10.8804 33750680 +11.3001 35818840 +11.5947 36342872 +12.0751 37221496 +12.6672 40904248 +12.9479 41086848 +13.3633 45513992 +13.6736 46001640 +14.0741 45906352 +14.3985 46431056 +14.8151 46147328 +15.088 46148304 +15.5022 46899016 +15.8266 48215048 +16.3914 51153080 +16.7159 50657864 +17.1478 53034488 +17.7011 54918488 +17.9328 55831856 +20.4962 457806760 +20.5717 457806760 +23.0798 457807824 +23.2197 726378400 +32.4663 457806760 +59.2689 189369608 +60.3042 323859416 +61.427 323859536 +62.5246 323859656 +63.6921 323859776 +64.8145 323859896 +65.9257 323860016 +67.1287 323860136 +68.235 323860256 +69.3707 323860376 +70.5502 323860496 +70.7926 323779112 +71.7807 323592984 +72.0124 33151680 diff --git a/check_cirq_8054/results/memtotal.745873.dat b/check_cirq_8054/results/memtotal.745873.dat new file mode 100644 index 000000000..24699b364 --- /dev/null +++ b/check_cirq_8054/results/memtotal.745873.dat @@ -0,0 +1,71 @@ +# time(s) total(B) +0 0 +0.439876 1829576 +0.712697 1830296 +1.22238 1831736 +1.54455 3046264 +1.83998 5437624 +2.14288 8749640 +2.65572 14939648 +2.91956 17238600 +3.28229 26787080 +3.56868 21758536 +3.95175 21585624 +4.47836 20255552 +4.92972 26125200 +5.45651 21245472 +5.84014 34344104 +6.3251 23060064 +6.6887 24443032 +7.17335 25376776 +7.59716 25365600 +7.83961 30404400 +8.2031 29802528 +8.4461 26947440 +8.89711 27086040 +9.34798 27264352 +9.68634 28727312 +10.1376 30677424 +10.3753 31882384 +10.8261 33476680 +11.31 35442640 +11.6753 36339408 +12.0428 39554944 +12.3813 38748120 +12.7532 40904248 +13.039 41389144 +13.3484 43592408 +13.597 45972584 +13.916 45479248 +14.1464 45691160 +14.4445 46336280 +14.7716 46145720 +15.2193 46147672 +15.7069 46174696 +16.0049 47706928 +16.4271 49642304 +16.8088 50411712 +17.1069 50738320 +17.416 53034152 +17.6448 55084024 +18.0919 55251568 +18.2006 55831600 +20.764 457806440 +20.8395 457806440 +23.3476 457807504 +23.4876 726378080 +39.8738 457806440 +64.4765 189369288 +65.5119 323859176 +66.7094 323859296 +67.9017 323859416 +69.0643 323859536 +70.1761 323859656 +71.277 323859776 +72.3618 323859896 +73.5147 323860016 +74.6539 323860136 +75.8451 323860256 +76.125 323778872 +77.1131 323592664 +77.3419 33886704 diff --git a/check_cirq_8054/results/plot_memory_use.png b/check_cirq_8054/results/plot_memory_use.png new file mode 100644 index 000000000..6242d8a29 Binary files /dev/null and b/check_cirq_8054/results/plot_memory_use.png differ diff --git a/check_cirq_8054/results/plot_memory_use.py b/check_cirq_8054/results/plot_memory_use.py new file mode 100644 index 000000000..8ea0d951c --- /dev/null +++ b/check_cirq_8054/results/plot_memory_use.py @@ -0,0 +1,39 @@ +import matplotlib.pyplot as plt +import numpy as np + +memuse_files_q20 = [ + # baseline qsim and cirq + "memtotal.733604.dat", + "memtotal.736557.dat", + # qsim and cirq with cirq-PR #8054 + "memtotal.742794.dat", + "memtotal.743107.dat", +] + +memuse_files_q24 = [ + # baseline qsim and cirq + "memtotal.737548.dat", + "memtotal.738662.dat", + # qsim and cirq with cirq-PR #8054 + "memtotal.743990.dat", + "memtotal.745873.dat", +] + +t_mem_q20 = [col for f in memuse_files_q20 for col in np.loadtxt(f, unpack=True)] +t_mem_q24 = [col for f in memuse_files_q24 for col in np.loadtxt(f, unpack=True)] + +fig, ax = plt.subplots(nrows=2) +lines_q20 = ax[0].plot(*t_mem_q20) +lines_q24 = ax[1].plot(*t_mem_q24) +# blue dashed line with dot markers for baseline cirq +plt.setp(lines_q20[0:2] + lines_q24[0:2], marker=".", linestyle=":", color="blue") +# green solid line with x markers for runs with cirq-PR #8054 +plt.setp(lines_q20[2:] + lines_q24[2:], marker="x", markersize=7, color="green") +# hide repeated runs +plt.setp(lines_q20[1::2] + lines_q24[1::2], visible=False) +plt.setp(ax, xlim=(0, 80), ylabel="total memory use (B)") +ax[0].legend(lines_q20[0::2], ["baseline qsim", "qsim with cirq #8054"]) +ax[0].set_title("qsim_simulation.py, 20 qubits") +ax[1].set_title("qsim_simulation.py, 24 qubits") +ax[1].set_xlabel("process time (s)") +plt.show() diff --git a/qsimcirq/qsim_simulator.py b/qsimcirq/qsim_simulator.py index 2b538167c..1163ecced 100644 --- a/qsimcirq/qsim_simulator.py +++ b/qsimcirq/qsim_simulator.py @@ -591,7 +591,9 @@ def simulate_sweep_iter( program, params, qubit_order, initial_state ): final_state = cirq.StateVectorSimulationState( - initial_state=state_vector, qubits=cirq_order + initial_state=state_vector, + qubits=cirq_order, + should_preserve_initial_state=False, ) # create result for this parameter # TODO: We need to support measurements.