# How can I write if conditions on quantum registers using qiskit

There is a quantum register measured to state $$\left|00\right>$$ How can i write a code to check weather the first qubit in measured state is 1 or zero and if 1 do some operations or if 0 do other operation (example).

If "some operations" in your question means quantum operations, then you can use Gate.c_if() to apply a gate based on a value in a classical register as follows:

qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(2, 'c')
circ = QuantumCircuit(qr, cr)
circ.h(0)
circ.barrier()

# Measure first qubit to cr[0]
circ.measure(qr[0], cr[0])
# Apply X-gate to second qubit if the measured value equals 1
circ.x(1).c_if(cr[0], 1)
# Apply Y-gate to second qubit if the measured value equals 0
circ.y(1).c_if(cr[0], 0)


The circuit looks like:

If, however, you mean classical operations, then you can do something like that:

# This function will be called if the measured value of first qubit equals 0
def func_0(bit_string):
print('In func_0 =>', bit_string)

# This function will be called if the measured value of first qubit equals 1
def func_1(bit_string):
print('In func_1 =>', bit_string)

# Specify how many times you want to run your circuit:
shots = 10

result = backend.run(circ, shots = shots, memory = True).result()

# Check the value of first qubit.
# Note that Qiskit uses little-endian bit ordering
if bit_string[1] == '1':
func_1(bit_string)
else:
func_0(bit_string)

• So If want to use some other functions that i built in python, I have to just see weather measured quantum states are $\left|0\right>$ or $\left|1\right>$ then I have to pass in to two different functions based on $\left|0\right>$ or $\left|1\right>$ in python.(from there its pure classical) I just need to see the qubits measured are either 0 or 1.
– j123
Jun 7 at 18:41
• I'm not sure I understand you well. Could you update your question and add some code to demonstrate what you are trying to do? Jun 7 at 19:01
• I believe OP is trying to trigger python functions rather than conditionally apply gates, they require m = circ.measure(...) which they can use in a python if condition if m == 0 Jun 7 at 20:25
• Exactly same but for quantum register suppose if I measure a two qubit state and found result is $\left|01\right>$ then I have to check those qubits are either 00 or 01 or10 or11, and make them as input for some other functions that are classical based on first qubit is zero $\left|0\right> or 1$\left|1\right>\$ I need to pass to different functions.
– j123
Jun 7 at 20:47
• I'm still not sure whether I understand you well. Check my updated answer and let me know if it does not answer your question. Jun 8 at 5:47