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

Fix spin op bug #2175

Closed
wants to merge 20 commits into from
Closed

Fix spin op bug #2175

wants to merge 20 commits into from

Conversation

sacpis
Copy link
Collaborator

@sacpis sacpis commented Aug 29, 2024

  • Fixing the phase calculation in spin_op multiplication operator
  • Removing terms with 0 coefficient
  • Adding unittests for all combinations of Pauli matrices multiplication

The output follows the Openfermion's implementation of Jordan Wigner transformation (mapping fermion operators to spin operators) implementation. (Above comment and following code examples by @amccaskey)

p = 0 
q = 2 
one_elec = openfermion.FermionOperator(((q,1),(p,0)))-openfermion.FermionOperator(((p,1),(q,0)))
print(openfermion.jordan_wigner(one_elec))

-0.5j [X0 Z1 Y2] +
0.5j [Y0 Z1 X2]

This is how @amccaskey is implementing Wigner transformation using CUDA-Q

#include "cudaq/spin_op.h"

using namespace cudaq; 

spin_op adag(std::size_t numQubits, std::size_t p) {
  spin_op zprod;
  for (std::size_t k = 0; k < p; k++)
    zprod *= spin::z(k);
  return 0.5 * zprod * (spin::x(p) - std::complex<double>{0, 1} * spin::y(p));
}

spin_op a(std::size_t numQubits, std::size_t q) {
  spin_op zprod;
  for (std::size_t k = 0; k < q; k++)
    zprod *= spin::z(k);
  return 0.5 * zprod * (spin::x(q) + std::complex<double>{0, 1} * spin::y(q));
}


int main() {
  std::size_t p = 0;
  std::size_t q = 2;

  auto op = adag(4, q) * a(4, p) - adag(4, p) * a(4, q);
  op.dump();
}

output

root@e57345e57788:/workspaces/cuda-quantum# nvq++ examples/cpp/other/test.cpp -o a.out && ./a.out
[0-0.5j] XZY
[0+0.5j] YZX

ctest result

100% tests passed, 0 tests failed out of 644

Label Time Summary:
gpu_required    =  88.55 sec*proc (367 tests)

Total Test time (real) = 1061.41 sec

Test result for tetsing done with code in Bettina's branch

# With block commented (in expressions)

pauliX(0) * (pauliZ(0) + pauliZ(1)): 

[[ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 2.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j -2.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]]

(pauliZ(0) + pauliZ(1)) * pauliX(0): 

[[ 0.+0.j  2.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j -2.+0.j  0.+0.j]]


# Without block commented (in expressions)

pauliX(0) * (pauliZ(0) + pauliZ(1)): 

[[ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 2.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j -2.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]]

(pauliZ(0) + pauliZ(1)) * pauliX(0): 

[[ 0.+0.j  2.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j -2.+0.j  0.+0.j]]

Tests from Marwa's script

Openfermion

a_p a^dagger_q
1.0 [3 7^]
(-0.25+0j) [X3 Z4 Z5 Z6 X7] +
0.25j [X3 Z4 Z5 Z6 Y7] +
-0.25j [Y3 Z4 Z5 Z6 X7] +
(-0.25+0j) [Y3 Z4 Z5 Z6 Y7]


a_q a^dagger_p
1.0 [7^ 3]
(0.25+0j) [X3 Z4 Z5 Z6 X7] +
-0.25j [X3 Z4 Z5 Z6 Y7] +
0.25j [Y3 Z4 Z5 Z6 X7] +
(0.25+0j) [Y3 Z4 Z5 Z6 Y7]

a_p a^dagger_q
1.0 [3^ 7]
(0.25+0j) [X3 Z4 Z5 Z6 X7] +
0.25j [X3 Z4 Z5 Z6 Y7] +
-0.25j [Y3 Z4 Z5 Z6 X7] +
(0.25+0j) [Y3 Z4 Z5 Z6 Y7]


a_q a^dagger_p
1.0 [7 3^]
(-0.25+0j) [X3 Z4 Z5 Z6 X7] +
-0.25j [X3 Z4 Z5 Z6 Y7] +
0.25j [Y3 Z4 Z5 Z6 X7] +
(-0.25+0j) [Y3 Z4 Z5 Z6 Y7]


From CUDA-Q

a_p a^dagger_q
3 7^
[-0.25+0j] IIIXZZZX
[0+0.25j] IIIXZZZY
[0-0.25j] IIIYZZZX
[-0.25+0j] IIIYZZZY

a_q a^dagger_p
7^ 3
[0.25+0j] IIIXZZZX
[0+0.25j] IIIYZZZX
[0-0.25j] IIIXZZZY
[0.25+0j] IIIYZZZY

a_q a^dagger_p
3^ 7
[0.25+0j] IIIXZZZX
[0+0.25j] IIIXZZZY
[0-0.25j] IIIYZZZX
[0.25+0j] IIIYZZZY

a_p a^dagger_q
7 3^
[-0.25+0j] IIIXZZZX
[0+0.25j] IIIYZZZX
[0-0.25j] IIIXZZZY
[-0.25+0j] IIIYZZZY

Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Aug 29, 2024
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Aug 30, 2024
github-actions bot pushed a commit that referenced this pull request Aug 31, 2024
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Sep 1, 2024
Copy link

github-actions bot commented Sep 1, 2024

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@sacpis sacpis marked this pull request as ready for review September 3, 2024 15:38
github-actions bot pushed a commit that referenced this pull request Sep 4, 2024
Copy link

github-actions bot commented Sep 4, 2024

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@sacpis
Copy link
Collaborator Author

sacpis commented Sep 5, 2024

Converting this PR to draft as need to check one case

X * I

@sacpis sacpis marked this pull request as draft September 5, 2024 01:13
Copy link

github-actions bot commented Sep 5, 2024

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Sep 5, 2024
github-actions bot pushed a commit that referenced this pull request Sep 6, 2024
Copy link

github-actions bot commented Sep 6, 2024

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Sep 23, 2024
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Sep 24, 2024
github-actions bot pushed a commit that referenced this pull request Oct 3, 2024
Copy link

github-actions bot commented Oct 3, 2024

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

Copy link

github-actions bot commented Oct 5, 2024

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Oct 5, 2024
Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
* Adding missing unitests for Pauli operators

Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
Signed-off-by: Sachin Pisal <[email protected]>
@sacpis sacpis force-pushed the fix_spin_op_bug branch 2 times, most recently from 702debc to 2468c27 Compare October 9, 2024 16:16
Copy link

github-actions bot commented Oct 9, 2024

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Oct 9, 2024
@sacpis sacpis closed this Oct 21, 2024
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.

1 participant