Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix qiskit-community/qiskit-aqua#1311

* fix qiskit-community/qiskit-aqua#1317

* add a test of reps for trotterization

* fix tests for qiskit-community/qiskit-aqua#1311

* fix typing

* fix qiskit-community/qiskit-aqua#1321

* add release note

* fix qiskit-community/qiskit-aqua#1381

* add a test for to_opflow

* Update qiskit/aqua/operators/converters/circuit_sampler.py

Co-authored-by: John Lapeyre <[email protected]>

* remove None from constructor

* simplify logic

* Revert "remove None from constructor"

This reverts commit c884ebba123adee22f252e11f90964a6defaec38.

* add releasenote

* simplify logic (not iscomplex to isreal)

* remove Optional

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

Co-authored-by: John Lapeyre <[email protected]>
Co-authored-by: Panagiotis Barkoutsos <[email protected]>
Co-authored-by: Steve Wood <[email protected]>
  • Loading branch information
4 people authored Oct 31, 2020
1 parent b5188f6 commit 6823329
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
16 changes: 14 additions & 2 deletions qiskit/aqua/operators/converters/circuit_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from qiskit.providers import Backend
from qiskit.circuit import QuantumCircuit, Parameter, ParameterExpression
from qiskit import QiskitError
from qiskit.aqua import QuantumInstance
from qiskit.aqua import QuantumInstance, AquaError
from qiskit.aqua.utils.backend_utils import is_aer_provider, is_statevector_backend
from qiskit.aqua.operators.operator_base import OperatorBase
from qiskit.aqua.operators.list_ops.list_op import ListOp
Expand Down Expand Up @@ -165,6 +165,8 @@ def convert(self,
Returns:
The converted Operator with CircuitStateFns replaced by DictStateFns or VectorStateFns.
Raises:
AquaError: if extracted circuits are empty.
"""
if self._last_op is None or id(operator) != id(self._last_op):
# Clear caches
Expand All @@ -181,6 +183,11 @@ def convert(self,
if not self._circuit_ops_cache:
self._circuit_ops_cache = {}
self._extract_circuitstatefns(self._reduced_op_cache)
if not self._circuit_ops_cache:
raise AquaError(
'Circuits are empty. '
'Check that the operator is an instance of CircuitStateFn or its ListOp.'
)

if params:
p_0 = list(params.values())[0] # type: ignore
Expand Down Expand Up @@ -243,8 +250,13 @@ def sample_circuits(self,
Returns:
The dictionary mapping ids of the CircuitStateFns to their replacement StateFns.
Raises:
AquaError: if extracted circuits are empty.
"""
if circuit_sfns or not self._transpiled_circ_cache:
if not circuit_sfns and not self._transpiled_circ_cache:
raise AquaError('CircuitStateFn is empty and there is no cache.')

if circuit_sfns:
if self._statevector:
circuits = [op_c.to_circuit(meas=False) for op_c in circuit_sfns]
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def __init__(self,
Args:
reps: The number of times to repeat the Trotterization circuit.
"""
super().__init__(order=1, reps=1)
super().__init__(order=1, reps=reps)
16 changes: 13 additions & 3 deletions qiskit/aqua/operators/legacy/weighted_pauli_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,19 @@ def to_opflow(self, reverse_endianness=False):
pauli_ops = []
for [w, p] in self.paulis:
pauli = Pauli.from_label(str(p)[::-1]) if reverse_endianness else p
# Adding the imaginary is necessary to handle the imaginary coefficients in UCCSD.
# TODO fix these or add support for them in Terra.
pauli_ops += [PrimitiveOp(pauli, coeff=np.real(w) + np.imag(w))]
# This weighted pauli operator has the coeff stored as a complex type
# irrespective of whether the value has any imaginary part or not.
# For many operators the coeff will be real. Hence below the coeff is made real,
# when creating the PrimitiveOp, since it can be stored then as a float, if its
# value is real, i.e. has no imaginary part. This avoids any potential issues around
# complex - but if there are complex coeffs then maybe that using the opflow
# later will fail if it happens to be used where complex is not supported.
# Now there are imaginary coefficients in UCCSD that would need to be handled
# when this is converted to opflow (evolution of hopping operators) where currently
# Terra does not handle complex.
# TODO fix these or add support for them in Terra
coeff = np.real(w) if np.isreal(w) else w
pauli_ops += [PrimitiveOp(pauli, coeff=coeff)]
return sum(pauli_ops)

@property
Expand Down
2 changes: 1 addition & 1 deletion qiskit/aqua/operators/state_fns/dict_state_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def sample(self,
shots: int = 1024,
massive: bool = False,
reverse_endianness: bool = False) -> dict:
probs = np.array(list(self.primitive.values()))**2
probs = np.square(np.abs(np.array(list(self.primitive.values()))))
unique, counts = np.unique(aqua_globals.random.choice(list(self.primitive.keys()),
size=shots,
p=(probs / sum(probs))),
Expand Down
2 changes: 1 addition & 1 deletion qiskit/aqua/operators/state_fns/operator_state_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class OperatorStateFn(StateFn):

# TODO allow normalization somehow?
def __init__(self,
primitive: Union[OperatorBase] = None,
primitive: OperatorBase = None,
coeff: Union[int, float, complex, ParameterExpression] = 1.0,
is_measurement: bool = False) -> None:
"""
Expand Down

0 comments on commit 6823329

Please sign in to comment.