# How to create a condition on only one classical bit when we have a total of 2 classic bits in the system

I am trying to make a quantum circuit with one qubit and 2 classical bits for each measurment in the system below: 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:

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

# building the circuit
qc.h(q)
qc.measure(q,c)
qc.x(q).c_if(c, 0)
qc.measure(q,c)
circuit_drawer(qc)


and the error is:

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


how to write it correctly?

When I try to change

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


with:

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


I succeed in building my circuit but I get circuit that I dont want to work with: I wish for help, thanks.

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: 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"!

• Do I need to put this algorithm between 2 measurments? – Daniel Vainshtein Sep 20 '18 at 11:35
• That depends on what you want to do. One thing I can say though, is that you second measurement should just read 0. – user1271772 Sep 20 '18 at 11:43
• If that is all ou want to do, the circuit I gave you in my answer is enough. You don't need anything else. – user1271772 Sep 20 '18 at 12:02
• 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. – user1271772 Sep 20 '18 at 12:16
• @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 :) – user1271772 Sep 20 '18 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:


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

qc.h(q)
qc.measure(q,c)
qc.x(q).c_if(c, 0)
qc.measure(q,c)
circuit_drawer(qc)


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

• I think at some point the API changed, but in any case the method is now called add_register not just add`. – dlyongemallo Jan 2 at 10:04