Qiskit / qiskit-qasm3-import

Importer from OpenQASM 3 to Qiskit's QuantumCircuit
https://qiskit.github.io/qiskit-qasm3-import
Apache License 2.0
15 stars 7 forks source link

Error loading the definition of `rxx` gate with the `loads` function #11

Open TheGupta2012 opened 1 year ago

TheGupta2012 commented 1 year ago

I was trying to make a mapping from the qasm2 standard gates present in qelib1.inc located in the terra lib. I am encountering the following error while loading the definition of the rxx gate as supplied in the .inc file -

ConversionError: 13,10: type error

The above exception was the direct cause of the following exception:

QASM3ImporterError                        Traceback (most recent call last)
Cell In[3], line 1
----> 1 circ = loads(qasm3)

File ~/Desktop/college/quantum/Unitary Fund/Unitary-Hack/qbraid-env/lib/python3.10/site-packages/qiskit/utils/lazy_tester.py:149, in LazyDependencyManager.require_in_call.<locals>.decorator.<locals>.out(*args, **kwargs)
    146 @functools.wraps(function)
    147 def out(*args, **kwargs):
    148     self.require_now(feature)
--> 149     return function(*args, **kwargs)

File ~/Desktop/college/quantum/Unitary Fund/Unitary-Hack/qbraid-env/lib/python3.10/site-packages/qiskit/qasm3/__init__.py:202, in loads(program)
    200     return qiskit_qasm3_import.parse(program)
    201 except qiskit_qasm3_import.ConversionError as exc:
--> 202     raise QASM3ImporterError(str(exc)) from exc

QASM3ImporterError: '13,10: type error'

I used the following script -

from qiskit.qasm3 import loads

 qasm3 = """
 OPENQASM 3.0;
 include "stdgates.inc";
 gate rxx(theta) a, b
 {
   u3(pi/2, theta, 0) a;
   h b;
   cx a,b;
   u1(-theta) b;
   cx a,b;
   h b;
   // why type error?
   u2(-pi, pi-theta) b;
 }
 qubit[3] q;
 rxx(0.5) q[0], q[1];
 """

 circ = loads(qasm3)

From the traceback, it looks like the line u2(-pi, pi-theta) b; is not getting parsed correctly. More specifically, the pi - theta parameter seems to be the source of the error.

jakelishman commented 1 year ago

Thanks for the report - looks like I made mistake with the typing of the pi built-in, and also that there's no promotion rules from angle to float in the expression evaluator. I think that there's no implicit promotion allowed in OQ3 for that because it's quite a non-trivial bit-to-bit transformation (unlike integer promotions), but if I fix the typing of pi, that shouldn't be a problem within the gate context you're concerned about here.

eginez commented 11 months ago

possibly related to: https://github.com/Qiskit/qiskit/issues/11071