3
$\begingroup$

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[5]; // Quantum Register
creg c[5]; // Classical Register
 
x q[0];
measure q[0] -> c[0];
if (c==1) x q[1];
measure q[1] -> c[1];

And this is how the circuit visually looks in IBM QX Circuit Composer:

enter image description here

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.

$\endgroup$
2
$\begingroup$

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.

Your code is correct:

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[5]; // Quantum Register
creg c[5]; // Classical Register
 
x q[0];
measure q[0] -> c[0];
if (c==1) x q[1];
measure q[1] -> c[1];
""")
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[5]; // Quantum Register
creg c[5]; // Classical Register
 
x q[0];
measure q[0] -> c[0];
if (c==0) x q[1];
measure q[1] -> c[1];
""")
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

There is note in the IQX documentation for visualizations: enter image description here

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[0];
if (c==1) x q[1];

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

When you changed to c==0, then the circuit is x q[0]; x q[1];, 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.

| improve this answer | |
$\endgroup$

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.