Obtain the quantum state on partial wires

Hi, I am implementing quantum auto-encoder with pennylane. Is there a way to obtain the quantum state on partial wires? Or any suggestions to construct circuit below with pennylane API?
qae

Thanks in advance.

Hey @Yang !

It looks like you need to make use of mid-circuit measurements. The functions you should check out are qml.measure and qml.cond.

Here is an example of both in use:

>>> dev = qml.device("default.qubit", wires=2)
>>> 
>>> @qml.qnode(dev)
... def circuit(x):
...     qml.Hadamard(0)
...     qml.Hadamard(1)
...     qml.RX(x, 0)
...     qml.CNOT([0, 1])
...     m0 = qml.measure(0)
...     qml.cond(m0, qml.RZ)(1.23, 1)
...     return qml.expval(qml.PauliZ(1))
... 
>>> circuit(4.56)
tensor(0., requires_grad=True)

Let me know if this helps!

Hi @isaacdevlugt,

Thanks for your advice. The functions qml.measure and qml.cond seems not to work in my case. Take the circuit shown in the figure as example. Denote the system consisting of the first two qubits by A, and denote the system consisting of the last two qubits by B. What I intend to implement is to obtain the state of subsystem B \rho_B=Tr_A(\rho_{AB}). Could I implement with pennylane?

Hi @Yang ,

It’s strange that qml.measure and qml.cond don’t work for you. It’s possible that you’re using an old version of PennyLane. Make sure you’re using version 0.28. You can check it by looking at the output of qml.about().

If you are using v0.28 and the error persists, could you please post your full error traceback and the output of qml.about()?

Hi @CatalinaAlbornoz ,

Thanks. The problem is solved by installing the latest Pennylane.

It’s great to hear that this solved your problem @Yang !


Hello, I have also encountered similar problems in the process of learning, this is the middle way measurement, then if there is no need to measure, I just need to input the final quantum state of the circuit of the previous repeating part as the initial state of the next repeating quantum circuit, in Pennylane writing, then what should I do? Thank you in advance. I really hope to get your help.

微信图片_20231223092202
This picture of a quantum circuit is what I need to achieve, consistent with the problem described above.

@isaacdevlugt @CatalinaAlbornoz I am very sorry if I bother you, but I have been confused for several days and still have not been solved. I hope you can help me. Just like the circuit shown above, I need to call the same module circuit repeatedly. It became a problem I couldn’t solve.

Hey @gbc,

Would using post-selection help? This is a new feature that we’ve added (you’ll need to have pennylane v0.33). Check out this blog entry that we made: PennyLane v0.33 released | PennyLane Blog

Let me know if this helps!