qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

`qasm3.loads` fails on circuits with `switch` due to unsupported `int` declaration

Open YaegakiY opened this issue 6 months ago • 2 comments

Environment

  • Qiskit version: 2.0.1
  • Python version: 3.13.2
  • Operating system: Windows 11, Version 24H2, Build 26100
  • OpenQASM 3.0 version: 1.0.1

What is happening?

When using qiskit.qasm3.dumps on a circuit containing a switch statement, the resulting QASM code includes an int declaration (int switch_dummy;). However, when reloading it via qasm3.loads, the process fails with:

qiskit.qasm3.exceptions.QASM3ImporterError: "4,0: declarations of type 'int' are not supported"

How can we reproduce the issue?

To Reproduce:

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile
from qiskit_aer import Aer

qreg = QuantumRegister(2) 
creg = ClassicalRegister(2) 
qc = QuantumCircuit(qreg, creg)

qc.h(qreg[0])
qc.cx(qreg[0], qreg[1])
qc.measure(qreg[0], creg[0])
qc.measure(qreg[1], creg[1])
with qc.switch(creg) as case:
	with case(0b00, 0b11):
		qc.x(1)
	with case(case.DEFAULT):
		qc.z(1)
qc.reset(qreg)
qc.measure(qreg[0], creg[0])
qc.measure(qreg[1], creg[1])

from qiskit.qasm3 import dumps, loads
code = dumps(qc)

print(code)

qasm3_to_qiskit = loads(code)

Generated QASM3 code:

OPENQASM 3.0;
include "stdgates.inc";
bit[2] c0;
int switch_dummy;
qubit[2] q0;
h q0[0];
cx q0[0], q0[1];
c0[0] = measure q0[0];
c0[1] = measure q0[1];
switch_dummy = c0;
switch (switch_dummy) {
  case 0, 3 {
    x q0[1];
  }
  default {
    z q0[1];
  }
}
reset q0[0];
reset q0[1];
c0[0] = measure q0[0];
c0[1] = measure q0[1];

What should happen?

The loads function should be able to parse the output from dumps.

Any suggestions?

No response

YaegakiY avatar Jun 03 '25 09:06 YaegakiY

Hello, I'd like to try working on this issue. Can you please assign this to me?

jan-an avatar Jun 07 '25 13:06 jan-an

Thanks for your interest! Sure, feel free to take it. I’m happy to see this get fixed. Let me know if you need any extra test cases or clarification.

YaegakiY avatar Jun 09 '25 03:06 YaegakiY