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

Compute variance and standard error #238

Merged
merged 6 commits into from
Oct 26, 2022

Conversation

jjgoings
Copy link
Contributor

Adds functionality to compute the variance and standard error of an expectation value.

  • Adds get_variance function to Simulator to compute the variance of expectation value
  • Adds get_standard_error function to Simulator to compute the standard error from the mean (shot-dependent)
  • Adds simple analytical test to check variance is computed correctly

@jjgoings jjgoings marked this pull request as ready for review October 22, 2022 00:40
@jjgoings jjgoings changed the title Compute variance Compute variance and standard error Oct 22, 2022
Copy link
Contributor

@alexfleury-sb alexfleury-sb left a comment

Choose a reason for hiding this comment

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

I just left one comment in the thread. Beside this, I am all good with the code (I will let you make changes if necessary and mark it as approved soon after that)!

tangelo/linq/simulator_base.py Outdated Show resolved Hide resolved
@jjgoings
Copy link
Contributor Author

@AlexandreF-1qbit should be good now! Tests for both n_shots = None and n_shots = 10**5 should pass.

Copy link
Contributor

@ValentinS4t1qbit ValentinS4t1qbit 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 your PR Joshua, really appreciate it ! You did a great job with adopting our coding guidelines and seemed to have found the right place to add your feature.

I found some minor things, that should be fairly easy to resolve / clean up.

There's code that may be redundant (reused from expectation value functions) but I'm not sure yet whether we should refactor this. For now I think it's better to address what has been brought up and resolve ongoing conversations: then it may become more obvious.

tangelo/linq/tests/test_simulator.py Outdated Show resolved Hide resolved
tangelo/linq/simulator_base.py Show resolved Hide resolved
tangelo/linq/simulator_base.py Outdated Show resolved Hide resolved
tangelo/linq/simulator_base.py Show resolved Hide resolved
tangelo/linq/simulator_base.py Outdated Show resolved Hide resolved
tangelo/linq/simulator_base.py Outdated Show resolved Hide resolved
tangelo/linq/simulator_base.py Show resolved Hide resolved
tangelo/linq/simulator_base.py Outdated Show resolved Hide resolved
tangelo/linq/tests/test_simulator.py Show resolved Hide resolved
tangelo/linq/tests/test_simulator.py Outdated Show resolved Hide resolved
@ValentinS4t1qbit
Copy link
Contributor

ValentinS4t1qbit commented Oct 25, 2022

@AlexandreF-1qbit should be good now! Tests for both n_shots = None and n_shots = 10**5 should pass.

Dammit, I just gave you a review but it may mean it was given on code that's now outdated. Hopefully the stuff is still relevant. I allowed automated tests to run, see "checks".

@jjgoings
Copy link
Contributor Author

jjgoings commented Oct 26, 2022

@ValentinS4t1qbit I believe I addressed all your points -- thanks! Let me know if there is anything more I can do.
I wanted to bring up two points for your consideration:

  1. Regarding refactoring, I totally agree this code is very similar to the get_expectation, as that's what I used as a template. I wonder if in the future we could have a function like get_moment where you can pass which moment you want to compute for mean, variance, skewness, etc.
  2. The functions here for variance assumes that covariance between Pauli products is zero. This is true if each term of a Hamiltonian is measured with an independent circuit. But when you group terms to reduce the number of circuits, this is not necessarily true. Depending on how you group the terms, this can make the variance increase (if Cov(Pa,Pb) > 0) or decrease (Cov(Pa,Pb) < 0). I'm not sure how to handle this, because it seems like a case-by-case basis? One fascinating pathological example was given in Sec B.2 of this paper (starts on page 14), also related in Sec IX.A in this paper. Turns out that although you can partition the expectation into two measurements, it will still require more shots (33% more) to achieve the same precision as the seemingly sub-optimal three measurement partitioning. Anyway, don't really have a suggestion here other than to be aware and maybe in the future we can support computing variances given a QWC group in post-processing.

Copy link
Contributor

@ValentinS4t1qbit ValentinS4t1qbit 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 contributing this feature to Tangelo Josh, we really appreciate it.
Congratulations on your 1st PR to this project !

@ValentinS4t1qbit ValentinS4t1qbit merged commit a7032a9 into goodchemistryco:develop Oct 26, 2022
@jjgoings jjgoings deleted the compute-variance branch October 26, 2022 22:01
ValentinS4t1qbit added a commit that referenced this pull request Nov 9, 2022
* Formula for estimating n_measurements for exp value to given accuracy updated with simpler form. (#192)
* Fix operator_expectation bug qith qubit operator / hamiltonian. (#191)
* iqcc ilc solver fork (#179)
* Make circuit iterable (#194)
* copy method for circuits + get_resources adjusted for some variations of VQE + invertible swap gate fix (#193)
* Release guide for project maintainers + version string (#195)
* Notebook: Important aspects of QChem modelling for QComputing (#199)
* Notebooks enabled for Google colab ("No local install" needed) (#198)
* Docker fix (#200)
* Excited states (#197)
* Translation function for qubit operators (#196)
* c2v axis definition changed (#205)
* Bidirectional conversion for Qiskit quantum circuits (#203)
* QEMISTClientConnection class (#207)
* Add n_qubits option to MultiformOperator compress function (#209)
* Quantum Signal Processing time-evolution (#204)
* added even trotter order > 2 (#210)
* Fixed number of qubits with scBK in ADAPT. (#214)
* Export coverage output file in github actions (#158)
* JKMN vector prep should return np array of int. (#215)
* Support for additional IonQ gates (#212)
* Remove unused n_min_orbitals attribute. (#218)
* Change JKMN Majorana definition so mapped Hamiltonian is real (#219)
* Relaxing condition fot iQCC-ILC test. (#221)
* Histogram class (#217)
* Compute energy from RDMs with FermionOperator (#220)
* IonQConnection: control update prints / verbosity. (#225)
* Refactor simulator (#213)
* added __repr__ method to Gate (#229)
* New bidirectional circuit translation functions and interface (#223)
* Add circuit.depth() in get_resources methods (#235)
* Variance and standard error of an expectation value (#238)
* VQESolver cleanup (#230)
* TETRIS-ADAPT-VQE (#241)
* TETRISADAPTSolver implementation + #224 fix
* Trotter order and active electrons for (i)QCC (#234)
* Symmetry post-selection (#232)
* Qiskit runtime integration (IBM quantum only) (#222)
* Add multi-control support for trotterization (#244)
* Compatibility with new QCloud outputs. (#246)
* Deprecate Simulator, introduce get_backend as replacement (#245)
* compute_rdms function (#228)
* Expanded FermionOperator attributes, defined logical and mathematical operations on it (add, mul, eq, etc)
* Bumping Tangelo version number in _version.py
* Update CHANGELOG.md

Co-authored-by: Valentin Senicourt <41597680+ValentinS4t1qbit@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: AlexandreF-1qbit <76115575+AlexandreF-1qbit@users.noreply.github.com>
Co-authored-by: James Brown <james.brown@1qbit.com>
Co-authored-by: JamesB-1qbit <84878946+JamesB-1qbit@users.noreply.github.com>
Co-authored-by: MPCoons <84400409+MPCoons@users.noreply.github.com>
Co-authored-by: elloyd-1qbit <58313607+elloyd-1qbit@users.noreply.github.com>
Co-authored-by: KrzysztofB-1qbit <86750444+KrzysztofB-1qbit@users.noreply.github.com>
Co-authored-by: Rudi Plesch <rudi.plesch@1qbit.com>
Co-authored-by: GitHub Actions <noreply@github.com>
Co-authored-by: Valentin Senicourt <valentin.senicourt@1qbit.com>
Co-authored-by: Joshua Goings <3915169+jjgoings@users.noreply.github.com>
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.

None yet

3 participants