diff --git a/qiskit/aqua/algorithms/quantum_algorithm.py b/qiskit/aqua/algorithms/quantum_algorithm.py index a420035927..34086da36f 100644 --- a/qiskit/aqua/algorithms/quantum_algorithm.py +++ b/qiskit/aqua/algorithms/quantum_algorithm.py @@ -60,7 +60,7 @@ def run(self, AquaError: If a quantum instance or backend has not been provided """ if quantum_instance is None and self.quantum_instance is None: - raise AquaError("Quantum device or backend " + raise AquaError("QuantumInstance or backend " "is needed since you are running quantum algorithm.") if isinstance(quantum_instance, (BaseBackend, Backend)): self.set_backend(quantum_instance, **kwargs) diff --git a/qiskit/aqua/operators/primitive_ops/circuit_op.py b/qiskit/aqua/operators/primitive_ops/circuit_op.py index 383e9d0b58..2bcc5d0791 100644 --- a/qiskit/aqua/operators/primitive_ops/circuit_op.py +++ b/qiskit/aqua/operators/primitive_ops/circuit_op.py @@ -35,9 +35,8 @@ class CircuitOp(PrimitiveOp): """ def __init__(self, - primitive: Union[Instruction, QuantumCircuit] = None, - coeff: Optional[Union[int, float, complex, - ParameterExpression]] = 1.0) -> None: + primitive: Union[Instruction, QuantumCircuit], + coeff: Union[int, float, complex, ParameterExpression] = 1.0) -> None: """ Args: primitive: The QuantumCircuit which defines the diff --git a/qiskit/aqua/operators/primitive_ops/matrix_op.py b/qiskit/aqua/operators/primitive_ops/matrix_op.py index ec91a450cd..49caa7922e 100644 --- a/qiskit/aqua/operators/primitive_ops/matrix_op.py +++ b/qiskit/aqua/operators/primitive_ops/matrix_op.py @@ -12,7 +12,7 @@ """ MatrixOp Class """ -from typing import Union, Optional, Set, Dict, List, cast +from typing import Union, Optional, Set, Dict, List, cast, get_type_hints import logging import numpy as np from scipy.sparse import spmatrix @@ -40,8 +40,8 @@ class MatrixOp(PrimitiveOp): """ def __init__(self, - primitive: Union[list, np.ndarray, spmatrix, Operator] = None, - coeff: Optional[Union[int, float, complex, ParameterExpression]] = 1.0) -> None: + primitive: Union[list, np.ndarray, spmatrix, Operator], + coeff: Union[int, float, complex, ParameterExpression] = 1.0) -> None: """ Args: primitive: The matrix-like object which defines the behavior of the underlying function. @@ -51,6 +51,7 @@ def __init__(self, TypeError: invalid parameters. ValueError: invalid parameters. """ + primitive_orig = primitive if isinstance(primitive, spmatrix): primitive = primitive.toarray() @@ -58,9 +59,10 @@ def __init__(self, primitive = Operator(primitive) if not isinstance(primitive, Operator): - raise TypeError( - 'MatrixOp can only be instantiated with MatrixOperator, ' - 'not {}'.format(type(primitive))) + type_hints = get_type_hints(MatrixOp.__init__).get('primitive') + valid_cls = [cls.__name__ for cls in type_hints.__args__] + raise TypeError(f"MatrixOp can only be instantiated with {valid_cls}, " + f"not '{primitive_orig.__class__.__name__}'") if not primitive.input_dims() == primitive.output_dims(): raise ValueError('Cannot handle non-square matrices yet.') diff --git a/qiskit/aqua/operators/primitive_ops/pauli_op.py b/qiskit/aqua/operators/primitive_ops/pauli_op.py index 6101995442..faf5547b3d 100644 --- a/qiskit/aqua/operators/primitive_ops/pauli_op.py +++ b/qiskit/aqua/operators/primitive_ops/pauli_op.py @@ -39,7 +39,7 @@ class PauliOp(PrimitiveOp): """ def __init__(self, - primitive: Union[Pauli] = None, + primitive: Union[Pauli], coeff: Union[int, float, complex, ParameterExpression] = 1.0) -> None: """ Args: diff --git a/qiskit/aqua/operators/primitive_ops/primitive_op.py b/qiskit/aqua/operators/primitive_ops/primitive_op.py index 62adbb7a91..68f0d7d12b 100644 --- a/qiskit/aqua/operators/primitive_ops/primitive_op.py +++ b/qiskit/aqua/operators/primitive_ops/primitive_op.py @@ -48,8 +48,9 @@ class PrimitiveOp(OperatorBase): @staticmethod # pylint: disable=unused-argument def __new__(cls, - primitive: Union[Instruction, QuantumCircuit, List, - np.ndarray, spmatrix, MatrixOperator, Pauli] = None, + primitive: + Optional[Union[Instruction, QuantumCircuit, List, + np.ndarray, spmatrix, MatrixOperator, Pauli]] = None, coeff: Union[int, float, complex, ParameterExpression] = 1.0) -> 'PrimitiveOp': """ A factory method to produce the correct type of PrimitiveOp subclass based on the primitive passed in. Primitive and coeff arguments are passed into @@ -85,9 +86,10 @@ def __new__(cls, 'factory constructor'.format(type(primitive))) def __init__(self, - primitive: Union[Instruction, QuantumCircuit, List, - np.ndarray, spmatrix, MatrixOperator, Pauli] = None, - coeff: Optional[Union[int, float, complex, ParameterExpression]] = 1.0) -> None: + primitive: + Optional[Union[Instruction, QuantumCircuit, List, + np.ndarray, spmatrix, MatrixOperator, Pauli]] = None, + coeff: Union[int, float, complex, ParameterExpression] = 1.0) -> None: """ Args: primitive: The operator primitive being wrapped. diff --git a/test/aqua/operators/test_op_construction.py b/test/aqua/operators/test_op_construction.py index 054c4761be..d4b6215a0c 100644 --- a/test/aqua/operators/test_op_construction.py +++ b/test/aqua/operators/test_op_construction.py @@ -35,6 +35,7 @@ CircuitStateFn, VectorStateFn, DictStateFn, OperatorStateFn, ListOp, ComposedOp, TensoredOp, SummedOp ) +from qiskit.aqua.operators import MatrixOperator # pylint: disable=invalid-name @@ -909,6 +910,31 @@ def test_to_circuit_op(self): sfn.to_circuit_op().eval().primitive.data, vector ) + def test_invalid_primitive(self): + """Test invalid MatrixOp construction""" + msg = "MatrixOp can only be instantiated with " \ + "['list', 'ndarray', 'spmatrix', 'Operator'], not " + + with self.assertRaises(TypeError) as cm: + _ = MatrixOp('invalid') + + self.assertEqual(str(cm.exception), msg + "'str'") + + with self.assertRaises(TypeError) as cm: + _ = MatrixOp(MatrixOperator(np.eye(2))) + + self.assertEqual(str(cm.exception), msg + "'MatrixOperator'") + + with self.assertRaises(TypeError) as cm: + _ = MatrixOp(None) + + self.assertEqual(str(cm.exception), msg + "'NoneType'") + + with self.assertRaises(TypeError) as cm: + _ = MatrixOp(2.0) + + self.assertEqual(str(cm.exception), msg + "'float'") + class TestOpMethods(QiskitAquaTestCase): """Basic method tests."""