# IF statement in OpenQASM on IBM Quantum Experience

This is a simple circuit introduced in Moran's book "Mastering Quantum Computing with IBM QX" to demonstrate how if works in OpenQASM:

OPENQASM 2.0;
include "qelib1.inc";

qreg q; // Quantum Register
creg c; // Classical Register

x q;
measure q -> c;
if (c==1) x q;
measure q -> c;


And this is how the circuit visually looks in IBM QX Circuit Composer: The book states that at the end of the circuit, the classical register should read 00011 because after the first measurement it holds a value 1 (as a decimal number), thus satisfying the if condition. However, when I test this exact circuit in IBM QX's Circuit Composer, the result shows 00001 with 100% probability.

Interestingly enough, when if (c==1) is changed to if (c==0), the result is now 00011 with 100% probability, which I don't think is logically right.

What should be an explanation for this discrepancy? The book argues that the if statement is not supported by IBM QX (as of Jan 2019), so could this be a sort of bug?

Any help would be appreciated.

I think is, indeed, a bug in IQX. There is an explanation for this, but it is a bit obscure. It will be explained later.

Here is the result in Qiskit.

With c==1:

from qiskit import *
qc = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";

qreg q; // Quantum Register
creg c; // Classical Register

x q;
measure q -> c;
if (c==1) x q;
measure q -> c;
""")
qc.draw('mpl')

backend = BasicAer.get_backend('qasm_simulator')
execute(qc,backend).result().get_counts()

{'00011': 1024}


With c==0:

from qiskit import *
qc = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";

qreg q; // Quantum Register
creg c; // Classical Register

x q;
measure q -> c;
if (c==0) x q;
measure q -> c;
""")
qc.draw('mpl')

backend = BasicAer.get_backend('qasm_simulator')
execute(qc,backend).result().get_counts()

{'00001': 1024}


## Why this does not work on IQX visualizations

The key part is:

The visualizations do not reflect any measurement operations in your circuit.

That means that your measure statement is ignored, leaving your code like this:

x q;
if (c==1) x q;


The initial state of c is 0. Therefore, just x q; resulting in 00001.

When you changed to c==0, then the circuit is x q; x q;, resulting in 00011.

I talked with the IQX developers and they are working in a better solution to avoid skipping measurements silently. You can still run your program in IQX using a simulator backend and the result will be the correct one. This issue only pops up in the client-side visualizations, since they are statevector based.