Skip to content

Add Statevec.to_dict and Statevec.to_prob_dict methods#457

Open
matulni wants to merge 10 commits intoTeamGraphix:masterfrom
matulni:sv-todict
Open

Add Statevec.to_dict and Statevec.to_prob_dict methods#457
matulni wants to merge 10 commits intoTeamGraphix:masterfrom
matulni:sv-todict

Conversation

@matulni
Copy link
Contributor

@matulni matulni commented Mar 5, 2026

This PR adds Statevec.to_dict and Statevec.to_prob_dict methods to convert a statevector in dictionary form as suggested in #100. It allows to choose between big-endian encoding (Graphix default) or little-endian encoding (Qiskit default) in the ket representation for easier cross-platform comparison.

@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.87%. Comparing base (927aa4b) to head (dfcbcbb).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #457      +/-   ##
==========================================
+ Coverage   88.85%   88.87%   +0.01%     
==========================================
  Files          44       44              
  Lines        6533     6544      +11     
==========================================
+ Hits         5805     5816      +11     
  Misses        728      728              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@emlynsg emlynsg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

\lvert\psi\rangle = q_0 \otimes q_1 \otimes q_2.

If ``encoding == "MSB"`` the first qubit is represented in the Most Significant Bit -> ``q0q1q2``. This is the default representation in Graphix.
If ``encoding == "LSB"`` the first qubit is represented in the Least Significant Bit -> ``q2q1q1``. This is the default representation in other software packages such as Qiskit.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

``q2q1q0``

Copy link
Contributor

@mgarnier59 mgarnier59 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

from graphix.parameter import ExpressionOrFloat, ExpressionOrSupportsFloat, Parameter
from graphix.sim.data import Data

_ENCODING = Literal["LSB", "MSB"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since _ENCODING appears to be used only for type checking, you could define it within the TYPE_CHECKING block. This would also allow moving the typing.Literal import there.


Parameters
----------
encoding : _ENCODING, default="MSB"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Private constants like _ENCODING should not be referenced in docstrings; inline the definition.

.. :meth:`to_dict`
"""
return {
key: float(abs(amp) ** 2) for key, amp in self.to_dict(encoding, rel_tol=rel_tol, abs_tol=abs_tol).items()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast to float can be removed, as abs(amp) is already a float for complex inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants