Skip to content

Commit

Permalink
Fix ListOp treatment of exp_i and make SummedOp combo_fn more efficie…
Browse files Browse the repository at this point in the history
…nt (qiskit-community/qiskit-aqua#978)

* Fix ListOp treatment of exp_i and make SummedOp combo_fn more efficient.

* Fix typo.
  • Loading branch information
dongreenberg authored May 13, 2020
1 parent ffbdac9 commit 2963f19
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
11 changes: 10 additions & 1 deletion qiskit/aqua/operators/evolutions/matrix_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ def convert(self, operator: OperatorBase) -> OperatorBase:
The converted operator.
"""
if isinstance(operator, EvolvedOp):
if not {'Matrix'} == operator.primitive_strings():
logger.warning('Evolved Hamiltonian is not composed of only MatrixOps, converting '
'to Matrix representation, which can be expensive.')
# Setting massive=False because this conversion is implicit. User can perform this
# action on the Hamiltonian with massive=True explicitly if they so choose.
# TODO explore performance to see whether we should avoid doing this repeatedly
matrix_ham = operator.primitive.to_matrix_op(massive=False)
operator = EvolvedOp(matrix_ham, coeff=operator.coeff)

if isinstance(operator.primitive, ListOp):
return operator.primitive.to_matrix_op().exp_i() * operator.coeff
return operator.primitive.exp_i() * operator.coeff
elif isinstance(operator.primitive, (MatrixOp, PauliOp)):
return operator.primitive.exp_i()
elif isinstance(operator, ListOp):
Expand Down
2 changes: 1 addition & 1 deletion qiskit/aqua/operators/list_ops/composed_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self,
Args:
oplist: The Operators being composed.
coeff: A coefficient multiplying the operator
abelian: Indicates whether the Operators in ``oplist`` are know to mutually commute.
abelian: Indicates whether the Operators in ``oplist`` are known to mutually commute.
"""
super().__init__(oplist,
combo_fn=partial(reduce, np.dot),
Expand Down
6 changes: 5 additions & 1 deletion qiskit/aqua/operators/list_ops/list_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self,
combo_fn (callable): The recombination function to combine classical results of the
``oplist`` Operators' eval functions (e.g. sum).
coeff: A coefficient multiplying the operator
abelian: Indicates whether the Operators in ``oplist`` are know to mutually commute.
abelian: Indicates whether the Operators in ``oplist`` are known to mutually commute.
Note that the default "recombination function" lambda above is essentially the
identity - it accepts the list of values, and returns them in a list.
Expand Down Expand Up @@ -274,6 +274,10 @@ def eval(self,

def exp_i(self) -> OperatorBase:
""" Return an ``OperatorBase`` equivalent to an exponentiation of self * -i, e^(-i*op)."""
# pylint: disable=unidiomatic-typecheck
if type(self) == ListOp:
return ListOp([op.exp_i() for op in self.oplist], coeff=self.coeff)

# pylint: disable=import-outside-toplevel
from qiskit.aqua.operators import EvolvedOp
return EvolvedOp(self)
Expand Down
6 changes: 3 additions & 3 deletions qiskit/aqua/operators/list_ops/summed_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from typing import List, Union
import copy
from functools import reduce, partial
from functools import reduce
import numpy as np

from qiskit.circuit import ParameterExpression
Expand All @@ -41,10 +41,10 @@ def __init__(self,
Args:
oplist: The Operators being summed.
coeff: A coefficient multiplying the operator
abelian: Indicates whether the Operators in ``oplist`` are know to mutually commute.
abelian: Indicates whether the Operators in ``oplist`` are known to mutually commute.
"""
super().__init__(oplist,
combo_fn=partial(reduce, lambda x, y: np.sum([x, y], axis=0)),
combo_fn=lambda x: np.sum(x, axis=0),
coeff=coeff,
abelian=abelian)

Expand Down
2 changes: 1 addition & 1 deletion qiskit/aqua/operators/list_ops/tensored_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self,
Args:
oplist: The Operators being tensored.
coeff: A coefficient multiplying the operator
abelian: Indicates whether the Operators in ``oplist`` are know to mutually commute.
abelian: Indicates whether the Operators in ``oplist`` are known to mutually commute.
"""
super().__init__(oplist,
combo_fn=partial(reduce, np.kron),
Expand Down

0 comments on commit 2963f19

Please sign in to comment.