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

Running a custom gate with name collision with a default gate causes segmentation fault #1612

Closed
rht opened this issue Oct 4, 2022 · 1 comment · Fixed by #1834
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@rht
Copy link

rht commented Oct 4, 2022

Environment

  • Qiskit Terra version: 0.21.2
  • Python version: 3.10.6
  • Operating system: Linux

What is happening?

The following minimal code causes segfault

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.gate import Gate
from qiskit.circuit.library.standard_gates import HGate
from qiskit import transpile
from qiskit_aer import Aer


class Custom(Gate):
    def __init__(self, label=None):
        super().__init__("p", 1, [], label=label)

    def _define(self):
        q = QuantumRegister(1, "q")
        qc = QuantumCircuit(q, name=self.name)
        qc._append(HGate(), [q[0]], [])
        self.definition = qc


qc = QuantumCircuit(1)
qc.append(Custom(), [0])
qc.measure_all()

backend_sim = Aer.get_backend("qasm_simulator")
transpiled = transpile(qc, backend_sim)
result_sim = backend_sim.run(transpiled, shots=10).result()

How can we reproduce the issue?

See above.

What should happen?

There should be no segfault.

Any suggestions?

No response

@rht rht added the bug Something isn't working label Oct 4, 2022
@jakelishman
Copy link
Member

The segfault here is a missing safety check in Aer - it should be a Python-space error - but overall, this failing is expected (and required by our data model). Aer says that it knows about a p gate in its basis set, and Aer expects that to take one parameter. This gate says "I supply the p gate", but it doesn't have any parameters, so when Aer looks for one it errors. Any backend that supports a gate called p with a parameter should throw an error when given this circuit after transpilation.

The simulations probably work if there's no measurement or storage because Aer throws away instructions without simulating them if it detects that they have no observable effect, so it wouldn't ever try to access the missing parameter.

I'll transfer this to Aer because I think we can insert a sanity check on the parameters when we lower this to the C++ parts to ensure the error is in Python space not a segfault, but an error is the correct behaviour.

@jakelishman jakelishman transferred this issue from Qiskit/qiskit Oct 4, 2022
@hhorii hhorii self-assigned this Nov 1, 2022
@hhorii hhorii added this to the Aer 0.12.1 milestone May 16, 2023
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

Successfully merging a pull request may close this issue.

3 participants