Skip to content

QASM2 exporter when appending empty circuit #10435

@MattePalte

Description

@MattePalte

Environment

  • Qiskit Terra version: qiskit==0.43.1
  • Python version: 3.10
  • Operating system: Ubuntu 18.04

What is happening?

The QASM2 exporter creates an invalid qasm when an empty circuit is appended.

How can we reproduce the issue?

Running this simple snippet, where a circuit is appended to another generates an invalid QASM2:

from qiskit import QuantumCircuit
q_1 = QuantumCircuit(5, name='legit_circuit')
q_2 = QuantumCircuit(name='empty_circuit')
q_1.append(q_2)
q_1.qasm(filename="c1.qasm")

Output:

OPENQASM 2.0;
include "qelib1.inc";
gate empty_circuit  {  }
qreg q[5];
empty_circuit ;

Where we have an empty instruction.
It is incorrect because if cannot be re-imported:

from qiskit import QuantumCircuit
QuantumCircuit().from_qasm_file("c1.qasm")

Failing with error:

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 '{'"

What should happen?

I would have expected a valid qasm file or an error while exporting.

Any suggestions?

Probably this corner case should be checked when creating custom gates at the QASM level exporter. Either an error should be thrown when exporting, or ideally all empty instructions should be removed before exporting.

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