2
$\begingroup$

I am trying to understand how to do mid circuit measurements in pennylane. As an example here is a simple preperation of a bell state:

import pennylane as qml

dev = qml.device('default.qubit', wires=2)

@qml.qnode(device=dev):
def func(x):
    qml.Hadamard(0)
    qml.CNOT(wires=[0, 1])
    return qml.state() 

I would like to perform a measurement of qubit 0 for example, save that measurement and then continue operating on my circuit. I have tried using the qml.measure method, also together with the conditional method qml.cond(qml.measure(0), qml.Identity)(1), but apparently, although the conditional gate is shown in the circuit, the qml.state() that is returned is still in the bell state and not a collapsed state, as it should be due to the measurement.

I have also tried using qml.Projector in order to avoid measuring but that also didn't Project the qubit into the state I desired.

I would really appreciate if someone could explain me how I can measure a qubit in the middle of a circuit and store the measured value in pennylane. As said the methods I found didn't seem to collapse the wavefunction, so I believe they didn't really measure the qubit.

Thank you very much

$\endgroup$
0

1 Answer 1

1
$\begingroup$

The problem here is that qml.state() always returns a pure state. However, after measuring one of the qubits, you get a classical statistical mixture between the quantum states $\left|00\right>$ and $\left|11\right>$. It would be nice to check that the state has indeed changed from $\left|00\right> + \left|11\right>$ to $\left|00\right>\!\left<00\right| + \left|11\right>\!\left<11\right|$ with qml.density_matrix([0, 1]), but sadly Pennylane does not allow us to compute a density matrix with already-measured qubits.


EDIT: Using dev = qml.device('default.mixed', wires=2) to allow for computations with mixed states, qml.state() actually returns the density matrix for the $\left|00\right> + \left|11\right>$ state, which leads me to believe that the simulation is using the Deferred-measurement principle. And according to Pennylane's documentation, qml.state()

returns the state density matrix of the circuit prior to measurement.

This could explain why we get the pure state instead of the mixed state. No measurement was done yet. Indeed, you can check the third note here from the developers.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.