# IBMQ: Can I implement a quantum measurement in the middle of a quantum circuit?

I am executing a quantum circuit on an IBM quantum device. The circuit is simple:

A single qubit (start from $$|0\rangle$$),

1. Rx($$\pi/2$$)
2. Measure (in z) 3 .Rx($$-\pi/2$$)
3. Measure (in z)

The final measurement probabilities should be Prob:1/2, result:0; Prob:1/2, result:1.

But the simulation results are always Prob:1, result:0. It is as if the simulator is ignoring the first measurement. So my question is how to implement a quantum measurement in the middle of a quantum circuit on IBMQ?

• What do you mean exactly by measuring in a middle of a quantum circuit? Technically you can measure your qubit anytime you want in the circuit, BUT once you measure a qubit you cannot run quantum operations on it anymore. I feel like you want to reset your qubit after your first RX gate, am I right? If so, there is a reset gate that put your qubit back to the $|0\rangle$ state, it is non-unitary but does not affect the qubit like the measure operation.
– Lena
Dec 1 '20 at 13:28
• Hi, thanks for the comment. Actually i don't want to reset the qubit. I just want to see how is the "state collapse" incorporated into the simulator. I should be allowed to still do the quantum experiment (run the circuit) after the first quantum measurement. For example, I want to implement the second rotation conditioned on the first measurement outcome, and upon the collapsed state. Dec 2 '20 at 3:00

If you start with $$|\psi\rangle$$ in the state $$|0\rangle$$ and apply $$Rx(\pi/2)$$ gate to it, which is the following circuit,

Your qubit is now in the state $$|\psi \rangle = \dfrac{|0\rangle - i|1\rangle}{\sqrt{2}}$$. If you are making a measurement at this step,

then you qubit state $$|\psi\rangle$$ will collapsed onto one of the eigenstate $$|0\rangle$$ or $$|1\rangle$$. In this case, it will have $$\dfrac{1}{2}$$ probability of collapsing into either $$|0\rangle$$ or $$|1\rangle$$ since $$\bigg| \dfrac{1}{\sqrt{2} } \bigg|^2 = \dfrac{1}{2}$$ and $$\bigg| \dfrac{-i}{\sqrt{2} } \bigg|^2 = \dfrac{1}{2}$$.

So after the measurement, you have two possible results:

1. $$|\psi \rangle = |0\rangle$$

2. $$|\psi \rangle = |1\rangle$$

In either case, if you are making another measurement here, then you will guarantee to collapsed into that particular state with $$100\%$$ probability. That is, if you your qubit collapsed to the state $$|0\rangle$$ after the first measurement, then the second measurement will reads out the state $$|0\rangle$$ with $$100\%$$ probability (assuming that there is no noise of course). And if you your qubit collapsed to the state $$|1\rangle$$ after the first measurement, then the second measurement will reads out the state $$|1 \rangle$$ with $$100\%$$ probability.

Now if you are running this experiments many times, then you will see that the probabilistic result on the first measurement will of course effect the readout of the second measurement. That is if you run the following circuit:

$$10,000$$ times then you expect $$5000$$ times it will be in the state $$|0\rangle$$ and $$5000$$ times is in the state $$|1\rangle$$, if there is no noise... but because of noise we will deviate from these numbers a bit. We can test this with the qasm_simulator from Qiskit on the circuit above:

Here is the code:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi
from qiskit import QuantumCircuit, BasicAer, execute
from qiskit.visualization import plot_histogram
%matplotlib inline

qreg_q = QuantumRegister(1, 'q')
creg_c = ClassicalRegister(1, 'c')
circuit = QuantumCircuit(qreg_q, creg_c)

circuit.rx(pi/2, qreg_q[0])
circuit.measure(qreg_q[0], creg_c[0])
circuit.measure(qreg_q[0], creg_c[0])

backend = BasicAer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots = 10000)
print('Counts of experiment:', job.result().get_counts())
plot_histogram(job.result().get_counts(), color='black', title="Result")


If you want to reset your qubit, that is you want to put it back to a particular state, then you can first use the reset function in Qiskit to first put the qubit to the state $$|0\rangle$$ then do some operation in follow to get the qubit to that particular state.

For instance, if you have some 1 qubit quantum circuit, and we want to reset it back to the state $$|1\rangle$$, then we can do the following:

The operation reset is indicated by the $$|0\rangle$$ symbol on the circuit. This put your qubit back to the state $$|0\rangle$$. Then by applying the $$X$$ gate (denoted as $$U_3(\pi, 0 , \pi)$$ on the circuit) to it, you put your qubit back to the state $$|1\rangle$$. So now, if you are making a measurement here, you will get a readout of the state $$|1\rangle$$ with $$100 \%$$ probability.

Here is the code to generate the circuit:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
creg_c = ClassicalRegister(1, 'c')
circuit = QuantumCircuit(qreg_q, creg_c)

circuit.rx(pi/2, qreg_q[0])
circuit.t(qreg_q[0])
circuit.s(qreg_q[0])
circuit.reset(qreg_q[0])
circuit.x(qreg_q[0])
circuit.decompose().draw( 'mpl',style={'name': 'bw'}, plot_barriers= False, initial_state = True, scale = 1)


Update after comment: To implement a $$R_x(\pi/4)$$ if the first measurement outcome is $$|0\rangle$$, and a $$R_x(\pi/8)$$ if the first outcome is $$|1\rangle$$ we can use c_if operation. The circuit is the following:

However, I don't think this is implementable on hardware yet. Below is the code:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi
from qiskit import QuantumCircuit, BasicAer, execute
from qiskit.visualization import plot_histogram
%matplotlib inline

qreg_q = QuantumRegister(1, 'q')
creg_c = ClassicalRegister(1, 'c')
circuit = QuantumCircuit(qreg_q, creg_c)

circuit.rx(pi/2, qreg_q[0])
circuit.measure(qreg_q[0], creg_c[0])
circuit.rx(pi/4, qreg_q[0]).c_if(creg_c, 0)
circuit.rx(pi/8, qreg_q[0]).c_if(creg_c, 1)
circuit.measure(qreg_q[0], creg_c[0])

backend = BasicAer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots = 1)
print('Counts of experiment:', job.result().get_counts())
plot_histogram(job.result().get_counts(), color='black', title="Result")

• Hi, thanks for the answer. Actually I don't want to reset my qubit. I just want to do the following operation upon the collapsed state (maybe this is not typically done though). Or let's say I want to achieve more: I want to implement a Rx(pi/4) if the first measurement outcome is 0, and a Rx(pi/8) if the first outcome is 1, upon the collapsed state after the first measurement. Then how could I achieve this on the IBMQ? Dec 2 '20 at 3:08
• I also tried the first example you give, but it cannot prove the first measurement is actually implemented. Because the outcome statistics is the same no matter you have one or two measurement. Dec 2 '20 at 3:09
• @dr.bian I posted the update on what I think you were asking for. And if you want to check to see whether the first measurement is being implemented, we can apply the $R_x(-\pi/2)$ to the qubit after the first measurement. If the first measurement is not being implemented then our qubit should be in the state $|0\rangle$ in the second measurement. If the first measurement is indeed being done then our second measurement will be in the state $|0\rangle$ or $|1\rangle$ with 1/2 probability. Dec 2 '20 at 3:12