Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.06k stars 2.33k forks source link

Failure to parse standard gates of `qelib1.inc` #10184

Closed TheGupta2012 closed 1 year ago

TheGupta2012 commented 1 year ago

Environment

What is happening?

While trying to parse a qasm2 string consisting of u0 gate call from the qelib1.inc gates, an error is encountered. The traceback for the error is :

...
File ~/Desktop/college/quantum/Unitary Fund/Unitary-Hack/qbraid-env/lib/python3.10/site-packages/qiskit/converters/ast_to_dag.py:430, in AstInterpreter._create_dag_op(self, name, params, qargs)
    418 def _create_dag_op(self, name, params, qargs):
    419     """
    420     Create a DAG node out of a parsed AST op node.
    421 
   (...)
    428         QiskitError: if encountering a non-basis opaque gate
    429     """
--> 430     op = self._create_op(name, params)
    431     op.condition = self.condition
    432     self.dag.apply_operation_back(op, qargs, [])

File ~/Desktop/college/quantum/Unitary Fund/Unitary-Hack/qbraid-env/lib/python3.10/site-packages/qiskit/converters/ast_to_dag.py:441, in AstInterpreter._create_op(self, name, params)
    438     op = Gate(name=name, num_qubits=self.gates[name]["n_bits"], params=params)
    439     if not self.gates[name]["opaque"]:
    440         # call a custom gate (otherwise, opaque)
--> 441         op.definition = self._gate_rules_to_qiskit_circuit(self.gates[name], params=params)
    442 else:
    443     raise QiskitError("unknown operation for ast node name %s" % name)

File ~/Desktop/college/quantum/Unitary Fund/Unitary-Hack/qbraid-env/lib/python3.10/site-packages/qiskit/converters/ast_to_dag.py:411, in AstInterpreter._gate_rules_to_qiskit_circuit(self, node, params)
    409             for param in param_list.children:
    410                 eparams.append(param.sym(nested_scope=[exp_args]))
--> 411     op = self._create_op(child_op.name, params=eparams)
    412     rules.append((op, qparams, []))
    413 circ = QuantumCircuit(qreg)

AttributeError: 'UniversalUnitary' object has no attribute 'name'

How can we reproduce the issue?

The following code snippet would reproduce the issue :


from qiskit.circuit import QuantumCircuit

qasm_str = """
         OPENQASM 2.0;
         include "qelib1.inc";
         qreg q[1];
         u0(0.5) q[0];
        """

qc = QuantumCircuit.from_qasm_str(qasm_str)

What should happen?

The qasm2 string should parse correctly into a QuantumCircuit object.

Any suggestions?

I think u0 case should be handled correctly in the file ast_to_dag.py while calling the AstInterpreter._process_node(self, node) function

jakelishman commented 1 year ago

The new OQ2 parser (qiskit.qasm2.load{,s}) released in 0.24 should have fixed this problem, and QuantumCircuit.from_qasm_str will switch to that implementation from Terra 0.25 in July/August.

Fwiw, u0(0.5) is logically invalid and the new parser will complain about it - u0 is a very old and now non-existent gate on IQX that represented an integer number of single-qubit-gate delay cycles, so its parameter could only be an integer.

TheGupta2012 commented 1 year ago

Alright, thanks a lot for the clarification!