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).


1 Answer 1


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)

# 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:

enter image description here

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()
readouts = result.get_memory()

for bit_string in readouts:
    # Check the value of first qubit.
    # Note that Qiskit uses little-endian bit ordering
    if bit_string[1] == '1':
  • $\begingroup$ 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. $\endgroup$
    – j123
    Jun 7 at 18:41
  • $\begingroup$ 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? $\endgroup$ Jun 7 at 19:01
  • $\begingroup$ 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 $\endgroup$ Jun 7 at 20:25
  • $\begingroup$ 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. $\endgroup$
    – j123
    Jun 7 at 20:47
  • $\begingroup$ 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. $\endgroup$ Jun 8 at 5:47

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.