Hello good morning dear colleagues. I’m trying to run code in Strawberry Fields and I’m facing problems in simulating and collecting results. The problem is specifically in the line “result = eng.run(prog, shots=1)” which, when executed, the program enters a kind of infinite loop and does not stop executing unless told to stop. I would like to understand how I extract the results and reconstruct the output states.
# Define o número de modos
n_modes = 6
alpha = 2 + 0.0j
a = np.abs(alpha)
# Cria o programa
prog = sf.Program(n_modes)
eng = sf.Engine("bosonic")
with prog.context as q:
# Prepara os estados nos modos
Coherent(a, np.pi) | q[0] # |A|
Coherent(a, np.pi) | q[1] # |B|
Coherent(a, np.pi) | q[2] # |C|
Catstate(a, 0, 0, 'real', 1e-12, 2) | q[3] # |cat|
Catstate(a, 0, 0, 'real', 1e-12, 2) | q[4] # |cat|
Catstate(a, 0, 0, 'real', 1e-12, 2) | q[5] # |cat|
# Circuito 1
BSgate(np.pi / 4, 0) | (q[0], q[1])
BSgate(np.pi / 4, 0) | (q[2], q[3])
BSgate(np.pi / 4, 0) | (q[4], q[5])
Rgate(np.pi) | q[0]
# Medições heterodinas nos modos
MeasureHeterodyne() | q[0]
MeasureHeterodyne() | q[1]
MeasureHeterodyne() | q[2]
MeasureHeterodyne() | q[3]
MeasureHeterodyne() | q[4]
MeasureHeterodyne() | q[5]
result = eng.run(prog, shots=1)
# Imprime os resultados das medições
heterodyne_samples = result.samples
print(f"Resultados das medições heterodinas: {heterodyne_samples}")
print(f"Resultados das medições heterodinas: {result}")
If you want help with diagnosing an error, please put the full error message below:
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
Cell In[5], line 1
----> 1 result = eng.run(prog, shots=1).samples[:, 0]
File ~\AppData\Roaming\Python\Python311\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 ~\AppData\Roaming\Python\Python311\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 ~\AppData\Roaming\Python\Python311\site-packages\strawberryfields\engine.py:875, in BosonicEngine._run_program(self, prog, **kwargs)
873 def _run_program(self, prog, **kwargs):
874 # Custom Bosonic run code
--> 875 applied, samples_dict = self.backend.run_prog(prog, **kwargs)
876 samples, samples_dict = self._combine_and_sort_samples(samples_dict)
877 return applied, samples, samples_dict
File ~\AppData\Roaming\Python\Python311\site-packages\strawberryfields\backends\bosonicbackend\backend.py:155, in BosonicBackend.run_prog(self, prog, **kwargs)
152 elif not isinstance(cmd.op, non_gauss_preps):
153 try:
154 # try to apply it to the backend and if op is a measurement, store outcome in values
--> 155 val = cmd.op.apply(cmd.reg, self, **kwargs)
156 if val is not None:
157 for i, r in enumerate(cmd.reg):
158 # Internally also store all the measurement outcomes
File ~\AppData\Roaming\Python\Python311\site-packages\strawberryfields\ops.py:325, in Measurement.apply(self, reg, backend, **kwargs)
322 if _shots is None:
323 return None
--> 325 values = super().apply(reg, backend, **kwargs)
327 # store the results in the register reference objects
328 for v, r in zip(np.transpose(values), reg):
File ~\AppData\Roaming\Python\Python311\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 ~\AppData\Roaming\Python\Python311\site-packages\strawberryfields\ops.py:1318, in MeasureHeterodyne._apply(self, reg, backend, shots, **kwargs)
1317 def _apply(self, reg, backend, shots=1, **kwargs):
-> 1318 return backend.measure_heterodyne(*reg, shots=shots, select=self.select, **kwargs)
File ~\AppData\Roaming\Python\Python311\site-packages\strawberryfields\backends\bosonicbackend\backend.py:789, in BosonicBackend.measure_heterodyne(self, mode, shots, select, **kwargs)
787 def measure_heterodyne(self, mode, shots=1, select=None, **kwargs):
788 if select is None:
--> 789 res = 0.5 * self.circuit.heterodyne(mode, shots=shots)
790 return np.array([res[:, 0] + 1j * res[:, 1]]).T
792 res = select
File ~\AppData\Roaming\Python\Python311\site-packages\strawberryfields\backends\bosonicbackend\bosoniccircuit.py:894, in BosonicModes.heterodyne(self, mode, shots)
883 r"""Performs a heterodyne measurement on a mode, simulated by a generaldyne
884 onto a coherent state.
885
(...)
891 array: heterodyne outcome
892 """
893 covmat = self.hbar * np.eye(2) / 2
--> 894 return self.measure_dyne(covmat, [mode], shots=shots)
File ~\AppData\Roaming\Python\Python311\site-packages\strawberryfields\backends\bosonicbackend\bosoniccircuit.py:750, in BosonicModes.measure_dyne(self, covmat, modes, shots)
747 diff_sample = peak_sample - means_quad
748 # Calculate arguments for the Gaussian functions used to calculate
749 # the exact probability distribution at the sampled point
--> 750 exp_arg = np.einsum(
751 "...j,...jk,...k",
752 (diff_sample),
753 np.linalg.inv(covs_quad),
754 (diff_sample),
755 )
757 # Make a copy to calculate the exponential arguments of the
758 # upper bounding function at the point
759 ub_exp_arg = np.copy(exp_arg)
File C:\ProgramData\anaconda3\Lib\site-packages\numpy\core\einsumfunc.py:1371, in einsum(out, optimize, *operands, **kwargs)
1369 if specified_out:
1370 kwargs['out'] = out
-> 1371 return c_einsum(*operands, **kwargs)
1373 # Check the kwargs to avoid a more cryptic error later, without having to
1374 # repeat default values here
1375 valid_einsum_kwargs = ['dtype', 'order', 'casting']
KeyboardInterrupt:
Anaconda 3
strawberryfields 0.23