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

QASM output for multiple R gates #11017

Closed
christian512 opened this issue Oct 13, 2023 · 3 comments
Closed

QASM output for multiple R gates #11017

christian512 opened this issue Oct 13, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@christian512
Copy link

Environment

  • Qiskit Terra version: 0.25.2.1
  • Python version: 3.11.5
  • Operating system: Fedora

What is happening?

I create a QuantumCircuit from a qasm string that contains multiple R gates. E.g.:

OPENQASM 2.0;
include "qelib1.inc";
gate r(param0,param1) q0 { u3(param0,param1 - pi/2,pi/2 - 1.0*param1) q0; }
qreg q[2];
r(0.1,0.1) q[0];
r(0.5,0.5) q[1];

Generating the qasm string from the previously generated QuantumCircuit does result in the R gates being defined multiple times:

OPENQASM 2.0;
include "qelib1.inc";
gate r(param0,param1) q0 { u3(0.1,-1.4707963267948965,1.4707963267948965) q0; }
gate r_140480215738000(param0,param1) q0 { u3(0.5,-1.0707963267948966,1.0707963267948966) q0; }
qreg q[2];
r(0.1,0.1) q[0];
r_140480215738000(0.5,0.5) q[1];

How can we reproduce the issue?

qc = QuantumCircuit(2)
qc.r(0.1,0.1,0)
qc.r(0.5,0.5,1)

qc2 = QuantumCircuit.from_qasm_str(qc.qasm())
qc2.qasm()
``

### What should happen?

I would expect the `qasm` input and output to a `QuantumCircuit` to be the same. Especially, if there are no changes applied to the object.

### Any suggestions?

_No response_
@christian512 christian512 added the bug Something isn't working label Oct 13, 2023
@christian512 christian512 changed the title QASM output for R gate changes QASM output for multiple R gates Oct 13, 2023
@jakelishman
Copy link
Member

jakelishman commented Oct 13, 2023

This is in principle a duplicate of #5082, which requires a larger rework of our data model to handle (which we are working on, I promise! - it's just a very very big change), but the reason you only see this after a re-import from the OQ2 string is because Qiskit can't know for certain that it's safe for it to use its built-in RGate to represent the gate r statement.

We're currently reworking how we'll define header files for OpenQASM 2, which will make this easier, but in the meantime, a direct workaround is to replace the legacy QuantumCircuit.from_qasm_str with qiskit.qasm2.loads, which is far more configurable. If you do this:

from qiskit import qasm2
from qiskit.circuit.library import RGate

prog = """
OPENQASM 2.0;
include "qelib1.inc";
gate r(param0,param1) q0 { u3(param0,param1 - pi/2,pi/2 - 1.0*param1) q0; }
qreg q[2];
r(0.1,0.1) q[0];
r(0.5,0.5) q[1];
"""

custom_instructions = list(qasm2.LEGACY_CUSTOM_INSTRUCTIONS)
custom_instructions.append(qasm2.CustomInstruction("r", 2, 1, RGate))

qc = qasm2.loads(prog, custom_instructions=custom_instructions)
print(qasm2.dumps(qc))

I think you'll get what you expect.

edit: just modified the code block - I'd flipped the arguments in CustomInstruction by accident.

@jakelishman
Copy link
Member

The gist of #10737 (linked above) is that we're going to start exposing new header files called things like qiskit/v1.qasm, and that will include definitions of all of Qiskit's standard gates, not just the legacy ones that kind of got mashed into Qiskit's not-very-accurate version of qelib1.inc (see #4312). That will let us safely know when we're importing OQ2 that's targetted at our standard gates, without making assumptions about user-defined gates.

@christian512
Copy link
Author

Thanks @jakelishman for the detailed response!
Using qasm2.loads() solved my issue. Looking forward to the new implementation.

I close this issue, since it is a duplicate of #5082.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants