Skip to content

EvolvedOperatorAnsatz with an empty list of operators #11680

@sesajad

Description

@sesajad

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions