Skip to content

QuantumState.to_dict does not honor decimals input in all cases #14181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sbuchholz2 opened this issue Apr 6, 2025 · 1 comment · May be fixed by #14302
Open

QuantumState.to_dict does not honor decimals input in all cases #14181

sbuchholz2 opened this issue Apr 6, 2025 · 1 comment · May be fixed by #14302
Assignees
Labels
bug Something isn't working mod: quantum info Related to the Quantum Info module (States & Operators)

Comments

@sbuchholz2
Copy link

sbuchholz2 commented Apr 6, 2025

Environment

  • Qiskit version: All versions
  • Python version: All versions
  • Operating system: All operating systems

What is happening?

When converting a QuantumState (or Statevector) to a dictionary, the _vector_to_dict function accepts an optional "decimals" parameter which should round the values to a given number of decimals.

While it is calculated, in the case where string_labels are present, the return result pulls the unrounded values.

How can we reproduce the issue?

from qiskit import QuantumCircuit, qasm3
from qiskit.quantum_info import Statevector
from qiskit import quantum_info as qi

def run_file(filename):
    qc = qasm3.load(filename)
    qc.remove_final_measurements()
    sv = qi.Statevector(qc)
    print(f"{sv.to_dict(decimals=3)}")```

What should happen?

The to_dict(decimals=3) should return the results rounded to 3 decimal places

Any suggestions?

At the end of _vector_to_dict, the item of vec[inds] should be changed to vals[inds], see below:

        # Make dict of tuples
        if string_labels:
            return dict(zip(kets, **vals**[inds]))

        return {tuple(ket): val for ket, val in zip(kets, vals[inds])}
@sbuchholz2 sbuchholz2 added the bug Something isn't working label Apr 6, 2025
@jakelishman
Copy link
Member

Sorry for the slow reply, and thanks for the report. Would you want to make a PR fixing it?

sbuchholz2 added a commit to sbuchholz2/qiskit that referenced this issue Apr 25, 2025
@ShellyGarion ShellyGarion added the mod: quantum info Related to the Quantum Info module (States & Operators) label May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: quantum info Related to the Quantum Info module (States & Operators)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants