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

Fix phase management in StabilizerState.expectation_value #7460

Merged
merged 11 commits into from
Feb 17, 2022

Conversation

ShellyGarion
Copy link
Member

Summary

close #7441

Fix a bug in StabilizerState.expectation_value() method when the Pauli operator has a non-trivial phase.

Details and comments

In addition, verify that the operator in indeed a Pauli, and extend the tests accordingly.

@coveralls
Copy link

coveralls commented Jan 4, 2022

Pull Request Test Coverage Report for Build 1858344895

  • 7 of 8 (87.5%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.006%) to 83.323%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/quantum_info/states/stabilizerstate.py 7 8 87.5%
Files with Coverage Reduction New Missed Lines %
qiskit/pulse/library/waveform.py 3 89.36%
Totals Coverage Status
Change from base Build 1855099047: -0.006%
Covered Lines: 52203
Relevant Lines: 62651

💛 - Coveralls

ikkoham
ikkoham previously approved these changes Jan 18, 2022
Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

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

LGTM. Let me make just one issue.


Args:
oper (BaseOperator): an operator to evaluate expval.
oper (Pauli): a Pauli operator to evaluate expval.
Copy link
Contributor

Choose a reason for hiding this comment

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

If we extend this to SparsePauliOp, we can use from_operator to apply it to the entire BaseOperator. However, I think this is beyond the scope of the bug fix and should be treated as a separate issue.

Copy link
Member Author

@ShellyGarion ShellyGarion Jan 20, 2022

Choose a reason for hiding this comment

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

I agree that we should open a separate PR to extend this to SparsePauliOp

Copy link
Member Author

Choose a reason for hiding this comment

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

I can start such a PR after this PR will be merged

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

In principle the change here looks straightforwards, but I don't actually know the maths, and there's an unchanged line that feels wrong. If it's actually correct, I'm happy to accept if we can beef up the new test case a little further.

qiskit/quantum_info/states/stabilizerstate.py Outdated Show resolved Hide resolved
qiskit/quantum_info/states/stabilizerstate.py Show resolved Hide resolved
qiskit/quantum_info/states/stabilizerstate.py Show resolved Hide resolved
test/python/quantum_info/states/test_stabilizerstate.py Outdated Show resolved Hide resolved
ikkoham
ikkoham previously approved these changes Jan 26, 2022
Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

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

Thank you for adding tests. I think it's more robust now. Maybe there are some things that could be improved that this PR haven't changed, but I think this is fine.
@jakelishman What do you think?

@ikkoham ikkoham added the mod: quantum info Related to the Quantum Info module (States & Operators) label Jan 26, 2022
@ShellyGarion ShellyGarion added this to the 0.20 milestone Feb 2, 2022
@1ucian0 1ucian0 self-assigned this Feb 16, 2022
@1ucian0 1ucian0 added Changelog: Bugfix Include in the "Fixed" section of the changelog on hold Can not fix yet labels Feb 16, 2022
@1ucian0 1ucian0 removed the on hold Can not fix yet label Feb 16, 2022
@1ucian0
Copy link
Member

1ucian0 commented Feb 16, 2022

I just changed the tests to follow the ddt patter. For the rest, I trust @ikkoham with the math. Let's get it merged.

@jakelishman jakelishman changed the title Fix a bug in StatbilizerState expectation_value method Fix phase management in StabilizerState.expectation_value Feb 16, 2022
jakelishman
jakelishman previously approved these changes Feb 16, 2022
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Sorry that I took so long to come back to this.

The tests look much more complete now, thanks - I've got way more confidence that we're doing the right thing in a lot more circumstances, even without in-depth knowledge of the algorithm.

I just have one outstanding comment/question in the algorithm. I'll approve now to save time in case the comment's way off base, but if not, it might be good just to put a shade more explanation at the bit I mentioned to help future reviewers (like me) who aren't 100% certain about the formalism.

qiskit/quantum_info/states/stabilizerstate.py Show resolved Hide resolved
@ShellyGarion
Copy link
Member Author

ShellyGarion commented Feb 17, 2022

@jakelishman - here is the algorithm of the expectation value calculation that we got from Sergey and implemented here and in Aer's stabilizer simulator.

Suppose one needs to compute <psi|P|psi> where P is a Pauli operator (with no phase) and psi is a stabilizer state with stabilizer generators S_1,...,S_n and destabilizer generators D_1,...,D_n.

If P anti-commutes with some stabilizer S_j then <psi|P|psi>=0.

Otherwise, P or -P is an element of the stabilizer group and thus P = (-1)^a S_1^{b_1} S_2^{b_2} ... S_n^{b_n}
for some unknown coefficients a,b_1,...,b_n \in {0,1}.
Note that <psi|P|psi> = (-1)^a since stabilizers act trivially on psi. Thus it suffices to compute a.

Note that b_j=1 iff P anti-commutes with the destabilizer D_j.
Thus we can peel off all stabilizers from P as follows:
for j = 1 to n
if P anti-commutes with D_j then multiply P by S_j
end
Now P = (-1)^a times the identity operator. The desired expected value is (-1)^a.

@jakelishman jakelishman added automerge stable backport potential The bug might be minimal and/or import enough to be port to stable labels Feb 17, 2022
@jakelishman jakelishman modified the milestones: 0.20, 0.19.3 Feb 17, 2022
@jakelishman
Copy link
Member

Thanks, I think I get it all now. I was getting all confused because we were calculating with a counter on the symplectic phase, but at the end we were only either leaving or negating the return value. The idea that it needed to remain real was fine for my intuition, but while I thought that all values of phase (1, -1, i, -i) were possible, I couldn't understand why only one would behave differently to the others. Since i and -i aren't actually possible, I have no problems.

@mergify mergify bot merged commit 6da136c into Qiskit:main Feb 17, 2022
mergify bot pushed a commit that referenced this pull request Feb 17, 2022
* fix a bug in expectation value. Handle Pauli phases.

* extend the test to include Pauli expectation vals with phases.

* add release notes

* add more 1-qubit and 2-qubit tests for exp_val=-1,i,-i

* more ddt

* Fix typo in error message

* Add comment on phase

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 6da136c)
mergify bot added a commit that referenced this pull request Feb 17, 2022
…7675)

* fix a bug in expectation value. Handle Pauli phases.

* extend the test to include Pauli expectation vals with phases.

* add release notes

* add more 1-qubit and 2-qubit tests for exp_val=-1,i,-i

* more ddt

* Fix typo in error message

* Add comment on phase

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 6da136c)

Co-authored-by: Shelly Garion <46566946+ShellyGarion@users.noreply.github.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 mod: quantum info Related to the Quantum Info module (States & Operators) 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.

Stabilizer.expectation_value() does not give the correct result
5 participants