Skip to content
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
5 changes: 4 additions & 1 deletion qiskit/algorithms/minimum_eigen_solvers/vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,10 @@ def get_optimal_vector(self) -> Union[List[float], Dict[str, int]]:
qc.barrier(q)
qc.measure(q, c)
ret = self._quantum_instance.execute(qc)
self._ret['min_vector'] = ret.get_counts(qc)
counts = ret.get_counts(qc)
# normalize, just as done in CircuitSampler.sample_circuits
shots = self._quantum_instance._run_config.shots
self._ret['min_vector'] = {b: (v / shots) ** 0.5 for (b, v) in counts.items()}
return self._ret['min_vector']

@property
Expand Down
16 changes: 16 additions & 0 deletions test/python/algorithms/test_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ def test_basic_aer_qasm(self):
result = vqe.run(self.qasm_simulator)
self.assertAlmostEqual(result.eigenvalue.real, -1.86823, places=2)

def test_qasm_aux_operators_normalized(self):
"""Test VQE with qasm_simulator returns normalized aux_operator eigenvalues."""
wavefunction = self.ry_wavefunction
vqe = VQE(self.h2_op, wavefunction, quantum_instance=self.qasm_simulator)

opt_params = [3.50437328, 3.87415376, 0.93684363, 5.92219622, -1.53527887, 1.87941418,
-4.5708326, 0.70187027]

vqe._ret = {}
vqe._ret['opt_params'] = opt_params
vqe._ret['opt_params_dict'] = \
dict(zip(sorted(wavefunction.parameters, key=lambda p: p.name), opt_params))

optimal_vector = vqe.get_optimal_vector()
self.assertAlmostEqual(sum([v ** 2 for v in optimal_vector.values()]), 1.0, places=4)

def test_with_aer_statevector(self):
"""Test VQE with Aer's statevector_simulator."""
try:
Expand Down