# Reading a classical register in OPENQASM

The IBM OpenQASM program below clones a state selected from an orthogonal pair. It seems to work but there is a mystery. I declare the classical register as an array c[1] but when I try to test its contents, the assembler will not allow me to index it. That is, it rejects "c[0]". I get similar results if there is an array of two registers; I always have to use the array name rather than a reference to an array element. My question is, how do you reference one classical register within an array, for example in an "if" statement.

There is a second question, less pressing but important. I don't know who to make a block of code that is executed when the "if" test passes. I am forced to repeat the test for each line in the block. How do you declare a block of code or, equivalently, use a branch statement to skip a block?

cloner1 = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[5];
creg c[1];
x q[2];
x q[4];

x q[0]; //Set up input
measure q[0] -> c[0];
if (c==1) CX q[1],q[2];
if (c==1) CX q[2],q[1];
if (c==1) CX q[1],q[2];
if (c==1) CX q[3],q[4];
if (c==1) CX q[4],q[3];
if (c==1) CX q[3],q[4];
measure q[1]->c[0];
measure q[3]->c[0];
"""
$$$$

• Hello, I'm confused by what you're trying to achieve. Do you want to clone any arbitrary state in the future? Because if so, it's not possible because of the no-cloning theorem, one of the basics theorems of QC. Also, why measure 3 times on the same classical register? You lose the info of the first and second measure by doing that, don't you need it? And about the if block, you can't really do this the same way you would classically. If you explain a bit more what you want to do exactly, maybe I or someone else could help on this issue! :)
– Lena
Commented Nov 17, 2021 at 13:37
• The no-cloning theorem does not apply when you only want to clone one of a pair of orthogonal states. You seem to be saying the "if (c==1)" is a measurement. I thought it was a test of the result of a measurement rather than an act of measurement. Basically, I want to perform a measruement on q[0] and use the result to decide whether or not to apply the CX gates. Commented Nov 18, 2021 at 1:33

## 1 Answer

In OpenQASM 2, if statement conditionally executes a single quantum operation based on the integer value stored across the classical register rather than on the individual bits.

As per the language specs (arXiv:1707.03429):

The if statement conditionally executes a quantum operation based on the value of a classical register. This allows measurement outcomes to determine future quantum operations. We choose to have one decision register for simplicity. This register is interpreted as an integer, using the bit at index zero as the low order bit. The quantum operation executes only if the register has the given integer value. Only quantum operations, i.e. builtin gates, gate (and opaque) subroutines, preparation, and measurement, can be prefaced by if.

This fact is repeated in OpenQASM 3 document also (arXiv:2104.14722):

The only control flow supported by OpenQASM2 are if statements. These can be used to compare the value of a classical bit-register (interpreted as a little-endian representation of an integer) to an integer, and conditionally execute a single gate. An example of such a statement is

if (c == 5) mygate q, r, s;

which would test whether a classical register c (of length three or more) stores a bit-string $$c_{n-1} ... c_3c_2c_1c_0$$ equal to $$0...0101$$, to determine whether to execute mygate q, r, s. If-statements (and classical register comparisons) of this kind are supported in OpenQASM3, as is more general syntax for if statements and other forms of control-flow.

Also, see this answer: https://quantumcomputing.stackexchange.com/a/4516/9474

Note that, in OpenQASM 3 you can condition on the value of single classical bit:

if (c[0] == 1) p(-𝜋/4) q[2];
if (c[1] == 1) p(-𝜋/2) q[2];


See the example in page 20 in the language specs. Moreover, the document states that:

We also extend the if statement syntax to allow for multiple instructions within the body of the if, and to allow for an else block to accompany it.

• How do you delimit the conditional block? Commented Nov 20, 2021 at 21:27
• Code blocks are enclosed by braces: {, and }` Commented Nov 21, 2021 at 3:00