Hi, everyone, Does the Pennylane support the evolution of density matrix? thanks

hi, everyone,
I want to know if Pennylane support the evolution of density matrix, that is, the input of the circuit is a density matrix but not a state vector.

Hi @shangshang_shi,

QubitDensityMatrix allows you to prepare a density matrix on a subsystem. Is this what you were looking for? You can also use density_matrix as the measurement in your circuit.

please click the picture
you can see the full screeshot

Hi @shangshang_shi,

The @qml.qnode(dev) part is very important because it’s the part that makes your circuit a quantum node (qnode). If you don’t add it then PennyLane won’t know what device to run this circuit on.

From what I can see in your code the problem is that you’re trying to put one quantum circuit into another. I would suggest that you try passing the density matrix as a parameter of the second circuit instead, and see if this fixes your problem.

Let me know if this helps!

1 Like

Hi @shangshang_shi,

You can use a single optimizer for both circuits but you need to have a single cost function. One reason why you may be running into problems is because you haven’t set your parameters as trainable. You can define your initial parameters ‘params’ outside of the ‘train’ function and set them as trainable by adding the “requires_grad” argument to the array like this: params = np.array([0.1,0.2,0.3,0.2],requires_grad=True) . This will let the optimizer know that you need to optimize over these parameters. You can do the same for any other parameters that you need to train.

I also saw somewhere in your code that you have ._value. This will cause problems with the differentiation. I recommend that you try to simplify your program so that you can avoid this.

Please let me know if this helps!

hi ,@CatalinaAlbornoz,
pennylane has the numpy library, if we set params = np.array([0.1,0.2,0.3,0.2]), arguments will has the property, “requires_grad=True”.
You give the other suggestion, help me a lot. Here’s where the problem arose.
Thank you very much.
You are a great problems solver.

I’m glad I could help you solve your problem @shangshang_shi! And thank you for the compliment :smile: