Skip to content

Commit b0b04c2

Browse files
authored
Chemistry auto Z2 symmetry reduction support (qiskit-community/qiskit-aqua#870)
* Automatic Z2Symmetry reduction support * Automatic Z2Symmetry reduction support
1 parent d319988 commit b0b04c2

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

qiskit/aqua/algorithms/eigen_solvers/numpy_eigen_solver.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def aux_operators(self, aux_operators: List[BaseOperator]) -> None:
103103
aux_operators = \
104104
[aux_operators] if not isinstance(aux_operators, list) else aux_operators
105105
self._aux_operators = \
106-
[op_converter.to_matrix_operator(aux_op) for aux_op in aux_operators]
106+
[op_converter.to_matrix_operator(aux_op) if aux_op is not None else None
107+
for aux_op in aux_operators]
107108

108109
@property
109110
def k(self) -> int:
@@ -164,14 +165,17 @@ def _get_energies(self):
164165
energies[i] = self._ret['eigvals'][i].real
165166
self._ret['energies'] = energies
166167
if self._aux_operators:
167-
aux_op_vals = np.empty([self._k, len(self._aux_operators), 2])
168+
aux_op_vals = []
168169
for i in range(self._k):
169-
aux_op_vals[i, :] = self._eval_aux_operators(self._ret['eigvecs'][i])
170+
aux_op_vals.append(self._eval_aux_operators(self._ret['eigvecs'][i]))
170171
self._ret['aux_ops'] = aux_op_vals
171172

172173
def _eval_aux_operators(self, wavefn, threshold=1e-12):
173174
values = []
174175
for operator in self._aux_operators:
176+
if operator is None:
177+
values.append(None)
178+
continue
175179
value = 0.0
176180
if not operator.is_empty():
177181
value, _ = operator.evaluate_with_statevector(wavefn)

qiskit/aqua/algorithms/minimum_eigen_solvers/vqe.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def _eval_aux_ops(self, threshold=1e-12, params=None):
328328
values = []
329329
params = []
330330
for idx, operator in enumerate(self._aux_operators):
331-
if not operator.is_empty():
331+
if operator is not None and not operator.is_empty():
332332
temp_circuit = QuantumCircuit() + wavefn_circuit
333333
circuit = operator.construct_evaluation_circuit(
334334
wave_function=temp_circuit,
@@ -345,6 +345,9 @@ def _eval_aux_ops(self, threshold=1e-12, params=None):
345345
result = self._quantum_instance.execute(to_be_simulated_circuits)
346346

347347
for idx, operator in enumerate(self._aux_operators):
348+
if operator is None:
349+
values.append(None)
350+
continue
348351
if operator.is_empty():
349352
mean, std = 0.0, 0.0
350353
else:
@@ -358,8 +361,7 @@ def _eval_aux_ops(self, threshold=1e-12, params=None):
358361
values.append((mean, std))
359362

360363
if values:
361-
aux_op_vals = np.empty([1, len(self._aux_operators), 2])
362-
aux_op_vals[0, :] = np.asarray(values)
364+
aux_op_vals = [np.asarray(values)]
363365
self._ret['aux_ops'] = aux_op_vals
364366

365367
def compute_minimum_eigenvalue(
@@ -390,6 +392,8 @@ def _run(self) -> 'VQEResult':
390392
self._operator = \
391393
self._config_the_best_mode(self._operator, self._quantum_instance.backend)
392394
for i in range(len(self._aux_operators)):
395+
if self._aux_operators[i] is None:
396+
continue
393397
if not self._aux_operators[i].is_empty():
394398
self._aux_operators[i] = \
395399
self._config_the_best_mode(self._aux_operators[i],

0 commit comments

Comments
 (0)