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

OpenQASM3 import/export mechanism not working with for loop #13725

Open
arthurostrauss opened this issue Jan 23, 2025 · 0 comments
Open

OpenQASM3 import/export mechanism not working with for loop #13725

arthurostrauss opened this issue Jan 23, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@arthurostrauss
Copy link
Contributor

Environment

  • Qiskit version: 1.3.2
  • Python version: 3.10
  • Operating system: Mac OS Sequoia

What is happening?

I am encountering an error when trying to play around with the OpenQASM3 importer from Qiskit, and seems related to a syntax that is not updated with latest openqasm3 (1.0.0):

`from qiskit import QuantumCircuit
from qiskit.circuit import Parameter, Gate, ParameterVector
from qiskit.qasm3 import Exporter, loads

qc = QuantumCircuit(1)
theta = Parameter("θ")
theta2 = Parameter("θ_2")
theta3 = ParameterVector("θ_3", 2)
n = Parameter("n")

gate = Gate("my_gate", 1, [theta3[0]])
with qc.for_loop([1, 3, 5]):
qc.x(0)
qc.rx(theta3[1]*n, 0)
qc.append(gate, [0])

Convert Qiskit circuit to OpenQASM3

exporter = Exporter(includes=(), basis_gates=("my_gate", "rx", "x"),
)
qasm3 = exporter.dumps(qc)
print(qasm3)

loads(qasm3)`

The export works correctly and yields:
OPENQASM 3.0; input float[64] n; input float[64] _θ_3_0_; input float[64] _θ_3_1_; qubit[1] q; for _ in {1, 3, 5} { x q[0]; rx(_θ_3_1_*n) q[0]; my_gate(_θ_3_0_) q[0]; }

But it is when trying to import through loads this generated file that the parser seems not able to cope with the loop parameter defined in the for loop. In fact, when trying to add the extra parameter n as a loop parameter, the same error appears:
line 6:4 no viable alternative at input '_'
When n is set as the loop parameter it displays the same thing but with 'n' at the end.
My guess is that it is because the type of this loop variable is not parametrized a priori and therefore Qiskit exporter is not up to date (I have the latest version of openqasm3), but that could be wrong.

How can we reproduce the issue?

`from qiskit import QuantumCircuit
from qiskit.circuit import Parameter, Gate, ParameterVector
from qiskit.qasm3 import Exporter, loads

qc = QuantumCircuit(1)
theta = Parameter("θ")
theta2 = Parameter("θ_2")
theta3 = ParameterVector("θ_3", 2)
n = Parameter("n")

gate = Gate("my_gate", 1, [theta3[0]])
with qc.for_loop([1, 3, 5]):
qc.x(0)
qc.rx(theta3[1]*n, 0)
qc.append(gate, [0])

Convert Qiskit circuit to OpenQASM3

exporter = Exporter(includes=(), basis_gates=("my_gate", "rx", "x"),
)
qasm3 = exporter.dumps(qc)
print(qasm3)

loads(qasm3)`

What should happen?

The exporter should either be conforming itself with latest OpenQASM3 AST syntax for the for loop, or the importer should be fixed somewhere.

Any suggestions?

No response

@arthurostrauss arthurostrauss added the bug Something isn't working label Jan 23, 2025
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

1 participant