Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions qiskit/primitives/base_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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
Expand Down Expand Up @@ -144,7 +143,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(
Expand Down Expand Up @@ -172,13 +171,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)}."
)
Expand Down Expand Up @@ -319,8 +318,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
Expand All @@ -335,7 +334,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."
)
Expand All @@ -346,7 +345,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."
)
Expand All @@ -358,27 +357,27 @@ 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."
)
parameter_values = [[]] * len(circuits)

# 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."
)
Expand All @@ -387,19 +386,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."
)
Expand Down Expand Up @@ -451,7 +450,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):
Expand All @@ -461,34 +460,34 @@ 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."
)
parameter_values = [[]] * len(circuits)

# 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})."
Expand Down
31 changes: 15 additions & 16 deletions qiskit/primitives/base_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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
Expand Down Expand Up @@ -125,7 +124,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(
Expand All @@ -148,7 +147,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)})"
)
Expand Down Expand Up @@ -253,8 +252,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):
Expand All @@ -268,7 +267,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."
)
Expand All @@ -279,28 +278,28 @@ 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."
)
parameter_values = [[]] * len(circuits)

# 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."
)
Expand Down Expand Up @@ -331,7 +330,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):
Expand All @@ -341,37 +340,37 @@ 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."
)
parameter_values = [[]] * len(circuits)

# 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."
)

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())})."
Expand Down
29 changes: 14 additions & 15 deletions test/python/primitives/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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],
Expand All @@ -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],
Expand Down Expand Up @@ -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):
Expand Down
Loading