From 96a9bcb5d92f810eaa98a309f20052b5c01856d7 Mon Sep 17 00:00:00 2001 From: Seyed Sajad Kahani Date: Thu, 7 Mar 2024 14:32:20 +0000 Subject: [PATCH] Fix evolved operator ansatz (#11682) * 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 * 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 --------- Co-authored-by: Julien Gacon (cherry picked from commit 8b6e4fe0fe55fd96e182e526860321db702a7a54) --- .../library/n_local/evolved_operator_ansatz.py | 9 +++++++-- ...ed-operator-ansatz-empty-ops-bf8ecfae8f1e1001.yaml | 11 +++++++++++ test/python/circuit/library/test_evolved_op_ansatz.py | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/fix-evolved-operator-ansatz-empty-ops-bf8ecfae8f1e1001.yaml diff --git a/qiskit/circuit/library/n_local/evolved_operator_ansatz.py b/qiskit/circuit/library/n_local/evolved_operator_ansatz.py index bb1eed3430d..a50b48ce488 100644 --- a/qiskit/circuit/library/n_local/evolved_operator_ansatz.py +++ b/qiskit/circuit/library/n_local/evolved_operator_ansatz.py @@ -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 @@ -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 diff --git a/releasenotes/notes/fix-evolved-operator-ansatz-empty-ops-bf8ecfae8f1e1001.yaml b/releasenotes/notes/fix-evolved-operator-ansatz-empty-ops-bf8ecfae8f1e1001.yaml new file mode 100644 index 00000000000..2f941d42a11 --- /dev/null +++ b/releasenotes/notes/fix-evolved-operator-ansatz-empty-ops-bf8ecfae8f1e1001.yaml @@ -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`. diff --git a/test/python/circuit/library/test_evolved_op_ansatz.py b/test/python/circuit/library/test_evolved_op_ansatz.py index a2b819886cd..8ea66b7012f 100644 --- a/test/python/circuit/library/test_evolved_op_ansatz.py +++ b/test/python/circuit/library/test_evolved_op_ansatz.py @@ -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]])