amazon-braket / BraketSimulator.jl

Apache License 2.0
9 stars 3 forks source link

feature: Expanded support for quantum chemistry operations #25

Open Fe-r-oz opened 4 weeks ago

Fe-r-oz commented 4 weeks ago

Resolving Issue #14

Description of changes: [Feature]: Expanded support for quantum chemistry operations

Test Summary: | Pass Total Time Single excitation plus | 11 11 0.1s Test.DefaultTestSet("Single excitation plus", Any[Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set raw", Any[], 2, false, false, true, 1.717924559668186e9, 1.717924559796476e9, false, "REPL[11]"), Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set decomp", Any[], 2, false, false, true, 1.717924559796525e9, 1.717924559796617e9, false, "REPL[11]"), Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set unitary", Any[], 2, false, false, true, 1.717924559796632e9, 1.717924559796652e9, false, "REPL[11]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set raw", Any[], 1, false, false, true, 1.717924559796663e9, 1.717924559796711e9, false, "REPL[11]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set decomp", Any[], 1, false, false, true, 1.717924559796721e9, 1.717924559796839e9, false, "REPL[11]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set unitary", Any[], 1, false, false, true, 1.71792455979685e9, 1.717924559796878e9, false, "REPL[11]")], 2, false, false, true, 1.717924559668118e9, 1.717924559796883e9, false, "REPL[11]")

Test Summary: | Pass Total Time Double excitation minus | 8 8 5.1s Test.DefaultTestSet("Double excitation minus", Any[Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set raw", Any[], 2, false, false, true, 1.717963078614289e9, 1.717963081728338e9, false, "REPL[4]"), Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set unitary", Any[], 2, false, false, true, 1.717963081728397e9, 1.717963082160096e9, false, "REPL[4]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set raw", Any[], 1, false, false, true, 1.717963082160141e9, 1.717963082967287e9, false, "REPL[4]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set unitary", Any[], 1, false, false, true, 1.717963082967335e9, 1.717963083440299e9, false, "REPL[4]")], 2, false, false, true, 1.717963078351866e9, 1.717963083445371e9, false, "REPL[4]")

Test Summary: | Pass Total Time Double excitation plus | 8 8 0.0s Test.DefaultTestSet("Double excitation plus", Any[Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set raw", Any[], 2, false, false, true, 1.717963084317922e9, 1.717963084323246e9, false, "REPL[5]"), Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set unitary", Any[], 2, false, false, true, 1.717963084323292e9, 1.717963084323328e9, false, "REPL[5]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set raw", Any[], 1, false, false, true, 1.717963084323344e9, 1.717963084341904e9, false, "REPL[5]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set unitary", Any[], 1, false, false, true, 1.71796308434195e9, 1.717963084342023e9, false, "REPL[5]")], 2, false, false, true, 1.71796308431786e9, 1.717963084347451e9, false, "REPL[5]")

Test Summary: | Pass Total Time FermionicSWAP | 8 8 0.4s Test.DefaultTestSet("FermionicSWAP", Any[Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set raw", Any[], 2, false, false, true, 1.717963112628164e9, 1.717963112893477e9, false, "REPL[6]"), Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set unitary", Any[], 2, false, false, true, 1.717963112893523e9, 1.717963112906691e9, false, "REPL[6]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set raw", Any[], 1, false, false, true, 1.717963112906736e9, 1.717963112953446e9, false, "REPL[6]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set unitary", Any[], 1, false, false, true, 1.717963112953494e9, 1.717963113056209e9, false, "REPL[6]")], 2, false, false, true, 1.717963112614308e9, 1.717963113060744e9, false, "REPL[6]")



## Merge Checklist

_Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request._

#### General
- [x] I have read the [CONTRIBUTING](https://github.com/amazon-braket/BraketSimulator.jl/blob/main/CONTRIBUTING.md) doc
- [x] I used the commit message format described in [CONTRIBUTING](https://github.com/amazon-braket/BraketSimulator.jl/blob/main/CONTRIBUTING.md#commit-your-change)
- [x] I have updated any necessary documentation, including [READMEs](https://github.com/amazon-braket/BraketSimulator.jl/blob/main/README.md) and [API docs](https://github.com/amazon-braket/BraketSimulator.jl/blob/main/CONTRIBUTING.md#documentation-guidelines) (if appropriate)
#### Tests

- [x] I have added tests that prove my fix is effective or that my feature works (if appropriate)
- [x] I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Fe-r-oz commented 4 weeks ago

Hi, @krneta, @speller26, @kshyatt-aws, I have submitted this qchem PR as part of ongoing unitaryhack that ends 12th on June. I have made sure to add all the tests and docstrings along with testing locally everything.

Kindly please review the pr as soon as convenient so that I can incorporate the changes (if needed), thus completing it within the given time!

Reminder: After the PR is closed, please assign the https://github.com/amazon-braket/BraketSimulator.jl/issues/14 to me so that the bot can track who completed the PR.

Thanks a lot!

Fe-r-oz commented 3 weeks ago

Testing: de_instructions

Single Excitation Plus (included in tests!)

[X(0),
 X(1),
 ControlledPhaseShift(1.78, wires=<Wires = [1, 0]>),
 X(0),
 X(1),
 ControlledPhaseShift(1.78, wires=<Wires = [0, 1]>),
 CNOT(wires=[0, 1]),
 CRY(3.56, wires=[1, 0])),
 CNOT(wires=[0, 1])]

Single Excitation Minus (included in tests!)

[X(0),
 X(1),
 ControlledPhaseShift(-1.78, wires=<Wires = [1, 0]>),
 X(0),
 X(1),
 ControlledPhaseShift(-1.78, wires=<Wires = [0, 1]>),
 CNOT(wires=[0, 1]),
 CRY(3.56, wires=[1, 0])),
 CNOT(wires=[0, 1])]

To test FermionicSWAP, instead of including de_instructions, I have tested the exact state vector! Meaning:

FermionicSWAP (Included in tests)

 state_vector = 0.5 * [1, exp(im*ϕ/2.0)*cos(ϕ / 2.0) - im*exp(im*ϕ/2.0)*sin(ϕ/2.0),
                              - im*exp(im*ϕ/2.0)*sin(ϕ/2.0) + exp(im*ϕ/2.0)*cos(ϕ/2.0), exp(im * ϕ)]
Fe-r-oz commented 3 weeks ago

Hi, @kshyatt-aws, I would be really grateful to get PRs reviewed today. I have made sure this is polished! It would be nice to have last day to respond to comments(if any). Thanks!

Fe-r-oz commented 3 weeks ago

I have a quick question to ask, I have been saving it for the review!

I computed the gate decomposition for FermionicSWAP as well. How to represent Exp(1.78j Identity) as an instruction (Instruction(....)) ? I am not sure it's same as ==Instruction(Unitary([-0.2079 + 0.9781*im 0; 0 -0.2079 + 0.9781*im]))

[Hadamard(wires=[0]),
 Hadamard(wires=[1]),
 MultiRZ(1.78, wires=[0, 1]),
 Hadamard(wires=[0]),
 Hadamard(wires=[1]),
 RX(1.5707963267948966, wires=[0]),
 RX(1.5707963267948966, wires=[1]),
 MultiRZ(1.78, wires=[0, 1]),
 RX(-1.5707963267948966, wires=[0]),
 RX(-1.5707963267948966, wires=[1]),
 RZ(1.78, wires=[0]),
 RZ(1.78, wires=[1]),
 Exp(1.78j Identity)]

I have built instruction set for FermionicSWAP which looks like:

This is almost complete de_instructions for FermionicSWAP that we can add. We just need to represent this Exp(1.78j Identity)] as last instruction!

Almost complete de_instructions! de_instructions = [Instruction(H(), [0]), Instruction(H(), [1]), Instruction(H(), [0]), Instruction(H(), [1]), Instruction(CNot(), [1, 0]), Instruction(Rz(1.78), [0]), Instruction(CNot(), [1, 0]), Instruction(H(), [0]), Instruction(H(), [1]), Instruction(Rx(1.5707963267948966), [0]), Instruction(Rx(1.5707963267948966), [1]), Instruction(CNot(), [1, 0]), Instruction(Rz(1.78), [0]), Instruction(CNot(), [1, 0]), Instruction(Rx(1.5707963267948966), [0]), Instruction(Rx(1.5707963267948966), [1]), Instruction(Rz(1.78), [0]), Instruction(Rz(1.78), [1])] (We just need to add this Exp(1.78j Identity)] as an instruction

kshyatt-aws commented 3 weeks ago

For the fermionic swap, you might need to make a larger diagonal matrix for the Unitary

Fe-r-oz commented 3 weeks ago

Fixed the Documentation!

For the fermionic swap, you might need to make a larger diagonal matrix for the Unitary

It seems I need a (4,1) flat vector since Fermionic SMatrix is of size (4,4)

Fe-r-oz commented 3 weeks ago

Hi, @kshyatt-aws, Please checkout this test for FermionicSWAP!

julia> @testset "FermionicSWAP" begin
               ϕ  = 3.56
               nq = 2
               # instructions for the matrix representation
               instructions = [Instruction(H(), [0]), Instruction(H(), [1]), Instruction(FermionicSWAP(ϕ), [0, 1])]
               u_instructions = [Instruction(H(), [0]), Instruction(H(), [1]), Instruction(Unitary(Matrix(matrix_rep(FermionicSWAP(ϕ)))), [1, 0])]
               state_vector = 0.5 * [1, exp(im*ϕ/2.0)*cos(ϕ / 2.0) - im*exp(im*ϕ/2.0)*sin(ϕ/2.0), 
                                     - im*exp(im*ϕ/2.0)*sin(ϕ/2.0) + exp(im*ϕ/2.0)*cos(ϕ/2.0), exp(im * ϕ)]
               probability_amplitudes = [0.2499999999999999, 0.2499999999999999, 0.2499999999999999, 0.2499999999999999]
               @testset "Simulator $sim, instruction set $ix_label" for sim in (StateVectorSimulator, DensityMatrixSimulator),
                   (ix_label, ixs) in (("raw", instructions), ("unitary", u_instructions))
                   simulation = sim(nq, 0)
                   simulation = evolve!(simulation, ixs)
                   if sim == StateVectorSimulator
                       @test state_vector ≈ collect(BraketSimulator.state_vector(simulation))
                   end
                   @test probability_amplitudes ≈
                         collect(BraketSimulator.probabilities(simulation))
               end
               @test qubit_count(FermionicSWAP(ϕ)) == 2
               @test inv(FermionicSWAP(ϕ)) == FermionicSWAP(-ϕ)
           end
Test Summary: | Pass  Total  Time
FermionicSWAP |    8      8  2.9s
Test.DefaultTestSet("FermionicSWAP", Any[Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set raw", Any[], 2, false, false, true, 1.718133648769734e9, 1.718133650951796e9, false, "REPL[26381]"), Test.DefaultTestSet("Simulator StateVectorSimulator, instruction set unitary", Any[], 2, false, false, true, 1.718133650951871e9, 1.718133650974729e9, false, "REPL[26381]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set raw", Any[], 1, false, false, true, 1.718133650974839e9, 1.718133651340173e9, false, "REPL[26381]"), Test.DefaultTestSet("Simulator DensityMatrixSimulator, instruction set unitary", Any[], 1, false, false, true, 1.718133651340226e9, 1.718133651499389e9, false, "REPL[26381]")], 2, false, false, true, 1.718133648585122e9, 1.718133651505309e9, false, "REPL[26381]")
Fe-r-oz commented 2 weeks ago

Thanks a lot, @kshyatt-aws for your comments. Added docstrings, and codereview suggestions and tested locally!

Test Summary:    |Time
Method ambiguity | None  0.0s
Test Summary:           | Pass  Total  Time
Unbound type parameters |    1      1  0.2s
Test Summary:     | Pass  Total  Time
Undefined exports |    1      1  0.0s
Test Summary:                              | Pass  Total  Time
Compare Project.toml and test/Project.toml |    1      1  0.2s
Test Summary:      | Pass  Total  Time
Stale dependencies |    1      1  4.5s
Test Summary: | Pass  Total  Time
Compat bounds |    4      4  0.6s
Test Summary: |Time
Piracy        | None  0.0s
Test Summary:    |Time
Persistent tasks | None  0.0s
Test Summary:   | Pass  Total     Time
BraketSimulator | 6772   6772  3m24.1s
     Testing BraketSimulator tests passed 
Fe-r-oz commented 2 weeks ago

@kshyatt-aws, A small request to kindly please assign/close the corresponding issue (14) as today the final day to assign/close issues!

Thanks to your comments, I have significantly improved the PR! Please feel free to review about your convenience. I am delighted to address any further comments.

Best,

Fe-r-oz commented 2 weeks ago

Tested again, Local Test Results:

     Testing Running tests...
Test Summary:    |Time
Method ambiguity | None  0.0s
Test Summary:           | Pass  Total  Time
Unbound type parameters |    1      1  0.2s
Test Summary:     | Pass  Total  Time
Undefined exports |    1      1  0.0s
Test Summary:                              | Pass  Total  Time
Compare Project.toml and test/Project.toml |    1      1  0.2s
Test Summary:      | Pass  Total  Time
Stale dependencies |    1      1  6.4s
Test Summary: | Pass  Total  Time
Compat bounds |    4      4  0.7s
Test Summary: |Time
Piracy        | None  0.0s
Test Summary:    |Time
Persistent tasks | None  0.0s
Test Summary:   | Pass  Total     Time
BraketSimulator | 6800   6800  4m40.9s
     Testing BraketSimulator tests passed 
kshyatt-aws commented 2 weeks ago

My approval here should be enough but just in case feel free to ping me again if UH doesn't award the bounty and I'll reach out to them. Thanks for the contribution and multiple revisions!

Fe-r-oz commented 2 weeks ago

All the tests are passing except the CI Documentation. Please have look at doctests (it seems to be a strange error it's showing). I am not sure what's causing that. Please let me know!

Btw, I have changed to Braket.I()instead of DoubleExcitationPlus((0.0,)) and likewise locally. Let's make sure all the tests passes!

Fe-r-oz commented 2 weeks ago

@kshyatt-aws, I fixed the CI doc error , but I when I committed and pushed the commit, a werid message which is as follows "@Fe-r-oz dismissed kshyatt-aws’s stale review via 81187a5 4 minutes ago" is now appearing and approval disappeared. Kindly please run the CI (hope the CI passes for documentation as well), Very sorry for the inconvenience, I am not sure why this message appeare. I was just trying to fix the CI doc error as well also added Braket.I()as well. 🙏🏼

julia> doctest(BraketSimulator)
┌ Warning: Unable to determine HTML(edit_link = ...) from remote HEAD branch, defaulting to "master".
│ Calling `git remote` failed with an exception. Set JULIA_DEBUG=Documenter to see the error.
│ Unless this is due to a configuration error, the relevant variable should be set explicitly.
└ @ Documenter ~/.julia/packages/Documenter/CJeWX/src/utilities/utilities.jl:640
[ Info: SetupBuildDirectory: setting up build directory.
[ Info: Doctest: running doctests.
[ Info: Skipped ExpandTemplates step (doctest only).
[ Info: Skipped CrossReferences step (doctest only).
[ Info: Skipped CheckDocument step (doctest only).
[ Info: Skipped Populate step (doctest only).
[ Info: Skipped RenderDocument step (doctest only).
Test Summary:             | Pass  Total   Time
Doctests: BraketSimulator |    1      1  12.4s
Test.DefaultTestSet("Doctests: BraketSimulator", Any[], 1, false, false, true, 1.718963335868262e9, 1.71896334831641e9, false, "/home/Transcendence/.julia/packages/Documenter/CJeWX/src/doctest.jl")
kshyatt-aws commented 1 week ago

Heya @Fe-r-oz , we haven't forgotten about this PR! As you can see, we have a big feature branch in progress (which shouldn't affect this much) -- once that is merged, we can pull this into main as well.