I'm trying to write a 1-bit teleportation error correction code and there is one part in it where I need to add a gate that's dependent on a measurement from earlier in the circuit. The measurement is stored on a single classical bit, and I'm trying to use c_if to apply the gate based on the measurement result of that bit.

Intuitively I would think this could be written as: qc.h(q[0]).c_if(c[3], 1) but that only returns an error while qc.h(q[0]).c_if(c, 1) gives no error (though it doesn't give the result I'm looking for). Is there a way to link c_if to a single classical bit instead of the classical register?

This question is similar and the answer seems to imply that it isn't possible to use c_if this way.

If c_if can't do this, would something like what they mentioned in their question: if(c[3]==1) qc.h(q[0]) work in qiskit? For my purposes the operation needs to be integrated into the circuit, so if it does work would the gate be applied automatically?


c_if must be used on an entire ClassicalRegister. However, it is still possible to use it on a single classical bit. You would need to create a ClassicalRegister of size 1, and attach that to your circuit. This would be the register that you input into the c_if call.

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

c1 = ClassicalRegister(1)
c3 = ClassicalRegister(3)
q4 = QuantumRegister(4)
qc = QuantumCircuit(q4, c3)  # Instantiate with one of the classical registers 
qc.add_register(c1) # Add the other classical register

qc.measure(range(4), range(4)
qc.x(0).c_if(c1, 1)  # Add an X-Gate if the classical register c1 outputs 1

Do note though, using c_if is not supported on the real hardware, so you would only be able to run this circuit on the simulator.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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