From 7c392cc6c1ee67c05ba5fb42fdcc10cf9cc4c17d Mon Sep 17 00:00:00 2001 From: ElePT Date: Fri, 30 Sep 2022 10:20:30 +0200 Subject: [PATCH 1/9] Add ansatz to result --- qiskit/algorithms/eigensolvers/eigensolver.py | 11 +++++++++++ qiskit/algorithms/eigensolvers/vqd.py | 1 + .../minimum_eigensolvers/minimum_eigensolver.py | 11 +++++++++++ qiskit/algorithms/minimum_eigensolvers/vqe.py | 6 +++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/qiskit/algorithms/eigensolvers/eigensolver.py b/qiskit/algorithms/eigensolvers/eigensolver.py index 0cafc9d64dcf..8974a8eedb88 100644 --- a/qiskit/algorithms/eigensolvers/eigensolver.py +++ b/qiskit/algorithms/eigensolvers/eigensolver.py @@ -18,6 +18,7 @@ from typing import Any import numpy as np +from qiskit.circuit import QuantumCircuit from qiskit.opflow import PauliSumOp from qiskit.quantum_info.operators.base_operator import BaseOperator @@ -73,9 +74,19 @@ class EigensolverResult(AlgorithmResult): def __init__(self) -> None: super().__init__() + self._ansatz = None self._eigenvalues = None self._aux_operators_evaluated = None + @property + def ansatz(self) -> QuantumCircuit: + """The ansatz to retrieve the minimum eigenstate.""" + return self._ansatz + + @ansatz.setter + def ansatz(self, ansatz: QuantumCircuit) -> None: + self._ansatz = ansatz + @property def eigenvalues(self) -> np.ndarray | None: """Return the eigenvalues.""" diff --git a/qiskit/algorithms/eigensolvers/vqd.py b/qiskit/algorithms/eigensolvers/vqd.py index 26b215aa760a..037d08e1eca3 100644 --- a/qiskit/algorithms/eigensolvers/vqd.py +++ b/qiskit/algorithms/eigensolvers/vqd.py @@ -294,6 +294,7 @@ def compute_eigenvalues( result.cost_function_evals = np.array(result.cost_function_evals) result.optimizer_times = np.array(result.optimizer_times) + result.ansatz = self.ansatz if aux_operators is not None: result.aux_operators_evaluated = aux_values diff --git a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py index ec3e280db0d3..afd8ae29b8c7 100644 --- a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py +++ b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py @@ -17,6 +17,7 @@ from abc import ABC, abstractmethod from typing import Any +from qiskit.circuit import QuantumCircuit from qiskit.opflow import PauliSumOp from qiskit.quantum_info.operators.base_operator import BaseOperator @@ -72,9 +73,19 @@ class MinimumEigensolverResult(AlgorithmResult): def __init__(self) -> None: super().__init__() + self._ansatz = None self._eigenvalue = None self._aux_operators_evaluated = None + @property + def ansatz(self) -> QuantumCircuit: + """The ansatz to retrieve the minimum eigenstate.""" + return self._ansatz + + @ansatz.setter + def ansatz(self, ansatz: QuantumCircuit) -> None: + self._ansatz = ansatz + @property def eigenvalue(self) -> complex | None: """The computed minimum eigenvalue.""" diff --git a/qiskit/algorithms/minimum_eigensolvers/vqe.py b/qiskit/algorithms/minimum_eigensolvers/vqe.py index ca8a260622d6..ac4d26f8f548 100644 --- a/qiskit/algorithms/minimum_eigensolvers/vqe.py +++ b/qiskit/algorithms/minimum_eigensolvers/vqe.py @@ -200,7 +200,9 @@ def compute_minimum_eigenvalue( else: aux_operators_evaluated = None - return self._build_vqe_result(optimizer_result, aux_operators_evaluated, optimizer_time) + return self._build_vqe_result( + self.ansatz, optimizer_result, aux_operators_evaluated, optimizer_time + ) @classmethod def supports_aux_operators(cls) -> bool: @@ -308,11 +310,13 @@ def _check_operator_ansatz(self, operator: BaseOperator | PauliSumOp): def _build_vqe_result( self, + ansatz: QuantumCircuit, optimizer_result: OptimizerResult, aux_operators_evaluated: ListOrDict[tuple[complex, tuple[complex, int]]], optimizer_time: float, ) -> VQEResult: result = VQEResult() + result.ansatz = ansatz result.eigenvalue = optimizer_result.fun result.cost_function_evals = optimizer_result.nfev result.optimal_point = optimizer_result.x From dfe354fe8dd6bc413fa13c0eb4b9db524db78c2b Mon Sep 17 00:00:00 2001 From: ElePT Date: Fri, 30 Sep 2022 10:44:02 +0200 Subject: [PATCH 2/9] Add ansatz to eigen. results --- test/python/algorithms/eigensolvers/test_vqd.py | 7 +++++++ test/python/algorithms/minimum_eigensolvers/test_vqe.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index e977461468d0..97fa8606bbd9 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -100,6 +100,13 @@ def test_basic_operator(self, op): with self.subTest(msg="assert optimizer_times is set"): self.assertIsNotNone(result.optimizer_times) + with self.subTest(msg="assert eigenstates can be retrieved"): + sampler = Sampler() + ansatz = result.ansatz + ansatz.measure_all() + eigenstate_job = sampler.run(ansatz, result.optimal_points[-1]) + self.assertIsNotNone(eigenstate_job.result().quasi_dists) + @data(H2_PAULI, H2_OP) def test_mismatching_num_qubits(self, op): """Ensuring circuit and operator mismatch is caught""" diff --git a/test/python/algorithms/minimum_eigensolvers/test_vqe.py b/test/python/algorithms/minimum_eigensolvers/test_vqe.py index 27f58f85ccfe..9e145b4840a9 100644 --- a/test/python/algorithms/minimum_eigensolvers/test_vqe.py +++ b/test/python/algorithms/minimum_eigensolvers/test_vqe.py @@ -107,6 +107,13 @@ def test_basic_aer_statevector(self, estimator): with self.subTest(msg="assert optimizer_result."): self.assertAlmostEqual(result.optimizer_result.fun, self.h2_energy, places=5) + with self.subTest(msg="assert eigenstate can be retrieved"): + sampler = Sampler() + ansatz = result.ansatz + ansatz.measure_all() + eigenstate_job = sampler.run(ansatz, result.optimal_point) + self.assertIsNotNone(eigenstate_job.result().quasi_dists) + def test_invalid_initial_point(self): """Test the proper error is raised when the initial point has the wrong size.""" ansatz = self.ryrz_wavefunction From e8eb32074ef73482cef891f4004ca619edaf94cb Mon Sep 17 00:00:00 2001 From: ElePT <57907331+ElePT@users.noreply.github.com> Date: Fri, 30 Sep 2022 11:44:02 +0200 Subject: [PATCH 3/9] Update qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py Co-authored-by: Julien Gacon --- .../algorithms/minimum_eigensolvers/minimum_eigensolver.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py index afd8ae29b8c7..0cb9fa15524a 100644 --- a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py +++ b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py @@ -79,7 +79,10 @@ def __init__(self) -> None: @property def ansatz(self) -> QuantumCircuit: - """The ansatz to retrieve the minimum eigenstate.""" + """The circuit preparing the parameterized ansatz state. + + Along with the optimal parameters, this can be used to retrieve the minimum eigenstate. + """ return self._ansatz @ansatz.setter From 484520fe42dd07c162046b043f4c4037202290cc Mon Sep 17 00:00:00 2001 From: ElePT Date: Fri, 30 Sep 2022 11:45:41 +0200 Subject: [PATCH 4/9] Copy ansatz --- qiskit/algorithms/eigensolvers/vqd.py | 2 +- qiskit/algorithms/minimum_eigensolvers/vqe.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit/algorithms/eigensolvers/vqd.py b/qiskit/algorithms/eigensolvers/vqd.py index 037d08e1eca3..2985a233bb3c 100644 --- a/qiskit/algorithms/eigensolvers/vqd.py +++ b/qiskit/algorithms/eigensolvers/vqd.py @@ -294,7 +294,7 @@ def compute_eigenvalues( result.cost_function_evals = np.array(result.cost_function_evals) result.optimizer_times = np.array(result.optimizer_times) - result.ansatz = self.ansatz + result.ansatz = self.ansatz.copy() if aux_operators is not None: result.aux_operators_evaluated = aux_values diff --git a/qiskit/algorithms/minimum_eigensolvers/vqe.py b/qiskit/algorithms/minimum_eigensolvers/vqe.py index ac4d26f8f548..1add4085a94f 100644 --- a/qiskit/algorithms/minimum_eigensolvers/vqe.py +++ b/qiskit/algorithms/minimum_eigensolvers/vqe.py @@ -316,7 +316,7 @@ def _build_vqe_result( optimizer_time: float, ) -> VQEResult: result = VQEResult() - result.ansatz = ansatz + result.ansatz = ansatz.copy() result.eigenvalue = optimizer_result.fun result.cost_function_evals = optimizer_result.nfev result.optimal_point = optimizer_result.x From ccbd0dbabf3cb294d8ad1d1755484ac29467876b Mon Sep 17 00:00:00 2001 From: ElePT Date: Fri, 30 Sep 2022 11:56:28 +0200 Subject: [PATCH 5/9] Apply comments --- .../minimum_eigensolvers/minimum_eigensolver.py | 6 +++--- test/python/algorithms/eigensolvers/test_vqd.py | 13 +++++++------ .../algorithms/minimum_eigensolvers/test_vqe.py | 10 ++++------ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py index 0cb9fa15524a..5a0b4f69cfbc 100644 --- a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py +++ b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py @@ -80,9 +80,9 @@ def __init__(self) -> None: @property def ansatz(self) -> QuantumCircuit: """The circuit preparing the parameterized ansatz state. - - Along with the optimal parameters, this can be used to retrieve the minimum eigenstate. - """ + + Along with the optimal parameters, this can be used to retrieve the minimum eigenstate. + """ return self._ansatz @ansatz.setter diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index 97fa8606bbd9..9f4c1c89283e 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -100,12 +100,13 @@ def test_basic_operator(self, op): with self.subTest(msg="assert optimizer_times is set"): self.assertIsNotNone(result.optimizer_times) - with self.subTest(msg="assert eigenstates can be retrieved"): - sampler = Sampler() - ansatz = result.ansatz - ansatz.measure_all() - eigenstate_job = sampler.run(ansatz, result.optimal_points[-1]) - self.assertIsNotNone(eigenstate_job.result().quasi_dists) + with self.subTest(msg="assert return ansatz is set"): + job = self.estimator.run( + [result.ansatz] * len(result.optimal_points), + [op] * len(result.optimal_points), + result.optimal_points, + ) + np.testing.assert_array_almost_equal(job.result().values, result.eigenvalues, 6) @data(H2_PAULI, H2_OP) def test_mismatching_num_qubits(self, op): diff --git a/test/python/algorithms/minimum_eigensolvers/test_vqe.py b/test/python/algorithms/minimum_eigensolvers/test_vqe.py index 9e145b4840a9..6cc2e865608e 100644 --- a/test/python/algorithms/minimum_eigensolvers/test_vqe.py +++ b/test/python/algorithms/minimum_eigensolvers/test_vqe.py @@ -107,12 +107,10 @@ def test_basic_aer_statevector(self, estimator): with self.subTest(msg="assert optimizer_result."): self.assertAlmostEqual(result.optimizer_result.fun, self.h2_energy, places=5) - with self.subTest(msg="assert eigenstate can be retrieved"): - sampler = Sampler() - ansatz = result.ansatz - ansatz.measure_all() - eigenstate_job = sampler.run(ansatz, result.optimal_point) - self.assertIsNotNone(eigenstate_job.result().quasi_dists) + with self.subTest(msg="assert return ansatz is set"): + estimator = Estimator() + job = estimator.run(result.ansatz, self.h2_op, result.optimal_point) + np.testing.assert_array_almost_equal(job.result().values, result.eigenvalue, 6) def test_invalid_initial_point(self): """Test the proper error is raised when the initial point has the wrong size.""" From 145ef85c60ffbc586d32218aba612bb7a5e3e8c4 Mon Sep 17 00:00:00 2001 From: ElePT Date: Fri, 30 Sep 2022 15:09:15 +0200 Subject: [PATCH 6/9] Change name to optimal_circuit(s) --- qiskit/algorithms/eigensolvers/eigensolver.py | 17 +++++++++-------- qiskit/algorithms/eigensolvers/vqd.py | 5 +++-- .../minimum_eigensolvers/adapt_vqe.py | 1 - .../minimum_eigensolvers/minimum_eigensolver.py | 17 ++++++++--------- .../minimum_eigensolvers/sampling_vqe.py | 9 ++++++++- qiskit/algorithms/minimum_eigensolvers/vqe.py | 2 +- test/python/algorithms/eigensolvers/test_vqd.py | 2 +- .../algorithms/minimum_eigensolvers/test_vqe.py | 2 +- 8 files changed, 31 insertions(+), 24 deletions(-) diff --git a/qiskit/algorithms/eigensolvers/eigensolver.py b/qiskit/algorithms/eigensolvers/eigensolver.py index 8974a8eedb88..5d1fa7dbb0e3 100644 --- a/qiskit/algorithms/eigensolvers/eigensolver.py +++ b/qiskit/algorithms/eigensolvers/eigensolver.py @@ -74,18 +74,19 @@ class EigensolverResult(AlgorithmResult): def __init__(self) -> None: super().__init__() - self._ansatz = None + self._optimal_circuits = None self._eigenvalues = None self._aux_operators_evaluated = None @property - def ansatz(self) -> QuantumCircuit: - """The ansatz to retrieve the minimum eigenstate.""" - return self._ansatz - - @ansatz.setter - def ansatz(self, ansatz: QuantumCircuit) -> None: - self._ansatz = ansatz + def optimal_circuits(self) -> list[QuantumCircuit]: + """The optimal circuits. Along with the optimal parameters, + these can be used to retrieve the different eigenstates.""" + return self._optimal_circuits + + @optimal_circuits.setter + def optimal_circuits(self, optimal_circuits: list[QuantumCircuit]) -> None: + self._optimal_circuits = optimal_circuits @property def eigenvalues(self) -> np.ndarray | None: diff --git a/qiskit/algorithms/eigensolvers/vqd.py b/qiskit/algorithms/eigensolvers/vqd.py index 2985a233bb3c..985f5bca552a 100644 --- a/qiskit/algorithms/eigensolvers/vqd.py +++ b/qiskit/algorithms/eigensolvers/vqd.py @@ -259,7 +259,7 @@ def compute_eigenvalues( eval_time = time() - start_time - self._update_vqd_result(result, opt_result, eval_time, self.ansatz) + self._update_vqd_result(result, opt_result, eval_time, self.ansatz.copy()) if aux_operators is not None: aux_value = estimate_observables( @@ -294,7 +294,6 @@ def compute_eigenvalues( result.cost_function_evals = np.array(result.cost_function_evals) result.optimizer_times = np.array(result.optimizer_times) - result.ansatz = self.ansatz.copy() if aux_operators is not None: result.aux_operators_evaluated = aux_values @@ -389,6 +388,7 @@ def _build_vqd_result() -> VQDResult: result.optimizer_times = [] result.eigenvalues = [] result.optimizer_results = [] + result.optimal_circuits = [] return result @staticmethod @@ -401,6 +401,7 @@ def _update_vqd_result(result, opt_result, eval_time, ansatz) -> VQDResult: result.optimizer_times.append(eval_time) result.eigenvalues.append(opt_result.fun + 0j) result.optimizer_results.append(opt_result) + result.optimal_circuits.append(ansatz) return result diff --git a/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py b/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py index 09abe09dfa31..0d4ee6afb4ce 100644 --- a/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py +++ b/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py @@ -258,7 +258,6 @@ def compute_minimum_eigenvalue( result.num_iterations = iteration result.final_max_gradient = max_grad[0] result.termination_criterion = termination_criterion - # once finished evaluate auxiliary operators if any if aux_operators is not None: aux_values = estimate_observables( diff --git a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py index 5a0b4f69cfbc..d29861f5372e 100644 --- a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py +++ b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py @@ -73,21 +73,20 @@ class MinimumEigensolverResult(AlgorithmResult): def __init__(self) -> None: super().__init__() - self._ansatz = None + self._optimal_circuit = None self._eigenvalue = None self._aux_operators_evaluated = None @property - def ansatz(self) -> QuantumCircuit: - """The circuit preparing the parameterized ansatz state. - - Along with the optimal parameters, this can be used to retrieve the minimum eigenstate. + def optimal_circuit(self) -> QuantumCircuit: + """The optimal circuits. Along with the optimal parameters, + these can be used to retrieve the minimum eigenstate. """ - return self._ansatz + return self._optimal_circuit - @ansatz.setter - def ansatz(self, ansatz: QuantumCircuit) -> None: - self._ansatz = ansatz + @optimal_circuit.setter + def optimal_circuit(self, optimal_circuit: QuantumCircuit) -> None: + self._optimal_circuit = optimal_circuit @property def eigenvalue(self) -> complex | None: diff --git a/qiskit/algorithms/minimum_eigensolvers/sampling_vqe.py b/qiskit/algorithms/minimum_eigensolvers/sampling_vqe.py index 36d691753f2d..7e84ec35e299 100755 --- a/qiskit/algorithms/minimum_eigensolvers/sampling_vqe.py +++ b/qiskit/algorithms/minimum_eigensolvers/sampling_vqe.py @@ -234,7 +234,12 @@ def compute_minimum_eigenvalue( aux_operators_evaluated = None return self._build_sampling_vqe_result( - optimizer_result, aux_operators_evaluated, best_measurement, final_state, optimizer_time + self.ansatz.copy(), + optimizer_result, + aux_operators_evaluated, + best_measurement, + final_state, + optimizer_time, ) def _get_evaluate_energy( @@ -306,6 +311,7 @@ def evaluate_energy(parameters): def _build_sampling_vqe_result( self, + ansatz: QuantumCircuit, optimizer_result: OptimizerResult, aux_operators_evaluated: ListOrDict[tuple[complex, tuple[complex, int]]], best_measurement: dict[str, Any], @@ -323,6 +329,7 @@ def _build_sampling_vqe_result( result.optimizer_result = optimizer_result result.best_measurement = best_measurement["best"] result.eigenstate = final_state + result.optimal_circuit = ansatz return result diff --git a/qiskit/algorithms/minimum_eigensolvers/vqe.py b/qiskit/algorithms/minimum_eigensolvers/vqe.py index 1add4085a94f..4c01ddc26191 100644 --- a/qiskit/algorithms/minimum_eigensolvers/vqe.py +++ b/qiskit/algorithms/minimum_eigensolvers/vqe.py @@ -316,7 +316,7 @@ def _build_vqe_result( optimizer_time: float, ) -> VQEResult: result = VQEResult() - result.ansatz = ansatz.copy() + result.optimal_circuit = ansatz.copy() result.eigenvalue = optimizer_result.fun result.cost_function_evals = optimizer_result.nfev result.optimal_point = optimizer_result.x diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index 9f4c1c89283e..f51ecb92147a 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -102,7 +102,7 @@ def test_basic_operator(self, op): with self.subTest(msg="assert return ansatz is set"): job = self.estimator.run( - [result.ansatz] * len(result.optimal_points), + result.optimal_circuits, [op] * len(result.optimal_points), result.optimal_points, ) diff --git a/test/python/algorithms/minimum_eigensolvers/test_vqe.py b/test/python/algorithms/minimum_eigensolvers/test_vqe.py index 6cc2e865608e..c3c0b89d9cce 100644 --- a/test/python/algorithms/minimum_eigensolvers/test_vqe.py +++ b/test/python/algorithms/minimum_eigensolvers/test_vqe.py @@ -109,7 +109,7 @@ def test_basic_aer_statevector(self, estimator): with self.subTest(msg="assert return ansatz is set"): estimator = Estimator() - job = estimator.run(result.ansatz, self.h2_op, result.optimal_point) + job = estimator.run(result.optimal_circuit, self.h2_op, result.optimal_point) np.testing.assert_array_almost_equal(job.result().values, result.eigenvalue, 6) def test_invalid_initial_point(self): From 92e49ac515257528417b0d283685f315e00cb263 Mon Sep 17 00:00:00 2001 From: ElePT <57907331+ElePT@users.noreply.github.com> Date: Fri, 30 Sep 2022 15:13:44 +0200 Subject: [PATCH 7/9] add blank line --- qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py b/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py index 0d4ee6afb4ce..09abe09dfa31 100644 --- a/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py +++ b/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py @@ -258,6 +258,7 @@ def compute_minimum_eigenvalue( result.num_iterations = iteration result.final_max_gradient = max_grad[0] result.termination_criterion = termination_criterion + # once finished evaluate auxiliary operators if any if aux_operators is not None: aux_values = estimate_observables( From 2ba613d48518739e485b8922115d42964741fba2 Mon Sep 17 00:00:00 2001 From: ElePT Date: Fri, 30 Sep 2022 16:29:19 +0200 Subject: [PATCH 8/9] move optimal circuit to variational result --- qiskit/algorithms/eigensolvers/eigensolver.py | 11 ----------- qiskit/algorithms/eigensolvers/vqd.py | 11 +++++++++++ .../minimum_eigensolvers/minimum_eigensolver.py | 13 ------------- qiskit/algorithms/variational_algorithm.py | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/qiskit/algorithms/eigensolvers/eigensolver.py b/qiskit/algorithms/eigensolvers/eigensolver.py index 5d1fa7dbb0e3..516c51e56b36 100644 --- a/qiskit/algorithms/eigensolvers/eigensolver.py +++ b/qiskit/algorithms/eigensolvers/eigensolver.py @@ -74,20 +74,9 @@ class EigensolverResult(AlgorithmResult): def __init__(self) -> None: super().__init__() - self._optimal_circuits = None self._eigenvalues = None self._aux_operators_evaluated = None - @property - def optimal_circuits(self) -> list[QuantumCircuit]: - """The optimal circuits. Along with the optimal parameters, - these can be used to retrieve the different eigenstates.""" - return self._optimal_circuits - - @optimal_circuits.setter - def optimal_circuits(self, optimal_circuits: list[QuantumCircuit]) -> None: - self._optimal_circuits = optimal_circuits - @property def eigenvalues(self) -> np.ndarray | None: """Return the eigenvalues.""" diff --git a/qiskit/algorithms/eigensolvers/vqd.py b/qiskit/algorithms/eigensolvers/vqd.py index 985f5bca552a..6b48a30090ed 100644 --- a/qiskit/algorithms/eigensolvers/vqd.py +++ b/qiskit/algorithms/eigensolvers/vqd.py @@ -416,6 +416,7 @@ def __init__(self) -> None: self._optimal_points = None self._optimal_parameters = None self._optimizer_results = None + self._optimal_circuits = None @property def cost_function_evals(self) -> Sequence[int] | None: @@ -476,3 +477,13 @@ def optimizer_results(self) -> Sequence[OptimizerResult] | None: def optimizer_results(self, value: Sequence[OptimizerResult]) -> None: """Sets optimizer results""" self._optimizer_results = value + + @property + def optimal_circuits(self) -> list[QuantumCircuit]: + """The optimal circuits. Along with the optimal parameters, + these can be used to retrieve the different eigenstates.""" + return self._optimal_circuits + + @optimal_circuits.setter + def optimal_circuits(self, optimal_circuits: list[QuantumCircuit]) -> None: + self._optimal_circuits = optimal_circuits diff --git a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py index d29861f5372e..ec3e280db0d3 100644 --- a/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py +++ b/qiskit/algorithms/minimum_eigensolvers/minimum_eigensolver.py @@ -17,7 +17,6 @@ from abc import ABC, abstractmethod from typing import Any -from qiskit.circuit import QuantumCircuit from qiskit.opflow import PauliSumOp from qiskit.quantum_info.operators.base_operator import BaseOperator @@ -73,21 +72,9 @@ class MinimumEigensolverResult(AlgorithmResult): def __init__(self) -> None: super().__init__() - self._optimal_circuit = None self._eigenvalue = None self._aux_operators_evaluated = None - @property - def optimal_circuit(self) -> QuantumCircuit: - """The optimal circuits. Along with the optimal parameters, - these can be used to retrieve the minimum eigenstate. - """ - return self._optimal_circuit - - @optimal_circuit.setter - def optimal_circuit(self, optimal_circuit: QuantumCircuit) -> None: - self._optimal_circuit = optimal_circuit - @property def eigenvalue(self) -> complex | None: """The computed minimum eigenvalue.""" diff --git a/qiskit/algorithms/variational_algorithm.py b/qiskit/algorithms/variational_algorithm.py index db28193d31f9..fc05d28af090 100644 --- a/qiskit/algorithms/variational_algorithm.py +++ b/qiskit/algorithms/variational_algorithm.py @@ -30,6 +30,8 @@ from abc import ABC, abstractmethod import numpy as np +from qiskit.circuit import QuantumCircuit + from .algorithm_result import AlgorithmResult from .optimizers import OptimizerResult @@ -61,6 +63,7 @@ def __init__(self) -> None: self._optimal_point = None self._optimal_parameters = None self._optimizer_result = None + self._optimal_circuit = None @property def optimizer_evals(self) -> Optional[int]: @@ -121,3 +124,14 @@ def optimizer_result(self) -> Optional[OptimizerResult]: def optimizer_result(self, value: OptimizerResult) -> None: """Sets optimizer result""" self._optimizer_result = value + + @property + def optimal_circuit(self) -> QuantumCircuit: + """The optimal circuits. Along with the optimal parameters, + these can be used to retrieve the minimum eigenstate. + """ + return self._optimal_circuit + + @optimal_circuit.setter + def optimal_circuit(self, optimal_circuit: QuantumCircuit) -> None: + self._optimal_circuit = optimal_circuit From 18f1b498d7bc0f49a204138a3331040bd1b8d3d1 Mon Sep 17 00:00:00 2001 From: ElePT <57907331+ElePT@users.noreply.github.com> Date: Fri, 30 Sep 2022 16:44:18 +0200 Subject: [PATCH 9/9] remove import --- qiskit/algorithms/eigensolvers/eigensolver.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qiskit/algorithms/eigensolvers/eigensolver.py b/qiskit/algorithms/eigensolvers/eigensolver.py index 516c51e56b36..0cafc9d64dcf 100644 --- a/qiskit/algorithms/eigensolvers/eigensolver.py +++ b/qiskit/algorithms/eigensolvers/eigensolver.py @@ -18,7 +18,6 @@ from typing import Any import numpy as np -from qiskit.circuit import QuantumCircuit from qiskit.opflow import PauliSumOp from qiskit.quantum_info.operators.base_operator import BaseOperator