Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs #6

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ def initialize_mcvqe(self) -> None:
num_spin_orbitals=num_spin_orbitals,
state_representation='dense')[:self.k]

self.initial_states = [QuantumCircuit(self.ansatz.num_qubits) for n in range(self.k)]
if self.initial_states is None:
self.initial_states = [QuantumCircuit(self.ansatz.num_qubits) for n in range(self.k)]

for n in range(self.k):
self.initial_states[n].initialize(initial_states[n], self.initial_states[n].qubits)

Expand Down Expand Up @@ -313,8 +315,8 @@ def initialize_mcvqe(self) -> None:

def compute_eigenvalues(
self,
operator: BaseOperator | PauliSumOp,
aux_operators: ListOrDict[BaseOperator | PauliSumOp] | None = None,
operator: BaseOperator,
aux_operators: BaseOperator | None = None,
) -> EigensolverResult:

ansatz = self._check_operator_ansatz(operator)
Expand Down Expand Up @@ -416,7 +418,7 @@ def _build_mcvqe_result(
optimizer_result: OptimizerResult,
aux_operators_evaluated: ListOrDict[tuple[complex, tuple[complex, int]]],
optimizer_time: float,
operator: BaseOperator | PauliSumOp,
operator: BaseOperator,
initialized_ansatz_list: list[QuantumCircuit],
) -> MCVQEResult:
result = MCVQEResult()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Callable, Optional, Union, Dict, List
from collections.abc import Callable, Sequence
from qiskit_algorithms.exceptions import AlgorithmError
from qiskit.primitives import BaseEstimator
from qiskit_algorithms.eigensolvers import Eigensolver, EigensolverResult
from qiskit_nature.second_q.mappers import QubitMapper
Expand Down Expand Up @@ -89,9 +91,22 @@ def __init__(self,
self.weight_vector = excited_states_solver.weight_vector
else:
self.weight_vector = [self.num_states - n for n in range(self.num_states)]

self.weight_vector = self._check_weight_vector(self.weight_vector)
self._energy_convergence_list = []
self._pauli_ops_expectation_values_dict_list = None

def _check_weight_vector(self, weight_vector: Sequence[float] = None) -> Sequence[float]:
"""Check that the number of weights matches the number of states."""
if weight_vector is None:
weight_vector = [self.num_states - n for n in range(self.num_states)]
elif len(weight_vector) != self.num_states:
raise AlgorithmError(
"The number of weights provided does not match the number of states."
)

return weight_vector

@property
def energy_convergence_list(self) -> List[float]:
"""Returns the list of outerloop iteration energy values."""
Expand Down Expand Up @@ -194,7 +209,7 @@ def compute_energies(self) -> EigensolverResult:
break

string_op_tuple_list = [(key, self._pauli_op_dict[key]) for key in self._pauli_op_dict]

results = [[] for n in range(self.num_states)]
for n in range(self.num_states):
ops_counter = 1
Expand Down
3 changes: 2 additions & 1 deletion tests/test_mcvqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# that they have been altered from the originals.

""" Test SSVQE """

import sys
sys.path.append('..')
import unittest
from tests.algorithms_test_case import QiskitAlgorithmsTestCase

Expand Down
3 changes: 2 additions & 1 deletion tests/test_optorbmcvqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def setUp(self):
num_particles=num_particles,
reps=2)

self.h2_energies = [-1.85539192, -1.46847994]
self.h2_energies = [-1.85703467, -1.46615986]
self.estimator = Estimator(approximation=True)

self.k = 2
Expand Down Expand Up @@ -126,6 +126,7 @@ def test_ground_state_es_problem(self, problem):
spin_conserving=True)

result = optorbvqe_instance.compute_energies()
print(result)

with self.subTest(msg="test eigenvalue, provided ElectronicStructureProblem"):
np.testing.assert_array_almost_equal(
Expand Down
Loading