3
$\begingroup$

I have written the following toy .qasm file, which has some adaptive measurements:

OPENQASM 2.0;
include "qelib1.inc";
qreg q_1[2];
qreg q_aux[2];
creg c_1[2];
creg c_aux[2];
h q_1[0];
cx q_1[0],q_aux[0];
measure q_aux[0] -> c_aux[0];
if(c_aux[0]==1) s q_1[0];
h q_1[0];
cx q_1[0],q_1[1];
cx q_1[1],q_aux[1];
measure q_aux[1] -> c_aux[1];
if(c_aux[1]==1) s q_1[1];
measure q_1[0] -> c_1[1];

The if statement is using the formatting that you can find in page 20 of https://arxiv.org/abs/2104.14722 :

enter image description here

The problem is that when I try importing this into Qiskit, I get an error:

enter image description here

So it looks like Qiskit is not liking the notation with the square brackets.

I have done some tests and have noticed that if I replace the if statements by something of the sort of:

if(c_aux0==1) s q_1[0];
...
if(c_aux1==1) s q_1[1];

then I wouldn't have an error. However, in general, this requires that I replace a single n-bit classical register, by n single-bit classical registers which I don't particularly like.

Does anyone know why the QasmError is happening? (Recall that my original code appears to be valid, according to https://arxiv.org/abs/2104.14722 .)

Thank you in advance!


sorry everyone this is my first experience with StackExchange and I think I've messed up and made the question as an unregistered user and so I can't seem to be able to change it properly. So I am using this answer to try to add some info (I know this is not how it is supposed to go, I apologize).

@luciano: thank you for your answer, but while the arxiv document is for OpenQASM 3 as you point out, the little snippet of code I took is (in theory) for OpenQASM 2. At least the top of page 20 (right before the code) states:

"As an example, consider an inverse quantum Fourier transform circuit as written in OpenQASM 2". So I think that this should work even for OpenQASM 2.0. But when I do:

qc1 = QuantumCircuit.from_qasm_file(f'{file_loc}{file_name}.qasm')

I get the error that I mentioned. That's what has me confused.

[Edit made by karatechop, copy pasted from answer that was meant to be edit to the question.]

$\endgroup$

1 Answer 1

2
$\begingroup$

Indeed, you are working with OpenQASM version 2 (see your header OPENQASM 2.0;). OpenQASM 2 can only do conditionals on full classical registers and, as consequence, does not allow bit subindexing in the condition. Your arxiv reference is for OpenQASM 3 which supports conditioning on single qubits.

The IBM Quantum Experience Composer only supports OpenQASM 2. Qiskit allows to do conditionals on single classical bits in coming versions 0.27 or higher.

qr = QuantumRegister(2, 'qr')
cr = ClassicalRegister(2, 'cr')
circuit = QuantumCircuit(qr, cr)
circuit.h(qr[0])
circuit.measure(qr[0], cr[0])
circuit.h(qr[1]).c_if(cr[0], True)
circuit.measure(qr[1], cr[1])

However, this circuit cannot be serialized to QASM2

circuit.qasm()
AttributeError: 'Clbit' object has no attribute 'name'
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.