Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

The sample() method in DictStateFn does not handle complex amplitudes #1311

Closed
hay-k opened this issue Oct 3, 2020 · 0 comments
Closed

The sample() method in DictStateFn does not handle complex amplitudes #1311

hay-k opened this issue Oct 3, 2020 · 0 comments
Assignees

Comments

@hay-k
Copy link
Contributor

hay-k commented Oct 3, 2020

Information

  • Qiskit Aqua version: 0.7.5
  • Python version: 3.7.4
  • Operating system: Windows 10

What is the current behavior?

Throws the following exception

  File "_generator.pyx", line 744, in numpy.random._generator.Generator.choice
TypeError: Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'

Steps to reproduce the problem

import qiskit.aqua.operators as op
op.DictStateFn({'0': 1+0j}).sample()

What is the expected behavior?

It is expected to return {'0': 1.0}

Suggested solutions

The line probs = np.array(list(self.primitive.values()))**2 in dict_state_fn.py is correct only in the case if all values in self.primitive are real. To handle complex values properly it should be modified as follows

value_arr = np.array(list(self.primitive.values()))
probs = (value_arr * value_arr.conj()).real
@ikkoham ikkoham self-assigned this Oct 23, 2020
ikkoham added a commit to ikkoham/qiskit-aqua that referenced this issue Oct 26, 2020
ikkoham added a commit to ikkoham/qiskit-aqua that referenced this issue Oct 26, 2020
This was referenced Oct 26, 2020
manoelmarques pushed a commit to manoelmarques/qiskit-aqua that referenced this issue Nov 9, 2020
* fix qiskit-community#1311

* fix qiskit-community#1317

* add a test of reps for trotterization

* fix tests for qiskit-community#1311

* fix typing

* fix qiskit-community#1321

* add release note

* fix qiskit-community#1381

* add a test for to_opflow

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

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* remove None from constructor

* simplify logic

* Revert "remove None from constructor"

This reverts commit c884ebb.

* 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 <jlapeyre@users.noreply.github.com>
Co-authored-by: Panagiotis Barkoutsos <bpa@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
mtreinish pushed a commit to mtreinish/qiskit-core that referenced this issue Nov 20, 2020
* 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 <jlapeyre@users.noreply.github.com>

* 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 <jlapeyre@users.noreply.github.com>
Co-authored-by: Panagiotis Barkoutsos <bpa@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
manoelmarques pushed a commit to manoelmarques/qiskit-terra that referenced this issue Dec 2, 2020
* 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 <jlapeyre@users.noreply.github.com>

* 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 <jlapeyre@users.noreply.github.com>
Co-authored-by: Panagiotis Barkoutsos <bpa@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
manoelmarques pushed a commit to manoelmarques/qiskit-terra that referenced this issue Dec 7, 2020
* 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 <jlapeyre@users.noreply.github.com>

* 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 <jlapeyre@users.noreply.github.com>
Co-authored-by: Panagiotis Barkoutsos <bpa@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants