Skip to content

Commit

Permalink
Fix evolved operator ansatz (#11682)
Browse files Browse the repository at this point in the history
* Fix handling of empty operators list in EvolvedOperatorAnsatz

* Add test for empty operator list in EvolvedOperatorAnsatz

* add release note for fixing evolved operator ansatz

* Update releasenotes/notes/fix-evolved-operator-ansatz-empty-ops-bf8ecfae8f1e1001.yaml

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* add a note to fix-evolved-operator-ansatz-empty-ops-bf8ecfae8f1e1001.yaml

* Update fix-evolved-operator-ansatz-empty-ops-bf8ecfae8f1e1001.yaml

* merge two conditions in evolved_operator_ansatz

Co-authored-by: Julien Gacon <gaconju@gmail.com>

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
  • Loading branch information
sesajad and Cryoris committed Mar 7, 2024
1 parent 67e0243 commit 8b6e4fe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions qiskit/circuit/library/n_local/evolved_operator_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def num_qubits(self) -> int:
if self.operators is None:
return 0

if isinstance(self.operators, list) and len(self.operators) > 0:
if isinstance(self.operators, list):
if len(self.operators) == 0:
return 0
return self.operators[0].num_qubits

return self.operators.num_qubits
Expand Down Expand Up @@ -152,7 +154,10 @@ def operators(self, operators=None) -> None:
operators = _validate_operators(operators)
self._invalidate()
self._operators = operators
self.qregs = [QuantumRegister(self.num_qubits, name="q")]
if self.num_qubits == 0:
self.qregs = []
else:
self.qregs = [QuantumRegister(self.num_qubits, name="q")]

# TODO: the `preferred_init_points`-implementation can (and should!) be improved!
@property
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
fixes:
- |
The :class:`.EvolvedOperatorAnsatz` now correctly handles the case where the
`operators` argument is an empty list. Previously, this would result in an
error.
- |
From now on, :class:`.EvolvedOperatorAnsatz` will not have any `qregs` when
thera are zero qubits, instead of having a :class:`.QuantumRegister` instance
with zero qubits. This behavior aligns more consistently with its superclass
:class:`.QuantumCircuit`.
5 changes: 5 additions & 0 deletions test/python/circuit/library/test_evolved_op_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ def test_empty_build_fails(self):
with self.assertRaises(ValueError):
_ = evo.draw()

def test_empty_operator_list(self):
"""Test setting an empty list of operators to be equal to an empty circuit."""
evo = EvolvedOperatorAnsatz([])
self.assertEqual(evo, QuantumCircuit())

def test_matrix_operator(self):
"""Test passing a quantum_info.Operator uses the HamiltonianGate."""
unitary = Operator([[0, 1], [1, 0]])
Expand Down

0 comments on commit 8b6e4fe

Please sign in to comment.