Skip to content

Commit 810ea44

Browse files
Fix evolved operator ansatz (#11682) (#11965)
* 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> (cherry picked from commit 8b6e4fe) Co-authored-by: Seyed Sajad Kahani <sska1377@gmail.com>
1 parent 555899e commit 810ea44

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

qiskit/circuit/library/n_local/evolved_operator_ansatz.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def num_qubits(self) -> int:
106106
if self.operators is None:
107107
return 0
108108

109-
if isinstance(self.operators, list) and len(self.operators) > 0:
109+
if isinstance(self.operators, list):
110+
if len(self.operators) == 0:
111+
return 0
110112
return self.operators[0].num_qubits
111113

112114
return self.operators.num_qubits
@@ -152,7 +154,10 @@ def operators(self, operators=None) -> None:
152154
operators = _validate_operators(operators)
153155
self._invalidate()
154156
self._operators = operators
155-
self.qregs = [QuantumRegister(self.num_qubits, name="q")]
157+
if self.num_qubits == 0:
158+
self.qregs = []
159+
else:
160+
self.qregs = [QuantumRegister(self.num_qubits, name="q")]
156161

157162
# TODO: the `preferred_init_points`-implementation can (and should!) be improved!
158163
@property
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
fixes:
3+
- |
4+
The :class:`.EvolvedOperatorAnsatz` now correctly handles the case where the
5+
`operators` argument is an empty list. Previously, this would result in an
6+
error.
7+
- |
8+
From now on, :class:`.EvolvedOperatorAnsatz` will not have any `qregs` when
9+
thera are zero qubits, instead of having a :class:`.QuantumRegister` instance
10+
with zero qubits. This behavior aligns more consistently with its superclass
11+
:class:`.QuantumCircuit`.

test/python/circuit/library/test_evolved_op_ansatz.py

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def test_empty_build_fails(self):
8989
with self.assertRaises(ValueError):
9090
_ = evo.draw()
9191

92+
def test_empty_operator_list(self):
93+
"""Test setting an empty list of operators to be equal to an empty circuit."""
94+
evo = EvolvedOperatorAnsatz([])
95+
self.assertEqual(evo, QuantumCircuit())
96+
9297
def test_matrix_operator(self):
9398
"""Test passing a quantum_info.Operator uses the HamiltonianGate."""
9499
unitary = Operator([[0, 1], [1, 0]])

0 commit comments

Comments
 (0)