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

Symbolic Hamiltonian with constants #1389

Merged
merged 4 commits into from
Jul 16, 2024
Merged

Conversation

alecandido
Copy link
Member

@alecandido alecandido commented Jul 12, 2024

Fixes #1383

Checklist:

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

@alecandido alecandido requested a review from mho291 July 12, 2024 10:39
@alecandido
Copy link
Member Author

@mho291 I started adding a minimal test that reproduces your issue.

I'm now going to add the patch that should make that test passing. Whenever you have time, confirm that the test is close enough to what you expect (then I will also ask to check the full fix).

Copy link

codecov bot commented Jul 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.84%. Comparing base (98a310a) to head (b51190c).
Report is 26 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1389      +/-   ##
==========================================
- Coverage   99.84%   99.84%   -0.01%     
==========================================
  Files          76       76              
  Lines       10844    10843       -1     
==========================================
- Hits        10827    10826       -1     
  Misses         17       17              
Flag Coverage Δ
unittests 99.84% <100.00%> (-0.01%) ⬇️

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

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

@alecandido alecandido requested a review from stavros11 July 12, 2024 16:53
@alecandido alecandido marked this pull request as ready for review July 12, 2024 16:53
@alecandido
Copy link
Member Author

For the time being, it's a quick patch (plus some docstrings beautifications).

I wanted to refactor a bit more the function. But I took a look at the rest of the module, and there are many things to refactor. So, it's not worth to do it now, better to allocate some time and do it consistently.

@@ -566,8 +568,8 @@ def expectation_from_samples(self, freq, qubit_map=None):
if subk.count(1) % 2 == 1:
expval_k = -1
expval_q += expval_k * counts[i]
expval += expval_q * float(coeff[j])
return expval
expval += expval_q * self.terms[j].coefficient.real
Copy link
Member Author

@alecandido alecandido Jul 12, 2024

Choose a reason for hiding this comment

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

Any clue why SymbolicTerm coefficient are stored as complex values?

self.coefficient = complex(coefficient)

@andrea-pasquale?

O.e. I believe they are terms in a Hamiltonian, that is hermitian, and I imagine in general a superposition of other Hermitian terms, with real coefficients.
Though you could also have anti-hermitian with imaginary coefficients, or sums with the Hermitian conjugate. So, maybe it's just here that, since terms made of only Z are required, the coefficients are expected to be real...

Copy link
Member

Choose a reason for hiding this comment

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

I think terms here may not necessarily be hermitian. A term is a product of symbols and it happens that the symbols we provide are the Paulis which are all hermitian, however the user can also define their own symbol using custom matrix. So there may be a case where you have non-hermitian terms and complex coefficients resulting to a hermitian Hamiltonian.

The constant, on the other hand, probably needs to be real in every case, since it is multiplying the idenity that is hermitian.

@mho291
Copy link
Contributor

mho291 commented Jul 16, 2024

Thanks @alecandido ! I have checked the code and verified it works. Will approve the changes.

@alecandido alecandido added this pull request to the merge queue Jul 16, 2024
Merged via the queue into master with commit 0be8f40 Jul 16, 2024
27 checks passed
@scarrazza scarrazza deleted the symbolic-with-constant branch August 3, 2024 07:31
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.

Expectation for observable does not take into account constants
3 participants