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

feature: ITensor-based MPS local simulator #89

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Fe-r-oz
Copy link

@Fe-r-oz Fe-r-oz commented Jun 12, 2024

Resolving Issue #81

Description of changes: Look into ITensor's support for DMRG-based evolution methods to implement tensor network circuit simulation. Users have control over how the simulation is run (e.g. TDVP vs TEBD, maximum bond dimension).

Testing done:
designed a simulate_dmrg wrapper so that we can do something like
result = simulate(simulator, sites, os, method=:DMRG, N=N, sites_type="S=1/2", linkdims=10, nsweeps=2, maxdim=[10, 20], cutoff=1e-8)
I believe this is the right track, but more can be done! I hope it you find it the right track for resoving Issue81

julia> simulator = ITensorSimulator("itensor_backend")

julia> N = 10
10

julia> sites = siteinds("S=1",N);

julia> os = OpSum();

julia> for j=1:N-1
                 os += "Sz",j,"Sz",j+1
                 os += 1/2,"S+",j,"S-",j+1
                 os += 1/2,"S-",j,"S+",j+1
             end

julia> result = simulate(simulator, sites, os, method=:DMRG, N=N, sites_type="S=1/2", linkdims=10, nsweeps=2, maxdim=[10, 20], cutoff=1e-8)
After sweep 1 energy=-12.7987815639034  maxlinkdim=10 maxerr=6.95E-03 time=17.163
After sweep 2 energy=-12.86346578444438  maxlinkdim=20 maxerr=2.74E-06 time=0.043
(-12.86346578444438, MPS
[1] ((dim=3|id=395|"Link,l=1"), (dim=3|id=760|"S=1,Site,n=1"))
[2] ((dim=9|id=210|"Link,l=2"), (dim=3|id=977|"S=1,Site,n=2"), (dim=3|id=395|"Link,l=1"))
[3] ((dim=3|id=476|"S=1,Site,n=3"), (dim=20|id=246|"Link,l=3"), (dim=9|id=210|"Link,l=2"))
[4] ((dim=3|id=22|"S=1,Site,n=4"), (dim=20|id=453|"Link,l=4"), (dim=20|id=246|"Link,l=3"))
[5] ((dim=3|id=292|"S=1,Site,n=5"), (dim=20|id=449|"Link,l=5"), (dim=20|id=453|"Link,l=4"))
[6] ((dim=3|id=22|"S=1,Site,n=6"), (dim=20|id=399|"Link,l=6"), (dim=20|id=449|"Link,l=5"))
[7] ((dim=3|id=377|"S=1,Site,n=7"), (dim=20|id=452|"Link,l=7"), (dim=20|id=399|"Link,l=6"))
[8] ((dim=3|id=130|"S=1,Site,n=8"), (dim=9|id=510|"Link,l=8"), (dim=20|id=452|"Link,l=7"))
[9] ((dim=3|id=392|"S=1,Site,n=9"), (dim=3|id=383|"Link,l=9"), (dim=9|id=510|"Link,l=8"))
[10] ((dim=3|id=904|"S=1,Site,n=10"), (dim=3|id=383|"Link,l=9"))
)

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

Tests

  • I have installed and run git secrets to make sure I did not commit any sensitive information (passwords or credentials)
  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • 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
Copy link
Author

Fe-r-oz commented Jun 12, 2024

Hi, @kshyatt-aws, I require your input on this. It seems to me that there are two approaches which can be used. By way of PackageExtension, we can make ITensorSimulator a subtype of LocalSimulator (by redefinition) that would allow us to use some of the functionalities like

LocalQuantumTask("dmrg_task", GateModelQuantumTaskResult(energy, psi))

Although GateModelQuantumTaskResult might not be suitable but it conveys the idea that we want to hook the ITensorSimuator to Local Simulator. I used this approach here and locally, and it did work in a sense that by using simulate of ITensorSimulator, it was able to access the methods of LocalSimulator. The other approach is to not make ITensorSimulator a subtype of LocalSimulator.

julia> result = simulate(simulator, sites; method=:DMRG, N=N, sites_type="S=1/2", linkdims=10, nsweeps=2, maxdim=[10, 20], cutoff=1e-8)
After sweep 1 energy=-12.831742146962878  maxlinkdim=10 maxerr=9.24E-03 time=20.468
After sweep 2 energy=-12.886444144050335  maxlinkdim=20 maxerr=6.08E-06 time=0.034
ERROR: MethodError: no method matching Braket.GateModelQuantumTaskResult(::Float64, ::MPS)

Closest candidates are:
  Braket.GateModelQuantumTaskResult(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any)
   @ Braket ~/Desktop/New/braket/6/Braket.jl/src/raw_task_result_types.jl:38

We may have to implement methods for LocaQuantumTask that does not require results from the typical Devices. Since LocalSimulator <: Devices, so it seems to me that at the moment, it only takes care of backends that are available on server, not for custom backends like "itensor_backend".

Also, I checked out GPU backends.

using ITensors

i, j, k = Index.((2, 2, 2))
A = random_itensor(i, j)
B = random_itensor(j, k)

# Perform tensor operations on CPU
A * B

###########################################
using CUDA # This will trigger the loading of `NDTensorsCUDAExt` in the background

# Move tensors to NVIDIA GPU
Acu = cu(A)
Bcu = cu(B)

# Perform tensor operations on NVIDIA GPU
Acu * Bcu

###########################################
using Metal # This will trigger the loading of `NDTensorsMetalExt` in the background

# Move tensors to Apple GPU
Amtl = mtl(A)
Bmtl = mtl(B)

# Perform tensor operations on Apple GPU
Amtl * Bmtl

Thanks!

Copy link
Member

@kshyatt-aws kshyatt-aws left a comment

Choose a reason for hiding this comment

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

I think this needs a lot more development. Ideally the simulator should be able to consume Braket.jl circuits and translate them automatically

@Fe-r-oz
Copy link
Author

Fe-r-oz commented Jun 12, 2024

Thanks for your comments!

@Fe-r-oz
Copy link
Author

Fe-r-oz commented Jun 13, 2024

I don't understand what consume Braket.jl circuit and translate means. Kindly please provide some reference and a bit more context about it.

I checked the documentation but I didn't find anything related it. It might be straightforward but I am missing some points.

@Fe-r-oz
Copy link
Author

Fe-r-oz commented Jun 17, 2024

Hi, I asked main dev from ITensor.jl since PastaQ.jl has implemented wrapper around ITensorMPS.jl: GTorlai/PastaQ.jl#312. This provides me with some pointers!

@Fe-r-oz
Copy link
Author

Fe-r-oz commented Jun 20, 2024

@kshyatt-aws, they extended the deadline to 26th June! I would be delighted to dedicate this week to pursue this PR!

Kindly please provide further details about

Ideally the simulator should be able to consume Braket.jl circuits and translate them automatically"

I am having trouble understanding this part 🙏🏼 . I don't know what consume and translate mean in this context. Please provide further clarification and some starting point for guidance. Thanks! 🙏🏼

@Fe-r-oz Fe-r-oz changed the title [Feature]: ITensor-based MPS local simulator feature: ITensor-based MPS local simulator Jun 20, 2024
@Fe-r-oz Fe-r-oz marked this pull request as draft August 16, 2024 12:18
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.

2 participants