Skip to content
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

Fixing the tools for plotting Pauli vec #10619

Merged
merged 6 commits into from
Aug 15, 2023
Merged

Conversation

jaygambetta
Copy link
Member

Summary

I am not sure where we lost a dimensional factor in the plotting of the state in the Pauli vec. Added it back in and made a quick test for the future.

Details and comments

@jaygambetta jaygambetta requested review from nonhermitian and a team as code owners August 14, 2023 01:19
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

@coveralls
Copy link

coveralls commented Aug 14, 2023

Pull Request Test Coverage Report for Build 5866932109

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • 34 unchanged lines in 9 files lost coverage.
  • Overall coverage decreased (-0.01%) to 87.247%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 93.76%
qiskit/exceptions.py 1 96.15%
qiskit/qasm2/init.py 2 93.33%
qiskit/qpy/exceptions.py 2 66.67%
qiskit/pulse/library/waveform.py 3 93.75%
qiskit/algorithms/amplitude_estimators/ae.py 4 93.94%
qiskit/circuit/init.py 4 84.85%
crates/qasm2/src/lex.rs 5 91.14%
crates/qasm2/src/parse.rs 12 96.67%
Totals Coverage Status
Change from base Build 5837476817: -0.01%
Covered Lines: 74288
Relevant Lines: 85147

💛 - Coveralls

@jaygambetta
Copy link
Member Author

@mtreinish I see this is beyond my ability. The test image for matplotlib is incorrect and as such we would need to update the file qiskit-terra/test/visual/mpl/graph/references/paulivec.png it should be +1 on the "I" and -1 on "Z". I don't know how you generate a png which can be used for comparison in the unit test infrastructure.

@mtreinish
Copy link
Member

The image tests actually upload the results in a tarball you can download in this case. The image file it generated was:

paulivec

you can just drop that image in https://github.com/Qiskit/qiskit-terra/tree/main/test/visual/mpl/graph/references and that should fix the failure.

@Cryoris
Copy link
Contributor

Cryoris commented Aug 14, 2023

Upon running the test with your local modifications, the graph output is stored in <local-qiskit-terra-folder>/test/visual/mpl/graph/graph_results/paulivec.png. Just copying this into the ../references subfolder should get the test to run. Edit: I was too slow answering 🙂

It seems the visualization uses the definition of the Pauli vector $\vec n$ such that the plotted state $\rho$ is constructed as

$$ \rho = \vec n \cdot \vec\sigma = \sum_i n_i \sigma_i, $$

which implies that the Pauli vector coefficients are

$$ n_i = \frac{1}{2^N} \mathrm{Tr}(\rho\sigma_i). $$

The example in the test is constructing plotting the Pauli vector for the $|1\rangle$ state, so with this defintion

$$ \rho = |1\rangle\langle 1| = \begin{pmatrix} 0 & 0 \newline 0 & 1 \end{pmatrix} = \frac{1}{2} (I - Z) \Rightarrow \vec n = \begin{pmatrix} 0.5 \newline 0 \newline 0 \newline -0.5 \end{pmatrix}. $$

Could this additional factor of 1/2 in the inner product could cause the discrepancy in what you were expecting?

@jaygambetta
Copy link
Member Author

jaygambetta commented Aug 14, 2023

The standard definition is

\rho = 1/d \sum_I x_I P_I

so that x_I = trace[P_I rho] and for a single qubit the coefficients are x, y and z which are the Bloch vectors and for stabilizers states the coefficients have a value of 1. I think we defined it for the first time in this paper 0908.1955.

If you include the d factor I the coefficients they your y axis will decay to zero exp with the number of qubits and to get anything meaningful you need to carry around a multiplication of d for each coefficient.

@Cryoris
Copy link
Contributor

Cryoris commented Aug 14, 2023

Yeah that makes sense, it might be nice to add that equation to the docstring and clarify what's expected -- I can also add that 🙂

@jaygambetta
Copy link
Member Author

please feel free to add. I also think it might be better to add this as a method to statevector rather than a hidden function if you want to use it. It is exponential but useful.

@Cryoris
Copy link
Contributor

Cryoris commented Aug 15, 2023

Docstring is updated in bdfd094 👍🏻

It's actually already possible to call the paulivec plot from the statevector class via Statevector(...).draw("paulivec"). That draw method supports also all other plot methods, maybe it would be nice to highlight this in a tutorial or the class docstring.

@mtreinish mtreinish added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog labels Aug 15, 2023
@mtreinish mtreinish added this to the 0.25.1 milestone Aug 15, 2023
@mtreinish mtreinish changed the title Fixing the tools for plotting Pauli vet Fixing the tools for plotting Pauli vec Aug 15, 2023
Copy link
Member

@mtreinish mtreinish 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 for fixing this.

@mtreinish mtreinish added this pull request to the merge queue Aug 15, 2023
Merged via the queue into Qiskit:main with commit 213580d Aug 15, 2023
13 checks passed
mergify bot pushed a commit that referenced this pull request Aug 15, 2023
* fixing the plot for pauli_vec to scaled to pm 1

* testing

* linting

* ref figure

* Update docstring to define Paulivector

* add reno

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
(cherry picked from commit 213580d)
github-merge-queue bot pushed a commit that referenced this pull request Aug 15, 2023
* fixing the plot for pauli_vec to scaled to pm 1

* testing

* linting

* ref figure

* Update docstring to define Paulivector

* add reno

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
(cherry picked from commit 213580d)

Co-authored-by: Jay Gambetta <jay.gambetta@us.ibm.com>
SamD-1998 pushed a commit to SamD-1998/qiskit-terra that referenced this pull request Sep 7, 2023
* fixing the plot for pauli_vec to scaled to pm 1

* testing

* linting

* ref figure

* Update docstring to define Paulivector

* add reno

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants