Skip to content

Fidelity of quantum states #1459

Merged
merged 8 commits into from Aug 11, 2012

7 participants

@gdevanla
gdevanla commented Aug 3, 2012

This PR implements the basic functionality to compute fidelity on quantum states.

@flacjacket and @ellisonbg please review.

@travisbot

This pull request fails (merged 30311c8 into 9625918).

@travisbot

This pull request fails (merged 82e3be2 into 9625918).

@Krastanov
SymPy member

SymPy Bot Summary: :red_circle: There were test failures.

@gdevanla: Please fix the test failures.

Test command: setup.py test
master hash: 9625918
branch hash: 82e3be2

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY_ZMjDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYqo8iDA

Interpreter 3: :red_circle: There were test failures.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY5_QiDA

Build HTML Docs: :eight_spoked_asterisk: All tests have passed.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYju0iDA

Automatic review by SymPy Bot.

@certik certik and 1 other commented on an outdated diff Aug 3, 2012
sympy/physics/quantum/tests/test_density.py
+ state4 = (sqrt(S(2)/3))*state1 + (1/sqrt(3))*state2
+
+ state1_dm = Density([state1, 1])
+ state2_dm = Density([state2, 1])
+ state3_dm = Density([state3, 1])
+
+ assert fidelity(state1_dm, state1_dm) == 1
+ assert fidelity(state1_dm, state2_dm) == 0
+ assert fidelity(state1_dm, state3_dm) == 1/sqrt(2)
+ assert fidelity(state3_dm, state2_dm) == 1/sqrt(2)
+
+ #using qubits/density(mixed states)
+ d1 = Density([state3, 0.70], [state4, 0.30])
+ d2 = Density([state3, 0.20], [state4, 0.80])
+ assert fidelity(d1, d1).round(3) == 1
+ assert fidelity(d1, d2).round(3) == 0.996
@certik
SymPy member
certik added a note Aug 3, 2012

This is wrong. You can't compare floating point numbers like that. You need to do it the following way:

abs(x-y) < 1e-3

Unless there is something that I am missing.

@gdevanla
gdevanla added a note Aug 3, 2012

Nope, my mistake. I was supposed to replace that with abs once I finished comparing some of my results with what QuTip was producing. Will fix it, now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@certik certik commented on an outdated diff Aug 3, 2012
sympy/physics/quantum/tests/test_density.py
+
+ state1_dm = Density([state1, 1])
+ state2_dm = Density([state2, 1])
+ state3_dm = Density([state3, 1])
+
+ assert fidelity(state1_dm, state1_dm) == 1
+ assert fidelity(state1_dm, state2_dm) == 0
+ assert fidelity(state1_dm, state3_dm) == 1/sqrt(2)
+ assert fidelity(state3_dm, state2_dm) == 1/sqrt(2)
+
+ #using qubits/density(mixed states)
+ d1 = Density([state3, 0.70], [state4, 0.30])
+ d2 = Density([state3, 0.20], [state4, 0.80])
+ assert fidelity(d1, d1).round(3) == 1
+ assert fidelity(d1, d2).round(3) == 0.996
+ assert fidelity(d1, d2).round(3) == fidelity(d2, d1).round(3)
@certik
SymPy member
certik added a note Aug 3, 2012

The same here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@certik certik commented on an outdated diff Aug 3, 2012
sympy/physics/quantum/tests/test_density.py
+ state3 = (1/sqrt(2))*state1 + (1/sqrt(2))*state2
+ state4 = (sqrt(S(2)/3))*state1 + (1/sqrt(3))*state2
+
+ state1_dm = Density([state1, 1])
+ state2_dm = Density([state2, 1])
+ state3_dm = Density([state3, 1])
+
+ assert fidelity(state1_dm, state1_dm) == 1
+ assert fidelity(state1_dm, state2_dm) == 0
+ assert fidelity(state1_dm, state3_dm) == 1/sqrt(2)
+ assert fidelity(state3_dm, state2_dm) == 1/sqrt(2)
+
+ #using qubits/density(mixed states)
+ d1 = Density([state3, 0.70], [state4, 0.30])
+ d2 = Density([state3, 0.20], [state4, 0.80])
+ assert fidelity(d1, d1).round(3) == 1
@certik
SymPy member
certik added a note Aug 3, 2012

And the same here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@travisbot

This pull request passes (merged 0eea679 into 9625918).

@Krastanov
SymPy member

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed.

Test command: setup.py test
master hash: 9625918
branch hash: 0eea679

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY_pMjDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY4_8hDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYj-0iDA

Build HTML Docs: :eight_spoked_asterisk: All tests have passed.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYmowjDA

Automatic review by SymPy Bot.

@certik certik commented on an outdated diff Aug 4, 2012
sympy/physics/quantum/density.py
+ arg1, arg2 : a density matrix or Matrix
+
+
+ Examples:
+ =========
+
+ >>> from sympy import S, sqrt
+ >>> from sympy.physics.quantum.dagger import Dagger
+ >>> from sympy.physics.quantum.spin import JzKet
+ >>> from sympy.physics.quantum.density import Density, fidelity
+ >>> from sympy.physics.quantum.represent import represent
+ >>>
+ >>> up = JzKet(S(1)/2,S(1)/2)
+ >>> down = JzKet(S(1)/2,-S(1)/2)
+ >>> amp = 1/sqrt(2)
+ >>> updown = (amp * up ) + (amp * down)
@certik
SymPy member
certik added a note Aug 4, 2012

You seem to have an extra space in (amp * up ). (This is minor.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@certik certik and 2 others commented on an outdated diff Aug 4, 2012
sympy/physics/quantum/density.py
+ >>> from sympy import S, sqrt
+ >>> from sympy.physics.quantum.dagger import Dagger
+ >>> from sympy.physics.quantum.spin import JzKet
+ >>> from sympy.physics.quantum.density import Density, fidelity
+ >>> from sympy.physics.quantum.represent import represent
+ >>>
+ >>> up = JzKet(S(1)/2,S(1)/2)
+ >>> down = JzKet(S(1)/2,-S(1)/2)
+ >>> amp = 1/sqrt(2)
+ >>> updown = (amp * up ) + (amp * down)
+ >>>
+ >>> # represent turns Kets into matrices
+ >>> up_dm = represent(up * Dagger(up))
+ >>> down_dm = represent(down * Dagger(down))
+ >>> updown_dm = represent(updown * Dagger(updown))
+ >>> updown2 = (sqrt(3)/2 )* up + (1/2)*down
@certik
SymPy member
certik added a note Aug 4, 2012

I would format (sqrt(3)/2 )* up as (sqrt(3)/2) * up.

@flacjacket
SymPy member
flacjacket added a note Aug 9, 2012

I agree with Ondrej, but you don't ever use updown2

@certik
SymPy member
certik added a note Aug 10, 2012

@gdevanla, what's up with this? Should updown2 be removed?

@gdevanla
gdevanla added a note Aug 10, 2012

@certik I missed that comment in my earlier push. Will address that soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@certik
SymPy member
certik commented Aug 4, 2012

Otherwise it looks good. After you fix it, I'll merge it.

@asmeurer asmeurer commented on an outdated diff Aug 4, 2012
sympy/physics/quantum/density.py
@@ -254,3 +253,73 @@ def entropy(density):
return -np.sum(eigvals*np.log(eigvals))
else:
raise ValueError("numpy.ndarray, scipy.sparse or sympy matrix expected")
+
+
+def fidelity(arg1, arg2):
@asmeurer
SymPy member
asmeurer added a note Aug 4, 2012

Maybe call these state1 and state2. That way the user can at least guess what the arguments should be from the function signature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@asmeurer asmeurer commented on an outdated diff Aug 4, 2012
sympy/physics/quantum/density.py
+ >>> # represent turns Kets into matrices
+ >>> up_dm = represent(up * Dagger(up))
+ >>> down_dm = represent(down * Dagger(down))
+ >>> updown_dm = represent(updown * Dagger(updown))
+ >>> updown2 = (sqrt(3)/2 )* up + (1/2)*down
+ >>>
+ >>> print fidelity(up_dm, up_dm)
+ 1
+ >>> print fidelity(up_dm, down_dm) #orthogonal states
+ 0
+ >>> print fidelity(up_dm, updown_dm).evalf().round(3)
+ 0.707
+
+
+ """
+ def sqrtm(mat):
@asmeurer
SymPy member
asmeurer added a note Aug 4, 2012

Matrix**Rational(1, 2) does exactly this already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ellisonbg
SymPy member

One other thing we should add that is fidelity related. By taking the inverse cos of the fidelity you get an angle that is a metric on state space. I am not sure what to call this angle (might need to do some background research on it). A few details can be found on the wikipedia page for quantum fidelity.

@gdevanla
gdevanla commented Aug 6, 2012

@ellisonbg I am assuming you are referring to the 'Bures angle/distance', described in here : http://en.wikipedia.org/wiki/Bures_metric.

@travisbot

This pull request fails (merged ac679d1 into 9625918).

@Krastanov
SymPy member

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed.

Test command: setup.py test
master hash: 3c2c3c7
branch hash: ac679d1

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYvd0iDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYv7YiDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYjrMjDA

Build HTML Docs: :eight_spoked_asterisk: All tests have passed.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYm-0iDA

Automatic review by SymPy Bot.

@flacjacket flacjacket commented on an outdated diff Aug 9, 2012
sympy/physics/quantum/density.py
+ >>> from sympy.physics.quantum.spin import JzKet
+ >>> from sympy.physics.quantum.density import Density, fidelity
+ >>> from sympy.physics.quantum.represent import represent
+ >>>
+ >>> up = JzKet(S(1)/2,S(1)/2)
+ >>> down = JzKet(S(1)/2,-S(1)/2)
+ >>> amp = 1/sqrt(2)
+ >>> updown = (amp * up) + (amp * down)
+ >>>
+ >>> # represent turns Kets into matrices
+ >>> up_dm = represent(up * Dagger(up))
+ >>> down_dm = represent(down * Dagger(down))
+ >>> updown_dm = represent(updown * Dagger(updown))
+ >>> updown2 = (sqrt(3)/2 )* up + (1/2)*down
+ >>>
+ >>> print fidelity(up_dm, up_dm)
@flacjacket
SymPy member
flacjacket added a note Aug 9, 2012

print is unnecessary here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@flacjacket flacjacket commented on an outdated diff Aug 9, 2012
sympy/physics/quantum/density.py
+ >>> updown2 = (sqrt(3)/2 )* up + (1/2)*down
+ >>>
+ >>> print fidelity(up_dm, up_dm)
+ 1
+ >>> print fidelity(up_dm, down_dm) #orthogonal states
+ 0
+ >>> print fidelity(up_dm, updown_dm).evalf().round(3)
+ 0.707
+
+ """
+ state1 = represent(state1) if isinstance(state1, Density) else state1
+ state2 = represent(state2) if isinstance(state2, Density) else state2
+
+ if (not isinstance(state1, Matrix) or
+ not isinstance(state2, Matrix)):
+ raise ValueError("state1 and state2 must be of type density or Matrix "
@flacjacket
SymPy member
flacjacket added a note Aug 9, 2012

Uppercase Density

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@flacjacket flacjacket commented on an outdated diff Aug 9, 2012
sympy/physics/quantum/density.py
+ >>> print fidelity(up_dm, down_dm) #orthogonal states
+ 0
+ >>> print fidelity(up_dm, updown_dm).evalf().round(3)
+ 0.707
+
+ """
+ state1 = represent(state1) if isinstance(state1, Density) else state1
+ state2 = represent(state2) if isinstance(state2, Density) else state2
+
+ if (not isinstance(state1, Matrix) or
+ not isinstance(state2, Matrix)):
+ raise ValueError("state1 and state2 must be of type density or Matrix "
+ "received type=%s for state1 and type=%s for state2" %
+ (type(state1), type(state2)))
+
+ if ( state1.shape != state2.shape and state1.rows == state2.cols):
@flacjacket
SymPy member
flacjacket added a note Aug 9, 2012

state1.rows != state2.cols
Though you could also use Matrix.is_square

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@flacjacket
SymPy member

This is definitely out of my wheelhouse, but I can at least try to comment on more syntax related improvements.

@Krastanov
SymPy member

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed.

Test command: setup.py test
master hash: dd8352a
branch hash: 5f0bd5d

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYi_UiDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYg5wjDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYivUiDA

Build HTML Docs: :eight_spoked_asterisk: All tests have passed.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYyd0iDA

Automatic review by SymPy Bot.

@gdevanla

I believe I have addressed all comments in this PR. except for the one by @ellisonbg regarding the metric. But that change can go with a follow-up PR and need not be part of this.

@ellisonbg
SymPy member

OK I think we can merge this.

@ellisonbg ellisonbg merged commit 3b77c94 into sympy:master Aug 11, 2012

1 check was pending

Details default The Travis build is in progress
@travisbot

This pull request passes (merged 02e7e5c into 9625918).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.