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

Gate instances (without a definition) are not qasm2-exported as opaque #5036

Closed
special-kay opened this issue Sep 7, 2020 · 6 comments · Fixed by #9953
Closed

Gate instances (without a definition) are not qasm2-exported as opaque #5036

special-kay opened this issue Sep 7, 2020 · 6 comments · Fixed by #9953
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export

Comments

@special-kay
Copy link

Information

  • Qiskit Terra version: 0.15.1
  • Python version: 3.8.5
  • Operating system: Mac OS Darwin

What is the current behavior?

Parsing a qasm string including a custom gate definiton - as given by Gate.qasm() - using QuantumCircuit.from_qasm_str() returns QasmError: "Cannot find gate definition for 'test', line 6 file ".

This issue was reported in #3066, which was closed, but the issue persists.

Steps to reproduce the problem

from qiskit.circuit import Gate
from qiskit.circuit import QuantumCircuit

test_gate = Gate('test',num_qubits=1,params=[])
test_qasm = 'OPENQASM 2.0;\ninclude "qelib1.inc";\n\nqreg q[2];\ncreg cr[2];\ntest q[0];\n'

test_circ = QuantumCircuit.from_qasm_str(test_qasm)

What is the expected behavior?

Generate quantum circuit with custom gate without throwing error.

Suggested solutions

@special-kay special-kay added the bug Something isn't working label Sep 7, 2020
@1ucian0
Copy link
Member

1ucian0 commented Sep 7, 2020

Custom gates should be opaque.

@special-kay
Copy link
Author

So does the functionality to create a circuit with custom gates from a qasm string exist?

@kdk
Copy link
Member

kdk commented Sep 9, 2020

In QASM, you can declare a custom opaque gate using the opaque statement, like

>>> qasm = '''
... OPENQASM 2.0;
... include "qelib1.inc";
... opaque test q0;
... qreg q[1];
... test q[0];
... '''
>>> qk.QuantumCircuit.from_qasm_str(qasm).draw()
     ┌──────┐
q_0: ┤ test ├
     └──────┘

or a non-opaque gate using the gate statement like

>>> qasm = '''
OPENQASM 2.0;
include "qelib1.inc";
gate circuit8 q0,q1 {cx q0,q1; cx q1,q0; }
qreg q[2];
h q[0];
h q[1];
circuit8 q[0],q[1];
'''
>>> qc.draw()
     ┌───┐┌───────────┐
q_0: ┤ H ├┤0          ├
     ├───┤│  circuit8 │
q_1: ┤ H ├┤1          ├
     └───┘└───────────┘

In your example, you are defining a python Gate object in qiskit, but the definition for that gate is not included the qasm string passed to QuantumCircuit.from_qasm_str, leading to the error.

I did find a related bug in that user-defined Gate objects are not properly being exported to qasm as opaque gates. Is this the issue you're seeing?

>>> from qiskit.circuit import Gate
>>> g = Gate('foo', 1, [])
>>> qc.append(g, [0])
>>> print(qc.qasm())
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
foo q[0];
>>> qk.QuantumCircuit.from_qasm_str((qc.qasm())
... )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 1730, in from_qasm_str
    return _circuit_from_qasm(qasm)
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 2293, in _circuit_from_qasm
    ast = qasm.parse()
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/qasm/qasm.py", line 54, in parse
    return qasm_p.parse(self._data)
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/qasm/qasmparser.py", line 1070, in parse
    self.parser.parse(data, lexer=self.lexer, debug=self.parse_deb)
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/lib/python3.5/site-packages/ply/yacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/lib/python3.5/site-packages/ply/yacc.py", line 1120, in parseopt_notrack
    p.callable(pslice)
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/qasm/qasmparser.py", line 638, in p_unitary_op_2
    self.verify_as_gate(program[1], program[2])
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/qasm/qasmparser.py", line 130, in verify_as_gate
    + "', line", str(obj.line), 'file', obj.file)
qiskit.qasm.exceptions.QasmError: "Cannot find gate definition for 'foo', line 8 file "

@special-kay
Copy link
Author

Yes that's the issue I'm having Kevin - thanks.

@1ucian0 1ucian0 added the mod: qasm2 Relating to OpenQASM 2 import or export label Oct 1, 2020
@jwoehr
Copy link
Contributor

jwoehr commented Dec 14, 2021

@kdk 's example:

>>> from qiskit import QuantumCircuit
>>> from qiskit.circuit import Gate
>>> qc = QuantumCircuit(1)
>>> g = Gate('foo', 1, [])
>>> qc.append(g, [0])
>>> print(qc.qasm())

does not work because QuantumCircuit._add_sub_instruction_to_existing_composite_circuits() raises on the empty instruction list.

@1ucian0 1ucian0 changed the title Qasm parsing error for custom gates. Gate instances (without a definition) are not qasm2-dumped as opaque Dec 14, 2021
@1ucian0
Copy link
Member

1ucian0 commented Dec 14, 2021

Indeed. Renaming the issue.

@kdk says:

the bug is that the qasm that’s generated is invalid (and thus fails during parsing). It’s a QASM2 exporter bug, not a from_qasm_str bug.

@1ucian0 1ucian0 changed the title Gate instances (without a definition) are not qasm2-dumped as opaque Gate instances (without a definition) are not qasm2-exported as opaque Dec 14, 2021
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

Successfully merging a pull request may close this issue.

4 participants