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 SymbolicHamiltonian.expectation for different nqubits #951

Merged
merged 8 commits into from
Jun 28, 2023

Conversation

stavros11
Copy link
Member

@stavros11 stavros11 commented Jun 24, 2023

Fixes #935. Specifically:

  • nqubits attribute was added to SymbolicHamiltonian as suggested in Wrong results using SymbolicHamiltonian #935 (comment). Note that the effect of this is still equivalent to padding, for example SymbolicHamiltonian(Z(0) * Z(1), nqubits=4) is equivalent to SymbolicHamiltonian(Z(0) * Z(1) * I(2) * I(3)).
  • A ValueError is raised if there is a mismatch between the number of qubits in the state and the Hamiltonian in expectation value calculations.
  • A documentation example about expectation_from_samples was added (@AlejandroSopena let me know if that is okay).

Checklist:

  • Reviewers confirm new code works as expected.
  • Tests are passing.
  • Coverage does not decrease.
  • Documentation is updated.

@stavros11 stavros11 requested review from scarrazza and igres26 June 24, 2023 09:37
@codecov
Copy link

codecov bot commented Jun 24, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (05e2d3c) 100.00% compared to head (23ce101) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #951   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           49        49           
  Lines         6905      6906    +1     
=========================================
+ Hits          6905      6906    +1     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/qibo/hamiltonians/hamiltonians.py 100.00% <100.00%> (ø)
src/qibo/hamiltonians/models.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Co-authored-by: Sergi Ramos <[email protected]>
@AlejandroSopena AlejandroSopena self-requested a review June 25, 2023 23:32
Copy link
Contributor

@AlejandroSopena AlejandroSopena left a comment

Choose a reason for hiding this comment

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

Thanks @stavros11 for adding the example. The tests are not passing because expectation_from_samples() only works when the observable is diagonal in the z basis as you mentioned, and XXZ() is not. I have pending to generalize this function #852 and I will do it at some point in August.

doc/source/code-examples/advancedexamples.rst Outdated Show resolved Hide resolved
@AlejandroSopena AlejandroSopena self-requested a review June 27, 2023 23:01
Copy link
Contributor

@AlejandroSopena AlejandroSopena left a comment

Choose a reason for hiding this comment

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

Thanks. Just a few more comments. When adding the three CNOTs, you have to replace c --> circuit To use expectation_from_samples() we have to add measurement gates because the expected value is calculated from frequencies.

doc/source/code-examples/advancedexamples.rst Outdated Show resolved Hide resolved
doc/source/code-examples/advancedexamples.rst Outdated Show resolved Hide resolved
@stavros11
Copy link
Member Author

Thanks for the fixes @AlejandroSopena. I had them locally in my computer but forgot to push. I hope it is okay now.

@AlejandroSopena AlejandroSopena self-requested a review June 28, 2023 09:14
@scarrazza scarrazza merged commit ccc6fe0 into master Jun 28, 2023
@stavros11 stavros11 deleted the fixsymbolic branch July 11, 2023 07:07
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.

Wrong results using SymbolicHamiltonian
4 participants