Qiskit / qiskit-qasm3-import

Importer from OpenQASM 3 to Qiskit's QuantumCircuit
https://qiskit.github.io/qiskit-qasm3-import
Apache License 2.0
15 stars 7 forks source link

qasm3.dumps Output Contains "!" Character Causing Loading Error #14

Closed PhysicsQoo closed 1 year ago

PhysicsQoo commented 1 year ago

When using qasm3.dumps to output QASM3 strings, an issue arises when a while loop with a condition of 0 is encountered. The generated QASM3 string contains a "!" character as part of the condition, which subsequently leads to an error during the loading of the QASM3 code.

Code Example

from qiskit.circuit import QuantumCircuit, Clbit, Qubit, qasm3
bits = [Qubit(), Qubit(), Clbit()]
qc = QuantumCircuit(bits)

with qc.while_loop((bits[2], 0)):
    qc.h(0)
    qc.cx(0, 1)
    qc.measure(0, 0)
print(qasm3_str:=qasm3.dumps(qc))

Generated QASM3 Code:

OPENQASM 3;
include "stdgates.inc";
bit _bit0;
qubit _qubit0;
qubit _qubit1;
while (!_bit0) {
  h _qubit0;
  cx _qubit0, _qubit1;
  _bit0 = measure _qubit0;
}

Attempting to Load QASM3 Code:

qc_load = qasm3.loads(qasm3_str)

Error Message:

QASM3ImporterError: "6,7: unhandled unary operator '!'"

Expected Behavior

To address the issue of the "!" character causing errors during loading, an alternative approach can be explored. This approach involves modifying the qasm3.loads function to handle the "!" character appropriately. By extending the functionality of the qasm3.loads function, it would be possible to interpret and process the "!" character within the while loop condition, preventing unhandled unary operator errors.

Steps to Reproduce

Execute the provided Python code snippet that uses qasm3.dumps to generate a QASM3 string containing a while loop with a 0 condition. Attempt to load the generated QASM3 string using the qasm3.loads function.

Additional Information

qiskit Version: 0.44.0 openqasm3 Version: 0.4.0 qiskit_qasm3_import Version: 0.2.0 Python Version: 3.9.7

jakelishman commented 1 year ago

Thanks for the report. Yeah, I haven't updated the OpenQASM 3 importer to take into account all the newly supported parts of Qiskit Terra 0.25 yet (Qiskit 0.44) yet, but I need to.