I am trying to make a quantum circuit with one qubit and 2 classical bits for each measurment in the system below: condition on the first measurment

I want to make condition on the first bit: if the first collapse to zero so x operator is act on the circuit, else (one) nothing is acting on the circuit.

I am using qiskit language.

but when I try to create my circuit, there is always an error:

q = QuantumRegister(1)
c = ClassicalRegister(2)
qc = QuantumCircuit(q,c)

# building the circuit
qc.x(q[0]).c[0]_if(c[0], 0)

and the error is:

  File "<ipython-input-4-66c70285946b>", line 3
    qc.x(q[0]).c[0]_if(c[0], 0)
SyntaxError: invalid syntax

how to write it correctly?

When I try to change

qc.x(q[0]).c[0]_if(c[0], 0)


qc.x(q).c_if(c, 0)

I succeed in building my circuit but I get circuit that I don`t want to work with: Unwanted condition on measurment

I wish for help, thanks.


2 Answers 2


The controlled NOT gate does the opposite of what you want, it applies $X$ to the target qubit if the control qubit is 1, and does nothing if the control qubit is 0.

What you want is to apply $X$ when the control qubit is 0 and do nothing when it is 1. This can be accomplished by applying a NOT gate (i.e. an $X$ gate) before doing the CNOT.

In the IBM composer it would look like this:

enter image description here

The code for doing CNOT in quiskit is:

gate cx c,t {
 CX c,t; 

Since there is a specific gate for what you want to do, you do not need any "if statements"!

  • $\begingroup$ Do I need to put this algorithm between 2 measurments? $\endgroup$ Sep 20, 2018 at 11:35
  • 1
    $\begingroup$ That depends on what you want to do. One thing I can say though, is that you second measurement should just read 0. $\endgroup$ Sep 20, 2018 at 11:43
  • 1
    $\begingroup$ If that is all ou want to do, the circuit I gave you in my answer is enough. You don't need anything else. $\endgroup$ Sep 20, 2018 at 12:02
  • 1
    $\begingroup$ It depends what you want to do brother. I have you the circuit for |00> to |01> and |10> to |10>. I don't know what your over goal is. $\endgroup$ Sep 20, 2018 at 12:16
  • 1
    $\begingroup$ @DanielVainshtein: Considering how many follow-up questions you've asked, and the amount of effort put into the answer, it might be a good idea to show some appreciation when people invest their time to helping you :) $\endgroup$ Sep 20, 2018 at 14:28

See the answer to this question for more on how the classical control works. Basically, your operations are controlled on the integer stored (in binary) across a register rather than on the individual bits themselves.

I also don't quite know the 'best practice' way of controlling on single bits, but I can tell you my workaround. Instead of creating a register with two bits, I create a list of two single qubit registers.

c = [ ClassicalRegister(1) for _ in range(2) ]

These can be added using the add method of a quantum circuit.

q = QuantumRegister(1)
qc = QuantumCircuit(q)
for register in c:
    qc.add_register( register )

Then it is possible to use your 'building the circuit' code can be done with

qc.x(q[0]).c_if(c[0], 0)

This works because c[0] now refers to a classical register, rather than a single bit from a classical register.

  • 1
    $\begingroup$ I think at some point the API changed, but in any case the method is now called add_register not just add. $\endgroup$ Jan 2, 2020 at 10:04

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .