Skip to content

Commit

Permalink
Merge pull request qiskit-community/qiskit-aqua#680 from chunfuchen/f…
Browse files Browse the repository at this point in the history
…ix/check_op_is_empty

Check operator is empty before using it
  • Loading branch information
chunfuchen authored Sep 12, 2019
2 parents 16d8d4f + 2de120b commit cd896c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
16 changes: 16 additions & 0 deletions qiskit/aqua/operators/matrix_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from scipy import linalg as scila
from qiskit import QuantumCircuit # pylint: disable=unused-import

from qiskit.aqua import AquaError
from .base_operator import BaseOperator

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -240,7 +241,12 @@ def evaluate_with_result(self, result, statevector_mode=True,
Returns:
float: the mean value
float: the standard deviation
Raises:
AquaError: if Operator is empty
"""
if self.is_empty():
raise AquaError("Operator is empty, check the operator.")

del use_simulator_operator_mode # unused
avg, std_dev = 0.0, 0.0
quantum_state = np.asarray(result.get_statevector(circuit_name_prefix + 'psi'))
Expand All @@ -257,7 +263,12 @@ def evaluate_with_statevector(self, quantum_state):
Returns:
float: the mean value
float: the standard deviation
Raises:
AquaError: if Operator is empty
"""
if self.is_empty():
raise AquaError("Operator is empty, check the operator.")

avg = np.vdot(quantum_state, self._matrix.dot(quantum_state))
return avg, 0.0

Expand Down Expand Up @@ -322,8 +333,13 @@ def evolve(self, state_in, evo_time=0, num_time_slices=0, expansion_mode='trotte
numpy.array: Return the matrix vector multiplication result.
Raises:
ValueError: Invalid arguments
AquaError: if Operator is empty
"""
from .op_converter import to_weighted_pauli_operator

if self.is_empty():
raise AquaError("Operator is empty, check the operator.")

# pylint: disable=no-member
if num_time_slices < 0 or not isinstance(num_time_slices, int):
raise ValueError('Number of time slices should be a non-negative integer.')
Expand Down
27 changes: 25 additions & 2 deletions qiskit/aqua/operators/weighted_pauli_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def __init__(self, paulis, basis=None, z2_symmetries=None, atol=1e-12, name=None
[(pauli[1], [i]) for i, pauli in enumerate(paulis)] if basis is None else basis
# combine the paulis and remove those with zero weight
self.simplify()
self._z2_symmetries = z2_symmetries
self._aer_paulis = None
self._atol = atol

Expand Down Expand Up @@ -594,7 +593,11 @@ def evaluate_with_statevector(self, quantum_state):
Returns:
float: the mean value
float: the standard deviation
Raises:
AquaError: if Operator is empty
"""
if self.is_empty():
raise AquaError("Operator is empty, check the operator.")
# convert to matrix first?
from .op_converter import to_matrix_operator
mat_op = to_matrix_operator(self)
Expand Down Expand Up @@ -627,10 +630,14 @@ def construct_evaluation_circuit(self, wave_function, statevector_mode,
circuit_name_prefix + Pauli string
Raises:
AquaError: if Operator is empty
AquaError: Can not find quantum register with `q` as the name and do not provide
quantum register explicitly
AquaError: The provided qr is not in the wave_function
"""
if self.is_empty():
raise AquaError("Operator is empty, check the operator.")

from qiskit.aqua.utils.run_circuits import find_regs_by_name

if qr is None:
Expand Down Expand Up @@ -685,7 +692,12 @@ def evaluation_instruction(self, statevector_mode, use_simulator_operator_mode=F
Returns:
dict: Pauli-instruction pair.
Raises:
AquaError: if Operator is empty
"""
if self.is_empty():
raise AquaError("Operator is empty, check the operator.")
instructions = {}
qr = QuantumRegister(self.num_qubits)
qc = QuantumCircuit(qr)
Expand Down Expand Up @@ -729,7 +741,12 @@ def evaluate_with_result(self, result, statevector_mode, use_simulator_operator_
Returns:
float: the mean value
float: the standard deviation
Raises:
AquaError: if Operator is empty
"""
if self.is_empty():
raise AquaError("Operator is empty, check the operator.")

avg, std_dev, variance = 0.0, 0.0, 0.0
if statevector_mode:
Expand Down Expand Up @@ -837,7 +854,11 @@ def evolve(self, state_in=None, evo_time=0, num_time_slices=1, quantum_registers
QuantumCircuit: The constructed circuit.
Raises:
AquaError: quantum_registers must be in the provided state_in circuit
AquaError: if operator is empty
"""
if self.is_empty():
raise AquaError("Operator is empty, can not evolve.")

if state_in is not None and quantum_registers is not None:
if not state_in.has_register(quantum_registers):
raise AquaError("quantum_registers must be in the provided state_in circuit.")
Expand Down Expand Up @@ -877,8 +898,10 @@ def evolve_instruction(self, evo_time=0, num_time_slices=1,
Raises:
ValueError: Number of time slices should be a non-negative integer
NotImplementedError: expansion mode not supported
AquaError: if operator is empty
"""
if self.is_empty():
raise AquaError("Operator is empty, can not build evolve instruction.")
# pylint: disable=no-member
if num_time_slices <= 0 or not isinstance(num_time_slices, int):
raise ValueError('Number of time slices should be a non-negative integer.')
Expand Down

0 comments on commit cd896c5

Please sign in to comment.