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

Standard Gates in 'qelib1.inc' Not Recognized by qiskit.qasm2.loads #12124

Open
p51lee opened this issue Apr 3, 2024 · 2 comments
Open

Standard Gates in 'qelib1.inc' Not Recognized by qiskit.qasm2.loads #12124

p51lee opened this issue Apr 3, 2024 · 2 comments
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export

Comments

@p51lee
Copy link

p51lee commented Apr 3, 2024

Environment

  • Qiskit version: 1.0.2
  • Python version: 3.10.9
  • Operating system: MacOS 14.4

What is happening?

qiskit.qasm2.loads is not recognizing standard gates provided in qelib1.inc.

How can we reproduce the issue?

To reproduce the issue, execute the following Python code:

import qiskit

qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
swap q[0], q[1];
"""
circuit = qiskit.qasm2.loads(qasm)

Running this code results in a QASMParseError:

qiskit.qasm2.exceptions.QASM2ParseError: "<input>:10,6: 'swap' is not defined in this scope"

This error is also happening with other standard gates from qelib1.inc:

  • u0
  • u
  • p
  • sx
  • sxdg
  • swap
  • cswap
  • crx
  • cry
  • cp
  • csx
  • cu
  • rxx
  • rzz
  • rccx
  • rc3x
  • c3x
  • c3sqrtx
  • c4x

What should happen?

The parser should recognize and correctly process these gates as pre-defined, analogous to how it handles the Hadamard gate.

Any suggestions?

No response

@p51lee p51lee added the bug Something isn't working label Apr 3, 2024
@jakelishman
Copy link
Member

The version of the "qelib1.inc" file you've linked here is actually a mutant version that Qiskit amended over time, and does not reflect the original definition of the file - all the gates you've commented on here are legacy Qiskit extensions to it. The qasm2.load and qasm2.loads parsers both treat qelib1.inc exactly as specified in the original paper, and that documentation also explains the differences between the "real" and "legacy" versions.

Unfortunately, qiskit.qasm2.dumps still outputs assuming the mutant version of qelib1.inc right now - we have a discussion (#10737) on fixing that situation, but we keep struggling to find time to prioritise it. In the mean time, you can make qasm2.loads (and load) understand the whole legacy file by doing:

from qiskit import qasm2

qasm2.loads(program, custom_instructions=qasm2.LEGACY_CUSTOM_INSTRUCTIONS)

@jakelishman jakelishman added the mod: qasm2 Relating to OpenQASM 2 import or export label Apr 3, 2024
@p51lee
Copy link
Author

p51lee commented Apr 3, 2024

Thank you for �the clarification. Indeed, I encountered issues when trying to dump and load QASM programs. I'll use LEGACY_CUSTOM_INSTRUCTIONS in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export
Projects
None yet
Development

No branches or pull requests

2 participants