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

Problems in the implementation of Density matrix. #25980

Open
alex180500 opened this issue Dec 12, 2023 · 3 comments · May be fixed by #25984 or #26018
Open

Problems in the implementation of Density matrix. #25980

alex180500 opened this issue Dec 12, 2023 · 3 comments · May be fixed by #25984 or #26018
Assignees

Comments

@alex180500
Copy link

alex180500 commented Dec 12, 2023

In Density.doit() there is a mistake for the _generate_outer_prod() method when returning return Mul(*c_part1)*Mul(*c_part2) * op. The commutative part 2 should be the complex conjugate, so the return should be return Mul(*c_part1)*conjugate(Mul(*c_part2))*op.

Example:

c = sp.Matrix(sp.symbols("c_:2:2", complex=True, real=False)).reshape(2, 2)
state = sum([el * Qubit(f"{bin(count)[2:].zfill(2)}") for count, el in enumerate(c)])

$$c_{00}\ket{00} + c_{01}\ket{01} + c_{10}\ket{10} + c_{11}\ket{11}$$

What should be the density matrix:

rho = state * q.Dagger(state)
q.represent(rho)

$$\left[\begin{matrix}c_{00} \overline{c_{00}} & c_{00} \overline{c_{01}} & c_{00} \overline{c_{10}} & c_{00} \overline{c_{11}}\\c_{01} \overline{c_{00}} & c_{01} \overline{c_{01}} & c_{01} \overline{c_{10}} & c_{01} \overline{c_{11}}\\c_{10} \overline{c_{00}} & c_{10} \overline{c_{01}} & c_{10} \overline{c_{10}} & c_{10} \overline{c_{11}}\\c_{11} \overline{c_{00}} & c_{11} \overline{c_{01}} & c_{11} \overline{c_{10}} & c_{11} \overline{c_{11}}\end{matrix}\right]$$

What the density matrix is using the Density class:

q.represent(Density([state, 1]))

$$\left[\begin{matrix}c_{00}^{2} & c_{00} c_{01} & c_{00} c_{10} & c_{00} c_{11}\\c_{00} c_{01} & c_{01}^{2} & c_{01} c_{10} & c_{01} c_{11}\\c_{00} c_{10} & c_{01} c_{10} & c_{10}^{2} & c_{10} c_{11}\\c_{00} c_{11} & c_{01} c_{11} & c_{10} c_{11} & c_{11}^{2}\end{matrix}\right]$$

@alex180500
Copy link
Author

Other than that also evaluating the partial trace over a Density matrix gets a wrong result (even assuming the wrong matrix):
Example:

rho = Density([state, 1])
q.represent(rho)

$$\left[\begin{matrix}c_{00}^{2} & c_{00} c_{01} & c_{00} c_{10} & c_{00} c_{11}\\c_{00} c_{01} & c_{01}^{2} & c_{01} c_{10} & c_{01} c_{11}\\c_{00} c_{10} & c_{01} c_{10} & c_{10}^{2} & c_{10} c_{11}\\c_{00} c_{11} & c_{01} c_{11} & c_{10} c_{11} & c_{11}^{2}\end{matrix}\right]$$

Partial trace calculates:

q.represent(Tr(rho, [0]).doit())

$$\left[\begin{matrix}c_{00}^{2} + c_{01}^{2} & 0\\0 & c_{10}^{2} + c_{11}^{2}\end{matrix}\right]$$

But instead we should get:

$$\left[\begin{matrix}c_{00}^{2} + c_{01}^{2} & c_{00} c_{10} + c_{01} c_{11}\\c_{00} c_{10} + c_{01} c_{11} & c_{10}^{2} + c_{11}^{2}\end{matrix}\right]$$

@alex180500 alex180500 changed the title Density matrix over a sum of kets is wrong. Problems in the implementation of Density matrix. Dec 13, 2023
amishhaa added a commit to amishhaa/sympy that referenced this issue Dec 14, 2023
This pull request addresses the issue sympy#25980
Where the calculation of density matrix using the density class was incorrect hence the code for returning the correct answer is implemented in this PR.
@amishhaa amishhaa linked a pull request Dec 14, 2023 that will close this issue
@alex180500
Copy link
Author

Other than that also evaluating the partial trace over a Density matrix gets a wrong result (even assuming the wrong matrix):

q.represent(Tr(rho, [0]).doit())

Thanks a lot @amishhaa for the PR, for this other problem I create another issue?

@amishhaa
Copy link

@alex180500 I think it's best to create another issue so it comes to attention faster and can be fixed ASAP.

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