### Install PackageCompiler for ITensors.compile() (Julia) Source: https://itensor.github.io/ITensors.jl/stable/index To use the `ITensors.compile()` function, you now need to install the PackageCompiler.jl package. This snippet shows the necessary Pkg command and how to include it in your code. ```julia using Pkg: Pkg Pkg.add("PackageCompiler") using PackageCompiler using ITensors # Now you can use ITensors.compile() ``` -------------------------------- ### Get and Set ITensor Elements Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Demonstrates how to set and retrieve individual elements of an ITensor using index-value pairs. ITensors are initialized with zero elements by default. The `=>` notation is used for clarity and flexibility in specifying index orders. ```julia i = Index(3,"index_i") j = Index(2,"index_j") k = Index(4,"index_k") T = ITensor(i,j,k) T[i=>2,j=>1,k=>3] = -3.2 el = T[j=>1,i=>2,k=>3] println("The (i,j,k) = (2,1,3) element of T is ",el) ``` -------------------------------- ### Install Julia on a Linux Cluster Source: https://itensor.github.io/ITensors.jl/stable/getting_started/Installing Commands to download, extract, and link a Julia binary on a Unix/Linux cluster. Assumes execution in the home directory and the presence of `wget` and `tar`. ```bash $ cd $ mkdir -p bin $ wget https://julialang-s3.julialang.org/bin/linux/x64/1.7/julia-1.7.2-linux-x86_64.tar.gz $ tar xvzf julia-1.7.2-linux-x86_64.tar.gz $ ln -s julia-1.7.2/bin/julia bin/julia ``` -------------------------------- ### Install and Use Pre-commit Hook Source: https://itensor.github.io/ITensors.jl/stable/faq/Development An alternative method for automatic code formatting before commits. It involves installing the pre-commit package and then initializing it in the project directory. ```shell pip install pre-commit pre-commit install ``` -------------------------------- ### ITensor Factorization Example using eigen Source: https://itensor.github.io/ITensors.jl/stable/ITensorType This example demonstrates how to factorize an ITensor `A` using the `eigen` decomposition. It shows the creation of indices, a random ITensor, the specification of left and right indices for the factorization, performing the `eigen` decomposition, identifying unique and common indices, and verifying the factorization with an approximate equality check. ```julia i, j, k, l = Index(2, "i"), Index(2, "j"), Index(2, "k"), Index(2, "l") A = random_itensor(i, j, k, l) Linds = (i, k) Rinds = (j, l) D, U = eigen(A, Linds, Rinds) dl, dr = uniqueind(D, U), commonind(D, U) Ul = replaceinds(U, (Rinds..., dr) => (Linds..., dl)) A * U ≈ Ul * D # true ``` -------------------------------- ### Install ITensors.jl Package Manager Source: https://itensor.github.io/ITensors.jl/stable/index Installs the ITensors.jl package using Julia's built-in package manager, Pkg. This method is recommended for managing Julia packages. ```julia using Pkg; Pkg.add("ITensors") ``` -------------------------------- ### Use ITensors.NDTensors Instead of NDTensors Source: https://itensor.github.io/ITensors.jl/stable/UpgradeGuide_0.1_to_0.2 Explains the internal restructuring of the NDTensors module within the ITensors package. Users who previously used both packages separately should now import NDTensors using `ITensors.NDTensors`. This snippet details the installation and usage. ```julia using ITensors.NDTensors using Pkg; Pkg.add("ITensors") ``` -------------------------------- ### Create ITensor from Array Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Shows how to initialize an ITensor with all its elements set from a given Julia array. This is applicable for both 2D matrices and higher-dimensional arrays. ```julia M = [1.0 2.0; 3.0 4.0] i = Index(2,"i") j = Index(2,"j") A = ITensor(M,i,j) ``` ```julia T_data = randn(4,7,2) k = Index(4,"index_k") l = Index(7,"index_l") m = Index(2,"index_m") B = ITensor(T_data,k,l,m) ``` -------------------------------- ### Write ITensor to HDF5 File Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Shows how to save an ITensor to an HDF5 file. This is useful for persisting tensor data, debugging, or as a starting point for subsequent calculations. Requires the HDF5 package. ```julia using HDF5 using ITensors i = Index(4, "i") T = random_itensor(i) f = h5open("myfile.h5", "w") write(f, "T", T) close(f) ``` -------------------------------- ### Install ITensors.jl Package in Julia REPL Source: https://itensor.github.io/ITensors.jl/stable/getting_started/Installing Instructions to add the ITensors.jl package using Julia's interactive package manager. This requires launching a Julia session and entering the package manager. ```julia julia> ] pkg> add ITensors ``` -------------------------------- ### Compile ITensor Library in Julia Source: https://itensor.github.io/ITensors.jl/stable/getting_started/Installing Optional but recommended step to compile the ITensor library for faster loading. This involves loading the package and running a compilation command within a Julia session. ```julia julia> using ITensors; ITensors.compile() ``` -------------------------------- ### QN Convention Change for "Qubit" Site Type in Julia Source: https://itensor.github.io/ITensors.jl/stable/UpgradeGuide_0.1_to_0.2 Details the change in the Quantum Number (QN) convention for the "Qubit" site type, which now tracks the total number of 1 bits instead of the net number. Includes code examples illustrating the changes and comparisons to other site types. ```julia s = siteinds("Qubit", 4; conserve_number=true) 4-element Vector{Index{Vector{Pair{QN, Int64}}}}: (dim=2|id=925|"Qubit,Site,n=1") 1: QN("Number",0) => 1 2: QN("Number",1) => 1 (dim=2|id=799|"Qubit,Site,n=2") 1: QN("Number",0) => 1 2: QN("Number",1) => 1 (dim=2|id=8|"Qubit,Site,n=3") 1: QN("Number",0) => 1 2: QN("Number",1) => 1 (dim=2|id=385|"Qubit,Site,n=4") 1: QN("Number",0) => 1 2: QN("Number",1) => 1 ``` ```julia s = siteinds("S=1/2", 4; conserve_sz=true) 4-element Vector{Index{Vector{Pair{QN, Int64}}}}: (dim=2|id=364|"S=1/2,Site,n=1") 1: QN("Sz",1) => 1 2: QN("Sz",-1) => 1 (dim=2|id=823|"S=1/2,Site,n=2") 1: QN("Sz",1) => 1 2: QN("Sz",-1) => 1 (dim=2|id=295|"S=1/2,Site,n=3") 1: QN("Sz",1) => 1 2: QN("Sz",-1) => 1 (dim=2|id=810|"S=1/2,Site,n=4") 1: QN("Sz",1) => 1 2: QN("Sz",-1) => 1 ``` -------------------------------- ### Starting Julia with Multiple Threads Source: https://itensor.github.io/ITensors.jl/stable/Multithreading Demonstrates various command-line methods to launch the Julia interpreter with a specified number of threads. This is a prerequisite for utilizing multithreaded features within libraries like ITensors.jl. ```bash $ julia --threads=4 ``` ```bash $ julia -t 4 ``` ```bash $ JULIA_NUM_THREADS=4 julia ``` -------------------------------- ### Enable HDF5 Support for ITensors.jl Source: https://itensor.github.io/ITensors.jl/stable/index Enables HDF5 read and write functions for ITensor, MPS, MPO, and associated types by installing the HDF5.jl package and importing it alongside ITensors.jl. ```julia using Pkg; Pkg.add("HDF5") using HDF5 using ITensors ``` -------------------------------- ### Read ITensor from HDF5 File Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Demonstrates how to load an ITensor from an HDF5 file. This allows for resuming calculations or analyzing previously saved tensor data. Requires the HDF5 package. ```julia using HDF5 using ITensors f = h5open("myfile.h5", "r") T = read(f, "T", ITensor) close(f) println("Read ITensor: ", T) ``` -------------------------------- ### Install ITensor Sysimage Kernel for Jupyter (Julia) Source: https://itensor.github.io/ITensors.jl/stable/getting_started/RunningCodes This code snippet installs a custom Julia kernel for Jupyter that uses a pre-compiled ITensor sysimage. This allows Jupyter notebooks to leverage the optimized sysimage for faster startup and execution. Requires the IJulia package. ```julia using IJulia installkernel("julia_ITensors","--sysimage=~/.julia/sysimages/sys_itensors.so") ``` -------------------------------- ### Format Code with JuliaFormatter Source: https://itensor.github.io/ITensors.jl/stable/faq/Development Ensures code contributions adhere to ITensor's formatting standards. Run this command in the project directory. It requires the JuliaFormatter package to be installed. ```julia using JuliaFormatter format(".") ``` -------------------------------- ### Tensor Index Properties: Tags and Prime Levels in ITensors.jl Source: https://itensor.github.io/ITensors.jl/stable/index Illustrates the creation and properties of ITensor Index objects. It shows how to define indices with specific dimensions, assign unique IDs, copy indices, and attach tags. The example also demonstrates how prime levels affect index equality. ```julia using ITensors let i = Index(3) # Index of dimension 3 @show dim(i) # = 3 @show id(i) # = 0x5d28aa559dd13001 or similar ci = copy(i) @show ci == i # = true j = Index(5,"j") # Index with a tag "j" @show j == i # = false s = Index(2,"n=1,Site") # Index with two tags, # "Site" and "n=1" @show hastags(s,"Site") # = true @show hastags(s,"n=1") # = true i1 = prime(i) # i1 has a "prime level" of 1 # but otherwise same properties as i @show i1 == i # = false, prime levels do not match nothing end ``` -------------------------------- ### Elementwise Operations on ITensors (Julia) Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Demonstrates how to perform element-wise operations on dense ITensors using Julia's broadcasting syntax. This includes arithmetic operations (*=, +=), function application (abs, one), and operations involving two ITensors. Note that these operations are not supported for block-sparse QN ITensors. ```Julia i = Index(2,"i") j = Index(3,"j") A = random_itensor(i,j) # Multiply every element of `A` by 2.0: A .*= 2.0 # Add 1.5 to every element of A A .+= 1.5 # Replace every element in A by its absolute value: A .= abs.(A) # Replace every element in A by the number 1.0 A .= one.(A) B = ITensor(j,i) # Add elements of A and B element-wise A .= A .+ B # Add elements of A and B element-wise with coefficients included A .= (2.0 .* A) .+ (-3.0 .* B) myf(x) = 1.0/(1.0+exp(-x)) T .= myf.(T) ``` -------------------------------- ### Start Julia with Multiple Threads Source: https://itensor.github.io/ITensors.jl/stable/Multithreading Illustrates how to launch the Julia interpreter with a specified number of threads using the '-t' flag. This enables Strided.jl's multithreading by default. ```bash $ julia -t 4 julia> Threads.nthreads() 4 ``` -------------------------------- ### ITensors Julia Code Template using a function Source: https://itensor.github.io/ITensors.jl/stable/getting_started/RunningCodes This example shows how to structure Julia code using ITensors within a function. This approach is beneficial for interactive development, allowing easy execution with different arguments. The function defines indices based on provided dimensions and generates a random ITensor. ```julia using ITensors function main(; d1 = 2, d2 = 3) # ... your own code goes here ... # For example: i = Index(d1,"i") j = Index(d2,"j") T = random_itensor(i,j) @show T end main(; d1 = 4, d2 = 5) ``` -------------------------------- ### Convert ITensor to Array Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Illustrates how to convert an ITensor back into a standard Julia array. The order of indices must be specified to define the resulting array's dimensions and ordering. Note that the returned array might be a view. ```julia k = Index(4,"index_k") m = Index(2,"index_m") T = random_itensor(k,m) A = Array(T,k,m) @show A ``` -------------------------------- ### Print ITensor Indices Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor This snippet shows how to display only the indices of an ITensor, which is useful when the full ITensor printout is too large. It uses the `inds` function to extract and print the indices. ```julia T = random_itensor() @show inds(T) ``` ```julia T = random_itensor() println("T inds = ",inds(T)) ``` -------------------------------- ### Allocating Storage for Empty ITensors Source: https://itensor.github.io/ITensors.jl/stable/UpgradeGuide_0.1_to_0.2 Shows how to allocate storage for an `EmptyStorage` ITensor by setting its elements. Once an element is set, the ITensor adopts the appropriate storage type (e.g., `Dense`) and element type (e.g., `Float64` or `ComplexF64`) based on the value assigned. ```julia A[i' => 1, i => 1] = 0.0 @show A ``` ```julia julia> A[i' => 1, i => 1] = 0.0 0.0 julia> @show A A = ITensor ord=2 Dim 1: (dim=2|id=346)' Dim 2: (dim=2|id=346) ITensors.NDTensors.Dense{Float64, Vector{Float64}} 2×2 0.0 0.0 0.0 0.0 ``` ```julia A = ITensor(i', dag(i)) A[i' => 1, i => 1] = 1.0 + 0.0im @show A ``` ```julia julia> A = ITensor(i', dag(i)) ITensor ord=2 (dim=2|id=346)' (dim=2|id=346) ITensors.NDTensors.EmptyStorage{ITensors.NDTensors.EmptyNumber, ITensors.NDTensors.Dense{ITensors.NDTensors.EmptyNumber, Vector{ITensors.NDTensors.EmptyNumber}}} julia> A[i' => 1, i => 1] = 1.0 + 0.0im 1.0 + 0.0im julia> @show A A = ITensor ord=2 Dim 1: (dim=2|id=346)' Dim 2: (dim=2|id=346) ITensors.NDTensors.Dense{ComplexF64, Vector{ComplexF64}} 2×2 1.0 + 0.0im 0.0 + 0.0im 0.0 + 0.0im 0.0 + 0.0im ``` -------------------------------- ### Arithmetic Operations with ITensors Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Demonstrates basic arithmetic operations (addition, subtraction, scalar multiplication) that can be performed on ITensors. ITensors can be added or subtracted even if their indices are in a different order, provided they share the same set of indices. ```julia i = Index(3,"i") j = Index(2,"j") k = Index(4,"k") A = random_itensor(i,j,k) B = random_itensor(i,j,k) C = random_itensor(k,i,j) R1 = A + B R2 = A - B R3 = A + B - C R4 = 2.0*A - B + C/(1+1im) ``` -------------------------------- ### Control BLAS Multithreading via Environment Variables Source: https://itensor.github.io/ITensors.jl/stable/Multithreading Demonstrates setting the number of threads for different BLAS implementations (MKL, OpenBLAS) or OpenMP using environment variables before starting Julia. This affects linear algebra operations. ```bash $ MKL_NUM_THREADS=4 julia # Set the number of MKL threads to 4 $ OPENBLAS_NUM_THREADS=4 julia # Set the number of OpenBLAS threads to 4 $ OMP_NUM_THREADS=4 julia # Set the number of OpenMP threads to 4, which will be used by MKL or OpenBLAS if they are not specifically set ``` -------------------------------- ### Benchmark Tensor Contraction with Threaded Block Sparse Operations Source: https://itensor.github.io/ITensors.jl/stable/Multithreading An example demonstrating how to benchmark a sparse tensor contraction using ITensors.jl. It compares the performance of serial contraction against threaded block sparse contraction and verifies the correctness of the results. It also includes setting BLAS and Strided threads to 1. ```julia using BenchmarkTools using ITensors using LinearAlgebra using Strided function main(; d = 20, order = 4) BLAS.set_num_threads(1) Strided.set_num_threads(1) println("#################################################") println("# order = ", order) println("# d = ", d) println("#################################################") println() i(n) = Index(QN(0) => d, QN(1) => d; tags = "i$n") is = ntuple(i, order ÷ 2) A = random_itensor(is'..., dag(is)...) B = random_itensor(is'..., dag(is)...) ITensors.enable_threaded_blocksparse(false) println("Serial contract:") @disable_warn_order begin C_contract = @btime $A' * $B samples = 5 end println() println("Threaded contract:") @disable_warn_order begin ITensors.enable_threaded_blocksparse(true) C_threaded_contract = @btime $A' * $B samples = 5 ITensors.enable_threaded_blocksparse(false) end println() @show C_contract ≈ C_threaded_contract return nothing end main(d = 20, order = 4) ``` -------------------------------- ### Replace IndexVal with Pair in Julia Source: https://itensor.github.io/ITensors.jl/stable/UpgradeGuide_0.1_to_0.2 Demonstrates the replacement of the IndexVal type with Julia's Pair type for representing index-value associations. This change simplifies the syntax and improves code readability. Includes example code snippets illustrating the substitution. ```julia i = Index(2) IndexVal(i, 2) i = Index(2) i => 2 # Or: Pair(i, 2) ``` -------------------------------- ### Basic ITensor Operations in Julia Source: https://itensor.github.io/ITensors.jl/stable/index Demonstrates the construction of ITensors, setting elements, performing contractions (multiplication), and addition. It involves creating Index objects, initializing ITensors, manipulating elements, and contracting them using the `*` operator. ```julia using ITensors let i = Index(3) j = Index(5) k = Index(2) l = Index(7) A = ITensor(i,j,k) B = ITensor(j,l) # Set elements of A A[i=>1,j=>1,k=>1] = 11.1 A[i=>2,j=>1,k=>2] = -21.2 A[k=>1,i=>3,j=>1] = 31.1 # can provide Index values in any order # ... # Contract over shared index j C = A * B @show hasinds(C,i,k,l) # = true D = random_itensor(k,j,i) # ITensor with random elements # Add two ITensors # must have same set of indices # but can be in any order R = A + D nothing end ``` -------------------------------- ### Rename `state` to `val` and Update `state` in Julia Source: https://itensor.github.io/ITensors.jl/stable/UpgradeGuide_0.1_to_0.2 Describes the renaming of the `state` function to `val` for mapping string names to index values and the updated definition of the `state` function to return single-index ITensors representing single-site states. Provides examples of how to use both functions. ```julia N = 10 s = siteinds("S=1/2", N) val(s[1], "Up") == 1 val(s[1], "Dn") == 2 ``` ```julia @show state(s[1], "Up"); state(s[1], "Up") = ITensor ord=1 Dim 1: (dim=2|id=597|"S=1/2,Site,n=1") ITensors.NDTensors.Dense{Float64, Vector{Float64}} 2-element 1.0 0.0 @show state(s[1], "Dn"); state(s[1], "Dn") = ITensor ord=1 Dim 1: (dim=2|id=597|"S=1/2,Site,n=1") ITensors.NDTensors.Dense{Float64, Vector{Float64}} 2-element 0.0 1.0 ``` ```julia @show state(s[1], "X+"); state(s[1], "X+") = ITensor ord=1 Dim 1: (dim=2|id=597|"S=1/2,Site,n=1") ITensors.NDTensors.Dense{Float64, Vector{Float64}} 2-element 0.7071067811865475 0.7071067811865475 @show state(s[1], "X-"); state(s[1], "X-") = ITensor ord=1 Dim 1: (dim=2|id=597|"S=1/2,Site,n=1") ITensors.NDTensors.Dense{Float64, Vector{Float64}} 2-element 0.7071067811865475 -0.7071067811865475 ``` ```julia ITensors.state(::SiteType"My_S=1/2", ::StateName"Up") = 1 ITensors.state(::SiteType"My_S=1/2", ::StateName"Dn") = 2 ``` ```julia ITensors.val(::SiteType"My_S=1/2", ::StateName"Up") = 1 ITensors.val(::SiteType"My_S=1/2", ::StateName"Dn") = 2 ``` -------------------------------- ### Running Julia with a compiled system image Source: https://itensor.github.io/ITensors.jl/stable/getting_started/RunningCodes This command shows how to launch Julia using a pre-compiled system image located at `~/.julia/sysimages/sys_itensors.so`. This results in much faster startup times. The `--sysimage` flag points to the system image file. ```shell $ julia --sysimage ~/.julia/sysimages/sys_itensors.so ``` -------------------------------- ### Remove ITensor Type Parameter (Julia) Source: https://itensor.github.io/ITensors.jl/stable/UpgradeGuide_0.1_to_0.2 Illustrates how to remove the tensor order type parameter from the ITensor type. Previously, one could specify `ITensor{3}`, but this is no longer supported. This change aims to simplify the ITensor type definition. The example shows the error encountered and the corrected usage. ```julia julia> i = Index(2) (dim=2|id=588) julia> ITensor{2}(i', i) ERROR: TypeError: in Type{...} expression, expected UnionAll, got Type{ITensor} Stacktrace: [1] top-level scope @ REPL[27]:1 ``` ```julia julia> ITensor(i', i) ITensor ord=2 (dim=2|id=913)' (dim=2|id=913) ITensors.NDTensors.EmptyStorage{ITensors.NDTensors.EmptyNumber, ITensors.NDTensors.Dense{ITensors.NDTensors.EmptyNumber, Vector{ITensors.NDTensors.EmptyNumber}}} ``` -------------------------------- ### Compiling ITensors system image Source: https://itensor.github.io/ITensors.jl/stable/getting_started/RunningCodes These commands demonstrate the process of compiling a Julia system image that includes ITensors, ITensorMPS, and PackageCompiler. This pre-compilation significantly speeds up Julia's startup time for subsequent sessions. The `ITensors.compile()` function performs the compilation. ```julia julia> using ITensors, ITensorMPS, PackageCompiler julia> ITensors.compile() ``` -------------------------------- ### Using MPS/MPO Functionality with ITensorMPS.jl (Julia) Source: https://itensor.github.io/ITensors.jl/stable/index As MPS/MPO functionality has moved to ITensorMPS.jl, this snippet demonstrates the updated way to use related functions. You need to import both ITensors and ITensorMPS. ```julia using ITensors, ITensorMPS # Example usage of MPS/MPO functionality: # dmrg(...) # siteinds(...) # MPS(...) # MPO(...) ``` -------------------------------- ### Basic ITensors Julia Code Template using let block Source: https://itensor.github.io/ITensors.jl/stable/getting_started/RunningCodes This code snippet demonstrates a basic template for writing Julia code that utilizes the ITensors library. It uses a `let...end` block to avoid potential issues with global scope in Julia, ensuring predictable behavior. The example initializes indices and creates a random ITensor. ```julia using ITensors let # ... your own code goes here ... # For example: i = Index(2,"i") j = Index(3,"j") T = random_itensor(i,j) @show T end ``` -------------------------------- ### Creating and Manipulating ITensors in Julia Source: https://itensor.github.io/ITensors.jl/stable/ITensorType Demonstrates how to create an ITensor with random elements, access and modify specific elements using index-value pairs, and inspect the ITensor's structure and storage. It also shows how ITensors can be added together even if their indices are in a different order. ```julia i = Index(2, "i") A = random_itensor(i', i) @show A; A[i => 1, i' => 2] = 1 @show A; @show storage(A); B = random_itensor(i, i') @show B; @show A + B; ``` -------------------------------- ### Get Index Direction Source: https://itensor.github.io/ITensors.jl/stable/IndexType Return the direction of an Index, which can be `ITensors.In`, `ITensors.Out`, or `ITensors.Neither`. ```julia dir(i::Index) ``` -------------------------------- ### Get Index Dimension Source: https://itensor.github.io/ITensors.jl/stable/IndexType Obtain the dimension of an Index. For QN Indices, this represents the sum of block dimensions. ```julia dim(i::Index) ``` -------------------------------- ### Enable Contraction Sequence Optimization (Julia) Source: https://itensor.github.io/ITensors.jl/stable/index This snippet shows how to enable optimized contraction sequences in ITensors.jl. It requires loading TensorOperations.jl or calling a specific enable function. This is necessary for the `optimal_contraction_sequence` function to work correctly. ```julia using ITensors using TensorOperations # Or to enable without loading TensorOperations.jl: # ITensors.enable_contraction_sequence_optimization() ``` -------------------------------- ### Get ITensor Indices using inds Source: https://itensor.github.io/ITensors.jl/stable/ITensorType Returns all the indices of an ITensor as a Tuple. This is useful for inspecting the structure and dimensions of the tensor. ```julia inds(T::ITensor) ``` -------------------------------- ### Get Specific ITensor Index using ind Source: https://itensor.github.io/ITensors.jl/stable/ITensorType Retrieves the Index object corresponding to a specific dimension of the ITensor. The dimension is specified by an integer. ```julia ind(T::ITensor, i::Int) ``` -------------------------------- ### Get Available CPU Threads Source: https://itensor.github.io/ITensors.jl/stable/Multithreading Retrieves the total number of CPU threads available to the system. This is a system-level setting and not directly controllable within Julia. ```julia julia> Sys.CPU_THREADS 6 ``` -------------------------------- ### Get QN Modulus by Name Source: https://itensor.github.io/ITensors.jl/stable/QN Retrieves the modulus associated with a specific name from a QN object. This is useful for QN objects that employ modular arithmetic. ```julia modulus(q::QN, name) ``` -------------------------------- ### Construct Undefined ITensor with QN Flux (Julia) Source: https://itensor.github.io/ITensors.jl/stable/ITensorType Initializes an ITensor with specified indices and QN flux, using BlockSparse storage with undefined elements. This is faster than initializing to a default value. The element type can be specified. ```Julia i = Index([QN(0)=>1, QN(1)=>2], "i") A = ITensor(undef,QN(0),i',dag(i)) B = ITensor(Float64,undef,QN(0),i',dag(i)) C = ITensor(ComplexF64,undef,QN(0),i',dag(i)) ``` -------------------------------- ### Get QN Value by Name Source: https://itensor.github.io/ITensors.jl/stable/QN Retrieves the integer value associated with a specific name from a QN object. If the name does not exist in the QN, it is treated as zero during operations. ```julia val(q::QN, name) ``` -------------------------------- ### Run ITensor Unit Tests Source: https://itensor.github.io/ITensors.jl/stable/faq/Development Executes the unit tests for the ITensor project to ensure code changes do not introduce regressions. Navigate to the test/ directory and run the provided script. ```shell cd test/ julia runtests.jl ``` -------------------------------- ### Get ITensor Index Direction using dir Source: https://itensor.github.io/ITensors.jl/stable/ITensorType Returns the direction of a given Index within an ITensor. This function helps understand the orientation or convention associated with an index. ```julia dir(A::ITensor, i::Index) ``` -------------------------------- ### Singular Value Decomposition (SVD) of a Matrix in Julia Source: https://itensor.github.io/ITensors.jl/stable/index Illustrates how to perform a Singular Value Decomposition (SVD) on a random ITensor matrix. The resulting factors (U, S, V) can be multiplied back together, with ITensor automatically handling index matching and summation. ```julia using ITensors let i = Index(10) # index of dimension 10 j = Index(20) # index of dimension 20 M = random_itensor(i,j) # random matrix, indices i,j U,S,V = svd(M,i) # compute SVD with i as row index @show M ≈ U*S*V # = true nothing end ``` -------------------------------- ### Running a Julia script from the command line Source: https://itensor.github.io/ITensors.jl/stable/getting_started/RunningCodes This command demonstrates how to execute a Julia script named `code.jl` directly from the command line. This method is suitable for longer computations or running jobs on a cluster. The `$` prefix indicates a shell command. ```shell $ julia code.jl ``` -------------------------------- ### Create ITensor with a Single Non-Zero Element (Julia) Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Shows how to create an ITensor with a single non-zero element (1.0) and all other elements as zero using the `onehot` function. This is useful for applications like creating product-state wavefunctions or setting specific tensor elements. The `onehot` function accepts index-value pairs to specify the location of the non-zero element. ```Julia i = Index(2) O1 = onehot(i=>1) println(O1) O2 = onehot(i=>2) println(O2) j = Index(3) T = onehot(i=>2,j=>3) println(T) ``` -------------------------------- ### QN Constructors Source: https://itensor.github.io/ITensors.jl/stable/QN Methods for constructing QN objects from various inputs, including tuples, named values, and single values with optional moduli. ```APIDOC ## Constructors: ITensors.QuantumNumbers.QN ### `QN(qvs...)` Constructs a `QN` object from a sequence of up to four named value tuples. Each tuple can be `(name, value)` or `(name, value, modulus)`. **Examples:** ```julia q = QN(("Sz", 1)) q = QN(("N", 1), ("Sz", -1)) q = QN(("P", 0, 2), ("Sz", 0)) ``` ### `QN(name, val::Int, modulus::Int=1)` Constructs a `QN` object with a single named value. `name` is the identifier, `val` is the integer value, and `modulus` (defaulting to 1) specifies the addition modulus. ### `QN(val::Int, modulus::Int=1)` Constructs a `QN` object with a single unnamed value (name is an empty string). `val` is the integer value, and `modulus` (defaulting to 1) specifies the addition modulus. ``` -------------------------------- ### Create ITensor from Array with QN Constraints (Julia) Source: https://itensor.github.io/ITensors.jl/stable/ITensorType Constructs an ITensor from a given array with specified indices and a tolerance for block sparsification. The resulting ITensor uses BlockSparse storage and respects QN constraints defined by the indices. ```Julia julia> i = Index([QN(0)=>1, QN(1)=>2], "i"); julia> A = [1e-9 0.0 0.0; 0.0 2.0 3.0; 0.0 1e-10 4.0]; julia> @show ITensor(A, i', dag(i); tol = 1e-8); ITensor(A, i', dag(i); tol = 1.0e-8) = ITensor ord=2 Dim 1: (dim=3|id=468|"i")' 1: QN(0) => 1 2: QN(1) => 2 Dim 2: (dim=3|id=468|"i") 1: QN(0) => 1 2: QN(1) => 2 NDTensors.BlockSparse{Float64,Array{Float64,1},2} 3×3 Block: (2, 2) [2:3, 2:3] 2.0 3.0 0.0 4.0 ``` -------------------------------- ### Get ITensor Element using getindex Source: https://itensor.github.io/ITensors.jl/stable/ITensorType Retrieves a specific element from an ITensor using a list of `IndexVal` or `Pair{<:Index, Int}`. This method allows for flexible access to tensor elements based on index values. ```julia getindex(T::ITensor, ivs...) ``` ```julia i = Index(2; tags = "i") A = ITensor(2.0, i, i') A[i => 1, i' => 2] # 2.0, same as: A[i' => 2, i => 1] ``` -------------------------------- ### Get and Check Index Tags Source: https://itensor.github.io/ITensors.jl/stable/IndexType Obtain the TagSet associated with an Index or verify if an Index contains specific tags. `tags` returns the TagSet, and `hastags` checks for the presence of given tags (as a string or TagSet). ```julia tags(i::Index) ``` ```julia hastags(i::Index, ts::Union{AbstractString,TagSet}) ``` ```julia i = Index(2, "SpinHalf,Site,n=3") println(hastags(i, "SpinHalf,Site")) ``` -------------------------------- ### Get and Check Index ID Source: https://itensor.github.io/ITensors.jl/stable/IndexType Retrieve the unique 64-digit integer ID of an Index or check if an Index possesses a specific ID. The `id` function returns the ID, while `hasid` performs a boolean check. ```julia id(i::Index) ``` ```julia hasid(i::Index, id::ITensors.IDType) ``` ```julia i = Index(2) println(hasid(i, id(i))) ``` -------------------------------- ### Tensor Factorization (SVD, QR) using ITensors (Julia) Source: https://itensor.github.io/ITensors.jl/stable/examples/ITensor Introduces ITensor's capabilities for performing tensor factorizations such as Singular Value Decomposition (SVD) and QR factorization. ITensors handle the underlying matrix reshaping and restoration of tensor structure automatically, allowing users to specify factorizations without needing to manage index ordering explicitly. ```Julia # Example usage for SVD (specific function calls not shown in input) # U, S, V = svd(A, (row_indices,), (col_indices,)) # Example usage for QR (specific function calls not shown in input) # Q, R = qr(A, (row_indices,), (col_indices,)) ```