 # Run several times with different parameters

HI! I have a circuit which I would like to run several times, each time with slightly different parameters. Is there some easy\elegant way to do so?

Hi @segev. The easiest way is to call the circuit in a loop. I will be happy to take a look at your code if you give me a working example. Thanks.

``````import strawberryfields as sf

from strawberryfields import ops

import numpy as np

eng = sf.Engine("fock", backend_options={"cutoff_dim": 2})

down50=np.array([[1,1],[1,-1]])*(1/np.sqrt(2))
down30=np.array([[1/np.sqrt(3),np.sqrt(2)/np.sqrt(3)],
[np.sqrt(2)/np.sqrt(3),-1/np.sqrt(3)]])
up30=np.array([[-1/np.sqrt(3),np.sqrt(2)/np.sqrt(3)],
[np.sqrt(2)/np.sqrt(3),1/np.sqrt(3)]])
Ht=np.array([[1j,1j],[1j,-1j]])*(1/np.sqrt(2))#H'

states = [[0.,0.,0.,0.]]*4
prog = sf.Program(6)

phase=[1.5708582431532068, 1.6284157332273947,
-1.5707280700719355, -1.0887547385694465,
-0.0576375381598552, 0.5956784337483751,
-0.4820424972232051, -0.019512793550738525]

x1=0
x2=0

for i in range(4):
x2=i%2
if (i==2):
x1=1
with prog.context as q:
tet1x=x1*np.pi/4
tet2x=x2*np.pi/4
phi1x=x1*np.pi/2
phi2x=x2*np.pi/2
ops.Fock(0)       | q
ops.Fock(0)       | q

ops.Fock(0)       | q
ops.Fock(1)       | q
ops.Fock(0)       | q
ops.Fock(1)       | q

ops.BSgate() | (q, q)
ops.BSgate() | (q, q)
ops.Rgate(phase) | q
ops.Rgate(phase) | q
ops.BSgate() | (q, q)
ops.BSgate() | (q, q)
ops.Rgate(phase) | q
ops.Rgate(phase) | q

ops.Interferometer(down50) | (q, q)#B3
ops.Interferometer(up30) | (q, q)#B1
ops.Interferometer(up30) | (q, q)#B2
ops.Interferometer(down30) | (q, q)#B5
ops.Interferometer(down50) | (q, q)#B4

ops.Rgate(phase) | q
ops.Rgate(phase) | q
ops.BSgate() | (q, q)
ops.BSgate() | (q, q)
ops.Rgate(phase) | q
ops.Rgate(phase) | q
ops.BSgate() | (q, q)
ops.BSgate() | (q, q)

ops.MeasureFock(select=(0)) | q
ops.MeasureFock(select=(0)) | q
ops.BSgate(tet1x,phi1x) | (q, q)
ops.BSgate(tet2x,phi2x) | (q, q)
result = eng.run(prog)
state = result.state
probs = state.all_fock_probs()
probs=np.array(probs)
states[i]=probs[0,0,1,0,1,0]
states[i]=probs[0,0,1,0,0,1]
states[i]=probs[0,0,0,1,1,0]
states[i]=probs[0,0,0,1,0,1]
``````

This is a part of my program, i’m trying to run the circuit 4 times in this loop but it keep saying" the program is locked"

Hi @segev. You get this error because the program gets locked when it is run once and no more operations can be performed when the loop restarts. Please see more information here. To fix the problem, you can simply create a function that runs the program and than call it many times with different parameters. I provide a simple example for generating samples with different displacement parameters:

``````import numpy as np
import strawberryfields as sf

np.random.seed(8)

def sample(param):

prog = sf.Program(2)
eng = sf.Engine("fock", backend_options={"cutoff_dim": 6})

with prog.context as q:
sf.ops.Dgate(param) | q
sf.ops.BSgate() | (q, q)
sf.ops.MeasureFock() | q

return eng.run(prog).samples

print([sample(param) for param in [0.1, 0.5, 1.0]])

# output: [array([0, 0]), array([1, 0]), array([1, 1])]
``````

I could not reproduce the same error you reported with your code because part of the code is not formatted properly. However, following the logic of the example I provided above should resolve the issue. I will be more than happy to test your code if you please format all of the lines similarly.

It worked! Thank you so much!