Skip to content

QASM2 exporter: barriers with empty list of qubits silently accepted #10465

@MattePalte

Description

@MattePalte

Environment

  • Qiskit Terra version: 0.43.1 meta package, terra 0.24.1
  • Python version: 3.10
  • Operating system: docker continuumio/miniconda3

What is happening?

When I append barriers with empty list of target qubits I get no warnings, but then the generated qasm is invalid.

How can we reproduce the issue?

Run this python script:

from qiskit import QuantumCircuit
qc = QuantumCircuit(4,4)
for q_index in range(0, 11, 4):
    # DEBUG ONLY
    print(f"q_index: {q_index}")
    print(f"qc.qubits[q_index:q_index+4]: {qc.qubits[q_index:q_index+4]}")
    # PROBLEMATIC CODE
    qc.barrier(qc.qubits[q_index:q_index+4])
print(qc.draw())

qc.qasm(filename="my.qasm")

QuantumCircuit.from_qasm_file("my.qasm").draw(output='mpl')

Produces this error:

q_index: 0
qc.qubits[q_index:q_index+4]: [Qubit(QuantumRegister(4, 'q'), 0), Qubit(QuantumRegister(4, 'q'), 1), Qubit(QuantumRegister(4, 'q'), 2), Qubit(QuantumRegister(4, 'q'), 3)]
q_index: 4
qc.qubits[q_index:q_index+4]: []
q_index: 8
qc.qubits[q_index:q_index+4]: []
      ░
q_0: ─░─
      ░
q_1: ─░─
      ░
q_2: ─░─
      ░
q_3: ─░─
      ░
c: 4/═══

Error near line 6 Column 32
...
  File ".../lib/python3.10/site-pack
ages/qiskit/qasm/qasmparser.py", line 397, in p_id_e                           raise QasmError("Expected an ID, received '" + str(program[1].value) +
"'")                                                                       qiskit.qasm.exceptions.QasmError: "Expected an ID, received ';'"

The error happens only when importing the qasm file, whereas I believe the mistake is done by the exporter, which generates the barrier; statement, see below.

This is the genrated QASM:

OPENQASM 2.0;
include "qelib1.inc";
qreg q[4];
creg c[4];
barrier q[0],q[1],q[2],q[3];
barrier;
barrier;

What should happen?

I would have expected the QASM exporter to generate a valid qasm, ready to be reimported.

Any suggestions?

I would suggest to make the addition of barrier with empty lists raise a warning to the developer, because the problem happens also with qc.barrier([]), and the qiskit is silently generating invalid qasm.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmod: qasm2Relating to OpenQASM 2 import or export

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions