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

Qiskit's circuit_to_qasm_string functionality can't handle qiskit.circuit.library.GR circuit #8090

Closed
vtomole opened this issue May 19, 2022 · 2 comments
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export

Comments

@vtomole
Copy link

vtomole commented May 19, 2022

Environment

  • Qiskit Terra version: 0.18.3
  • Python version: Python 3.8.10
  • Operating system: Ubuntu 20.04.4 LTS

What is happening?

Converting qiskit.circuit.library.GR capitalizes the GR instruction (GR) when it should be gr. GR is not valid QASM. It also defines the GR gate in int parameters instead of ID.

How can we reproduce the issue?

import qiskit
circuit = qiskit.circuit.library.GR(2, 0.1, 0.2)  # (note GR() outputs a QuantumCircuit, not a Gate)
qasm_str = circuit.qasm()
print(qasm_str)  
# prints:
# """
# OPENQASM 2.0;
# include "qelib1.inc";
# gate r(param0,param1) q0 { u3(0.1,-1.3707963,1.3707963) q0; }
# gate GR(0.10, 0.20) q0,q1 { r(0.1,0.2) q0; r(0.1,0.2) q1; }
# qreg q[2];
# GR(0.10, 0.20) q[0],q[1];
# """

_ = qiskit.QuantumCircuit.from_qasm_str(qasm_str)  # raises: "qiskit.qasm.exceptions.QasmError: 'Unable to match any token rule, got -->G<-- Check your OPENQASM source and any include statements.'"

Converting the qasm definition to gr comes up with a different error

qasm_str =  """
    OPENQASM 2.0;
    include "qelib1.inc";
    gate r(param0,param1) q0 { u3(0.1,-1.3707963,1.3707963) q0; }
    gate gr(0.10, 0.20) q0,q1 { r(0.1,0.2) q0; r(0.1,0.2) q1; }
    """

_ = qiskit.QuantumCircuit.from_qasm_str(qasm_str) # raises qiskit.qasm.exceptions.QasmError: "Expected an ID, received '0.10'"

What should happen?

The above snippets shouldn't throw exceptions.

Any suggestions?

No response

@jakelishman
Copy link
Member

jakelishman commented May 23, 2022

Thanks for the report! Please could you try this on a more recent version of Terra? I believe this should be fixed by #7157, which was released in Terra 0.19. I think the bug came because the GR gate used to say its "name" was GR(0.1, 0.2) and that it had zero parameters - the QASM 2 exporter used to just take the name verbatim (which was clearly incorrect), so here it ends up outputting something that looks like a gate definition, but is completely wrong.

At any rate, with the current main branch of Terra I get:

from qiskit.circuit.library import GR
print(GR(2, 0.1, 0.2).qasm())
OPENQASM 2.0;
include "qelib1.inc";
gate r(param0,param1) q0 { u3(0.1,-1.3707963,1.3707963) q0; }
gate gate_GR_0_10__0_20_ q0,q1 { r(0.1,0.2) q0; r(0.1,0.2) q1; }
qreg q[2];
gate_GR_0_10__0_20_ q[0],q[1];

@HuangJunye HuangJunye added the mod: qasm2 Relating to OpenQASM 2 import or export label May 23, 2022
@vtomole
Copy link
Author

vtomole commented May 23, 2022

Yup, this is fixed by terra version 0.19. Thanks!

@vtomole vtomole closed this as completed May 23, 2022
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

3 participants