Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions check_cirq_8054/get-total-memory
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 31 additions & 0 deletions check_cirq_8054/oom_qsim_simulation.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions check_cirq_8054/qsim_simulation.py
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions check_cirq_8054/results/README.md
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions check_cirq_8054/results/memtotal.733604.dat
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions check_cirq_8054/results/memtotal.736557.dat
Original file line number Diff line number Diff line change
@@ -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
99 changes: 99 additions & 0 deletions check_cirq_8054/results/memtotal.737548.dat
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading