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

Add extract_expression_from_wavefunction to sympy.physics.quantum.state.Wavefunction #26520

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

icarosadero
Copy link

@icarosadero icarosadero commented Apr 20, 2024

References to other Issues or PRs

N/A

Brief description of what is fixed or changed

Added a method on sympy.physics.quantum.state.Wavefunction to replace instances of Wavefunction objects with their internal expressions.

Refer to this stack overflow question for more context: link.

Other comments

N/A

Release Notes

  • physics.quantum
    • Added a new method to help extract internal expressions from Wavefunctions objects.
    • Orthogonal Bra and Ket combinations will output a Kronecker Delta object, as it should.

@sympy-bot
Copy link

sympy-bot commented Apr 20, 2024

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • physics.quantum
    • Added a new method to help extract internal expressions from Wavefunctions objects. (#26520 by @icarosadero)

    • Orthogonal Bra and Ket combinations will output a Kronecker Delta object, as it should. (#26520 by @icarosadero)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.13.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
N/A

#### Brief description of what is fixed or changed
Added a method on `sympy.physics.quantum.state.Wavefunction` to replace instances of `Wavefunction` objects with their internal expressions.

Refer to this stack overflow question for more context: [link](https://stackoverflow.com/q/78356909/6451772).

#### Other comments
N/A

#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* physics.quantum
  * Added a new method to help extract internal expressions from `Wavefunctions` objects.
  * Orthogonal Bra and Ket combinations will output a Kronecker Delta object, as it should.
<!-- END RELEASE NOTES -->

Copy link

github-actions bot commented Apr 22, 2024

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

| Change   | Before [2487dbb5]    | After [95085aaa]    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------|---------------------|---------|----------------------------------------------------------------------|
| -        | 67.7±0.7ms           | 44.3±0.4ms          |    0.65 | integrate.TimeIntegrationRisch02.time_doit(10)                       |
| -        | 66.8±0.6ms           | 43.3±0.3ms          |    0.65 | integrate.TimeIntegrationRisch02.time_doit_risch(10)                 |
| +        | 18.5±0.2μs           | 30.9±0.2μs          |    1.67 | integrate.TimeIntegrationRisch03.time_doit(1)                        |
| -        | 5.41±0.02ms          | 2.83±0.04ms         |    0.52 | logic.LogicSuite.time_load_file                                      |
| -        | 72.2±0.2ms           | 29.3±0.09ms         |    0.41 | polys.TimeGCD_GaussInt.time_op(1, 'dense')                           |
| -        | 25.5±0.06ms          | 16.9±0.07ms         |    0.67 | polys.TimeGCD_GaussInt.time_op(1, 'expr')                            |
| -        | 72.5±0.4ms           | 29.6±0.2ms          |    0.41 | polys.TimeGCD_GaussInt.time_op(1, 'sparse')                          |
| -        | 253±2ms              | 127±0.3ms           |    0.5  | polys.TimeGCD_GaussInt.time_op(2, 'dense')                           |
| -        | 252±0.5ms            | 127±0.6ms           |    0.51 | polys.TimeGCD_GaussInt.time_op(2, 'sparse')                          |
| -        | 652±1ms              | 383±3ms             |    0.59 | polys.TimeGCD_GaussInt.time_op(3, 'dense')                           |
| -        | 648±2ms              | 382±1ms             |    0.59 | polys.TimeGCD_GaussInt.time_op(3, 'sparse')                          |
| -        | 490±7μs              | 286±1μs             |    0.58 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(1, 'dense')            |
| -        | 1.76±0ms             | 1.04±0ms            |    0.59 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(2, 'dense')            |
| -        | 5.81±0.04ms          | 3.10±0.02ms         |    0.53 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(3, 'dense')            |
| -        | 444±7μs              | 230±2μs             |    0.52 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(1, 'dense')               |
| -        | 1.48±0ms             | 699±10μs            |    0.47 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(2, 'dense')               |
| -        | 4.86±0.03ms          | 1.66±0.02ms         |    0.34 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(3, 'dense')               |
| -        | 376±2μs              | 204±2μs             |    0.54 | polys.TimeGCD_SparseGCDHighDegree.time_op(1, 'dense')                |
| -        | 2.45±0.02ms          | 1.23±0ms            |    0.5  | polys.TimeGCD_SparseGCDHighDegree.time_op(3, 'dense')                |
| -        | 10.0±0.08ms          | 4.37±0.02ms         |    0.44 | polys.TimeGCD_SparseGCDHighDegree.time_op(5, 'dense')                |
| -        | 358±0.9μs            | 171±1μs             |    0.48 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(1, 'dense')            |
| -        | 2.50±0.02ms          | 892±5μs             |    0.36 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(3, 'dense')            |
| -        | 9.57±0.1ms           | 2.62±0.01ms         |    0.27 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(5, 'dense')            |
| -        | 1.02±0.01ms          | 425±3μs             |    0.41 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'dense')           |
| -        | 1.71±0.01ms          | 500±2μs             |    0.29 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'sparse')          |
| -        | 5.76±0.06ms          | 1.79±0.01ms         |    0.31 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'dense')           |
| -        | 8.41±0.06ms          | 1.49±0ms            |    0.18 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'sparse')          |
| -        | 282±0.9μs            | 65.2±0.4μs          |    0.23 | polys.TimePREM_QuadraticNonMonicGCD.time_op(1, 'sparse')             |
| -        | 3.40±0.03ms          | 399±2μs             |    0.12 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'dense')              |
| -        | 3.98±0.03ms          | 277±0.7μs           |    0.07 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'sparse')             |
| -        | 6.92±0.03ms          | 1.27±0.01ms         |    0.18 | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'dense')              |
| -        | 8.51±0.06ms          | 851±10μs            |    0.1  | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'sparse')             |
| -        | 4.98±0.02ms          | 2.96±0.01ms         |    0.59 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(2, 'sparse') |
| -        | 12.2±0.1ms           | 6.53±0.03ms         |    0.54 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'dense')  |
| -        | 22.2±0.08ms          | 8.97±0.01ms         |    0.4  | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'sparse') |
| -        | 5.23±0.03ms          | 858±4μs             |    0.16 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(1, 'sparse')    |
| -        | 12.4±0.07ms          | 6.98±0.02ms         |    0.56 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(2, 'sparse')    |
| -        | 102±0.5ms            | 25.5±0.1ms          |    0.25 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'dense')     |
| -        | 168±2ms              | 53.7±0.3ms          |    0.32 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'sparse')    |
| -        | 172±2μs              | 112±0.5μs           |    0.65 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'dense')      |
| -        | 363±2μs              | 215±0.9μs           |    0.59 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'sparse')     |
| -        | 4.19±0.04ms          | 838±1μs             |    0.2  | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'dense')      |
| -        | 5.29±0.04ms          | 382±1μs             |    0.07 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'sparse')     |
| -        | 19.8±0.1ms           | 2.78±0.01ms         |    0.14 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'dense')      |
| -        | 22.6±0.2ms           | 622±2μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'sparse')     |
| -        | 475±1μs              | 135±0.9μs           |    0.29 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(1, 'sparse') |
| -        | 4.58±0.02ms          | 605±2μs             |    0.13 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'dense')  |
| -        | 5.26±0.02ms          | 141±0.7μs           |    0.03 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'sparse') |
| -        | 13.5±0.05ms          | 1.30±0ms            |    0.1  | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'dense')  |
| -        | 13.9±0.2ms           | 142±0.5μs           |    0.01 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'sparse') |
| -        | 131±0.4μs            | 75.3±0.3μs          |    0.57 | solve.TimeMatrixOperations.time_rref(3, 0)                           |
| -        | 256±2μs              | 88.3±0.2μs          |    0.34 | solve.TimeMatrixOperations.time_rref(4, 0)                           |
| -        | 23.9±0.2ms           | 10.2±0.03ms         |    0.43 | solve.TimeSolveLinSys189x49.time_solve_lin_sys                       |
| -        | 28.6±0.3ms           | 15.5±0.1ms          |    0.54 | solve.TimeSparseSystem.time_linsolve_Aaug(20)                        |
| -        | 54.3±0.3ms           | 25.0±0.1ms          |    0.46 | solve.TimeSparseSystem.time_linsolve_Aaug(30)                        |
| -        | 28.0±0.1ms           | 15.2±0.07ms         |    0.54 | solve.TimeSparseSystem.time_linsolve_Ab(20)                          |
| -        | 54.5±0.2ms           | 24.6±0.1ms          |    0.45 | solve.TimeSparseSystem.time_linsolve_Ab(30)                          |

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@@ -1266,4 +1266,4 @@ Augusto Borges <borges.augustoar@gmail.com>
Han Wei Ang <ang.h.w@u.nus.edu>
Pablo <48098178+PabloRuizCuevas@users.noreply.github.com>
Congxu Yang <u7189828@anu.edu.au>
Saicharan <62512681+saicharan2804@users.noreply.github.com>
Saicharan <62512681+saicharan2804@users.noreply.github.com>
Copy link
Contributor

Choose a reason for hiding this comment

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

The AUTHORS file should not be edited.

@@ -665,7 +666,7 @@ def _eval_innerproduct(self, bra, **hints):
return S.Zero # i.e. Integer(0)

if is_zero is None:
return None
return KroneckerDelta(arg, bra_arg)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure about this.

Copy link
Author

Choose a reason for hiding this comment

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

The point of this is that $\langle i | j \rangle$ should be simplified to the Kronecker Delta for the case where they are orthonormal. In fact, maybe this entire method should be just a wrapper to the Kronecker Delta function. Leaving it as simply $\langle i | j \rangle$ is very annoying to perform summation over because you would have to replace it with $\delta_{ij}$ anyway to further simplify.

@@ -909,6 +910,50 @@ def expr(self):
"""
return self._args[0]

@staticmethod
def extract_expression_from_wavefunction(input_expression):
Copy link
Contributor

Choose a reason for hiding this comment

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

This function does not seem to be used anywhere.

Copy link
Author

Choose a reason for hiding this comment

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

I added this as a handicap to extract the expression from the wavefunction objects. In the current state, simply doing .expr on a linear combination of wavefunction objects yields an error.

@oscarbenjamin
Copy link
Contributor

I don't quite understand the purpose of this PR but tests should be added for any new functionality.

@icarosadero
Copy link
Author

icarosadero commented Apr 25, 2024

I don't quite understand the purpose of this PR but tests should be added for any new functionality.

The purpose of this PR is to improve the usability of the affected classes and make the package less infuriating to work with. Will add a test for them soon.

@oscarbenjamin
Copy link
Contributor

I don't quite understand the purpose of this PR but tests should be added for any new functionality.

The purpose of this PR is to improve the usability of the affected classes and make the package less infuriating to work with.

This does not explain at all what the purpose of this PR is. If you want these changes to be considered you will need to explain their purpose.

Please understand that I do not know the quantum module myself (it is unmaintained).

@icarosadero
Copy link
Author

icarosadero commented Apr 28, 2024

I don't quite understand the purpose of this PR but tests should be added for any new functionality.

The purpose of this PR is to improve the usability of the affected classes and make the package less infuriating to work with.

This does not explain at all what the purpose of this PR is. If you want these changes to be considered you will need to explain their purpose.

Please understand that I do not know the quantum module myself (it is unmaintained).

OthogonalKet -> OrhtonormalKet

When you have an orthonormal basis of Kets $\{|j\rangle\}$ over $j$, their inner product $\langle j|k \rangle$ is just $\delta_{jk}$. Also, notice that $\langle j|j+1 \rangle$ is just a special case.

Wavefunction

I added that method so that it is easier to convert a Wavefunction object to an ordinary Function object.

Let me know if you need further clarification

@icarosadero
Copy link
Author

Community is more important than code.
ERROR: Failed to import 'sympy.parsing.latex._parse_latex_antlr'.
ERROR: Failed to import 'sympy.parsing.tests.test_latex_lark'.
ERROR: Failed to import 'sympy.physics.optics'.
ERROR: Failed to import 'sympy.physics.paulialgebra'.
ERROR: Failed to import 'sympy.physics.quantum'.
ERROR: Failed to import 'sympy.physics.tests.test_paulialgebra'.
ERROR: Failed to import 'sympy.printing.tests.test_latex'.
ERROR: Failed to import 'sympy.printing.tests.test_mathml'.
ERROR: Failed to import 'sympy.utilities.tests.test_pickling'.
ERROR: Failed to import 'sympy.utilities.tests.test_wester'.
Oh no, found some problems!

No idea what this is about

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants