From 4273dc2abb08974b4e5274c65bda558062eb037d Mon Sep 17 00:00:00 2001 From: ikkoham Date: Thu, 15 Sep 2022 15:06:05 +0900 Subject: [PATCH 1/2] Fix tests --- qiskit/primitives/base_estimator.py | 45 ++++++++++++------------ qiskit/primitives/base_sampler.py | 35 +++++++++--------- test/python/primitives/test_estimator.py | 29 ++++++++------- test/python/primitives/test_sampler.py | 21 ++++++----- 4 files changed, 63 insertions(+), 67 deletions(-) diff --git a/qiskit/primitives/base_estimator.py b/qiskit/primitives/base_estimator.py index 446aaebd9156..63cd73444d09 100644 --- a/qiskit/primitives/base_estimator.py +++ b/qiskit/primitives/base_estimator.py @@ -110,7 +110,6 @@ from qiskit.circuit import Parameter, QuantumCircuit from qiskit.circuit.parametertable import ParameterView -from qiskit.exceptions import QiskitError from qiskit.opflow import PauliSumOp from qiskit.providers import JobV1 as Job from qiskit.providers import Options @@ -151,7 +150,7 @@ def __init__( options: Default options. Raises: - QiskitError: For mismatch of circuits and parameters list. + ValueError: For mismatch of circuits and parameters list. """ if circuits is not None or observables is not None or parameters is not None: warn( @@ -179,13 +178,13 @@ def __init__( else: self._parameters = [ParameterView(par) for par in parameters] if len(self._parameters) != len(self._circuits): - raise QiskitError( + raise ValueError( f"Different number of parameters ({len(self._parameters)}) and " f"circuits ({len(self._circuits)})" ) for i, (circ, params) in enumerate(zip(self._circuits, self._parameters)): if circ.num_parameters != len(params): - raise QiskitError( + raise ValueError( f"Different numbers of parameters of {i}-th circuit: " f"expected {circ.num_parameters}, actual {len(params)}." ) @@ -326,8 +325,8 @@ def __call__( EstimatorResult: The result of the estimator. Raises: - QiskitError: For mismatch of object id. - QiskitError: For mismatch of length of Sequence. + ValueError: For mismatch of object id. + ValueError: For mismatch of length of Sequence. """ # Support ndarray @@ -342,7 +341,7 @@ def __call__( for circuit in circuits ] if any(circuit is None for circuit in circuits): - raise QiskitError( + raise ValueError( "The circuits passed when calling estimator is not one of the circuits used to " "initialize the session." ) @@ -353,7 +352,7 @@ def __call__( for observable in observables ] if any(observable is None for observable in observables): - raise QiskitError( + raise ValueError( "The observables passed when calling estimator is not one of the observables used to " "initialize the session." ) @@ -365,7 +364,7 @@ def __call__( if parameter_values is None: for i in circuits: if len(self._circuits[i].parameters) != 0: - raise QiskitError( + raise ValueError( f"The {i}-th circuit is parameterised," "but parameter values are not given." ) @@ -373,19 +372,19 @@ def __call__( # Validation if len(circuits) != len(observables): - raise QiskitError( + raise ValueError( f"The number of circuits ({len(circuits)}) does not match " f"the number of observables ({len(observables)})." ) if len(circuits) != len(parameter_values): - raise QiskitError( + raise ValueError( f"The number of circuits ({len(circuits)}) does not match " f"the number of parameter value sets ({len(parameter_values)})." ) for i, value in zip(circuits, parameter_values): if len(value) != len(self._parameters[i]): - raise QiskitError( + raise ValueError( f"The number of values ({len(value)}) does not match " f"the number of parameters ({len(self._parameters[i])}) for the {i}-th circuit." ) @@ -394,19 +393,19 @@ def __call__( circuit_num_qubits = self.circuits[circ_i].num_qubits observable_num_qubits = self.observables[obs_i].num_qubits if circuit_num_qubits != observable_num_qubits: - raise QiskitError( + raise ValueError( f"The number of qubits of the {circ_i}-th circuit ({circuit_num_qubits}) does " f"not match the number of qubits of the {obs_i}-th observable " f"({observable_num_qubits})." ) if max(circuits) >= len(self.circuits): - raise QiskitError( + raise ValueError( f"The number of circuits is {len(self.circuits)}, " f"but the index {max(circuits)} is given." ) if max(observables) >= len(self.observables): - raise QiskitError( + raise ValueError( f"The number of circuits is {len(self.observables)}, " f"but the index {max(observables)} is given." ) @@ -463,7 +462,7 @@ def run( The job object of EstimatorResult. Raises: - QiskitError: Invalid arguments are given. + ValueError: Invalid arguments are given. """ # Support ndarray if isinstance(parameter_values, np.ndarray): @@ -473,7 +472,7 @@ def run( if parameter_values is None: for i, circuit in enumerate(circuits): if circuit.num_parameters != 0: - raise QiskitError( + raise ValueError( f"The {i}-th circuit is parameterised," "but parameter values are not given." ) @@ -484,39 +483,39 @@ def run( else: parameter_views = [ParameterView(par) for par in parameters] if len(self._parameters) != len(self._circuits): - raise QiskitError( + raise ValueError( f"Different number of parameters ({len(self._parameters)}) and " f"circuits ({len(self._circuits)})" ) for i, (circ, params) in enumerate(zip(self._circuits, self._parameters)): if circ.num_parameters != len(params): - raise QiskitError( + raise ValueError( f"Different numbers of parameters of {i}-th circuit: " f"expected {circ.num_parameters}, actual {len(params)}." ) # Validation if len(circuits) != len(observables): - raise QiskitError( + raise ValueError( f"The number of circuits ({len(circuits)}) does not match " f"the number of observables ({len(observables)})." ) if len(circuits) != len(parameter_values): - raise QiskitError( + raise ValueError( f"The number of circuits ({len(circuits)}) does not match " f"the number of parameter value sets ({len(parameter_values)})." ) for i, (circuit, parameter_value) in enumerate(zip(circuits, parameter_values)): if len(parameter_value) != circuit.num_parameters: - raise QiskitError( + raise ValueError( f"The number of values ({len(parameter_value)}) does not match " f"the number of parameters ({circuit.num_parameters}) for the {i}-th circuit." ) for i, (circuit, observable) in enumerate(zip(circuits, observables)): if circuit.num_qubits != observable.num_qubits: - raise QiskitError( + raise ValueError( f"The number of qubits of the {i}-th circuit ({circuit.num_qubits}) does " f"not match the number of qubits of the {i}-th observable " f"({observable.num_qubits})." diff --git a/qiskit/primitives/base_sampler.py b/qiskit/primitives/base_sampler.py index f36469d09630..e72c5297ba71 100644 --- a/qiskit/primitives/base_sampler.py +++ b/qiskit/primitives/base_sampler.py @@ -99,7 +99,6 @@ from qiskit.circuit import Parameter, QuantumCircuit from qiskit.circuit.parametertable import ParameterView -from qiskit.exceptions import QiskitError from qiskit.providers import JobV1 as Job from qiskit.providers import Options from qiskit.utils.deprecation import deprecate_arguments, deprecate_function @@ -130,7 +129,7 @@ def __init__( options: Default options. Raises: - QiskitError: For mismatch of circuits and parameters list. + ValueError: For mismatch of circuits and parameters list. """ if circuits is not None or parameters is not None: warn( @@ -153,7 +152,7 @@ def __init__( else: self._parameters = [ParameterView(par) for par in parameters] if len(self._parameters) != len(self._circuits): - raise QiskitError( + raise ValueError( f"Different number of parameters ({len(self._parameters)}) " f"and circuits ({len(self._circuits)})" ) @@ -258,8 +257,8 @@ def __call__( ``parameter_values[i]``. Raises: - QiskitError: For mismatch of object id. - QiskitError: For mismatch of length of Sequence. + ValueError: For mismatch of object id. + ValueError: For mismatch of length of Sequence. """ # Support ndarray if isinstance(parameter_values, np.ndarray): @@ -273,7 +272,7 @@ def __call__( for circuit in circuits ] if any(circuit is None for circuit in circuits): - raise QiskitError( + raise ValueError( "The circuits passed when calling sampler is not one of the circuits used to " "initialize the session." ) @@ -284,7 +283,7 @@ def __call__( if parameter_values is None: for i in circuits: if len(self._circuits[i].parameters) != 0: - raise QiskitError( + raise ValueError( f"The {i}-th circuit ({len(circuits)}) is parameterised," "but parameter values are not given." ) @@ -292,20 +291,20 @@ def __call__( # Validation if len(circuits) != len(parameter_values): - raise QiskitError( + raise ValueError( f"The number of circuits ({len(circuits)}) does not match " f"the number of parameter value sets ({len(parameter_values)})." ) for i, value in zip(circuits, parameter_values): if len(value) != len(self._parameters[i]): - raise QiskitError( + raise ValueError( f"The number of values ({len(value)}) does not match " f"the number of parameters ({len(self._parameters[i])}) for the {i}-th circuit." ) if max(circuits) >= len(self.circuits): - raise QiskitError( + raise ValueError( f"The number of circuits is {len(self.circuits)}, " f"but the index {max(circuits)} is given." ) @@ -339,7 +338,7 @@ def run( ``circuits[i]`` evaluated with parameters bound as ``parameter_values[i]``. Raises: - QiskitError: Invalid arguments are given. + ValueError: Invalid arguments are given. """ # Support ndarray if isinstance(parameter_values, np.ndarray): @@ -349,7 +348,7 @@ def run( if parameter_values is None: for i, circuit in enumerate(circuits): if circuit.num_parameters != 0: - raise QiskitError( + raise ValueError( f"The {i}-th circuit ({len(circuits)}) is parameterised," "but parameter values are not given." ) @@ -360,34 +359,34 @@ def run( else: parameter_views = [ParameterView(par) for par in parameters] if len(self._parameters) != len(self._circuits): - raise QiskitError( + raise ValueError( f"Different number of parameters ({len(self._parameters)}) and " f"circuits ({len(self._circuits)})" ) for i, (circ, params) in enumerate(zip(self._circuits, self._parameters)): if circ.num_parameters != len(params): - raise QiskitError( + raise ValueError( f"Different numbers of parameters of {i}-th circuit: " f"expected {circ.num_parameters}, actual {len(params)}." ) # Validation if len(circuits) != len(parameter_values): - raise QiskitError( + raise ValueError( f"The number of circuits ({len(circuits)}) does not match " f"the number of parameter value sets ({len(parameter_values)})." ) for i, (circuit, parameter_value) in enumerate(zip(circuits, parameter_values)): if len(parameter_value) != circuit.num_parameters: - raise QiskitError( + raise ValueError( f"The number of values ({len(parameter_value)}) does not match " f"the number of parameters ({circuit.num_parameters}) for the {i}-th circuit." ) for i, circuit in enumerate(circuits): if circuit.num_clbits == 0: - raise QiskitError( + raise ValueError( f"The {i}-th circuit does not have any classical bit. " "Sampler requires classical bits, plus measurements " "on the desired qubits." @@ -395,7 +394,7 @@ def run( mapping = final_measurement_mapping(circuit) if set(range(circuit.num_clbits)) != set(mapping.values()): - raise QiskitError( + raise ValueError( f"Some classical bits of the {i}-th circuit are not used for measurements." f" the number of classical bits ({circuit.num_clbits})," f" the used classical bits ({set(mapping.values())})." diff --git a/test/python/primitives/test_estimator.py b/test/python/primitives/test_estimator.py index c3125380fe34..36b9682cc545 100644 --- a/test/python/primitives/test_estimator.py +++ b/test/python/primitives/test_estimator.py @@ -18,7 +18,6 @@ from qiskit.circuit import QuantumCircuit from qiskit.circuit.library import RealAmplitudes -from qiskit.exceptions import QiskitError from qiskit.opflow import PauliSumOp from qiskit.primitives import Estimator, EstimatorResult from qiskit.providers import JobV1 @@ -288,17 +287,17 @@ def test_errors(self): with self.assertWarns(DeprecationWarning): est = Estimator([qc, qc2], [op, op2], [[]] * 2) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): est([0], [1], [[]]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): est([1], [0], [[]]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): est([0], [0], [[1e4]]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): est([1], [1], [[1, 2]]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): est([0, 1], [1], [[1]]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): est([0], [0, 1], [[1]]) def test_empty_parameter(self): @@ -362,7 +361,7 @@ def test_passing_objects(self): circuit = QuantumCircuit(2) with self.assertWarns(DeprecationWarning): estimator = Estimator([self.ansatz], [self.observable]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): result = estimator( circuits=[self.ansatz, circuit], observables=[self.observable, self.observable], @@ -373,7 +372,7 @@ def test_passing_objects(self): observable = SparsePauliOp(["ZX"]) with self.assertWarns(DeprecationWarning): estimator = Estimator([self.ansatz], [self.observable]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): result = estimator( circuits=[self.ansatz, self.ansatz], observables=[observable, self.observable], @@ -521,17 +520,17 @@ def test_run_errors(self): op2 = SparsePauliOp.from_list([("II", 1)]) est = Estimator() - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): est.run([qc], [op2], [[]]).result() - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): est.run([qc2], [op], [[]]).result() - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): est.run([qc], [op], [[1e4]]).result() - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): est.run([qc2], [op2], [[1, 2]]).result() - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): est.run([qc, qc2], [op2], [[1]]).result() - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): est.run([qc], [op, op2], [[1]]).result() def test_run_numpy_params(self): diff --git a/test/python/primitives/test_sampler.py b/test/python/primitives/test_sampler.py index dbdc048b69b6..a53d3ef7315c 100644 --- a/test/python/primitives/test_sampler.py +++ b/test/python/primitives/test_sampler.py @@ -21,7 +21,6 @@ from qiskit import QuantumCircuit, pulse, transpile from qiskit.circuit import Parameter from qiskit.circuit.library import RealAmplitudes -from qiskit.exceptions import QiskitError from qiskit.primitives import Sampler, SamplerResult from qiskit.primitives.utils import _circuit_key from qiskit.providers import JobStatus, JobV1 @@ -352,11 +351,11 @@ def test_errors(self): with self.assertWarns(DeprecationWarning): sampler = Sampler([qc1, qc2], [qc1.parameters, qc2.parameters]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): sampler([0], [[1e2]]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): sampler([1], [[]]) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): sampler([1], [[1e2]]) def test_empty_parameter(self): @@ -425,7 +424,7 @@ def test_passing_objects(self): circuit = QuantumCircuit(2) with self.assertWarns(DeprecationWarning): sampler = Sampler(circuits=self._pqc) - with self.assertRaises(QiskitError), self.assertWarns(DeprecationWarning): + with self.assertRaises(ValueError), self.assertWarns(DeprecationWarning): result = sampler(circuits=[circuit], parameter_values=params) @combine(indices=[[0], [1], [0, 1]]) @@ -584,22 +583,22 @@ def test_run_errors(self): sampler = Sampler() with self.subTest("set parameter values to a non-parameterized circuit"): - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): _ = sampler.run([qc1], [[1e2]]) with self.subTest("missing all parameter values for a parameterized circuit"): - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): _ = sampler.run([qc2], [[]]) with self.subTest("missing some parameter values for a parameterized circuit"): - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): _ = sampler.run([qc2], [[1e2]]) with self.subTest("too many parameter values for a parameterized circuit"): - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): _ = sampler.run([qc2], [[1e2]] * 100) with self.subTest("no classical bits"): - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): _ = sampler.run([qc3], [[]]) with self.subTest("no measurement"): - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): _ = sampler.run([qc4], [[]]) def test_run_empty_parameter(self): From 7a02db6c854a13bd66da7a1830b2714119204ab9 Mon Sep 17 00:00:00 2001 From: ikkoham Date: Tue, 20 Sep 2022 12:36:00 +0900 Subject: [PATCH 2/2] fix tests --- .../algorithms/state_fidelities/test_compute_uncompute.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/python/algorithms/state_fidelities/test_compute_uncompute.py b/test/python/algorithms/state_fidelities/test_compute_uncompute.py index d4a605dbb964..d5ee5d1bdc6f 100644 --- a/test/python/algorithms/state_fidelities/test_compute_uncompute.py +++ b/test/python/algorithms/state_fidelities/test_compute_uncompute.py @@ -21,7 +21,6 @@ from qiskit.primitives import Sampler from qiskit.algorithms.state_fidelities import ComputeUncompute from qiskit.test import QiskitTestCase -from qiskit import QiskitError class TestComputeUncompute(QiskitTestCase): @@ -144,7 +143,7 @@ def test_param_mismatch(self): fidelity = ComputeUncompute(self._sampler) n = len(self._left_params) - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): job = fidelity.run( [self._circuit[0]] * n, [self._circuit[1]] * n, @@ -153,7 +152,7 @@ def test_param_mismatch(self): ) job.result() - with self.assertRaises(QiskitError): + with self.assertRaises(ValueError): job = fidelity.run( [self._circuit[0]] * n, [self._circuit[1]] * n,