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

EvolvedOperatorAnsatz with an empty list of operators #11680

Closed
sesajad opened this issue Jan 30, 2024 · 0 comments · Fixed by #11682
Closed

EvolvedOperatorAnsatz with an empty list of operators #11680

sesajad opened this issue Jan 30, 2024 · 0 comments · Fixed by #11682
Assignees
Labels
bug Something isn't working

Comments

@sesajad
Copy link
Contributor

sesajad commented Jan 30, 2024

Environment

  • Qiskit version: 0.45.2
  • Python version: 3.11.6
  • Operating system: GNU/Linux

What is happening?

If we set operators in EvolvedOperatorAnsatz to an empty list, it raises an unhandled exception and fails to create an object.

This case can happen when we use UCCSD ansatz from qiskit_nature with a molecule that has no electrons.

How can we reproduce the issue?

Running the following code

from qiskit.circuit.library import EvolvedOperatorAnsatz
from qiskit import QuantumCircuit

e = EvolvedOperatorAnsatz(operators=[])
e == QuantumCircuit()

raises the following error:

File "/..../site-packages/qiskit/circuit/library/evolved_operator_ansatz.py", line 109, in num_qubits
    return self.operators.num_qubits
           ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'num_qubits'

What should happen?

It was expected to return True as the case with no qubit is handled well in QuantumCircuit and its subclasses. It was partially handled in the EvolvedOperatorAnsatz too. For example

if isinstance(self.operators, list) and len(self.operators) > 0:

shows the previous intention to handle this case. But it is not completely done so it raises an unhandled exception.

Any suggestions?

I'll create a pull request to discuss the proposed solution.

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.

1 participant