Hello, good morning dear colleagues. I’m trying to evolve a set of coherent states through a network of 50/50 symmetric divisors. My problem is related to the amount of RAM required by my code. Are there any ways around this problem? What would they be?
import strawberryfields as sf
from strawberryfields.ops import *
import numpy as np
from numpy import pi, sqrt
# set the random seed
np.random.seed(42)
prog = sf.Program(6)
alpha = 1+0.5j
r = np.abs(alpha)
phi = np.angle(alpha)
with prog.context as q:
# prepare initial states
Coherent(r, phi) | q[0]
Coherent(r, phi) | q[1]
Coherent(r, phi) | q[2]
Coherent(r, phi) | q[3]
Coherent(r, phi) | q[4]
Coherent(r, phi) | q[5]
# apply gates
# Primeira bateria de BS
BSgate() | (q[0], q[1])
BSgate() | (q[2], q[3])
BSgate() | (q[4], q[5])
# Segunda bateria de BS
BSgate() | (q[2], q[4])
BSgate() | (q[0], q[5])
BSgate() | (q[1], q[3])
eng = sf.Engine('fock', backend_options={"cutoff_dim": 6})
result = eng.run(prog, shots=1, modes=None, compile_options={})
eng = sf.Engine('fock', backend_options={"cutoff_dim": 6})
result = eng.run(prog, shots=1, modes=None, compile_options={})
If you want help with diagnosing an error, please put the full error message below:
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
Cell In[9], line 1
----> 1 result = eng.run(prog, shots=1, modes=None, compile_options={})
File ~\anaconda3\lib\site-packages\strawberryfields\engine.py:570, in LocalEngine.run(self, program, args, compile_options, **kwargs)
565 if c.op.measurement_deps and eng_run_options["shots"] > 1:
566 raise NotImplementedError(
567 "Feed-forwarding of measurements cannot be used together with multiple shots."
568 )
--> 570 return super()._run(
571 program_lst, args=args, compile_options=compile_options, **eng_run_options
572 )
File ~\anaconda3\lib\site-packages\strawberryfields\engine.py:306, in BaseEngine._run(self, program, args, compile_options, **kwargs)
303 p.bind_params(args)
304 p.lock()
--> 306 _, self.samples, self.samples_dict = self._run_program(p, **kwargs)
307 self.run_progs.append(p)
309 if isinstance(p, TDMProgram) and received_rolled:
File ~\anaconda3\lib\site-packages\strawberryfields\engine.py:430, in LocalEngine._run_program(self, prog, **kwargs)
427 for cmd in prog.circuit:
428 try:
429 # try to apply it to the backend and, if op is a measurement, store it in values
--> 430 val = cmd.op.apply(cmd.reg, self.backend, **kwargs)
431 if val is not None:
432 for i, r in enumerate(cmd.reg):
File ~\anaconda3\lib\site-packages\strawberryfields\ops.py:228, in Operation.apply(self, reg, backend, **kwargs)
226 temp = [rr.ind for rr in reg]
227 # call the child class specialized _apply method
--> 228 return self._apply(temp, backend, **kwargs)
File ~\anaconda3\lib\site-packages\strawberryfields\ops.py:631, in Coherent._apply(self, reg, backend, **kwargs)
627 phi = par_evaluate(self.p[1])
629 self._check_for_complex_args([r, phi], "Coherent(r, phi)")
--> 631 backend.prepare_coherent_state(r, phi, *reg)
File ~\anaconda3\lib\site-packages\strawberryfields\backends\fockbackend\backend.py:153, in FockBackend.prepare_coherent_state(self, r, phi, mode)
152 def prepare_coherent_state(self, r, phi, mode):
--> 153 self.circuit.prepare_mode_coherent(r, phi, self._remap_modes(mode))
File ~\anaconda3\lib\site-packages\strawberryfields\backends\fockbackend\circuit.py:505, in Circuit.prepare_mode_coherent(self, r, phi, mode)
501 """
502 Prepares a mode in a coherent state.
503 """
504 if self._pure:
--> 505 self.prepare(ops.coherentState(r, phi, self._trunc), mode)
506 else:
507 st = ops.coherentState(r, phi, self._trunc)
File ~\anaconda3\lib\site-packages\strawberryfields\backends\fockbackend\circuit.py:487, in Circuit.prepare(self, state, mode)
485 if isinstance(mode, int):
486 mode = [mode]
--> 487 self.prepare_multimode(state, mode)
File ~\anaconda3\lib\site-packages\strawberryfields\backends\fockbackend\circuit.py:458, in Circuit.prepare_multimode(self, state, modes)
455 reduced_state = ops.partial_trace(self._state, self._num_modes, modes)
457 # Insert state at the end (I know there is also tensor() from ops but it has extra aguments wich only confuse here)
--> 458 self._state = np.tensordot(reduced_state, state, axes=0)
460 # unless the preparation was meant to go into the last modes in the standard order, we need to swap indices around
461 if modes != list(range(self._num_modes - len(modes), self._num_modes)):
File <__array_function__ internals>:180, in tensordot(*args, **kwargs)
File ~\anaconda3\lib\site-packages\numpy\core\numeric.py:1138, in tensordot(a, b, axes)
1136 at = a.transpose(newaxes_a).reshape(newshape_a)
1137 bt = b.transpose(newaxes_b).reshape(newshape_b)
-> 1138 res = dot(at, bt)
1139 return res.reshape(olda + oldb)
File <__array_function__ internals>:180, in dot(*args, **kwargs)
MemoryError: Unable to allocate 32.4 GiB for an array with shape (60466176, 36) and data type complex128
conda 23.3.1
strawberryfields 0.23.0
numpy 1.23.5