About the depth of circuits

https://strawberryfields.ai/photonics/demos/run_state_learner.html
In the example above, the depth of corcuits is given:

# Number of layers
depth = 15

I am confused that according to the definition of ‘depth’, after the circuits structure is defined, the depth will be defined too. So why should I define the depth?

def layer(i, q):
    Rgate(sf_params[i][0]) | q
    Sgate(sf_params[i][1], sf_params[i][2]) | q
    Rgate(sf_params[i][3]) | q
    Dgate(sf_params[i][4], sf_params[i][5]) | q
    Kgate(sf_params[i][6]) | q
    return q

And in this example I think the depth will be 5 (there are five gates), do I misunderstand the definition of ‘depth’? I think the gates in red box represents a ‘layer’.

Hi @Shertin, you’ve actually come across something very interesting. In quantum computing a layer is effectively what you suggest. However in machine learning a layer is different. If you notice in the image you posted, it says that all of these “5 layers” are actually “Layer L”.

It shows that it’s actually the machine learning terminology for depth and layer that is being used.

In fact, if you look at the “Constructing the circuit” section you will notice that here we define how a layer looks, and you will then see how we apply each layer to the circuit until we get to the desired depth.

This is why we need to define the depth first, because it’s the number of layers we want to apply to our circuit.

Please let me know if this helped answer your question!

Hi @CatalinaAlbornoz, thanks a lot for your reply! Now I’m clear more.

So in each layer, there are same quantum gates structure with different parameters? Will the neural network work more efficiently with more layers, or it has a medium best value that we have to search? Thanks!

Exactly @shertin, each layer has the same structure but with different parameters. This doesn’t necessarily have to be the case. You can build neural networks where the structure changes from one layer to the next.

In general, the more layers and parameters that you have the better your chances are of finding a good solution. However, having too many layers is costly in computational terms and it can lead to problems such as overparametrization and overfitting.

The ideal is to find the least number of layers that still gives you a reasonable solution for your problem.

Please let me know if this helps!

Thanks @CatalinaAlbornoz, that helps!

I still have a question that what is the relationship between output state and the layers? Will each layer output a state and the final output state of the circuit be the one with highest fidelity? As in a experimental circuit, we only need to build one layer (is it right?).

Hi @Shertin! Good question.

The layers are all applied to the same circuit. This means that our circuit will be: initial state + Layer 1 + Layer 2 + … + Layer N + Measurement

The layers can all be the same as in the example above, but they could all be different too.

The need to have many layers is to have a lot of parameters. If I have more parameters in a structure that is convenient for my problem, then my circuit can represent more complex functions. This means that my quantum machine learning algorithm can teach my circuit the intrinsic complexities of the problem at hand, and it will be better suited to produce a good output.

The output is not being calculated after every layer, only at the end. It’s the ensemble of the circuit that is better. So if you decided to keep all layers with their current parameters and remove the last one, the result probably wouldn’t be the best. You would have to retrain your model.

Please let me know if this answers your question!

Thanks a lot @CatalinaAlbornoz, but I am still confused that when I experimentally build a circuit, do I have to build all these layers (with layer times gates)?

Another question is if I want to get the process state (like state after D gate), can I feed the optimized parameters (get them after the optimization of the entire circuit) of -R-S-R-D- to a new program and run it? Or does there exist any simple method?
image

Hi @Shertin!

Yes, when you run the circuit on actual hardware you will need to apply each layer one after the other, knowing that each layer contains a certain number of gates. In programmable quantum computing we talk about applying gates instead of building gates so I would recommend you watch this amazing video of how a Xanadu photonic quantum computer works.

About the second question I’m not sure.

Hi @Shertin,

Your guess is correct. You can construct another Strawberry Fields program which omits the gates you don’t want to apply (e.g., the K gate), and any final measurements. Then you can pass in the value of the parameters that you found from an earlier optimization on the complete circuit. You would then run the program and inpect the state vector (ket) or density matrix (dm)

state = eng.run(truncated_prog).state

# Extract the statevector
ket = state.ket()

# Extract the density matrix
dm = state.dm()