# Using principle of deferred measurement to replace gates conditional on classical bits (c_if)

I am trying to implement the Iterative Phase Estimation algorithm on one of Qiskit's labs. I can do it for a 'nice' phase, such as 1/4 :

But if I want to implement the algo generally (as a subroutine for other algorithms, etc), the R_z gates will have to be conditioned on the values of the classical bits I measure (first gate conditioned on c_3 = 1, second gate conditioned on c_3 = 1, third gate conditioned on c_2 = 1).

I know that by the Deferred Measurement Principle, I can get a circuit with the same outcome by changing the conditioned R_z's in some way and measuring everything at the end. The question is how? Is there a general rule / set of principles on how to do this for a given circuit?

I'm asking for two reasons. First is that if I keep the c_if's, they have to be conditioned on individual bits, which isn't supported by Qiskit unless I go around it by saying that c3 = 1 if c = 4,5,6,7, etc.; and that's just not nice. Second is that I would like to execute the circuit on a real quantum computer and IBM computers don't support c_if.

If you use deferred measurement, you will end up with a circuit that has same depth and width as the original PE algorithm. And you will lose one of the main adventages of IPE algorithm; its robustness in the presence of gate errors.

First is that if I keep the c_if's, they have to be conditioned on individual bits, which isn't supported by Qiskit unless I go around it by saying that c3 = 1 if c = 4,5,6,7, etc.

You can use an array of classical registers:

cregs = []
for m in range(N):
cregs.append(ClassicalRegister(1, 'b' + str(m)))

This way the conditions will be:

c_if(cregs[k], 1)

And if you want to use it as a subroutine, you can load the content of these registers into a quantum register:

for m in range(N):
qc.x(qr[m]).c_if(cregs[N - m - 1], 1)

Please note that, many algorithms which use PE as a subroutine (e.g., HHL) depend the fact that if the input to it is a superposition of eigenstates, its output will be a superposition of the eigenvalues. This feature is not supported by IPE.

Update

According to the principle of deferred measurement, measuring commutes with conditioning. That means measuring a qubit then conditioning on the measurement result is equivalent to conditioning on the qubit value then measuring it.

In our case, to apply the principle of deferred measurement, you need to copy the output to another qubit set to $$|0⟩$$ using a $$CNOT$$ gate. This new qubit can be used to control the conditioned operations instead of using c_if. At the end, measure this qubit to get its value. In your case you need to introduce two qubits as the last measurement is done at the end already.

Note that, reset operation is implemented in IBM Quantum systems as $$X$$-gate conditioned on the measurement outcome of the qubit (see here). That means there is a measurement "hidden" in each reset operation and you have to replace it with a $$CNOT$$ conditioned on the previously introduced qubit.

The resulting circuit will be something like that except I used Phase gate instead of repeated T-gate.

• Thanks! I was asking a different question though (how to replace the c_if's with quantum gates + measurement at the end), do you know how that could be done? I realise it loses the advantage of IPE over the usual QPE, but I would like to gain the intuition behind deferred measurement to use in the future generally. – Mykolas Sveistrys Apr 4 at 9:51
• I updated my answer to cover this point also. – Egretta.Thula Apr 4 at 17:45
• Thank you so much! – Mykolas Sveistrys Apr 4 at 18:31