From 5ecdf405e31ec73467fdbf4d428294fdfafd2ed7 Mon Sep 17 00:00:00 2001 From: Cryoris Date: Wed, 19 Aug 2020 14:43:12 +0200 Subject: [PATCH 01/11] eval on a statefn returns the sampling probs --- qiskit/aqua/operators/state_fns/circuit_state_fn.py | 3 +++ qiskit/aqua/operators/state_fns/dict_state_fn.py | 2 ++ qiskit/aqua/operators/state_fns/operator_state_fn.py | 5 +++++ qiskit/aqua/operators/state_fns/state_fn.py | 4 +++- qiskit/aqua/operators/state_fns/vector_state_fn.py | 5 +++++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/qiskit/aqua/operators/state_fns/circuit_state_fn.py b/qiskit/aqua/operators/state_fns/circuit_state_fn.py index eed5b5e7f5..851bfc87da 100644 --- a/qiskit/aqua/operators/state_fns/circuit_state_fn.py +++ b/qiskit/aqua/operators/state_fns/circuit_state_fn.py @@ -277,6 +277,9 @@ def assign_parameters(self, param_dict: dict) -> OperatorBase: def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: + if front is None: + return self.to_matrix_op().eval() + if not self.is_measurement and isinstance(front, OperatorBase): raise ValueError( 'Cannot compute overlap with StateFn or Operator if not Measurement. Try taking ' diff --git a/qiskit/aqua/operators/state_fns/dict_state_fn.py b/qiskit/aqua/operators/state_fns/dict_state_fn.py index 99ad5550da..e8b01ec00d 100644 --- a/qiskit/aqua/operators/state_fns/dict_state_fn.py +++ b/qiskit/aqua/operators/state_fns/dict_state_fn.py @@ -184,6 +184,8 @@ def __str__(self) -> str: def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: + if front is None: + return self.to_matrix_op().eval() if not self.is_measurement and isinstance(front, OperatorBase): raise ValueError( diff --git a/qiskit/aqua/operators/state_fns/operator_state_fn.py b/qiskit/aqua/operators/state_fns/operator_state_fn.py index 507f733966..49da8e5cf4 100644 --- a/qiskit/aqua/operators/state_fns/operator_state_fn.py +++ b/qiskit/aqua/operators/state_fns/operator_state_fn.py @@ -18,6 +18,7 @@ import numpy as np from qiskit.circuit import ParameterExpression +from qiskit.quantum_info import Statevector from ..operator_base import OperatorBase from .state_fn import StateFn @@ -181,6 +182,10 @@ def __str__(self) -> str: def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: + if front is None: + matrix = self.to_matrix_op().primitive.primitive.data + return Statevector(matrix[0, :]) + if not self.is_measurement and isinstance(front, OperatorBase): raise ValueError( 'Cannot compute overlap with StateFn or Operator if not Measurement. Try taking ' diff --git a/qiskit/aqua/operators/state_fns/state_fn.py b/qiskit/aqua/operators/state_fns/state_fn.py index 1c03f57c2e..e48f9e363c 100644 --- a/qiskit/aqua/operators/state_fns/state_fn.py +++ b/qiskit/aqua/operators/state_fns/state_fn.py @@ -290,7 +290,9 @@ def __repr__(self) -> str: def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: - raise NotImplementedError + # by default to the inefficient matrix evaluation + print('base: returning matrixop eval') + return self.to_matrix_op().eval(front) @property def parameters(self): diff --git a/qiskit/aqua/operators/state_fns/vector_state_fn.py b/qiskit/aqua/operators/state_fns/vector_state_fn.py index 8e9bf85c6f..4e8b998dc1 100644 --- a/qiskit/aqua/operators/state_fns/vector_state_fn.py +++ b/qiskit/aqua/operators/state_fns/vector_state_fn.py @@ -130,6 +130,11 @@ def __str__(self) -> str: def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: + # If eval is called on a state, just return the sampling probabilities + if front is None: + print('front is none, returning primitive') + return self.primitive + if not self.is_measurement and isinstance(front, OperatorBase): raise ValueError( 'Cannot compute overlap with StateFn or Operator if not Measurement. Try taking ' From 08306b258ff89d730e56d403986a0ea716f81b51 Mon Sep 17 00:00:00 2001 From: Cryoris Date: Wed, 19 Aug 2020 17:36:36 +0200 Subject: [PATCH 02/11] add tests, rm print --- .../operators/state_fns/vector_state_fn.py | 1 - test/aqua/operators/test_op_construction.py | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/qiskit/aqua/operators/state_fns/vector_state_fn.py b/qiskit/aqua/operators/state_fns/vector_state_fn.py index 4e8b998dc1..a60ec9d571 100644 --- a/qiskit/aqua/operators/state_fns/vector_state_fn.py +++ b/qiskit/aqua/operators/state_fns/vector_state_fn.py @@ -132,7 +132,6 @@ def eval(self, OperatorBase] = None) -> Union[OperatorBase, float, complex]: # If eval is called on a state, just return the sampling probabilities if front is None: - print('front is none, returning primitive') return self.primitive if not self.is_measurement and isinstance(front, OperatorBase): diff --git a/test/aqua/operators/test_op_construction.py b/test/aqua/operators/test_op_construction.py index 591ac5bc4c..e415155937 100644 --- a/test/aqua/operators/test_op_construction.py +++ b/test/aqua/operators/test_op_construction.py @@ -23,12 +23,12 @@ from qiskit.circuit import QuantumCircuit, QuantumRegister, Instruction, Parameter, ParameterVector from qiskit.extensions.exceptions import ExtensionError -from qiskit.quantum_info.operators import Operator, Pauli +from qiskit.quantum_info import Operator, Pauli, Statevector from qiskit.circuit.library import CZGate, ZGate from qiskit.aqua.operators import ( X, Y, Z, I, CX, T, H, PrimitiveOp, SummedOp, PauliOp, Minus, CircuitOp, MatrixOp, ListOp, - ComposedOp, StateFn + ComposedOp, StateFn, VectorStateFn, OperatorStateFn, CircuitStateFn, DictStateFn, ) @@ -500,6 +500,21 @@ def test_list_op_parameters(self): params.append(lam) self.assertEqual(list_op.parameters, set(params)) + def test_statefn_eval(self): + """Test calling eval on StateFn returns the statevector.""" + qc = QuantumCircuit(1) + ops = [VectorStateFn([1, 0]), + DictStateFn({'0': 1}), + CircuitStateFn(qc), + OperatorStateFn(I), + OperatorStateFn(MatrixOp([[1, 0], [0, 1]])), + OperatorStateFn(CircuitOp(qc))] + + expected = Statevector([1, 0]) + for op in ops: + with self.subTest(op): + self.assertEqual(op.eval(), expected) + class TestListOpComboFn(QiskitAquaTestCase): """Test combo fn is propagated.""" From a54786fe321a25ec5b77a8df7fe55945070ca8b2 Mon Sep 17 00:00:00 2001 From: Cryoris Date: Thu, 20 Aug 2020 11:33:58 +0200 Subject: [PATCH 03/11] add reno --- releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml diff --git a/releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml b/releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml new file mode 100644 index 0000000000..fc69eaef75 --- /dev/null +++ b/releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Allow calling ``eval`` on state function objects with no argument, which returns the + ``VectorStateFn`` representation of the state function. + This is consistent behaviour with ``OperatorBase.eval``, which returns the + ``MatrixOp`` representation, if no argument is passed. + From 7ece8df2f9731ddd3d3f0f205b67ee1a32c2fa85 Mon Sep 17 00:00:00 2001 From: Cryoris Date: Thu, 20 Aug 2020 12:21:25 +0200 Subject: [PATCH 04/11] return VectorStateFn instead of Statevector --- .../aqua/operators/state_fns/circuit_state_fn.py | 4 +++- qiskit/aqua/operators/state_fns/dict_state_fn.py | 4 +++- .../aqua/operators/state_fns/operator_state_fn.py | 6 +++--- qiskit/aqua/operators/state_fns/state_fn.py | 4 +--- qiskit/aqua/operators/state_fns/vector_state_fn.py | 5 ++--- test/aqua/operators/test_op_construction.py | 14 +++++++------- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/qiskit/aqua/operators/state_fns/circuit_state_fn.py b/qiskit/aqua/operators/state_fns/circuit_state_fn.py index 851bfc87da..39dd6d0e39 100644 --- a/qiskit/aqua/operators/state_fns/circuit_state_fn.py +++ b/qiskit/aqua/operators/state_fns/circuit_state_fn.py @@ -278,7 +278,9 @@ def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: if front is None: - return self.to_matrix_op().eval() + vector_state_fn = self.to_matrix_op().eval() + vector_state_fn = cast(OperatorBase, vector_state_fn) + return vector_state_fn if not self.is_measurement and isinstance(front, OperatorBase): raise ValueError( diff --git a/qiskit/aqua/operators/state_fns/dict_state_fn.py b/qiskit/aqua/operators/state_fns/dict_state_fn.py index e8b01ec00d..b5ac6a4506 100644 --- a/qiskit/aqua/operators/state_fns/dict_state_fn.py +++ b/qiskit/aqua/operators/state_fns/dict_state_fn.py @@ -185,7 +185,9 @@ def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: if front is None: - return self.to_matrix_op().eval() + vector_state_fn = self.to_matrix_op().eval() + vector_state_fn = cast(OperatorBase, vector_state_fn) + return vector_state_fn if not self.is_measurement and isinstance(front, OperatorBase): raise ValueError( diff --git a/qiskit/aqua/operators/state_fns/operator_state_fn.py b/qiskit/aqua/operators/state_fns/operator_state_fn.py index 49da8e5cf4..3cfd3998e1 100644 --- a/qiskit/aqua/operators/state_fns/operator_state_fn.py +++ b/qiskit/aqua/operators/state_fns/operator_state_fn.py @@ -18,10 +18,10 @@ import numpy as np from qiskit.circuit import ParameterExpression -from qiskit.quantum_info import Statevector from ..operator_base import OperatorBase from .state_fn import StateFn +from .vector_state_fn import VectorStateFn from ..list_ops.list_op import ListOp from ..list_ops.summed_op import SummedOp @@ -183,8 +183,8 @@ def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: if front is None: - matrix = self.to_matrix_op().primitive.primitive.data - return Statevector(matrix[0, :]) + matrix = self.primitive.to_matrix_op().primitive.data + return VectorStateFn(matrix[0, :]) if not self.is_measurement and isinstance(front, OperatorBase): raise ValueError( diff --git a/qiskit/aqua/operators/state_fns/state_fn.py b/qiskit/aqua/operators/state_fns/state_fn.py index e48f9e363c..1c03f57c2e 100644 --- a/qiskit/aqua/operators/state_fns/state_fn.py +++ b/qiskit/aqua/operators/state_fns/state_fn.py @@ -290,9 +290,7 @@ def __repr__(self) -> str: def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: - # by default to the inefficient matrix evaluation - print('base: returning matrixop eval') - return self.to_matrix_op().eval(front) + raise NotImplementedError @property def parameters(self): diff --git a/qiskit/aqua/operators/state_fns/vector_state_fn.py b/qiskit/aqua/operators/state_fns/vector_state_fn.py index a60ec9d571..512fc07ccd 100644 --- a/qiskit/aqua/operators/state_fns/vector_state_fn.py +++ b/qiskit/aqua/operators/state_fns/vector_state_fn.py @@ -130,9 +130,8 @@ def __str__(self) -> str: def eval(self, front: Union[str, dict, np.ndarray, OperatorBase] = None) -> Union[OperatorBase, float, complex]: - # If eval is called on a state, just return the sampling probabilities - if front is None: - return self.primitive + if front is None: # this object is already a VectorStateFn + return self if not self.is_measurement and isinstance(front, OperatorBase): raise ValueError( diff --git a/test/aqua/operators/test_op_construction.py b/test/aqua/operators/test_op_construction.py index 868a6f6769..17dfbbf747 100644 --- a/test/aqua/operators/test_op_construction.py +++ b/test/aqua/operators/test_op_construction.py @@ -510,17 +510,17 @@ def test_list_op_parameters(self): def test_statefn_eval(self): """Test calling eval on StateFn returns the statevector.""" qc = QuantumCircuit(1) - ops = [VectorStateFn([1, 0]), - DictStateFn({'0': 1}), - CircuitStateFn(qc), - OperatorStateFn(I), - OperatorStateFn(MatrixOp([[1, 0], [0, 1]])), - OperatorStateFn(CircuitOp(qc))] + ops = [VectorStateFn([1, 0]), + DictStateFn({'0': 1}), + CircuitStateFn(qc), + OperatorStateFn(I), + OperatorStateFn(MatrixOp([[1, 0], [0, 1]])), + OperatorStateFn(CircuitOp(qc))] expected = Statevector([1, 0]) for op in ops: with self.subTest(op): - self.assertEqual(op.eval(), expected) + self.assertEqual(op.eval().primitive, expected) class TestListOpComboFn(QiskitAquaTestCase): From a937d3ee886f51511aef9bfa5f10187c458626fa Mon Sep 17 00:00:00 2001 From: Cryoris Date: Sat, 22 Aug 2020 09:21:05 +0200 Subject: [PATCH 05/11] improve typehints --- qiskit/aqua/operators/operator_base.py | 9 +++++++-- qiskit/aqua/operators/primitive_ops/circuit_op.py | 4 ++-- qiskit/aqua/operators/primitive_ops/matrix_op.py | 4 ++-- qiskit/aqua/operators/primitive_ops/pauli_op.py | 4 ++-- qiskit/aqua/operators/primitive_ops/primitive_op.py | 4 ++-- qiskit/aqua/operators/state_fns/circuit_state_fn.py | 4 ++-- qiskit/aqua/operators/state_fns/dict_state_fn.py | 4 ++-- qiskit/aqua/operators/state_fns/state_fn.py | 4 ++-- qiskit/aqua/operators/state_fns/vector_state_fn.py | 4 ++-- 9 files changed, 23 insertions(+), 18 deletions(-) diff --git a/qiskit/aqua/operators/operator_base.py b/qiskit/aqua/operators/operator_base.py index 46bc428042..7a137ea951 100644 --- a/qiskit/aqua/operators/operator_base.py +++ b/qiskit/aqua/operators/operator_base.py @@ -77,14 +77,19 @@ def eval(self, defined to be evaluated from Zero implicitly (i.e. it is as if ``.eval('0000')`` is already called implicitly to always "indexing" from column 0). + If ``front`` is None, the matrix-represenation of the operator is returned. + Args: front: The bitstring, dict of bitstrings (with values being coefficients), or - StateFn to evaluated by the Operator's underlying function. + StateFn to evaluated by the Operator's underlying function, or None. Returns: The output of the Operator's evaluation function. If self is a ``StateFn``, the result is a float or complex. If self is an Operator (``PrimitiveOp, ComposedOp, SummedOp, - EvolvedOp,`` etc.), the result is a StateFn. If either self or front contain proper + EvolvedOp,`` etc.), the result is a StateFn. + If ``front`` is None, the matrix-representation of the operator is returned, which + is a ``MatrixOp`` for the operators and a ``VectorStateFn`` for state-functions. + If either self or front contain proper ``ListOps`` (not ListOp subclasses), the result is an n-dimensional list of complex or StateFn results, resulting from the recursive evaluation by each OperatorBase in the ListOps. diff --git a/qiskit/aqua/operators/primitive_ops/circuit_op.py b/qiskit/aqua/operators/primitive_ops/circuit_op.py index 4647307ee8..48187ef33a 100644 --- a/qiskit/aqua/operators/primitive_ops/circuit_op.py +++ b/qiskit/aqua/operators/primitive_ops/circuit_op.py @@ -186,8 +186,8 @@ def assign_parameters(self, param_dict: dict) -> OperatorBase: return self.__class__(qc, coeff=param_value) def eval(self, - front: Union[str, dict, np.ndarray, - OperatorBase] = None) -> Union[OperatorBase, float, complex]: + front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None + ) -> Union[OperatorBase, float, complex]: # pylint: disable=import-outside-toplevel from ..state_fns import CircuitStateFn from ..list_ops import ListOp diff --git a/qiskit/aqua/operators/primitive_ops/matrix_op.py b/qiskit/aqua/operators/primitive_ops/matrix_op.py index d860d34780..283f96cc21 100644 --- a/qiskit/aqua/operators/primitive_ops/matrix_op.py +++ b/qiskit/aqua/operators/primitive_ops/matrix_op.py @@ -134,8 +134,8 @@ def __str__(self) -> str: return "{} * {}".format(self.coeff, prim_str) def eval(self, - front: Union[str, dict, np.ndarray, - OperatorBase] = None) -> Union[OperatorBase, float, complex]: + front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None + ) -> Union[OperatorBase, float, complex]: # For other ops' eval we return self.to_matrix_op() here, but that's unnecessary here. if front is None: return self diff --git a/qiskit/aqua/operators/primitive_ops/pauli_op.py b/qiskit/aqua/operators/primitive_ops/pauli_op.py index c6cf221ca8..c252086b0b 100644 --- a/qiskit/aqua/operators/primitive_ops/pauli_op.py +++ b/qiskit/aqua/operators/primitive_ops/pauli_op.py @@ -145,8 +145,8 @@ def __str__(self) -> str: return "{} * {}".format(self.coeff, prim_str) def eval(self, - front: Union[str, dict, np.ndarray, - OperatorBase] = None) -> Union[OperatorBase, float, complex]: + front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None + ) -> Union[OperatorBase, float, complex]: if front is None: return self.to_matrix_op() diff --git a/qiskit/aqua/operators/primitive_ops/primitive_op.py b/qiskit/aqua/operators/primitive_ops/primitive_op.py index 043d589057..01027e0b81 100644 --- a/qiskit/aqua/operators/primitive_ops/primitive_op.py +++ b/qiskit/aqua/operators/primitive_ops/primitive_op.py @@ -213,8 +213,8 @@ def __repr__(self) -> str: return "{}({}, coeff={})".format(type(self).__name__, repr(self.primitive), self.coeff) def eval(self, - front: Union[str, dict, np.ndarray, - OperatorBase] = None) -> Union[OperatorBase, float, complex]: + front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None + ) -> Union[OperatorBase, float, complex]: raise NotImplementedError @property diff --git a/qiskit/aqua/operators/state_fns/circuit_state_fn.py b/qiskit/aqua/operators/state_fns/circuit_state_fn.py index 39dd6d0e39..fedea52a3f 100644 --- a/qiskit/aqua/operators/state_fns/circuit_state_fn.py +++ b/qiskit/aqua/operators/state_fns/circuit_state_fn.py @@ -275,8 +275,8 @@ def assign_parameters(self, param_dict: dict) -> OperatorBase: return self.__class__(qc, coeff=param_value, is_measurement=self.is_measurement) def eval(self, - front: Union[str, dict, np.ndarray, - OperatorBase] = None) -> Union[OperatorBase, float, complex]: + front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None + ) -> Union[OperatorBase, float, complex]: if front is None: vector_state_fn = self.to_matrix_op().eval() vector_state_fn = cast(OperatorBase, vector_state_fn) diff --git a/qiskit/aqua/operators/state_fns/dict_state_fn.py b/qiskit/aqua/operators/state_fns/dict_state_fn.py index b5ac6a4506..d060c91d87 100644 --- a/qiskit/aqua/operators/state_fns/dict_state_fn.py +++ b/qiskit/aqua/operators/state_fns/dict_state_fn.py @@ -182,8 +182,8 @@ def __str__(self) -> str: # pylint: disable=too-many-return-statements def eval(self, - front: Union[str, dict, np.ndarray, - OperatorBase] = None) -> Union[OperatorBase, float, complex]: + front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None + ) -> Union[OperatorBase, float, complex]: if front is None: vector_state_fn = self.to_matrix_op().eval() vector_state_fn = cast(OperatorBase, vector_state_fn) diff --git a/qiskit/aqua/operators/state_fns/state_fn.py b/qiskit/aqua/operators/state_fns/state_fn.py index 1c03f57c2e..ea25b79161 100644 --- a/qiskit/aqua/operators/state_fns/state_fn.py +++ b/qiskit/aqua/operators/state_fns/state_fn.py @@ -288,8 +288,8 @@ def __repr__(self) -> str: self.coeff, self.is_measurement) def eval(self, - front: Union[str, dict, np.ndarray, - OperatorBase] = None) -> Union[OperatorBase, float, complex]: + front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None + ) -> Union[OperatorBase, float, complex]: raise NotImplementedError @property diff --git a/qiskit/aqua/operators/state_fns/vector_state_fn.py b/qiskit/aqua/operators/state_fns/vector_state_fn.py index 512fc07ccd..025a6fbed3 100644 --- a/qiskit/aqua/operators/state_fns/vector_state_fn.py +++ b/qiskit/aqua/operators/state_fns/vector_state_fn.py @@ -128,8 +128,8 @@ def __str__(self) -> str: # pylint: disable=too-many-return-statements def eval(self, - front: Union[str, dict, np.ndarray, - OperatorBase] = None) -> Union[OperatorBase, float, complex]: + front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None + ) -> Union[OperatorBase, float, complex]: if front is None: # this object is already a VectorStateFn return self From 6b850c15bf113f100664cb7bf19669414527a329 Mon Sep 17 00:00:00 2001 From: Cryoris Date: Sat, 22 Aug 2020 09:32:22 +0200 Subject: [PATCH 06/11] fix missing Dict import --- qiskit/aqua/operators/primitive_ops/circuit_op.py | 2 +- qiskit/aqua/operators/primitive_ops/matrix_op.py | 2 +- qiskit/aqua/operators/primitive_ops/pauli_op.py | 2 +- qiskit/aqua/operators/primitive_ops/primitive_op.py | 2 +- qiskit/aqua/operators/state_fns/circuit_state_fn.py | 2 +- qiskit/aqua/operators/state_fns/dict_state_fn.py | 2 +- qiskit/aqua/operators/state_fns/vector_state_fn.py | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/qiskit/aqua/operators/primitive_ops/circuit_op.py b/qiskit/aqua/operators/primitive_ops/circuit_op.py index 48187ef33a..a5601c5014 100644 --- a/qiskit/aqua/operators/primitive_ops/circuit_op.py +++ b/qiskit/aqua/operators/primitive_ops/circuit_op.py @@ -14,7 +14,7 @@ """ CircuitOp Class """ -from typing import Union, Optional, Set, List, cast +from typing import Union, Optional, Set, List, Dict, cast import logging import numpy as np diff --git a/qiskit/aqua/operators/primitive_ops/matrix_op.py b/qiskit/aqua/operators/primitive_ops/matrix_op.py index 283f96cc21..4b2a776ea7 100644 --- a/qiskit/aqua/operators/primitive_ops/matrix_op.py +++ b/qiskit/aqua/operators/primitive_ops/matrix_op.py @@ -14,7 +14,7 @@ """ MatrixOp Class """ -from typing import Union, Optional, Set +from typing import Union, Optional, Set, Dict import logging import numpy as np from scipy.sparse import spmatrix diff --git a/qiskit/aqua/operators/primitive_ops/pauli_op.py b/qiskit/aqua/operators/primitive_ops/pauli_op.py index c252086b0b..696cc5632a 100644 --- a/qiskit/aqua/operators/primitive_ops/pauli_op.py +++ b/qiskit/aqua/operators/primitive_ops/pauli_op.py @@ -14,7 +14,7 @@ """ PauliOp Class """ -from typing import Union, Set, Dict, cast +from typing import Union, Set, Dict, Optional, cast import logging import numpy as np from scipy.sparse import spmatrix diff --git a/qiskit/aqua/operators/primitive_ops/primitive_op.py b/qiskit/aqua/operators/primitive_ops/primitive_op.py index 01027e0b81..c6ced2fc58 100644 --- a/qiskit/aqua/operators/primitive_ops/primitive_op.py +++ b/qiskit/aqua/operators/primitive_ops/primitive_op.py @@ -14,7 +14,7 @@ """ PrimitiveOp Class """ -from typing import Optional, Union, Set, List +from typing import Optional, Union, Set, List, Dict import logging import numpy as np from scipy.sparse import spmatrix diff --git a/qiskit/aqua/operators/state_fns/circuit_state_fn.py b/qiskit/aqua/operators/state_fns/circuit_state_fn.py index fedea52a3f..fe799570a8 100644 --- a/qiskit/aqua/operators/state_fns/circuit_state_fn.py +++ b/qiskit/aqua/operators/state_fns/circuit_state_fn.py @@ -15,7 +15,7 @@ """ CircuitStateFn Class """ -from typing import Union, Set, List, cast +from typing import Union, Set, List, Optional, Dict, cast import numpy as np from qiskit import QuantumCircuit, BasicAer, execute, ClassicalRegister diff --git a/qiskit/aqua/operators/state_fns/dict_state_fn.py b/qiskit/aqua/operators/state_fns/dict_state_fn.py index d060c91d87..f34cc997a8 100644 --- a/qiskit/aqua/operators/state_fns/dict_state_fn.py +++ b/qiskit/aqua/operators/state_fns/dict_state_fn.py @@ -14,7 +14,7 @@ """ DictStateFn Class """ -from typing import Union, Set, cast +from typing import Optional, Union, Set, cast import itertools import numpy as np from scipy import sparse diff --git a/qiskit/aqua/operators/state_fns/vector_state_fn.py b/qiskit/aqua/operators/state_fns/vector_state_fn.py index 025a6fbed3..2392fec1de 100644 --- a/qiskit/aqua/operators/state_fns/vector_state_fn.py +++ b/qiskit/aqua/operators/state_fns/vector_state_fn.py @@ -15,7 +15,7 @@ """ VectorStateFn Class """ -from typing import Union, Set +from typing import Union, Set, Optional, Dict import numpy as np from qiskit.quantum_info import Statevector From 069599d8d3474191a8ad6c2446158b06026bc5e1 Mon Sep 17 00:00:00 2001 From: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Date: Sat, 22 Aug 2020 11:55:54 -0400 Subject: [PATCH 07/11] Update qiskit/aqua/operators/operator_base.py --- qiskit/aqua/operators/operator_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit/aqua/operators/operator_base.py b/qiskit/aqua/operators/operator_base.py index 7a137ea951..937bffea23 100644 --- a/qiskit/aqua/operators/operator_base.py +++ b/qiskit/aqua/operators/operator_base.py @@ -77,7 +77,7 @@ def eval(self, defined to be evaluated from Zero implicitly (i.e. it is as if ``.eval('0000')`` is already called implicitly to always "indexing" from column 0). - If ``front`` is None, the matrix-represenation of the operator is returned. + If ``front`` is None, the matrix-representation of the operator is returned. Args: front: The bitstring, dict of bitstrings (with values being coefficients), or From ee039134f7b0991656681cf307bb6317cbae27b0 Mon Sep 17 00:00:00 2001 From: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Date: Sat, 22 Aug 2020 11:56:38 -0400 Subject: [PATCH 08/11] Update qiskit/aqua/operators/operator_base.py --- qiskit/aqua/operators/operator_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit/aqua/operators/operator_base.py b/qiskit/aqua/operators/operator_base.py index 937bffea23..0d5b37d5bd 100644 --- a/qiskit/aqua/operators/operator_base.py +++ b/qiskit/aqua/operators/operator_base.py @@ -86,8 +86,8 @@ def eval(self, Returns: The output of the Operator's evaluation function. If self is a ``StateFn``, the result is a float or complex. If self is an Operator (``PrimitiveOp, ComposedOp, SummedOp, - EvolvedOp,`` etc.), the result is a StateFn. - If ``front`` is None, the matrix-representation of the operator is returned, which + EvolvedOp,`` etc.), the result is a StateFn. + If ``front`` is None, the matrix-representation of the operator is returned, which is a ``MatrixOp`` for the operators and a ``VectorStateFn`` for state-functions. If either self or front contain proper ``ListOps`` (not ListOp subclasses), the result is an n-dimensional list of complex From a6e89f4bf5ae08c4d6704abdf47e97ec203fcaf8 Mon Sep 17 00:00:00 2001 From: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Date: Sat, 22 Aug 2020 11:57:38 -0400 Subject: [PATCH 09/11] Update qiskit/aqua/operators/state_fns/dict_state_fn.py --- qiskit/aqua/operators/state_fns/dict_state_fn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit/aqua/operators/state_fns/dict_state_fn.py b/qiskit/aqua/operators/state_fns/dict_state_fn.py index f34cc997a8..86ca618f63 100644 --- a/qiskit/aqua/operators/state_fns/dict_state_fn.py +++ b/qiskit/aqua/operators/state_fns/dict_state_fn.py @@ -14,7 +14,7 @@ """ DictStateFn Class """ -from typing import Optional, Union, Set, cast +from typing import Optional, Union, Set, Dict, cast import itertools import numpy as np from scipy import sparse From d88138400095adedb036e3731bfd65a621b4ef30 Mon Sep 17 00:00:00 2001 From: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Date: Sat, 22 Aug 2020 12:00:03 -0400 Subject: [PATCH 10/11] Update releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml --- releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml b/releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml index fc69eaef75..a32c2f407c 100644 --- a/releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml +++ b/releasenotes/notes/statefn-eval-51ecf38a7a3cc087.yaml @@ -3,6 +3,5 @@ features: - | Allow calling ``eval`` on state function objects with no argument, which returns the ``VectorStateFn`` representation of the state function. - This is consistent behaviour with ``OperatorBase.eval``, which returns the + This is consistent behavior with ``OperatorBase.eval``, which returns the ``MatrixOp`` representation, if no argument is passed. - From 86eefdac563de2ce8e7b3033828d20bc14bcc918 Mon Sep 17 00:00:00 2001 From: woodsp Date: Mon, 24 Aug 2020 12:35:16 -0400 Subject: [PATCH 11/11] Change test case to use ddt instead of subtest loop --- test/aqua/operators/test_op_construction.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/test/aqua/operators/test_op_construction.py b/test/aqua/operators/test_op_construction.py index 73e2050d71..e18fabc5b2 100644 --- a/test/aqua/operators/test_op_construction.py +++ b/test/aqua/operators/test_op_construction.py @@ -619,20 +619,16 @@ def test_list_op_parameters(self): params.append(lam) self.assertEqual(list_op.parameters, set(params)) - def test_statefn_eval(self): + @data(VectorStateFn([1, 0]), + DictStateFn({'0': 1}), + CircuitStateFn(QuantumCircuit(1)), + OperatorStateFn(I), + OperatorStateFn(MatrixOp([[1, 0], [0, 1]])), + OperatorStateFn(CircuitOp(QuantumCircuit(1)))) + def test_statefn_eval(self, op): """Test calling eval on StateFn returns the statevector.""" - qc = QuantumCircuit(1) - ops = [VectorStateFn([1, 0]), - DictStateFn({'0': 1}), - CircuitStateFn(qc), - OperatorStateFn(I), - OperatorStateFn(MatrixOp([[1, 0], [0, 1]])), - OperatorStateFn(CircuitOp(qc))] - expected = Statevector([1, 0]) - for op in ops: - with self.subTest(op): - self.assertEqual(op.eval().primitive, expected) + self.assertEqual(op.eval().primitive, expected) class TestOpMethods(QiskitAquaTestCase):