#Building up the function to minimize by looping through batch
for i in range(batch_size):
# Probabilities corresponding to a single photon in either mode
prob = tf.abs(ket[i, classification[i, 0], classification[i, 1]]) ** 2
In second line prob = I m getting error
It shows error like FockstateTf object is not subscriptable.
I m trying to run this code in current version…pls help…
Hi @JEEVARATHINAM, I think the problem is that you’re trying to extract information from “ket” in an invalid way: ket[i, classification[i, 0], classification[i, 1]])
Have you tried using state.fock_prob() to get the probabilities you need? I’m not sure if this will do what you’re looking for but it may help. You can read more about it here.
I built this minimum working example which may help you move forward with your project
import strawberryfields as sf
mode_number=2
prog = sf.Program(mode_number)
with prog.context as q:
sf.ops.Sgate(0.54) | q[0]
sf.ops.Sgate(0.54) | q[1]
sf.ops.BSgate(0.43, 0.1) | (q[0], q[1])
eng = sf.Engine(backend="fock", backend_options={"cutoff_dim": 5})
result = eng.run(prog)
state = result.state
prob=state.fock_prob([0, 0])
print(prob)
As you can see in this example eng.run(prog) produces a result, and from that result you can extract the state. You can find a similar example here.
In your code you’re trying to extract the probabilities from the result instead of the state so this will cause an error.
On the other hand, if you look at the documentation for fock_prob you will notice that you need to input a parameter n.
" n ( Sequence[int] ) – the Fock state |→n⟩|n→⟩ that we want to measure the probability of"
In my example, n is [0,0].
Finally, you will notice that I changed the backend to “fock”. You can use “tf” as a backend but you may get some warnings about how you installed it.
# Building up the function to minimize by looping through batch
for i in range(batch_size):
# Probabilities corresponding to a single photon in either mode
prob = tf.abs(prob[i, classification[i, 0], classification[i, 1]]) ** 2
Hi @JEEVARATHINAM, as you can see in the previous cell, “prob” is a scalar number. This means that prob[a,b,c] is invalid because it’s a single number, not a list.
I’m not sure what you want to do. fock_prob gives you the probability of a specific fock state so you have to decide what you want to do with that information, and even decide whether or not that’s the information you need.