Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qasm3.Exporter raises an obscure error on invalid basis-gate name #12886

Closed
t-imamichi opened this issue Aug 2, 2024 · 5 comments · Fixed by #12945
Closed

qasm3.Exporter raises an obscure error on invalid basis-gate name #12886

t-imamichi opened this issue Aug 2, 2024 · 5 comments · Fixed by #12945
Assignees
Labels
bug Something isn't working priority: low

Comments

@t-imamichi
Copy link
Member

Environment

  • Qiskit version: 1.2.0 rc1
  • Python version: 3.12.4
  • Operating system: macOS 14.6

What is happening?

qasm3.Exporter raises an error with a simple case with 1.2.0 rc1 while 1.1.1 works fine.

How can we reproduce the issue?

from qiskit import QuantumCircuit, qasm3
from qiskit.providers.fake_provider import GenericBackendV2

circuit = QuantumCircuit(2)
backend = GenericBackendV2(2)
exporter = qasm3.Exporter(basis_gates=backend.operation_names)
result = exporter.dumps(circuit)
print(result)

qiskit 1.2.0 rc1

Traceback (most recent call last):
  File "/Users/ima/tasks/4_2024/qiskit/qasm3-import/tmp/test_min.py", line 7, in <module>
    result = exporter.dumps(circuit)
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/qasm3/exporter.py", line 182, in dumps
    self.dump(circuit, stream)
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/qasm3/exporter.py", line 196, in dump
    builder.build_program()
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/qasm3/exporter.py", line 629, in build_program
    self.symbols.register_gate_without_definition(builtin, None)
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/qasm3/exporter.py", line 435, in register_gate_without_definition
    name = self.escaped_declarable_name(name, allow_rename=False, unique=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/qasm3/exporter.py", line 352, in escaped_declarable_name
    raise QASM3ExporterError(f"cannot use the keyword '{name}' as a variable name")
qiskit.qasm3.exceptions.QASM3ExporterError: "cannot use the keyword 'reset' as a variable name"

qiskit 1.1.1

OPENQASM 3.0;
include "stdgates.inc";
qubit[1] q;

What should happen?

It would be nice to run without error or show a warning instead of the error.

Any suggestions?

No response

@t-imamichi t-imamichi added the bug Something isn't working label Aug 2, 2024
@jakelishman
Copy link
Member

Imo it's right to error here, but the message could be a lot better. Passing arbitrary values in basis_gates is not really a good idea - the whole option is kind of a hack, because it causes invalid OQ3 to be generated (in general) by declaring gates that are not defined.

This code example is telling the exporter to treat reset as if it's a "basis gate" that will accept any number of parameters and qubits, so the exporter attempts to put that into its symbol table. That cannot succeed, because reset is a keyword of the language. The older versions of the exporter did far less symbol tracking (and had a lot more bugs as a result).

The code example here probably shouldn't set basis_gates at all, tbh.

@jakelishman
Copy link
Member

Though that said, if there are backends doing this, including passing reset here, I'm open to making it a warning now and a hard error in the future (and the warning will have to explain that the call is invalid).

@t-imamichi
Copy link
Member Author

Thank you for your information. I will stop passing backend's basis gates to basis_gates.
It would be nice to make the message a bit better. But, I don't have a strong opinion. So, you can close it.

@jakelishman
Copy link
Member

Let's leave it open - the error message isn't good, and I'll write a better one.

@1ucian0 1ucian0 changed the title qasm3.Exporter raises an error with Qiskit 1.2.0 rc1 qasm3.Exporter raises an obscure error Aug 7, 2024
@1ucian0
Copy link
Member

1ucian0 commented Aug 7, 2024

Editing the title to reflect the discussion conclusion

@jakelishman jakelishman changed the title qasm3.Exporter raises an obscure error qasm3.Exporter raises an obscure error on invalid basis-gate name Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority: low
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants