Skip to content

Commit

Permalink
Setting is_measurement for converted state functions and backend arg …
Browse files Browse the repository at this point in the history
…required to CircuitSampler (qiskit-community/qiskit-aqua#1344)

* 1) backend arg required to CircuitSampler
2) setting is_measurement for converted state functions
3) new test case in TestStateOpMeasEvals

* test if is_measurement correctly propagated in CircuitSampler

* test_is_measurement_correctly_propagated simplified

* release note added

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
  • Loading branch information
3 people authored Oct 15, 2020
1 parent 6e4584a commit 75c9d9e
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions qiskit/aqua/operators/converters/circuit_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
from qiskit.aqua import QuantumInstance
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.operator_globals import Zero
from qiskit.aqua.operators.list_ops.list_op import ListOp
from qiskit.aqua.operators.state_fns.state_fn import StateFn
from qiskit.aqua.operators.state_fns.circuit_state_fn import CircuitStateFn
from qiskit.aqua.operators.state_fns.dict_state_fn import DictStateFn
from qiskit.aqua.operators.converters.converter_base import ConverterBase

logger = logging.getLogger(__name__)
Expand All @@ -49,7 +49,7 @@ class CircuitSampler(ConverterBase):
"""

def __init__(self,
backend: Union[Backend, BaseBackend, QuantumInstance] = None,
backend: Union[Backend, BaseBackend, QuantumInstance],
statevector: Optional[bool] = None,
param_qobj: bool = False,
attach_results: bool = False) -> None:
Expand Down Expand Up @@ -306,13 +306,16 @@ def sample_circuits(self,
avg = avg[0] + 1j * avg[1]
# Will be replaced with just avg when eval is called later
num_qubits = circuit_sfns[0].num_qubits
result_sfn = (Zero ^ num_qubits).adjoint() * avg
result_sfn = DictStateFn('0' * num_qubits,
is_measurement=op_c.is_measurement) * avg
elif self._statevector:
result_sfn = StateFn(op_c.coeff * results.get_statevector(circ_index))
result_sfn = StateFn(op_c.coeff * results.get_statevector(circ_index),
is_measurement=op_c.is_measurement)
else:
shots = self.quantum_instance._run_config.shots
result_sfn = StateFn({b: (v / shots) ** 0.5 * op_c.coeff
for (b, v) in results.get_counts(circ_index).items()})
for (b, v) in results.get_counts(circ_index).items()},
is_measurement=op_c.is_measurement)
if self._attach_results:
result_sfn.execution_results = circ_results
c_statefns.append(result_sfn)
Expand Down

0 comments on commit 75c9d9e

Please sign in to comment.