Add Statevec.to_dict and Statevec.to_prob_dict methods#457
Open
matulni wants to merge 10 commits intoTeamGraphix:masterfrom
Open
Add Statevec.to_dict and Statevec.to_prob_dict methods#457matulni wants to merge 10 commits intoTeamGraphix:masterfrom
Statevec.to_dict and Statevec.to_prob_dict methods#457matulni wants to merge 10 commits intoTeamGraphix:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
emlynsg
approved these changes
Mar 9, 2026
| \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. |
| from graphix.parameter import ExpressionOrFloat, ExpressionOrSupportsFloat, Parameter | ||
| from graphix.sim.data import Data | ||
|
|
||
| _ENCODING = Literal["LSB", "MSB"] |
Collaborator
There was a problem hiding this comment.
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" |
Collaborator
There was a problem hiding this comment.
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() |
Collaborator
There was a problem hiding this comment.
This cast to float can be removed, as abs(amp) is already a float for complex inputs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds
Statevec.to_dictandStatevec.to_prob_dictmethods 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.