From ff6a871f760d8f335cee1aea01d8c103b5e23a01 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Sun, 28 Apr 2024 21:15:16 -0400 Subject: [PATCH] [ITensors] Start organizing into submodules (#1400) --- Project.toml | 2 +- ext/ITensorsHDF5Ext/ITensorsHDF5Ext.jl | 2 - ext/ITensorsHDF5Ext/tagset.jl | 2 +- src/ITensors.jl | 125 ++++- src/exports.jl | 78 --- src/{physics => fermions}/fermions.jl | 7 +- src/global_variables.jl | 35 -- src/index.jl | 43 +- src/indexset.jl | 32 +- src/itensor.jl | 106 +--- .../ContractionSequenceOptimization.jl | 1 - .../{ => src}/breadth_first_constructive.jl | 0 .../{ => src}/contraction_cost.jl | 0 .../{ => src}/depth_first_constructive.jl | 0 .../{ => src}/three_tensors.jl | 0 .../{ => src}/utils.jl | 0 src/lib/ITensorChainRules/physics/sitetype.jl | 1 - .../{ => src}/ITensorChainRules.jl | 3 - .../{ => src}/LazyApply/LazyApply.jl | 0 .../{ => src}/NDTensors/dense.jl | 0 .../{ => src}/NDTensors/tensor.jl | 0 .../ITensorChainRules/{ => src}/indexset.jl | 0 .../ITensorChainRules/{ => src}/itensor.jl | 0 .../{ => src}/mps/abstractmps.jl | 0 .../ITensorChainRules/{ => src}/mps/mpo.jl | 0 .../ITensorChainRules/{ => src}/mps/mps.jl | 0 .../ITensorChainRules/{ => src}/projection.jl | 0 .../{ => src}/zygoterules.jl | 0 src/lib/ITensorMPS/{ => src}/ITensorMPS.jl | 18 +- src/lib/ITensorMPS/{ => src}/abstractmps.jl | 93 ++-- .../abstractprojmpo/abstractprojmpo.jl | 0 .../{ => src}/abstractprojmpo/diskprojmpo.jl | 0 .../{ => src}/abstractprojmpo/projmpo.jl | 0 .../abstractprojmpo/projmpo_apply.jl | 0 .../{ => src}/abstractprojmpo/projmpo_mps.jl | 0 .../{ => src}/abstractprojmpo/projmpo_mps2.jl | 0 .../{ => src}/abstractprojmpo/projmposum.jl | 0 .../{ => src}/abstractprojmpo/projmps.jl | 0 .../{ => src}/abstractprojmpo/projmps2.jl | 0 src/lib/ITensorMPS/{ => src}/adapt.jl | 0 .../{ => src}/alternating_update.jl | 0 .../ITensorMPS/{ => src}/contract_mpo_mps.jl | 0 src/lib/ITensorMPS/{ => src}/defaults.jl | 0 src/lib/ITensorMPS/{ => src}/deprecated.jl | 0 src/lib/ITensorMPS/{ => src}/dmrg.jl | 0 src/lib/ITensorMPS/{ => src}/dmrg_x.jl | 0 src/lib/ITensorMPS/{ => src}/exports.jl | 9 - src/lib/ITensorMPS/{ => src}/imports.jl | 6 +- .../ITensorMPS/src/lattices}/lattices.jl | 0 src/lib/ITensorMPS/{ => src}/linsolve.jl | 0 src/lib/ITensorMPS/{ => src}/mpo.jl | 11 +- src/lib/ITensorMPS/{ => src}/mps.jl | 7 +- src/lib/ITensorMPS/{ => src}/observer.jl | 0 .../{autompo => src/opsum_to_mpo}/matelem.jl | 0 .../opsum_to_mpo}/opsum_to_mpo.jl | 0 .../opsum_to_mpo}/opsum_to_mpo_generic.jl | 1 + .../opsum_to_mpo}/opsum_to_mpo_qn.jl | 0 .../opsum_to_mpo}/qnmatelem.jl | 0 src/lib/ITensorMPS/{ => src}/solver_utils.jl | 0 src/lib/ITensorMPS/{ => src}/sweep_update.jl | 0 src/lib/ITensorMPS/{ => src}/sweeps.jl | 0 src/lib/ITensorMPS/{ => src}/tdvp.jl | 0 src/lib/ITensorMPS/{ => src}/tdvp_sweeps.jl | 0 src/lib/ITensorMPS/{ => src}/tdvpinfo.jl | 0 src/lib/ITensorMPS/{ => src}/tdvporder.jl | 0 .../ITensorMPS/{ => src}/update_observer.jl | 0 .../{ => src}/ITensorNetworkMaps.jl | 0 .../{ => src}/ITensorVisualizationCore.jl | 0 .../{ => src}/visualize_macro.jl | 0 src/lib/ITensorsOpsExt/src/ITensorsOpsExt.jl | 79 +++ .../src/ITensorsSiteTypesExt.jl | 79 +++ src/lib/LazyApply/{ => src}/LazyApply.jl | 0 src/lib/Ops/ops_itensor.jl | 4 +- src/lib/Ops/src/Ops.jl | 4 + src/lib/Ops/{Ops.jl => src/op.jl} | 4 - src/lib/Ops/{ => src}/trotter.jl | 10 +- src/lib/QuantumNumbers/src/QuantumNumbers.jl | 5 + src/{ => lib/QuantumNumbers/src}/arrow.jl | 3 +- src/{qn => lib/QuantumNumbers/src}/qn.jl | 106 +--- src/lib/QuantumNumbers/src/qnval.jl | 63 +++ src/lib/SiteTypes/src/SiteTypes.jl | 19 + .../src/SiteTypesChainRulesCoreExt.jl | 7 + .../SiteTypes/src}/sitetype.jl | 48 +- .../SiteTypes/src/sitetypes}/aliases.jl | 0 .../SiteTypes/src/sitetypes}/boson.jl | 12 +- .../SiteTypes/src/sitetypes}/electron.jl | 132 ++--- .../SiteTypes/src/sitetypes}/fermion.jl | 44 +- .../SiteTypes/src/sitetypes}/generic_sites.jl | 4 + src/lib/SiteTypes/src/sitetypes/qubit.jl | 502 ++++++++++++++++++ .../SiteTypes/src/sitetypes}/qudit.jl | 8 +- .../SiteTypes/src/sitetypes}/spinhalf.jl | 16 +- src/lib/SiteTypes/src/sitetypes/spinone.jl | 159 ++++++ src/lib/SiteTypes/src/sitetypes/tj.jl | 234 ++++++++ src/lib/SmallStrings/src/SmallStrings.jl | 4 + .../src/SmallStringsChainRulesCoreExt.jl | 5 + src/{ => lib/SmallStrings/src}/smallstring.jl | 5 +- src/{tagset.jl => lib/TagSets/src/TagSets.jl} | 60 ++- src/name.jl | 1 + src/nullspace.jl | 4 +- src/physics/site_types/qubit.jl | 500 ----------------- src/physics/site_types/spinone.jl | 158 ------ src/physics/site_types/tj.jl | 234 -------- src/qn/qnindex.jl | 19 +- src/qn/qnitensor.jl | 5 +- src/val.jl | 1 + test/base/test_exports.jl | 6 +- test/base/test_smallstring.jl | 2 + test/lib/ITensorMPS/Project.toml | 10 + test/lib/ITensorMPS/base/Project.toml | 1 + .../ITensorMPS}/base/test_lattices.jl | 0 110 files changed, 1661 insertions(+), 1478 deletions(-) rename src/{physics => fermions}/fermions.jl (98%) rename src/lib/ContractionSequenceOptimization/{ => src}/ContractionSequenceOptimization.jl (99%) rename src/lib/ContractionSequenceOptimization/{ => src}/breadth_first_constructive.jl (100%) rename src/lib/ContractionSequenceOptimization/{ => src}/contraction_cost.jl (100%) rename src/lib/ContractionSequenceOptimization/{ => src}/depth_first_constructive.jl (100%) rename src/lib/ContractionSequenceOptimization/{ => src}/three_tensors.jl (100%) rename src/lib/ContractionSequenceOptimization/{ => src}/utils.jl (100%) delete mode 100644 src/lib/ITensorChainRules/physics/sitetype.jl rename src/lib/ITensorChainRules/{ => src}/ITensorChainRules.jl (91%) rename src/lib/ITensorChainRules/{ => src}/LazyApply/LazyApply.jl (100%) rename src/lib/ITensorChainRules/{ => src}/NDTensors/dense.jl (100%) rename src/lib/ITensorChainRules/{ => src}/NDTensors/tensor.jl (100%) rename src/lib/ITensorChainRules/{ => src}/indexset.jl (100%) rename src/lib/ITensorChainRules/{ => src}/itensor.jl (100%) rename src/lib/ITensorChainRules/{ => src}/mps/abstractmps.jl (100%) rename src/lib/ITensorChainRules/{ => src}/mps/mpo.jl (100%) rename src/lib/ITensorChainRules/{ => src}/mps/mps.jl (100%) rename src/lib/ITensorChainRules/{ => src}/projection.jl (100%) rename src/lib/ITensorChainRules/{ => src}/zygoterules.jl (100%) rename src/lib/ITensorMPS/{ => src}/ITensorMPS.jl (74%) rename src/lib/ITensorMPS/{ => src}/abstractmps.jl (96%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/abstractprojmpo.jl (100%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/diskprojmpo.jl (100%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/projmpo.jl (100%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/projmpo_apply.jl (100%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/projmpo_mps.jl (100%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/projmpo_mps2.jl (100%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/projmposum.jl (100%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/projmps.jl (100%) rename src/lib/ITensorMPS/{ => src}/abstractprojmpo/projmps2.jl (100%) rename src/lib/ITensorMPS/{ => src}/adapt.jl (100%) rename src/lib/ITensorMPS/{ => src}/alternating_update.jl (100%) rename src/lib/ITensorMPS/{ => src}/contract_mpo_mps.jl (100%) rename src/lib/ITensorMPS/{ => src}/defaults.jl (100%) rename src/lib/ITensorMPS/{ => src}/deprecated.jl (100%) rename src/lib/ITensorMPS/{ => src}/dmrg.jl (100%) rename src/lib/ITensorMPS/{ => src}/dmrg_x.jl (100%) rename src/lib/ITensorMPS/{ => src}/exports.jl (99%) rename src/lib/ITensorMPS/{ => src}/imports.jl (97%) rename src/{physics => lib/ITensorMPS/src/lattices}/lattices.jl (100%) rename src/lib/ITensorMPS/{ => src}/linsolve.jl (100%) rename src/lib/ITensorMPS/{ => src}/mpo.jl (98%) rename src/lib/ITensorMPS/{ => src}/mps.jl (99%) rename src/lib/ITensorMPS/{ => src}/observer.jl (100%) rename src/lib/ITensorMPS/{autompo => src/opsum_to_mpo}/matelem.jl (100%) rename src/lib/ITensorMPS/{autompo => src/opsum_to_mpo}/opsum_to_mpo.jl (100%) rename src/lib/ITensorMPS/{autompo => src/opsum_to_mpo}/opsum_to_mpo_generic.jl (99%) rename src/lib/ITensorMPS/{autompo => src/opsum_to_mpo}/opsum_to_mpo_qn.jl (100%) rename src/lib/ITensorMPS/{autompo => src/opsum_to_mpo}/qnmatelem.jl (100%) rename src/lib/ITensorMPS/{ => src}/solver_utils.jl (100%) rename src/lib/ITensorMPS/{ => src}/sweep_update.jl (100%) rename src/lib/ITensorMPS/{ => src}/sweeps.jl (100%) rename src/lib/ITensorMPS/{ => src}/tdvp.jl (100%) rename src/lib/ITensorMPS/{ => src}/tdvp_sweeps.jl (100%) rename src/lib/ITensorMPS/{ => src}/tdvpinfo.jl (100%) rename src/lib/ITensorMPS/{ => src}/tdvporder.jl (100%) rename src/lib/ITensorMPS/{ => src}/update_observer.jl (100%) rename src/lib/ITensorNetworkMaps/{ => src}/ITensorNetworkMaps.jl (100%) rename src/lib/ITensorVisualizationCore/{ => src}/ITensorVisualizationCore.jl (100%) rename src/lib/ITensorVisualizationCore/{ => src}/visualize_macro.jl (100%) create mode 100644 src/lib/ITensorsOpsExt/src/ITensorsOpsExt.jl create mode 100644 src/lib/ITensorsSiteTypesExt/src/ITensorsSiteTypesExt.jl rename src/lib/LazyApply/{ => src}/LazyApply.jl (100%) create mode 100644 src/lib/Ops/src/Ops.jl rename src/lib/Ops/{Ops.jl => src/op.jl} (99%) rename src/lib/Ops/{ => src}/trotter.jl (74%) create mode 100644 src/lib/QuantumNumbers/src/QuantumNumbers.jl rename src/{ => lib/QuantumNumbers/src}/arrow.jl (91%) rename src/{qn => lib/QuantumNumbers/src}/qn.jl (74%) create mode 100644 src/lib/QuantumNumbers/src/qnval.jl create mode 100644 src/lib/SiteTypes/src/SiteTypes.jl create mode 100644 src/lib/SiteTypes/src/SiteTypesChainRulesCoreExt.jl rename src/{physics => lib/SiteTypes/src}/sitetype.jl (94%) rename src/{physics/site_types => lib/SiteTypes/src/sitetypes}/aliases.jl (100%) rename src/{physics/site_types => lib/SiteTypes/src/sitetypes}/boson.jl (59%) rename src/{physics/site_types => lib/SiteTypes/src/sitetypes}/electron.jl (51%) rename src/{physics/site_types => lib/SiteTypes/src/sitetypes}/fermion.jl (58%) rename src/{physics/site_types => lib/SiteTypes/src/sitetypes}/generic_sites.jl (91%) create mode 100644 src/lib/SiteTypes/src/sitetypes/qubit.jl rename src/{physics/site_types => lib/SiteTypes/src/sitetypes}/qudit.jl (94%) rename src/{physics/site_types => lib/SiteTypes/src/sitetypes}/spinhalf.jl (72%) create mode 100644 src/lib/SiteTypes/src/sitetypes/spinone.jl create mode 100644 src/lib/SiteTypes/src/sitetypes/tj.jl create mode 100644 src/lib/SmallStrings/src/SmallStrings.jl create mode 100644 src/lib/SmallStrings/src/SmallStringsChainRulesCoreExt.jl rename src/{ => lib/SmallStrings/src}/smallstring.jl (97%) rename src/{tagset.jl => lib/TagSets/src/TagSets.jl} (85%) create mode 100644 src/name.jl delete mode 100644 src/physics/site_types/qubit.jl delete mode 100644 src/physics/site_types/spinone.jl delete mode 100644 src/physics/site_types/tj.jl create mode 100644 src/val.jl create mode 100644 test/lib/ITensorMPS/Project.toml rename test/{ => lib/ITensorMPS}/base/test_lattices.jl (100%) diff --git a/Project.toml b/Project.toml index d099639674..8355027c6a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ITensors" uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5" authors = ["Matthew Fishman ", "Miles Stoudenmire "] -version = "0.4.0" +version = "0.4.1" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/ext/ITensorsHDF5Ext/ITensorsHDF5Ext.jl b/ext/ITensorsHDF5Ext/ITensorsHDF5Ext.jl index f3c9db0376..4a6df70e59 100644 --- a/ext/ITensorsHDF5Ext/ITensorsHDF5Ext.jl +++ b/ext/ITensorsHDF5Ext/ITensorsHDF5Ext.jl @@ -1,5 +1,4 @@ module ITensorsHDF5Ext - include("index.jl") include("itensor.jl") include("qnindex.jl") @@ -8,5 +7,4 @@ include("qn.jl") include("tagset.jl") include("ITensorMPS/mps.jl") include("ITensorMPS/mpo.jl") - end diff --git a/ext/ITensorsHDF5Ext/tagset.jl b/ext/ITensorsHDF5Ext/tagset.jl index 47d226e204..d838d90b27 100644 --- a/ext/ITensorsHDF5Ext/tagset.jl +++ b/ext/ITensorsHDF5Ext/tagset.jl @@ -1,5 +1,5 @@ using HDF5: HDF5, attributes, create_group, open_group, read, write -using ITensors: TagSet, tagstring +using ITensors.TagSets: TagSet, tagstring function HDF5.write(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, T::TagSet) g = create_group(parent, name) diff --git a/src/ITensors.jl b/src/ITensors.jl index 7759f03d71..17d7d88ecb 100644 --- a/src/ITensors.jl +++ b/src/ITensors.jl @@ -51,65 +51,136 @@ Documentation: https://itensor.github.io/ITensors.jl/stable/ module ITensors include("usings.jl") include("utils.jl") -include("lib/ContractionSequenceOptimization/ContractionSequenceOptimization.jl") +include("lib/ContractionSequenceOptimization/src/ContractionSequenceOptimization.jl") +# TODO: `using .ContractionSequenceOptimization: ContractionSequenceOptimization, ...`. using .ContractionSequenceOptimization -include("lib/LazyApply/LazyApply.jl") +include("lib/LazyApply/src/LazyApply.jl") +# TODO: `using .LazyApply: LazyApply, ...`. using .LazyApply -include("lib/Ops/Ops.jl") +using .LazyApply: Prod, Scaled, Sum, coefficient +export Prod, Scaled, Sum, coefficient +include("lib/Ops/src/Ops.jl") +# TODO: `using .Ops: Ops, ...`. using .Ops +using .Ops: Ops, Op, Trotter import .Ops: sites, name +export Ops, Op, Trotter include("exports.jl") include("imports.jl") include("global_variables.jl") +# TODO: Move to `lib/LastVals/src/LastVals.jl`. include("lastval.jl") -include("smallstring.jl") +include("lib/SmallStrings/src/SmallStrings.jl") +using .SmallStrings: SmallStrings, IntChar, SmallString, Tag, isint, isnull include("readwrite.jl") +export readcpp +# TODO: Move to `lib/Nots/src/Nots.jl`. include("not.jl") -include("tagset.jl") -include("arrow.jl") +export not +include("lib/TagSets/src/TagSets.jl") +using .TagSets: TagSets, set_strict_tags!, using_strict_tags +# TODO: Move to `lib/Names/src/Names.jl`. +include("name.jl") +# TODO: Move to `lib/Vals/src/Vals.jl`. +include("val.jl") +export val +include("lib/QuantumNumbers/src/QuantumNumbers.jl") +using .QuantumNumbers: + Arrow, + In, + Neither, + Out, + QN, + QNVal, + hasname, + have_same_mods, + have_same_qns, + isactive, + maxQNs, + modulus, + nactive +export QN, isactive, modulus include("symmetrystyle.jl") include("index.jl") include("set_operations.jl") include("indexset.jl") include("itensor.jl") +include("qn/flux.jl") include("oneitensor.jl") include("tensor_operations/tensor_algebra.jl") include("tensor_operations/matrix_algebra.jl") include("tensor_operations/permutations.jl") +include("lib/SiteTypes/src/SiteTypes.jl") +using .SiteTypes: + SiteTypes, + OpName, + SiteType, + StateName, + TagType, + ValName, + @OpName_str, + @SiteType_str, + @StateName_str, + @TagType_str, + @ValName_str, + alias, + has_fermion_string, + op, + op!, + ops, + state +export OpName, + SiteType, + StateName, + TagType, + ValName, + @OpName_str, + @SiteType_str, + @StateName_str, + @TagType_str, + @ValName_str, + has_fermion_string, + op, + ops, + state, + val +include("lib/ITensorsSiteTypesExt/src/ITensorsSiteTypesExt.jl") include("broadcast.jl") include("tensor_operations/matrix_decomposition.jl") include("adapt.jl") include("set_types.jl") include("tensor_operations/itensor_combiner.jl") -include("qn/flux.jl") -include("qn/qn.jl") include("qn/qnindex.jl") include("qn/qnindexset.jl") include("qn/qnitensor.jl") include("nullspace.jl") -include("lib/Ops/ops_itensor.jl") -include("physics/sitetype.jl") -include("physics/lattices.jl") -include("physics/site_types/aliases.jl") -include("physics/site_types/generic_sites.jl") -include("physics/site_types/qubit.jl") -include("physics/site_types/spinhalf.jl") -include("physics/site_types/spinone.jl") -include("physics/site_types/fermion.jl") -include("physics/site_types/electron.jl") -include("physics/site_types/tj.jl") -include("physics/site_types/qudit.jl") -include("physics/site_types/boson.jl") -include("physics/fermions.jl") -include("lib/ITensorMPS/ITensorMPS.jl") +include("lib/ITensorsOpsExt/src/ITensorsOpsExt.jl") +include("fermions/fermions.jl") +export fparity, isfermionic +include("lib/ITensorMPS/src/ITensorMPS.jl") +# TODO: `using .ITensorMPS: ITensorMPS, ...` and +# explicit export list. @reexport using .ITensorMPS include("lib/ITensorsNamedDimsArraysExt/src/ITensorsNamedDimsArraysExt.jl") using .ITensorsNamedDimsArraysExt: ITensorsNamedDimsArraysExt -include("lib/Ops/trotter.jl") -include("lib/ITensorChainRules/ITensorChainRules.jl") -include("lib/ITensorNetworkMaps/ITensorNetworkMaps.jl") -include("lib/ITensorVisualizationCore/ITensorVisualizationCore.jl") +include("lib/ITensorChainRules/src/ITensorChainRules.jl") +include("lib/ITensorNetworkMaps/src/ITensorNetworkMaps.jl") +include("lib/ITensorVisualizationCore/src/ITensorVisualizationCore.jl") +# TODO: `using .ITensorVisualizationCore: ITensorVisualizationCore, ...`. using .ITensorVisualizationCore +using .ITensorVisualizationCore: + @visualize, + @visualize!, + @visualize_noeval, + @visualize_noeval!, + @visualize_sequence, + @visualize_sequence_noeval +export @visualize, + @visualize!, + @visualize_noeval, + @visualize_noeval!, + @visualize_sequence, + @visualize_sequence_noeval include("deprecated.jl") include("argsdict/argsdict.jl") include("packagecompile/compile.jl") diff --git a/src/exports.jl b/src/exports.jl index 08ba0c8986..d0e2d57dae 100644 --- a/src/exports.jl +++ b/src/exports.jl @@ -3,11 +3,9 @@ export # LinearAlgebra nullspace, tr, - # Modules LinearAlgebra, NDTensors, - # NDTensors module # Types Block, @@ -19,22 +17,10 @@ export truncerror, # Deprecated addblock!, - - # ITensorVisualizationCore module - # Macros - @visualize, - @visualize!, - @visualize_noeval, - @visualize_noeval!, - @visualize_sequence, - @visualize_sequence_noeval, - # ITensors.jl index_id_rng, - # argsdict/argsdict.jl argsdict, - # tensor_operations/matrix_decomposition.jl eigen, factorize, @@ -45,18 +31,14 @@ export ql, svd, diag, - # tensor_operations/tensor_algebra.jl contract, - # global_variables.jl # Methods - # Macros @disable_warn_order, @reset_warn_order, @set_warn_order, - # index.jl # Types Index, @@ -90,8 +72,6 @@ export space, splitblocks, tags, - val, - # indexset.jl # Types IndexSet, @@ -118,7 +98,6 @@ export swapinds, swapprime, swaptags, - # itensor.jl # Types ITensor, @@ -199,13 +178,6 @@ export unionind, vector, emptyITensor, - - # not.jl - not, - - # readwrite.jl - readcpp, - # tagset.jl # Types TagSet, @@ -214,60 +186,10 @@ export # Methods addtags, hastags, - - # LazyApply/LazyApply.jl - Scaled, - Sum, - Prod, - coefficient, - - # Ops/Ops.jl - Ops, - Op, - - # Ops/trotter.jl - Trotter, - # physics/autompo.jl AutoMPO, OpSum, add!, - - # physics/fermions.jl - fparity, - isfermionic, - - # physics/lattices.jl - Lattice, - LatticeBond, - square_lattice, - triangular_lattice, - - # physics/sitetype.jl - SiteType, - @SiteType_str, - StateName, - @StateName_str, - op, - ops, - OpName, - @OpName_str, - ValName, - @ValName_str, - state, - TagType, - @TagType_str, - has_fermion_string, - - # qn/qn.jl - # Types - QN, - # Methods - isactive, - isfermionic, - modulus, - val, - # qn/qnindex.jl blockdim, flux, diff --git a/src/physics/fermions.jl b/src/fermions/fermions.jl similarity index 98% rename from src/physics/fermions.jl rename to src/fermions/fermions.jl index a6e7a2364c..1ff55a7a40 100644 --- a/src/physics/fermions.jl +++ b/src/fermions/fermions.jl @@ -1,3 +1,4 @@ +using .QuantumNumbers: QuantumNumbers, QN """ parity_sign(P) @@ -19,7 +20,7 @@ function parity_sign(P)::Int return s end -isfermionic(qv::QNVal) = (modulus(qv) < 0) +isfermionic(qv::QuantumNumbers.QNVal) = (QuantumNumbers.modulus(qv) < 0) isfermionic(qn::QN) = any(isfermionic, qn) @@ -326,7 +327,7 @@ function NDTensors.before_combiner_signs( end return T -end #NDTensors.before_combiner_signs +end function NDTensors.after_combiner_signs( R, labelsR, indsR::NTuple{NR,QNIndex}, C, labelsC, indsC::NTuple{NC,QNIndex} @@ -374,4 +375,4 @@ function NDTensors.after_combiner_signs( end return R -end #NDTensors.after_combiner_signs +end diff --git a/src/global_variables.jl b/src/global_variables.jl index 9baed140a7..1db9724698 100644 --- a/src/global_variables.jl +++ b/src/global_variables.jl @@ -227,38 +227,3 @@ function disable_contraction_sequence_optimization() _using_contraction_sequence_optimization[] = false return nothing end - -# -# Turn the strict tags checking on and off -# - -const _using_strict_tags = Ref(false) - -""" -$(TYPEDSIGNATURES) -See if checking for overflow of the number of tags of a TagSet -or the number of characters of a tag is enabled or disabled. - -See also [`ITensors.set_strict_tags!`](@ref). -""" -function using_strict_tags() - return _using_strict_tags[] -end - -""" -$(TYPEDSIGNATURES) -Enable or disable checking for overflow of the number of tags of a TagSet -or the number of characters of a tag. If enabled (set to `true`), an error -will be thrown if overflow occurs, otherwise the overflow will be ignored -and the extra tags or tag characters will be dropped. This could cause -unexpected bugs if tags are being used to distinguish Index objects that -have the same ids and prime levels, but that is generally discouraged and -should only be used if you know what you are doing. - -See also [`ITensors.using_strict_tags`](@ref). -""" -function set_strict_tags!(enable::Bool) - previous = using_strict_tags() - _using_strict_tags[] = enable - return previous -end diff --git a/src/index.jl b/src/index.jl index a8f92376f2..6c38ca3911 100644 --- a/src/index.jl +++ b/src/index.jl @@ -1,3 +1,7 @@ +using NDTensors: NDTensors, sim +using .QuantumNumbers: QuantumNumbers, Arrow, In, Neither, Out +using .TagSets: + TagSets, TagSet, @ts_str, addtags, commontags, hastags, removetags, replacetags #const IDType = UInt128 const IDType = UInt64 @@ -128,7 +132,7 @@ copy(i::Index) = Index(id(i), copy(space(i)), dir(i), tags(i), plev(i)) Produces an `Index` with the same properties (dimension or QN structure) but with a new `id`. """ -function sim(i::Index; tags=copy(tags(i)), plev=plev(i), dir=dir(i)) +function NDTensors.sim(i::Index; tags=copy(tags(i)), plev=plev(i), dir=dir(i)) return Index(rand(index_id_rng(), IDType), copy(space(i)), dir, tags, plev) end @@ -199,9 +203,9 @@ Obtain the TagSet of an Index. """ tags(i::Index) = i.tags -commontags(is::Index...) = commontags(tags.(is)...) -commontags(is::Index) = tags(is) -commontags() = ts"" +TagSets.commontags(is::Index...) = commontags(tags.(is)...) +TagSets.commontags(is::Index) = tags(is) +TagSets.commontags() = ts"" """ plev(i::Index) @@ -230,9 +234,9 @@ julia> hastags(i, "Link") false ``` """ -hastags(i::Index, ts::Union{AbstractString,TagSet}) = hastags(tags(i), ts) +TagSets.hastags(i::Index, ts::Union{AbstractString,TagSet}) = hastags(tags(i), ts) -hastags(ts::Union{AbstractString,TagSet}) = x -> hastags(x, ts) +TagSets.hastags(ts::Union{AbstractString,TagSet}) = x -> hastags(x, ts) """ hasplev(i::Index, plev::Int) @@ -411,7 +415,7 @@ specified tags added to the existing ones. The `ts` argument can be a comma-separated string of tags or a TagSet. """ -addtags(i::Index, ts) = settags(i, addtags(tags(i), ts)) +TagSets.addtags(i::Index, ts) = settags(i, addtags(tags(i), ts)) """ removetags(i::Index, ts) @@ -420,7 +424,7 @@ Return a copy of Index `i` with the specified tags removed. The `ts` argument can be a comma-separated string of tags or a TagSet. """ -removetags(i::Index, ts) = settags(i, removetags(tags(i), ts)) +TagSets.removetags(i::Index, ts) = settags(i, removetags(tags(i), ts)) """ replacetags(i::Index, tsold, tsnew) @@ -445,9 +449,9 @@ julia> replacetags(i, "l" => "m") (dim=2|id=83|"m,x")' ``` """ -replacetags(i::Index, tsold, tsnew) = settags(i, replacetags(tags(i), tsold, tsnew)) +TagSets.replacetags(i::Index, tsold, tsnew) = settags(i, replacetags(tags(i), tsold, tsnew)) -replacetags(i::Index, rep_ts::Pair) = replacetags(i, rep_ts...) +TagSets.replacetags(i::Index, rep_ts::Pair) = replacetags(i, rep_ts...) """ prime(i::Index, plinc::Int = 1) @@ -567,7 +571,7 @@ removeqns(i::Index) = i Remove the specified QN from the Index, if it has any. """ -removeqn(i::Index, qn_name::String) = i +QuantumNumbers.removeqn(i::Index, qn_name::String) = i """ mergeblocks(::Index) @@ -597,10 +601,6 @@ end NDTensors.ind(iv::Pair{<:Index}) = first(iv) -val(iv::Pair{<:Index}) = val(iv.first, iv.second) - -val(i::Index, l::LastVal) = l.f(dim(i)) - """ isindequal(i::Index, iv::IndexVal) @@ -618,16 +618,6 @@ isindequal(iv1::Pair{<:Index}, iv2::Pair{<:Index}) = (ind(iv1) == ind(iv2)) plev(iv::Pair{<:Index}) = plev(ind(iv)) -# TODO: -# Implement a macro with a general definition: -# f(iv::Pair{<:Index}, args...) = (f(ind(iv), args...) => val(iv)) -prime(iv::Pair{<:Index}, inc::Integer=1) = (prime(ind(iv), inc) => val(iv)) -sim(iv::Pair{<:Index}, args...) = (sim(ind(iv), args...) => val(iv)) - -dag(iv::Pair{<:Index}) = (dag(ind(iv)) => val(iv)) - -Base.adjoint(iv::Pair{<:Index}) = (prime(ind(iv)) => val(iv)) - dir(iv::Pair{<:Index}) = dir(ind(iv)) ####################### @@ -655,7 +645,8 @@ function Base.show(io::IO, i::Index) idstr = "$(id(i) % 1000)" if length(tags(i)) > 0 print( - io, "(dim=$(space(i))|id=$(idstr)|\"$(tagstring(tags(i)))\")$(primestring(plev(i)))" + io, + "(dim=$(space(i))|id=$(idstr)|\"$(TagSets.tagstring(tags(i)))\")$(primestring(plev(i)))", ) else print(io, "(dim=$(space(i))|id=$(idstr))$(primestring(plev(i)))") diff --git a/src/indexset.jl b/src/indexset.jl index 96400e68f0..446b8a2c45 100644 --- a/src/indexset.jl +++ b/src/indexset.jl @@ -1,3 +1,7 @@ +using NDTensors: NDTensors, sim +using .QuantumNumbers: QuantumNumbers, Arrow, removeqn +using .TagSets: TagSets, addtags, commontags, hastags, removetags, replacetags + # Represents a static order of an ITensor @eval struct Order{N} (OrderT::Type{<:Order})() = $(Expr(:new, :OrderT)) @@ -151,7 +155,7 @@ Make a new Indices with similar indices. You can also use the broadcast version `sim.(is)`. """ -sim(is::Indices) = map(i -> sim(i), is) +NDTensors.sim(is::Indices) = map(i -> sim(i), is) function trivial_index(is::Indices) if isempty(is) @@ -197,7 +201,7 @@ end Return a TagSet of the tags that are common to all of the indices. """ -commontags(is::Indices) = commontags(is...) +TagSets.commontags(is::Indices) = commontags(is...) # # Set operations @@ -412,9 +416,11 @@ function replaceprime(is::Indices, rep_pls::Pair{Int,Int}...; kwargs...) return replaceprime(fmatch(; kwargs...), is, rep_pls...) end -addtags(f::Function, is::Indices, args...) = map(i -> f(i) ? addtags(i, args...) : i, is) +function TagSets.addtags(f::Function, is::Indices, args...) + return map(i -> f(i) ? addtags(i, args...) : i, is) +end -function addtags(is::Indices, tags, args...; kwargs...) +function TagSets.addtags(is::Indices, tags, args...; kwargs...) return addtags(fmatch(args...; kwargs...), is, tags) end @@ -464,11 +470,11 @@ end eachindval(is::Index...) = eachindval(is) eachindval(is::Tuple{Vararg{Index}}) = (is .=> Tuple(ns) for ns in eachval(is)) -function removetags(f::Function, is::Indices, args...) +function TagSets.removetags(f::Function, is::Indices, args...) return map(i -> f(i) ? removetags(i, args...) : i, is) end -function removetags(is::Indices, tags, args...; kwargs...) +function TagSets.removetags(is::Indices, tags, args...; kwargs...) return removetags(fmatch(args...; kwargs...), is, tags) end @@ -489,7 +495,7 @@ Check if any of the indices in the Indices have the specified tags. """ anyhastags(is::Indices, ts) = any(i -> hastags(i, ts), is) -hastags(is::Indices, ts) = anyhastags(is, ts) +TagSets.hastags(is::Indices, ts) = anyhastags(is, ts) # XXX new syntax # hastags(all, is, ts) @@ -501,18 +507,20 @@ Check if all of the indices in the Indices have the specified tags. allhastags(is::Indices, ts::String) = all(i -> hastags(i, ts), is) # Version taking a list of Pairs -function replacetags(f::Function, is::Indices, rep_ts::Pair...) +function TagSets.replacetags(f::Function, is::Indices, rep_ts::Pair...) return map(i -> f(i) ? _replacetags(i, rep_ts...) : i, is) end -function replacetags(is::Indices, rep_ts::Pair...; kwargs...) +function TagSets.replacetags(is::Indices, rep_ts::Pair...; kwargs...) return replacetags(fmatch(; kwargs...), is, rep_ts...) end # Version taking two input TagSets/Strings -replacetags(f::Function, is::Indices, tags1, tags2) = replacetags(f, is, tags1 => tags2) +function TagSets.replacetags(f::Function, is::Indices, tags1, tags2) + return replacetags(f, is, tags1 => tags2) +end -function replacetags(is::Indices, tags1, tags2, args...; kwargs...) +function TagSets.replacetags(is::Indices, tags1, tags2, args...; kwargs...) return replacetags(fmatch(args...; kwargs...), is, tags1 => tags2) end @@ -631,7 +639,7 @@ end swapind(is::Indices, i1::Index, i2::Index) = swapinds(is, (i1,), (i2,)) removeqns(is::Indices) = map(removeqns, is) -function removeqn(is::Indices, qn_name::String; mergeblocks=true) +function QuantumNumbers.removeqn(is::Indices, qn_name::String; mergeblocks=true) return map(i -> removeqn(i, qn_name; mergeblocks), is) end mergeblocks(is::Indices) = map(mergeblocks, is) diff --git a/src/itensor.jl b/src/itensor.jl index efa9249382..57d6f76d34 100644 --- a/src/itensor.jl +++ b/src/itensor.jl @@ -1,3 +1,5 @@ +using .TagSets: TagSets, hastags, replacetags + # Private inner constructor function _ITensor end @@ -551,35 +553,6 @@ delta(is...) = delta(Float64, is...) const δ = delta -""" - onehot(ivs...) - setelt(ivs...) - onehot(::Type, ivs...) - setelt(::Type, ivs...) - -Create an ITensor with all zeros except the specified value, -which is set to 1. - -# Examples -```julia -i = Index(2,"i") -A = onehot(i=>2) -# A[i=>2] == 1, all other elements zero - -# Specify the element type -A = onehot(Float32, i=>2) - -j = Index(3,"j") -B = onehot(i=>1,j=>3) -# B[i=>1,j=>3] == 1, all other element zero -``` -""" -function onehot(datatype::Type{<:AbstractArray}, ivs::Pair{<:Index}...) - A = ITensor(eltype(datatype), ind.(ivs)...) - A[val.(ivs)...] = one(eltype(datatype)) - return Adapt.adapt(datatype, A) -end - function onehot(eltype::Type{<:Number}, ivs::Pair{<:Index}...) return onehot(NDTensors.default_datatype(eltype), ivs...) end @@ -1126,14 +1099,6 @@ end # CartesianIndices @propagate_inbounds getindex(T::ITensor, I::CartesianIndex)::Any = T[Tuple(I)...] -@propagate_inbounds @inline function _getindex(T::Tensor, ivs::Vararg{Any,N}) where {N} - # Tried ind.(ivs), val.(ivs) but it is slower - p = NDTensors.getperm(inds(T), ntuple(n -> ind(@inbounds ivs[n]), Val(N))) - fac = NDTensors.permfactor(p, ivs...) # possible sign - return fac * - _getindex(T, NDTensors.permute(ntuple(n -> val(@inbounds ivs[n]), Val(N)), p)...) -end - """ getindex(T::ITensor, ivs...) @@ -1162,10 +1127,6 @@ A[i => 1, i' => 2] # 2.0, same as: A[i' => 2, i => 1] return tensor(T)[] end -function _vals(is::Indices, I::String...) - return val.(is, I) -end - function _vals(T::ITensor, I::String...) return _vals(inds(T), I...) end @@ -1247,31 +1208,12 @@ end return setindex!(T, x, Tuple(I)...) end -@propagate_inbounds @inline function _setindex!!( - T::Tensor, x::Number, ivs::Vararg{Any,N} -) where {N} - # Would be nice to split off the functions for extracting the `ind` and `val` as Tuples, - # but it was slower. - p = NDTensors.getperm(inds(T), ntuple(n -> ind(@inbounds ivs[n]), Val(N))) - fac = NDTensors.permfactor(p, ivs...) # possible sign - return _setindex!!( - T, fac * x, NDTensors.permute(ntuple(n -> val(@inbounds ivs[n]), Val(N)), p)... - ) -end - @propagate_inbounds @inline function setindex!( T::ITensor, x::Number, I::Vararg{Any,N} ) where {N} return settensor!(T, _setindex!!(tensor(T), x, I...)) end -@propagate_inbounds @inline function setindex!( - T::ITensor, x::Number, I1::Pair{<:Index,String}, I::Pair{<:Index,String}... -) - Iv = map(i -> i.first => val(i.first, i.second), (I1, I...)) - return setindex!(T, x, Iv...) -end - # XXX: what is this definition for? Base.checkbounds(::Any, ::Block) = nothing @@ -1523,22 +1465,22 @@ filterinds(is::Indices) = is inds(A...; kwargs...) = filterinds(A...; kwargs...) # in-place versions of priming and tagging -for fname in ( - :prime, - :setprime, - :noprime, - :replaceprime, - :swapprime, - :addtags, - :removetags, - :replacetags, - :settags, - :swaptags, - :replaceind, - :replaceinds, - :swapind, - :swapinds, -) +for (fname, fname!) in [ + (:(prime), :(prime!)), + (:(setprime), :(setprime!)), + (:(noprime), :(noprime!)), + (:(replaceprime), :(replaceprime!)), + (:(swapprime), :(swapprime!)), + (:(TagSets.addtags), :(addtags!)), + (:(TagSets.removetags), :(removetags!)), + (:(TagSets.replacetags), :(replacetags!)), + (:(settags), :(settags!)), + (:(swaptags), :(swaptags!)), + (:(replaceind), :(replaceind!)), + (:(replaceinds), :(replaceinds!)), + (:(swapind), :(swapind!)), + (:(swapinds), :(swapinds!)), +] @eval begin $fname(f::Function, A::ITensor, args...) = ITensor($fname(f, tensor(A), args...)) @@ -1547,7 +1489,7 @@ for fname in ( return setinds(A, $fname(f, inds(A), args...)) end - function $(Symbol(fname, :!))(f::Function, A::ITensor, args...) + function $(fname!)(f::Function, A::ITensor, args...) return settensor!(A, $fname(f, tensor(A), args...)) end @@ -1558,7 +1500,7 @@ for fname in ( return setinds(A, $fname(inds(A), args...; kwargs...)) end - function $(Symbol(fname, :!))(A::ITensor, args...; kwargs...) + function $(fname!)(A::ITensor, args...; kwargs...) return settensor!(A, $fname(tensor(A), args...; kwargs...)) end end @@ -1642,7 +1584,7 @@ $priming_tagging_doc Add the tags `ts` to the indices of an ITensor or collection of indices. $priming_tagging_doc -""" addtags(::ITensor, ::Any...) +""" TagSets.addtags(::ITensor, ::Any...) @doc """ removetags[!](A::ITensor, ts::String; ) -> ITensor @@ -1652,7 +1594,7 @@ $priming_tagging_doc Remove the tags `ts` from the indices of an ITensor or collection of indices. $priming_tagging_doc -""" removetags(::ITensor, ::Any...) +""" TagSets.removetags(::ITensor, ::Any...) @doc """ settags[!](A::ITensor, ts::String; ) -> ITensor @@ -1672,7 +1614,7 @@ $priming_tagging_doc Replace the tags `tsold` with `tsnew` for the indices of an ITensor. $priming_tagging_doc -""" replacetags(::ITensor, ::Any...) +""" TagSets.replacetags(::ITensor, ::Any...) @doc """ swaptags[!](A::ITensor, ts1::String, ts2::String; ) -> ITensor @@ -1740,7 +1682,7 @@ Check if any of the indices in the ITensor have the specified tags. """ anyhastags(A::ITensor, ts) = anyhastags(inds(A), ts) -hastags(A::ITensor, ts) = hastags(inds(A), ts) +TagSets.hastags(A::ITensor, ts) = hastags(inds(A), ts) # XXX: rename to: # hastags(all, A, ts) diff --git a/src/lib/ContractionSequenceOptimization/ContractionSequenceOptimization.jl b/src/lib/ContractionSequenceOptimization/src/ContractionSequenceOptimization.jl similarity index 99% rename from src/lib/ContractionSequenceOptimization/ContractionSequenceOptimization.jl rename to src/lib/ContractionSequenceOptimization/src/ContractionSequenceOptimization.jl index 11924e0cfa..e580b90903 100644 --- a/src/lib/ContractionSequenceOptimization/ContractionSequenceOptimization.jl +++ b/src/lib/ContractionSequenceOptimization/src/ContractionSequenceOptimization.jl @@ -1,4 +1,3 @@ - module ContractionSequenceOptimization export optimal_contraction_sequence, contraction_cost diff --git a/src/lib/ContractionSequenceOptimization/breadth_first_constructive.jl b/src/lib/ContractionSequenceOptimization/src/breadth_first_constructive.jl similarity index 100% rename from src/lib/ContractionSequenceOptimization/breadth_first_constructive.jl rename to src/lib/ContractionSequenceOptimization/src/breadth_first_constructive.jl diff --git a/src/lib/ContractionSequenceOptimization/contraction_cost.jl b/src/lib/ContractionSequenceOptimization/src/contraction_cost.jl similarity index 100% rename from src/lib/ContractionSequenceOptimization/contraction_cost.jl rename to src/lib/ContractionSequenceOptimization/src/contraction_cost.jl diff --git a/src/lib/ContractionSequenceOptimization/depth_first_constructive.jl b/src/lib/ContractionSequenceOptimization/src/depth_first_constructive.jl similarity index 100% rename from src/lib/ContractionSequenceOptimization/depth_first_constructive.jl rename to src/lib/ContractionSequenceOptimization/src/depth_first_constructive.jl diff --git a/src/lib/ContractionSequenceOptimization/three_tensors.jl b/src/lib/ContractionSequenceOptimization/src/three_tensors.jl similarity index 100% rename from src/lib/ContractionSequenceOptimization/three_tensors.jl rename to src/lib/ContractionSequenceOptimization/src/three_tensors.jl diff --git a/src/lib/ContractionSequenceOptimization/utils.jl b/src/lib/ContractionSequenceOptimization/src/utils.jl similarity index 100% rename from src/lib/ContractionSequenceOptimization/utils.jl rename to src/lib/ContractionSequenceOptimization/src/utils.jl diff --git a/src/lib/ITensorChainRules/physics/sitetype.jl b/src/lib/ITensorChainRules/physics/sitetype.jl deleted file mode 100644 index 71363237ec..0000000000 --- a/src/lib/ITensorChainRules/physics/sitetype.jl +++ /dev/null @@ -1 +0,0 @@ -@non_differentiable has_fermion_string(::AbstractString, ::Index) diff --git a/src/lib/ITensorChainRules/ITensorChainRules.jl b/src/lib/ITensorChainRules/src/ITensorChainRules.jl similarity index 91% rename from src/lib/ITensorChainRules/ITensorChainRules.jl rename to src/lib/ITensorChainRules/src/ITensorChainRules.jl index 0b5aceaeb8..fceb135850 100644 --- a/src/lib/ITensorChainRules/ITensorChainRules.jl +++ b/src/lib/ITensorChainRules/src/ITensorChainRules.jl @@ -26,7 +26,6 @@ include(joinpath("NDTensors", "tensor.jl")) include(joinpath("NDTensors", "dense.jl")) include("indexset.jl") include("itensor.jl") -include(joinpath("physics", "sitetype.jl")) include(joinpath("mps", "abstractmps.jl")) include(joinpath("mps", "mps.jl")) include(joinpath("mps", "mpo.jl")) @@ -41,8 +40,6 @@ include("zygoterules.jl") @non_differentiable commoninds(::Any...) @non_differentiable noncommoninds(::Any...) @non_differentiable uniqueinds(::Any...) -@non_differentiable SiteType(::Any) -@non_differentiable ITensors._sitetypes(::Any) @non_differentiable addtags(::TagSet, ::Any) @non_differentiable ITensors.filter_inds_set_function(::Function, ::Function, ::Any...) @non_differentiable ITensors.filter_inds_set_function(::Function, ::Any...) diff --git a/src/lib/ITensorChainRules/LazyApply/LazyApply.jl b/src/lib/ITensorChainRules/src/LazyApply/LazyApply.jl similarity index 100% rename from src/lib/ITensorChainRules/LazyApply/LazyApply.jl rename to src/lib/ITensorChainRules/src/LazyApply/LazyApply.jl diff --git a/src/lib/ITensorChainRules/NDTensors/dense.jl b/src/lib/ITensorChainRules/src/NDTensors/dense.jl similarity index 100% rename from src/lib/ITensorChainRules/NDTensors/dense.jl rename to src/lib/ITensorChainRules/src/NDTensors/dense.jl diff --git a/src/lib/ITensorChainRules/NDTensors/tensor.jl b/src/lib/ITensorChainRules/src/NDTensors/tensor.jl similarity index 100% rename from src/lib/ITensorChainRules/NDTensors/tensor.jl rename to src/lib/ITensorChainRules/src/NDTensors/tensor.jl diff --git a/src/lib/ITensorChainRules/indexset.jl b/src/lib/ITensorChainRules/src/indexset.jl similarity index 100% rename from src/lib/ITensorChainRules/indexset.jl rename to src/lib/ITensorChainRules/src/indexset.jl diff --git a/src/lib/ITensorChainRules/itensor.jl b/src/lib/ITensorChainRules/src/itensor.jl similarity index 100% rename from src/lib/ITensorChainRules/itensor.jl rename to src/lib/ITensorChainRules/src/itensor.jl diff --git a/src/lib/ITensorChainRules/mps/abstractmps.jl b/src/lib/ITensorChainRules/src/mps/abstractmps.jl similarity index 100% rename from src/lib/ITensorChainRules/mps/abstractmps.jl rename to src/lib/ITensorChainRules/src/mps/abstractmps.jl diff --git a/src/lib/ITensorChainRules/mps/mpo.jl b/src/lib/ITensorChainRules/src/mps/mpo.jl similarity index 100% rename from src/lib/ITensorChainRules/mps/mpo.jl rename to src/lib/ITensorChainRules/src/mps/mpo.jl diff --git a/src/lib/ITensorChainRules/mps/mps.jl b/src/lib/ITensorChainRules/src/mps/mps.jl similarity index 100% rename from src/lib/ITensorChainRules/mps/mps.jl rename to src/lib/ITensorChainRules/src/mps/mps.jl diff --git a/src/lib/ITensorChainRules/projection.jl b/src/lib/ITensorChainRules/src/projection.jl similarity index 100% rename from src/lib/ITensorChainRules/projection.jl rename to src/lib/ITensorChainRules/src/projection.jl diff --git a/src/lib/ITensorChainRules/zygoterules.jl b/src/lib/ITensorChainRules/src/zygoterules.jl similarity index 100% rename from src/lib/ITensorChainRules/zygoterules.jl rename to src/lib/ITensorChainRules/src/zygoterules.jl diff --git a/src/lib/ITensorMPS/ITensorMPS.jl b/src/lib/ITensorMPS/src/ITensorMPS.jl similarity index 74% rename from src/lib/ITensorMPS/ITensorMPS.jl rename to src/lib/ITensorMPS/src/ITensorMPS.jl index fd4479e7ac..315b627d36 100644 --- a/src/lib/ITensorMPS/ITensorMPS.jl +++ b/src/lib/ITensorMPS/src/ITensorMPS.jl @@ -1,7 +1,5 @@ module ITensorMPS - using ..ITensors - include("imports.jl") include("exports.jl") include("abstractmps.jl") @@ -17,13 +15,12 @@ include("abstractprojmpo/projmpo_mps.jl") include("observer.jl") include("dmrg.jl") include("adapt.jl") -include("autompo/matelem.jl") -include("autompo/qnmatelem.jl") -include("autompo/opsum_to_mpo_generic.jl") -include("autompo/opsum_to_mpo.jl") -include("autompo/opsum_to_mpo_qn.jl") +include("opsum_to_mpo/matelem.jl") +include("opsum_to_mpo/qnmatelem.jl") +include("opsum_to_mpo/opsum_to_mpo_generic.jl") +include("opsum_to_mpo/opsum_to_mpo.jl") +include("opsum_to_mpo/opsum_to_mpo_qn.jl") include("deprecated.jl") - include("abstractprojmpo/projmpo_apply.jl") include("abstractprojmpo/projmps2.jl") include("abstractprojmpo/projmpo_mps2.jl") @@ -38,5 +35,6 @@ include("tdvp.jl") include("dmrg_x.jl") include("contract_mpo_mps.jl") include("linsolve.jl") - -end # module ITensorMPS +include("lattices/lattices.jl") +export Lattice, LatticeBond, square_lattice, triangular_lattice +end diff --git a/src/lib/ITensorMPS/abstractmps.jl b/src/lib/ITensorMPS/src/abstractmps.jl similarity index 96% rename from src/lib/ITensorMPS/abstractmps.jl rename to src/lib/ITensorMPS/src/abstractmps.jl index c4b98a2716..140573b1bf 100644 --- a/src/lib/ITensorMPS/abstractmps.jl +++ b/src/lib/ITensorMPS/src/abstractmps.jl @@ -1,5 +1,9 @@ using IsApprox: Approx, IsApprox -using NDTensors: using_auto_fermion, scalartype, tensor +using ..ITensors: ITensors +using NDTensors: NDTensors, using_auto_fermion, scalartype, tensor +using ..QuantumNumbers: QuantumNumbers, removeqn +using ..SiteTypes: SiteTypes, siteinds +using ..TagSets: TagSets abstract type AbstractMPS end @@ -8,16 +12,16 @@ abstract type AbstractMPS end The number of sites of an MPS/MPO. """ -length(m::AbstractMPS) = length(data(m)) +Base.length(m::AbstractMPS) = length(data(m)) """ size(::MPS/MPO) The number of sites of an MPS/MPO, in a tuple. """ -size(m::AbstractMPS) = size(data(m)) +Base.size(m::AbstractMPS) = size(data(m)) -ndims(m::AbstractMPS) = ndims(data(m)) +Base.ndims(m::AbstractMPS) = ndims(data(m)) function promote_itensor_eltype(m::Vector{ITensor}) T = isassigned(m, 1) ? eltype(m[1]) : Number @@ -61,12 +65,12 @@ The element type of the MPS/MPO. Always returns `ITensor`. For the element type of the ITensors of the MPS/MPO, use `promote_itensor_eltype`. """ -eltype(::AbstractMPS) = ITensor +Base.eltype(::AbstractMPS) = ITensor -complex(ψ::AbstractMPS) = complex.(ψ) -real(ψ::AbstractMPS) = real.(ψ) -imag(ψ::AbstractMPS) = imag.(ψ) -conj(ψ::AbstractMPS) = conj.(ψ) +Base.complex(ψ::AbstractMPS) = complex.(ψ) +Base.real(ψ::AbstractMPS) = real.(ψ) +Base.imag(ψ::AbstractMPS) = imag.(ψ) +Base.conj(ψ::AbstractMPS) = conj.(ψ) function convert_leaf_eltype(eltype::Type, ψ::AbstractMPS) return map(ψᵢ -> convert_leaf_eltype(eltype, ψᵢ), ψ; set_limits=false) @@ -278,9 +282,9 @@ julia> norm(M3) 3.0000000000000004 ``` """ -copy(m::AbstractMPS) = typeof(m)(copy(data(m)), leftlim(m), rightlim(m)) +Base.copy(m::AbstractMPS) = typeof(m)(copy(data(m)), leftlim(m), rightlim(m)) -similar(m::AbstractMPS) = typeof(m)(similar(data(m)), 0, length(m)) +Base.similar(m::AbstractMPS) = typeof(m)(similar(data(m)), 0, length(m)) """ deepcopy(::MPS) @@ -463,7 +467,7 @@ end Get the site index (or indices) of MPO `A` that is unique to `A` (not shared with MPS/MPO `B`). """ -function siteinds( +function SiteTypes.siteinds( f::Union{typeof(uniqueinds),typeof(uniqueind)}, A::AbstractMPS, B::AbstractMPS, @@ -483,7 +487,7 @@ end Get the site indices of MPO `A` that are unique to `A` (not shared with MPS/MPO `B`), as a `Vector{<:Index}`. """ -function siteinds( +function SiteTypes.siteinds( f::Union{typeof(uniqueinds),typeof(uniqueind)}, A::AbstractMPS, B::AbstractMPS; kwargs... ) return [siteinds(f, A, B, j; kwargs...) for j in eachindex(A)] @@ -495,7 +499,7 @@ end Get the site index (or indices) of the `j`th MPO tensor of `A` that is shared with MPS/MPO `B`. """ -function siteinds( +function SiteTypes.siteinds( f::Union{typeof(commoninds),typeof(commonind)}, A::AbstractMPS, B::AbstractMPS, @@ -511,7 +515,7 @@ end Get a vector of the site index (or indices) of MPO `A` that is shared with MPS/MPO `B`. """ -function siteinds( +function SiteTypes.siteinds( f::Union{typeof(commoninds),typeof(commonind)}, A::AbstractMPS, B::AbstractMPS; kwargs... ) return [siteinds(f, A, B, j) for j in eachindex(A)] @@ -617,7 +621,7 @@ Return the first site Index found on the MPS or MPO You can choose different filters, like prime level and tags, with the `kwargs`. """ -function siteind(::typeof(first), M::AbstractMPS, j::Integer; kwargs...) +function SiteTypes.siteind(::typeof(first), M::AbstractMPS, j::Integer; kwargs...) N = length(M) (N == 1) && return firstind(M[1]; kwargs...) if j == 1 @@ -639,7 +643,7 @@ at the site `j` as an IndexSet. Optionally filter prime tags and prime levels with keyword arguments like `plev` and `tags`. """ -function siteinds(M::AbstractMPS, j::Integer; kwargs...) +function SiteTypes.siteinds(M::AbstractMPS, j::Integer; kwargs...) N = length(M) (N == 1) && return inds(M[1]; kwargs...) if j == 1 @@ -652,19 +656,19 @@ function siteinds(M::AbstractMPS, j::Integer; kwargs...) return si end -function siteinds(::typeof(all), ψ::AbstractMPS, n::Integer; kwargs...) +function SiteTypes.siteinds(::typeof(all), ψ::AbstractMPS, n::Integer; kwargs...) return siteinds(ψ, n; kwargs...) end -function siteinds(::typeof(first), ψ::AbstractMPS; kwargs...) +function SiteTypes.siteinds(::typeof(first), ψ::AbstractMPS; kwargs...) return [siteind(first, ψ, j; kwargs...) for j in 1:length(ψ)] end -function siteinds(::typeof(only), ψ::AbstractMPS; kwargs...) +function SiteTypes.siteinds(::typeof(only), ψ::AbstractMPS; kwargs...) return [siteind(only, ψ, j; kwargs...) for j in 1:length(ψ)] end -function siteinds(::typeof(all), ψ::AbstractMPS; kwargs...) +function SiteTypes.siteinds(::typeof(all), ψ::AbstractMPS; kwargs...) return [siteinds(ψ, j; kwargs...) for j in 1:length(ψ)] end @@ -699,20 +703,18 @@ function map(f::Function, M::AbstractMPS; set_limits::Bool=true) return map!(f, copy(M); set_limits=set_limits) end -for fname in ( - :dag, - :prime, - :setprime, - :noprime, - :swapprime, - :replaceprime, - :addtags, - :removetags, - :replacetags, - :settags, -) - fname! = Symbol(fname, :!) - +for (fname, fname!) in [ + (:(ITensors.dag), :(dag!)), + (:(ITensors.prime), :(ITensors.prime!)), + (:(ITensors.setprime), :(ITensors.setprime!)), + (:(ITensors.noprime), :(ITensors.noprime!)), + (:(ITensors.swapprime), :(ITensors.swapprime!)), + (:(ITensors.replaceprime), :(ITensors.replaceprime!)), + (:(TagSets.addtags), :(ITensors.addtags!)), + (:(TagSets.removetags), :(ITensors.removetags!)), + (:(TagSets.replacetags), :(ITensors.replacetags!)), + (:(ITensors.settags), :(ITensors.settags!)), +] @eval begin """ $($fname)[!](M::MPS, args...; kwargs...) @@ -847,9 +849,16 @@ function hassamenuminds(::typeof(siteinds), M1::AbstractMPS, M2::AbstractMPS) return true end -for fname in - (:sim, :prime, :setprime, :noprime, :addtags, :removetags, :replacetags, :settags) - fname! = Symbol(fname, :!) +for (fname, fname!) in [ + (:(NDTensors.sim), :(sim!)), + (:(ITensors.prime), :(ITensors.prime!)), + (:(ITensors.setprime), :(ITensors.setprime!)), + (:(ITensors.noprime), :(ITensors.noprime!)), + (:(TagSets.addtags), :(ITensors.addtags!)), + (:(TagSets.removetags), :(ITensors.removetags!)), + (:(TagSets.replacetags), :(ITensors.replacetags!)), + (:(ITensors.settags), :(ITensors.settags!)), +] @eval begin """ $($fname)[!](linkinds, M::MPS, args...; kwargs...) @@ -2346,7 +2355,7 @@ function splitblocks(::typeof(linkinds), M::AbstractMPS; tol=0) end removeqns(M::AbstractMPS) = map(removeqns, M; set_limits=false) -function removeqn(M::AbstractMPS, qn_name::String) +function QuantumNumbers.removeqn(M::AbstractMPS, qn_name::String) return map(m -> removeqn(m, qn_name), M; set_limits=false) end @@ -2369,11 +2378,13 @@ function copyto!(ψ::AbstractMPS, b::Broadcasted) return ψ end -function similar(bc::Broadcasted{Style{MPST}}, ElType::Type) where {MPST<:AbstractMPS} +function Base.similar(bc::Broadcasted{Style{MPST}}, ElType::Type) where {MPST<:AbstractMPS} return similar(Array{ElType}, axes(bc)) end -function similar(bc::Broadcasted{Style{MPST}}, ::Type{ITensor}) where {MPST<:AbstractMPS} +function Base.similar( + bc::Broadcasted{Style{MPST}}, ::Type{ITensor} +) where {MPST<:AbstractMPS} # In general, we assume the broadcast operation # will mess up the orthogonality so we use # a generic constructor where we don't specify diff --git a/src/lib/ITensorMPS/abstractprojmpo/abstractprojmpo.jl b/src/lib/ITensorMPS/src/abstractprojmpo/abstractprojmpo.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/abstractprojmpo.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/abstractprojmpo.jl diff --git a/src/lib/ITensorMPS/abstractprojmpo/diskprojmpo.jl b/src/lib/ITensorMPS/src/abstractprojmpo/diskprojmpo.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/diskprojmpo.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/diskprojmpo.jl diff --git a/src/lib/ITensorMPS/abstractprojmpo/projmpo.jl b/src/lib/ITensorMPS/src/abstractprojmpo/projmpo.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/projmpo.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/projmpo.jl diff --git a/src/lib/ITensorMPS/abstractprojmpo/projmpo_apply.jl b/src/lib/ITensorMPS/src/abstractprojmpo/projmpo_apply.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/projmpo_apply.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/projmpo_apply.jl diff --git a/src/lib/ITensorMPS/abstractprojmpo/projmpo_mps.jl b/src/lib/ITensorMPS/src/abstractprojmpo/projmpo_mps.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/projmpo_mps.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/projmpo_mps.jl diff --git a/src/lib/ITensorMPS/abstractprojmpo/projmpo_mps2.jl b/src/lib/ITensorMPS/src/abstractprojmpo/projmpo_mps2.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/projmpo_mps2.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/projmpo_mps2.jl diff --git a/src/lib/ITensorMPS/abstractprojmpo/projmposum.jl b/src/lib/ITensorMPS/src/abstractprojmpo/projmposum.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/projmposum.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/projmposum.jl diff --git a/src/lib/ITensorMPS/abstractprojmpo/projmps.jl b/src/lib/ITensorMPS/src/abstractprojmpo/projmps.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/projmps.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/projmps.jl diff --git a/src/lib/ITensorMPS/abstractprojmpo/projmps2.jl b/src/lib/ITensorMPS/src/abstractprojmpo/projmps2.jl similarity index 100% rename from src/lib/ITensorMPS/abstractprojmpo/projmps2.jl rename to src/lib/ITensorMPS/src/abstractprojmpo/projmps2.jl diff --git a/src/lib/ITensorMPS/adapt.jl b/src/lib/ITensorMPS/src/adapt.jl similarity index 100% rename from src/lib/ITensorMPS/adapt.jl rename to src/lib/ITensorMPS/src/adapt.jl diff --git a/src/lib/ITensorMPS/alternating_update.jl b/src/lib/ITensorMPS/src/alternating_update.jl similarity index 100% rename from src/lib/ITensorMPS/alternating_update.jl rename to src/lib/ITensorMPS/src/alternating_update.jl diff --git a/src/lib/ITensorMPS/contract_mpo_mps.jl b/src/lib/ITensorMPS/src/contract_mpo_mps.jl similarity index 100% rename from src/lib/ITensorMPS/contract_mpo_mps.jl rename to src/lib/ITensorMPS/src/contract_mpo_mps.jl diff --git a/src/lib/ITensorMPS/defaults.jl b/src/lib/ITensorMPS/src/defaults.jl similarity index 100% rename from src/lib/ITensorMPS/defaults.jl rename to src/lib/ITensorMPS/src/defaults.jl diff --git a/src/lib/ITensorMPS/deprecated.jl b/src/lib/ITensorMPS/src/deprecated.jl similarity index 100% rename from src/lib/ITensorMPS/deprecated.jl rename to src/lib/ITensorMPS/src/deprecated.jl diff --git a/src/lib/ITensorMPS/dmrg.jl b/src/lib/ITensorMPS/src/dmrg.jl similarity index 100% rename from src/lib/ITensorMPS/dmrg.jl rename to src/lib/ITensorMPS/src/dmrg.jl diff --git a/src/lib/ITensorMPS/dmrg_x.jl b/src/lib/ITensorMPS/src/dmrg_x.jl similarity index 100% rename from src/lib/ITensorMPS/dmrg_x.jl rename to src/lib/ITensorMPS/src/dmrg_x.jl diff --git a/src/lib/ITensorMPS/exports.jl b/src/lib/ITensorMPS/src/exports.jl similarity index 99% rename from src/lib/ITensorMPS/exports.jl rename to src/lib/ITensorMPS/src/exports.jl index e3a253dba5..2f5f142f3c 100644 --- a/src/lib/ITensorMPS/exports.jl +++ b/src/lib/ITensorMPS/src/exports.jl @@ -1,7 +1,6 @@ export # dmrg.jl dmrg, - # abstractmps.jl # Macros @preserve_ortho, @@ -29,11 +28,9 @@ export set_ortho_lims!, siteinds, sim!, - # autompo/ AutoMPO, add!, - # mpo.jl # Types MPO, @@ -49,7 +46,6 @@ export truncate!, unique_siteind, unique_siteinds, - # mps.jl # Types MPS, @@ -75,7 +71,6 @@ export replace_siteinds, swapbondsites, totalqn, - # observer.jl # Types AbstractObserver, @@ -88,7 +83,6 @@ export measure!, measurements, truncerrors, - # projmpo.jl checkflux, disk, @@ -99,13 +93,10 @@ export noiseterm, nsite, position!, - # projmposum.jl ProjMPOSum, - # projmpo_mps.jl ProjMPO_MPS, - # sweeps.jl Sweeps, cutoff, diff --git a/src/lib/ITensorMPS/imports.jl b/src/lib/ITensorMPS/src/imports.jl similarity index 97% rename from src/lib/ITensorMPS/imports.jl rename to src/lib/ITensorMPS/src/imports.jl index 4c22e664c2..a83de90384 100644 --- a/src/lib/ITensorMPS/imports.jl +++ b/src/lib/ITensorMPS/src/imports.jl @@ -50,6 +50,7 @@ import Base: length, map, map!, + ndims, print, promote_rule, push!, @@ -102,7 +103,6 @@ import ..ITensors.NDTensors: import ..ITensors: AbstractRNG, - addtags, Apply, apply, argument, @@ -124,7 +124,6 @@ import ..ITensors: isfermionic, maxdim, mindim, - ndims, noprime, noprime!, norm, @@ -139,12 +138,9 @@ import ..ITensors: replaceinds, replaceprime, replacetags, - replacetags!, setprime, sim, site, - siteind, - siteinds, splitblocks, store, Style, diff --git a/src/physics/lattices.jl b/src/lib/ITensorMPS/src/lattices/lattices.jl similarity index 100% rename from src/physics/lattices.jl rename to src/lib/ITensorMPS/src/lattices/lattices.jl diff --git a/src/lib/ITensorMPS/linsolve.jl b/src/lib/ITensorMPS/src/linsolve.jl similarity index 100% rename from src/lib/ITensorMPS/linsolve.jl rename to src/lib/ITensorMPS/src/linsolve.jl diff --git a/src/lib/ITensorMPS/mpo.jl b/src/lib/ITensorMPS/src/mpo.jl similarity index 98% rename from src/lib/ITensorMPS/mpo.jl rename to src/lib/ITensorMPS/src/mpo.jl index d496e917a3..654d3105bd 100644 --- a/src/lib/ITensorMPS/mpo.jl +++ b/src/lib/ITensorMPS/src/mpo.jl @@ -1,5 +1,6 @@ using Adapt: adapt using Random: Random +using ..SiteTypes: SiteTypes, siteind, siteinds """ MPO @@ -238,7 +239,7 @@ end Get the first site Index of the MPO found, by default with prime level 0. """ -siteind(M::MPO, j::Int; kwargs...) = siteind(first, M, j; plev=0, kwargs...) +SiteTypes.siteind(M::MPO, j::Int; kwargs...) = siteind(first, M, j; plev=0, kwargs...) # TODO: make this return the site indices that would have # been used to create the MPO? I.e.: @@ -248,9 +249,9 @@ siteind(M::MPO, j::Int; kwargs...) = siteind(first, M, j; plev=0, kwargs...) Get a Vector of IndexSets of all the site indices of M. """ -siteinds(M::MPO; kwargs...) = siteinds(all, M; kwargs...) +SiteTypes.siteinds(M::MPO; kwargs...) = siteinds(all, M; kwargs...) -function siteinds(Mψ::Tuple{MPO,MPS}, n::Int; kwargs...) +function SiteTypes.siteinds(Mψ::Tuple{MPO,MPS}, n::Int; kwargs...) return siteinds(uniqueinds, Mψ[1], Mψ[2], n; kwargs...) end @@ -261,7 +262,9 @@ function nsites(Mψ::Tuple{MPO,MPS}) return N end -siteinds(Mψ::Tuple{MPO,MPS}; kwargs...) = [siteinds(Mψ, n; kwargs...) for n in 1:nsites(Mψ)] +function SiteTypes.siteinds(Mψ::Tuple{MPO,MPS}; kwargs...) + return [siteinds(Mψ, n; kwargs...) for n in 1:nsites(Mψ)] +end # XXX: rename originalsiteinds? """ diff --git a/src/lib/ITensorMPS/mps.jl b/src/lib/ITensorMPS/src/mps.jl similarity index 99% rename from src/lib/ITensorMPS/mps.jl rename to src/lib/ITensorMPS/src/mps.jl index fae378de69..d3ab6b8b11 100644 --- a/src/lib/ITensorMPS/mps.jl +++ b/src/lib/ITensorMPS/src/mps.jl @@ -1,6 +1,7 @@ using Adapt: adapt using NDTensors: using_auto_fermion using Random: Random +using ..SiteTypes: SiteTypes, siteind, siteinds """ MPS @@ -482,14 +483,14 @@ MPS(sites::Vector{<:Index}, states) = MPS(Float64, sites, states) Get the first site Index of the MPS. Return `nothing` if none is found. """ -siteind(M::MPS, j::Int; kwargs...) = siteind(first, M, j; kwargs...) +SiteTypes.siteind(M::MPS, j::Int; kwargs...) = siteind(first, M, j; kwargs...) """ siteind(::typeof(only), M::MPS, j::Int; kwargs...) Get the only site Index of the MPS. Return `nothing` if none is found. """ -function siteind(::typeof(only), M::MPS, j::Int; kwargs...) +function SiteTypes.siteind(::typeof(only), M::MPS, j::Int; kwargs...) is = siteinds(M, j; kwargs...) if isempty(is) return nothing @@ -511,7 +512,7 @@ Get a vector of the only site Index found on each tensor of the MPS. Errors if m Get a vector of the all site Indices found on each tensor of the MPS. Returns a Vector of IndexSets. """ -siteinds(M::MPS; kwargs...) = siteinds(first, M; kwargs...) +SiteTypes.siteinds(M::MPS; kwargs...) = siteinds(first, M; kwargs...) function replace_siteinds!(M::MPS, sites) for j in eachindex(M) diff --git a/src/lib/ITensorMPS/observer.jl b/src/lib/ITensorMPS/src/observer.jl similarity index 100% rename from src/lib/ITensorMPS/observer.jl rename to src/lib/ITensorMPS/src/observer.jl diff --git a/src/lib/ITensorMPS/autompo/matelem.jl b/src/lib/ITensorMPS/src/opsum_to_mpo/matelem.jl similarity index 100% rename from src/lib/ITensorMPS/autompo/matelem.jl rename to src/lib/ITensorMPS/src/opsum_to_mpo/matelem.jl diff --git a/src/lib/ITensorMPS/autompo/opsum_to_mpo.jl b/src/lib/ITensorMPS/src/opsum_to_mpo/opsum_to_mpo.jl similarity index 100% rename from src/lib/ITensorMPS/autompo/opsum_to_mpo.jl rename to src/lib/ITensorMPS/src/opsum_to_mpo/opsum_to_mpo.jl diff --git a/src/lib/ITensorMPS/autompo/opsum_to_mpo_generic.jl b/src/lib/ITensorMPS/src/opsum_to_mpo/opsum_to_mpo_generic.jl similarity index 99% rename from src/lib/ITensorMPS/autompo/opsum_to_mpo_generic.jl rename to src/lib/ITensorMPS/src/opsum_to_mpo/opsum_to_mpo_generic.jl index 7a855ea213..aea3a3e47c 100644 --- a/src/lib/ITensorMPS/autompo/opsum_to_mpo_generic.jl +++ b/src/lib/ITensorMPS/src/opsum_to_mpo/opsum_to_mpo_generic.jl @@ -1,4 +1,5 @@ using NDTensors: using_auto_fermion +using ..Ops: Op # TODO: Deprecate. const AutoMPO = OpSum diff --git a/src/lib/ITensorMPS/autompo/opsum_to_mpo_qn.jl b/src/lib/ITensorMPS/src/opsum_to_mpo/opsum_to_mpo_qn.jl similarity index 100% rename from src/lib/ITensorMPS/autompo/opsum_to_mpo_qn.jl rename to src/lib/ITensorMPS/src/opsum_to_mpo/opsum_to_mpo_qn.jl diff --git a/src/lib/ITensorMPS/autompo/qnmatelem.jl b/src/lib/ITensorMPS/src/opsum_to_mpo/qnmatelem.jl similarity index 100% rename from src/lib/ITensorMPS/autompo/qnmatelem.jl rename to src/lib/ITensorMPS/src/opsum_to_mpo/qnmatelem.jl diff --git a/src/lib/ITensorMPS/solver_utils.jl b/src/lib/ITensorMPS/src/solver_utils.jl similarity index 100% rename from src/lib/ITensorMPS/solver_utils.jl rename to src/lib/ITensorMPS/src/solver_utils.jl diff --git a/src/lib/ITensorMPS/sweep_update.jl b/src/lib/ITensorMPS/src/sweep_update.jl similarity index 100% rename from src/lib/ITensorMPS/sweep_update.jl rename to src/lib/ITensorMPS/src/sweep_update.jl diff --git a/src/lib/ITensorMPS/sweeps.jl b/src/lib/ITensorMPS/src/sweeps.jl similarity index 100% rename from src/lib/ITensorMPS/sweeps.jl rename to src/lib/ITensorMPS/src/sweeps.jl diff --git a/src/lib/ITensorMPS/tdvp.jl b/src/lib/ITensorMPS/src/tdvp.jl similarity index 100% rename from src/lib/ITensorMPS/tdvp.jl rename to src/lib/ITensorMPS/src/tdvp.jl diff --git a/src/lib/ITensorMPS/tdvp_sweeps.jl b/src/lib/ITensorMPS/src/tdvp_sweeps.jl similarity index 100% rename from src/lib/ITensorMPS/tdvp_sweeps.jl rename to src/lib/ITensorMPS/src/tdvp_sweeps.jl diff --git a/src/lib/ITensorMPS/tdvpinfo.jl b/src/lib/ITensorMPS/src/tdvpinfo.jl similarity index 100% rename from src/lib/ITensorMPS/tdvpinfo.jl rename to src/lib/ITensorMPS/src/tdvpinfo.jl diff --git a/src/lib/ITensorMPS/tdvporder.jl b/src/lib/ITensorMPS/src/tdvporder.jl similarity index 100% rename from src/lib/ITensorMPS/tdvporder.jl rename to src/lib/ITensorMPS/src/tdvporder.jl diff --git a/src/lib/ITensorMPS/update_observer.jl b/src/lib/ITensorMPS/src/update_observer.jl similarity index 100% rename from src/lib/ITensorMPS/update_observer.jl rename to src/lib/ITensorMPS/src/update_observer.jl diff --git a/src/lib/ITensorNetworkMaps/ITensorNetworkMaps.jl b/src/lib/ITensorNetworkMaps/src/ITensorNetworkMaps.jl similarity index 100% rename from src/lib/ITensorNetworkMaps/ITensorNetworkMaps.jl rename to src/lib/ITensorNetworkMaps/src/ITensorNetworkMaps.jl diff --git a/src/lib/ITensorVisualizationCore/ITensorVisualizationCore.jl b/src/lib/ITensorVisualizationCore/src/ITensorVisualizationCore.jl similarity index 100% rename from src/lib/ITensorVisualizationCore/ITensorVisualizationCore.jl rename to src/lib/ITensorVisualizationCore/src/ITensorVisualizationCore.jl diff --git a/src/lib/ITensorVisualizationCore/visualize_macro.jl b/src/lib/ITensorVisualizationCore/src/visualize_macro.jl similarity index 100% rename from src/lib/ITensorVisualizationCore/visualize_macro.jl rename to src/lib/ITensorVisualizationCore/src/visualize_macro.jl diff --git a/src/lib/ITensorsOpsExt/src/ITensorsOpsExt.jl b/src/lib/ITensorsOpsExt/src/ITensorsOpsExt.jl new file mode 100644 index 0000000000..48bef834ae --- /dev/null +++ b/src/lib/ITensorsOpsExt/src/ITensorsOpsExt.jl @@ -0,0 +1,79 @@ +module ITensorsOpsExt +using ITensors: ITensors, ITensor, Index, apply, dag, hascommoninds, swapprime +using ..LazyApply: LazyApply, Applied, Exp, Prod, Scaled, Sum, argument, coefficient +using LinearAlgebra: UniformScaling, I +using ..Ops: Op +using ..SiteTypes: SiteTypes, op + +function SiteTypes.op(I::UniformScaling, s::Index...) + return I.λ * op("Id", s...) +end + +function ITensors.ITensor(o::Op, s::Vector{<:Index}) + return op(o.which_op, map(n -> s[n], o.sites)...; o.params...) +end + +function ITensors.ITensor(o::Scaled, s::Vector{<:Index}) + c = coefficient(o) + if isreal(c) + c = real(c) + end + return c * ITensor(argument(o), s) +end + +function ITensors.ITensor(o::Prod, s::Vector{<:Index}) + T = ITensor(true) + for a in o.args[1] + Tₙ = ITensor(a, s) + # TODO: Implement this logic inside `apply` + if hascommoninds(T, Tₙ) + T = T(Tₙ) + else + T *= Tₙ + end + end + return T +end + +function ITensors.ITensor(o::Sum, s::Vector{<:Index}) + T = ITensor() + for a in o.args[1] + T += ITensor(a, s) + end + return T +end + +function ITensors.ITensor(o::Exp, s::Vector{<:Index}) + return exp(ITensor(argument(o), s)) +end + +function ITensors.ITensor(o::LazyApply.Adjoint, s::Vector{<:Index}) + return swapprime(dag(ITensor(o', s)), 0 => 1) +end + +function LazyApply.Sum{ITensor}(o::Sum, s::Vector{<:Index}) + return Applied(sum, (map(oₙ -> ITensor(oₙ, s), only(o.args)),)) +end + +function LazyApply.Prod{ITensor}(o::Prod, s::Vector{<:Index}) + return Applied(prod, (map(oₙ -> ITensor(oₙ, s), only(o.args)),)) +end + +function LazyApply.Prod{ITensor}(o::Scaled{C,Prod{Op}}, s::Vector{<:Index}) where {C} + t = Prod{ITensor}(argument(o), s) + t1 = coefficient(o) * only(t.args)[1] + return Applied(prod, (vcat([t1], only(t.args)[2:end]),)) +end + +function ITensors.apply(o::Prod{ITensor}, v::ITensor; kwargs...) + ov = v + for oₙ in reverse(only(o.args)) + ov = apply(oₙ, ov; kwargs...) + end + return ov +end + +function (o::Prod{ITensor})(v::ITensor; kwargs...) + return apply(o, v; kwargs...) +end +end diff --git a/src/lib/ITensorsSiteTypesExt/src/ITensorsSiteTypesExt.jl b/src/lib/ITensorsSiteTypesExt/src/ITensorsSiteTypesExt.jl new file mode 100644 index 0000000000..970465e7d9 --- /dev/null +++ b/src/lib/ITensorsSiteTypesExt/src/ITensorsSiteTypesExt.jl @@ -0,0 +1,79 @@ +module ITensorsSiteTypesExt +using ..ITensors: ITensors, Index, LastVal, dag, prime, val +using NDTensors: NDTensors, dim, sim +using ..SiteTypes: SiteTypes +SiteTypes.val(iv::Pair{<:Index}) = val(iv.first, iv.second) +SiteTypes.val(i::Index, l::LastVal) = l.f(dim(i)) +# TODO: +# Implement a macro with a general definition: +# f(iv::Pair{<:Index}, args...) = (f(ind(iv), args...) => val(iv)) +ITensors.prime(iv::Pair{<:Index}, inc::Integer=1) = (prime(ind(iv), inc) => val(iv)) +NDTensors.sim(iv::Pair{<:Index}, args...) = (sim(ind(iv), args...) => val(iv)) +ITensors.dag(iv::Pair{<:Index}) = (dag(ind(iv)) => val(iv)) +Base.adjoint(iv::Pair{<:Index}) = (prime(ind(iv)) => val(iv)) + +using ..ITensors: ITensors, Indices +function ITensors._vals(is::Indices, I::String...) + return val.(is, I) +end + +using Adapt: Adapt +using ..ITensors: ITensors, Index, ITensor, ind, inds +using NDTensors: NDTensors, Tensor +using ..SiteTypes: val +Base.@propagate_inbounds @inline function ITensors._getindex( + T::Tensor, ivs::Vararg{Any,N} +) where {N} + # Tried ind.(ivs), val.(ivs) but it is slower + p = NDTensors.getperm(inds(T), ntuple(n -> ind(@inbounds ivs[n]), Val(N))) + fac = NDTensors.permfactor(p, ivs...) # possible sign + return fac * ITensors._getindex( + T, NDTensors.permute(ntuple(n -> val(@inbounds ivs[n]), Val(N)), p)... + ) +end +Base.@propagate_inbounds @inline function ITensors._setindex!!( + T::Tensor, x::Number, ivs::Vararg{Any,N} +) where {N} + # Would be nice to split off the functions for extracting the `ind` and `val` as Tuples, + # but it was slower. + p = NDTensors.getperm(inds(T), ntuple(n -> ind(@inbounds ivs[n]), Val(N))) + fac = NDTensors.permfactor(p, ivs...) # possible sign + return ITensors._setindex!!( + T, fac * x, NDTensors.permute(ntuple(n -> val(@inbounds ivs[n]), Val(N)), p)... + ) +end +Base.@propagate_inbounds @inline function Base.setindex!( + T::ITensor, x::Number, I1::Pair{<:Index,String}, I::Pair{<:Index,String}... +) + Iv = map(i -> i.first => val(i.first, i.second), (I1, I...)) + return setindex!(T, x, Iv...) +end +""" + onehot(ivs...) + setelt(ivs...) + onehot(::Type, ivs...) + setelt(::Type, ivs...) + +Create an ITensor with all zeros except the specified value, +which is set to 1. + +# Examples +```julia +i = Index(2,"i") +A = onehot(i=>2) +# A[i=>2] == 1, all other elements zero + +# Specify the element type +A = onehot(Float32, i=>2) + +j = Index(3,"j") +B = onehot(i=>1,j=>3) +# B[i=>1,j=>3] == 1, all other element zero +``` +""" +function ITensors.onehot(datatype::Type{<:AbstractArray}, ivs::Pair{<:Index}...) + A = ITensor(eltype(datatype), ind.(ivs)...) + A[val.(ivs)...] = one(eltype(datatype)) + return Adapt.adapt(datatype, A) +end +end diff --git a/src/lib/LazyApply/LazyApply.jl b/src/lib/LazyApply/src/LazyApply.jl similarity index 100% rename from src/lib/LazyApply/LazyApply.jl rename to src/lib/LazyApply/src/LazyApply.jl diff --git a/src/lib/Ops/ops_itensor.jl b/src/lib/Ops/ops_itensor.jl index b88e1260f5..e650376dcf 100644 --- a/src/lib/Ops/ops_itensor.jl +++ b/src/lib/Ops/ops_itensor.jl @@ -1,4 +1,6 @@ -function op(I::UniformScaling, s::Index...) +using .SiteTypes: SiteTypes, op + +function SiteTypes.op(I::UniformScaling, s::Index...) return I.λ * op("Id", s...) end diff --git a/src/lib/Ops/src/Ops.jl b/src/lib/Ops/src/Ops.jl new file mode 100644 index 0000000000..fcbff091ea --- /dev/null +++ b/src/lib/Ops/src/Ops.jl @@ -0,0 +1,4 @@ +module Ops +include("op.jl") +include("trotter.jl") +end diff --git a/src/lib/Ops/Ops.jl b/src/lib/Ops/src/op.jl similarity index 99% rename from src/lib/Ops/Ops.jl rename to src/lib/Ops/src/op.jl index 64e9c360ed..886bb969b9 100644 --- a/src/lib/Ops/Ops.jl +++ b/src/lib/Ops/src/op.jl @@ -1,5 +1,3 @@ -module Ops - using ..LazyApply import Base: ==, +, -, *, /, convert, exp, show, adjoint, isless, hash @@ -335,5 +333,3 @@ function show(io::IO, ::MIME"text/plain", o::LazyApply.Adjoint{Op}) return nothing end show(io::IO, o::LazyApply.Adjoint{Op}) = show(io, MIME("text/plain"), o) - -end diff --git a/src/lib/Ops/trotter.jl b/src/lib/Ops/src/trotter.jl similarity index 74% rename from src/lib/Ops/trotter.jl rename to src/lib/Ops/src/trotter.jl index 09b4433072..9c3b1a5641 100644 --- a/src/lib/Ops/trotter.jl +++ b/src/lib/Ops/src/trotter.jl @@ -1,3 +1,5 @@ +using ..LazyApply: Applied, Sum + abstract type ExpAlgorithm end struct Exact <: ExpAlgorithm end @@ -6,13 +8,13 @@ struct Trotter{Order} <: ExpAlgorithm nsteps::Int end Trotter{Order}() where {Order} = Trotter{Order}(1) -one(::Trotter{Order}) where {Order} = Trotter{Order}(1) +Base.one(::Trotter{Order}) where {Order} = Trotter{Order}(1) -function exp(o::Sum; alg::ExpAlgorithm=Exact()) +function Base.exp(o::Sum; alg::ExpAlgorithm=Exact()) return exp(alg, o) end -function exp(::Exact, o::Sum) +function Base.exp(::Exact, o::Sum) return Applied(prod, ([Applied(exp, (o,))],)) end @@ -27,7 +29,7 @@ function exp_one_step(trotter::Trotter{2}, o::Sum) return exp_o end -function exp(trotter::Trotter, o::Sum) +function Base.exp(trotter::Trotter, o::Sum) expδo = exp_one_step(one(trotter), o / trotter.nsteps) return expδo^trotter.nsteps end diff --git a/src/lib/QuantumNumbers/src/QuantumNumbers.jl b/src/lib/QuantumNumbers/src/QuantumNumbers.jl new file mode 100644 index 0000000000..1e2c436516 --- /dev/null +++ b/src/lib/QuantumNumbers/src/QuantumNumbers.jl @@ -0,0 +1,5 @@ +module QuantumNumbers +include("arrow.jl") +include("qnval.jl") +include("qn.jl") +end diff --git a/src/arrow.jl b/src/lib/QuantumNumbers/src/arrow.jl similarity index 91% rename from src/arrow.jl rename to src/lib/QuantumNumbers/src/arrow.jl index 445b5412ea..68d4be8e11 100644 --- a/src/arrow.jl +++ b/src/lib/QuantumNumbers/src/arrow.jl @@ -1,4 +1,3 @@ - """ Arrow @@ -12,6 +11,6 @@ associated with an index, i.e. the index leg is directed into or out of a given Reverse direction of a directed `Arrow`. """ -function Base.:-(dir::Arrow) +function Base.:(-)(dir::Arrow) return Arrow(-Int(dir)) end diff --git a/src/qn/qn.jl b/src/lib/QuantumNumbers/src/qn.jl similarity index 74% rename from src/qn/qn.jl rename to src/lib/QuantumNumbers/src/qn.jl index 638b8d6dbb..dda6f9d3e9 100644 --- a/src/qn/qn.jl +++ b/src/lib/QuantumNumbers/src/qn.jl @@ -1,64 +1,7 @@ -struct QNVal - name::SmallString - val::Int - modulus::Int - - function QNVal(name, v::Int, m::Int=1) - am = abs(m) - if am > 1 - return new(SmallString(name), mod(v, am), m) - end - return new(SmallString(name), v, m) - end -end - -QNVal(v::Int, m::Int=1) = QNVal("", v, m) -QNVal() = QNVal("", 0, 0) - -name(qv::QNVal) = qv.name -val(qv::QNVal) = qv.val -modulus(qv::QNVal) = qv.modulus -isactive(qv::QNVal) = modulus(qv) != 0 -(qv1::QNVal < qv2::QNVal) = (name(qv1) < name(qv2)) - -function qn_mod(val::Int, modulus::Int) - amod = abs(modulus) - amod <= 1 && return val - return mod(val, amod) -end - -function -(qv::QNVal) - return QNVal(name(qv), qn_mod(-val(qv), modulus(qv)), modulus(qv)) -end - -Base.zero(::Type{QNVal}) = QNVal() - -zero(qv::QNVal) = QNVal(name(qv), 0, modulus(qv)) - -(dir::Arrow * qv::QNVal) = QNVal(name(qv), Int(dir) * val(qv), modulus(qv)) - -(qv::QNVal * dir::Arrow) = (dir * qv) - -function pm(qv1::QNVal, qv2::QNVal, fac::Int) - if name(qv1) != name(qv2) - error("Cannot add QNVals with different names \"$(name(qv1))\", \"$(name(qv2))\"") - end - if modulus(qv1) != modulus(qv2) - error( - "QNVals with matching name \"$(name(qv1))\" cannot have different modulus values " - ) - end - m1 = modulus(qv1) - if m1 == 1 || m1 == -1 - return QNVal(name(qv1), val(qv1) + fac * val(qv2), m1) - end - return QNVal(name(qv1), Base.mod(val(qv1) + fac * val(qv2), abs(m1)), m1) -end - -(qv1::QNVal + qv2::QNVal) = pm(qv1, qv2, +1) -(qv1::QNVal - qv2::QNVal) = pm(qv1, qv2, -1) - -const ZeroVal = QNVal() +using ..ITensors: ITensors, name, val +using NDTensors: NDTensors +using ..SmallStrings: SmallString +using StaticArrays: MVector, SVector const maxQNs = 4 const QNStorage = SVector{maxQNs,QNVal} @@ -79,12 +22,10 @@ collection, its value is treated as zero. """ struct QN data::QNStorage - function QN() s = QNStorage(ntuple(_ -> ZeroVal, Val(maxQNs))) return new(s) end - QN(s::QNStorage) = new(s) end @@ -151,11 +92,11 @@ QN(val::Int, modulus::Int=1) = QN(("", val, modulus)) data(qn::QN) = qn.data -getindex(q::QN, n::Int) = getindex(data(q), n) +Base.getindex(q::QN, n::Int) = getindex(data(q), n) -length(qn::QN) = length(data(qn)) +Base.length(qn::QN) = length(data(qn)) -lastindex(qn::QN) = length(qn) +Base.lastindex(qn::QN) = length(qn) isactive(qn::QN) = isactive(qn[1]) @@ -166,12 +107,12 @@ function nactive(q::QN) return maxQNs end -function iterate(qn::QN, state::Int=1) +function Base.iterate(qn::QN, state::Int=1) (state > length(qn)) && return nothing return (qn[state], state + 1) end -keys(qn::QN) = keys(data(qn)) +Base.keys(qn::QN) = keys(data(qn)) """ val(q::QN,name) @@ -179,7 +120,7 @@ keys(qn::QN) = keys(data(qn)) Get the value within the QN q corresponding to the string `name` """ -function val(q::QN, name_) +function ITensors.val(q::QN, name_) sname = SmallString(name_) for n in 1:maxQNs name(q[n]) == sname && return val(q[n]) @@ -208,7 +149,7 @@ Returns a QN object containing the same names as q, but with all values set to zero. """ -function zero(qn::QN) +function Base.zero(qn::QN) mqn = MQNStorage(undef) for i in 1:length(mqn) mqn[i] = zero(qn[i]) @@ -216,7 +157,7 @@ function zero(qn::QN) return QN(mqn) end -function (dir::Arrow * qn::QN) +function Base.:(*)(dir::Arrow, qn::QN) mqn = MQNStorage(undef) for i in 1:length(mqn) mqn[i] = dir * qn[i] @@ -224,9 +165,9 @@ function (dir::Arrow * qn::QN) return QN(mqn) end -(qn::QN * dir::Arrow) = (dir * qn) +Base.:(*)(qn::QN, dir::Arrow) = (dir * qn) -function -(qn::QN) +function Base.:(-)(qn::QN) mqn = MQNStorage(undef) for i in 1:length(mqn) mqn[i] = -qn[i] @@ -234,9 +175,8 @@ function -(qn::QN) return QN(mqn) end -function (a::QN + b::QN) +function Base.:(+)(a::QN, b::QN) !isactive(b[1]) && return a - ma = MQNStorage(data(a)) @inbounds for nb in 1:maxQNs !isactive(b[nb]) && break @@ -261,7 +201,7 @@ function (a::QN + b::QN) return QN(QNStorage(ma)) end -(a::QN - b::QN) = (a + (-b)) +Base.:(-)(a::QN, b::QN) = (a + (-b)) function hasname(qn::QN, qv_find::QNVal) for qv in qn @@ -317,7 +257,7 @@ function isequal_assume_filled(qn1::QN, qn2::QN) return true end -function ==(qn1::QN, qn2::QN; assume_filled=false) +function Base.:(==)(qn1::QN, qn2::QN; assume_filled=false) if !assume_filled qn1, qn2 = fillqns(qn1, qn2) end @@ -333,11 +273,11 @@ function isless_assume_filled(qn1::QN, qn2::QN) return false end -function isless(qn1::QN, qn2::QN; assume_filled=false) +function Base.isless(qn1::QN, qn2::QN; assume_filled=false) return <(qn1, qn2; assume_filled=assume_filled) end -function <(qn1::QN, qn2::QN; assume_filled=false) +function Base.:(<)(qn1::QN, qn2::QN; assume_filled=false) if !assume_filled qn1, qn2 = fillqns(qn1, qn2) end @@ -360,7 +300,6 @@ end function removeqn(qn::QN, qn_name::String) ss_qn_name = SmallString(qn_name) - # Find the location of the QNVal to remove n_qn = nothing for n in 1:length(qn) @@ -372,16 +311,15 @@ function removeqn(qn::QN, qn_name::String) if isnothing(n_qn) return qn end - qn_data = data(qn) for j in n_qn:(length(qn) - 1) - qn_data = setindex(qn_data, qn_data[j + 1], j) + qn_data = Base.setindex(qn_data, qn_data[j + 1], j) end - qn_data = setindex(qn_data, QNVal(), length(qn)) + qn_data = Base.setindex(qn_data, QNVal(), length(qn)) return QN(qn_data) end -function show(io::IO, q::QN) +function Base.show(io::IO, q::QN) print(io, "QN(") Na = nactive(q) for n in 1:Na diff --git a/src/lib/QuantumNumbers/src/qnval.jl b/src/lib/QuantumNumbers/src/qnval.jl new file mode 100644 index 0000000000..aaa218a4d6 --- /dev/null +++ b/src/lib/QuantumNumbers/src/qnval.jl @@ -0,0 +1,63 @@ +using ..ITensors: ITensors, name, val +using ..SmallStrings: SmallString + +struct QNVal + name::SmallString + val::Int + modulus::Int + function QNVal(name, v::Int, m::Int=1) + am = abs(m) + if am > 1 + return new(SmallString(name), mod(v, am), m) + end + return new(SmallString(name), v, m) + end +end + +QNVal(v::Int, m::Int=1) = QNVal("", v, m) +QNVal() = QNVal("", 0, 0) + +ITensors.name(qv::QNVal) = qv.name +ITensors.val(qv::QNVal) = qv.val +modulus(qv::QNVal) = qv.modulus +isactive(qv::QNVal) = modulus(qv) != 0 +Base.:(<)(qv1::QNVal, qv2::QNVal) = (name(qv1) < name(qv2)) + +function qn_mod(val::Int, modulus::Int) + amod = abs(modulus) + amod <= 1 && return val + return mod(val, amod) +end + +function Base.:(-)(qv::QNVal) + return QNVal(name(qv), qn_mod(-val(qv), modulus(qv)), modulus(qv)) +end + +Base.zero(::Type{QNVal}) = QNVal() + +Base.zero(qv::QNVal) = QNVal(name(qv), 0, modulus(qv)) + +Base.:(*)(dir::Arrow, qv::QNVal) = QNVal(name(qv), Int(dir) * val(qv), modulus(qv)) + +Base.:(*)(qv::QNVal, dir::Arrow) = (dir * qv) + +function pm(qv1::QNVal, qv2::QNVal, fac::Int) + if name(qv1) != name(qv2) + error("Cannot add QNVals with different names \"$(name(qv1))\", \"$(name(qv2))\"") + end + if modulus(qv1) != modulus(qv2) + error( + "QNVals with matching name \"$(name(qv1))\" cannot have different modulus values " + ) + end + m1 = modulus(qv1) + if m1 == 1 || m1 == -1 + return QNVal(name(qv1), val(qv1) + fac * val(qv2), m1) + end + return QNVal(name(qv1), Base.mod(val(qv1) + fac * val(qv2), abs(m1)), m1) +end + +Base.:(+)(qv1::QNVal, qv2::QNVal) = pm(qv1, qv2, +1) +Base.:(-)(qv1::QNVal, qv2::QNVal) = pm(qv1, qv2, -1) + +const ZeroVal = QNVal() diff --git a/src/lib/SiteTypes/src/SiteTypes.jl b/src/lib/SiteTypes/src/SiteTypes.jl new file mode 100644 index 0000000000..a9457bfc6a --- /dev/null +++ b/src/lib/SiteTypes/src/SiteTypes.jl @@ -0,0 +1,19 @@ +module SiteTypes +# TODO: This is a bit strange, but required for backwards +# compatibility since `val` is also used by `QNVal`. +import ..ITensors: val +# TODO: Use explicit overloading with `NDTensors.space`. +import ..ITensors: space +include("sitetype.jl") +include("SiteTypesChainRulesCoreExt.jl") +include("sitetypes/aliases.jl") +include("sitetypes/generic_sites.jl") +include("sitetypes/qubit.jl") +include("sitetypes/spinhalf.jl") +include("sitetypes/spinone.jl") +include("sitetypes/fermion.jl") +include("sitetypes/electron.jl") +include("sitetypes/tj.jl") +include("sitetypes/qudit.jl") +include("sitetypes/boson.jl") +end diff --git a/src/lib/SiteTypes/src/SiteTypesChainRulesCoreExt.jl b/src/lib/SiteTypes/src/SiteTypesChainRulesCoreExt.jl new file mode 100644 index 0000000000..625d64e6ab --- /dev/null +++ b/src/lib/SiteTypes/src/SiteTypesChainRulesCoreExt.jl @@ -0,0 +1,7 @@ +module SiteTypesChainRulesCoreExt +using ChainRulesCore: @non_differentiable +using ..SiteTypes: SiteType, _sitetypes, has_fermion_string +@non_differentiable has_fermion_string(::AbstractString, ::Any) +@non_differentiable SiteType(::Any) +@non_differentiable _sitetypes(::Any) +end diff --git a/src/physics/sitetype.jl b/src/lib/SiteTypes/src/sitetype.jl similarity index 94% rename from src/physics/sitetype.jl rename to src/lib/SiteTypes/src/sitetype.jl index 5a6ab983cd..f609eb2351 100644 --- a/src/physics/sitetype.jl +++ b/src/lib/SiteTypes/src/sitetype.jl @@ -1,3 +1,8 @@ +using ChainRulesCore: @ignore_derivatives +using ..ITensors: + ITensors, Index, ITensor, itensor, dag, onehot, prime, product, swapprime, tags +using ..SmallStrings: SmallString +using ..TagSets: TagSets, TagSet, addtags, commontags @eval struct SiteType{T} (f::Type{<:SiteType})() = $(Expr(:new, :f)) @@ -26,10 +31,10 @@ To make a SiteType value or object, you can use the notation: `SiteType("MyTag")` There are currently a few built-in site types -recognized by `ITensors.jl`. The system is easily extensible +recognized by `jl`. The system is easily extensible by users. To add new operators to an existing site type, or to create new site types, you can follow the instructions -[here](https://itensor.github.io/ITensors.jl/stable/examples/Physics.html). +[here](https://itensor.github.io/jl/stable/examples/Physics.html). The current built-in site types are: @@ -102,24 +107,24 @@ Many operators are available, for example: - ... You can view the source code for the internal SiteType definitions -and operators that are defined [here](https://github.com/ITensor/ITensors.jl/tree/main/src/physics/site_types). +and operators that are defined [here](https://github.com/ITensor/jl/tree/main/src/physics/site_types). """ -SiteType(s::AbstractString) = SiteType{Tag(s)}() +SiteType(s::AbstractString) = SiteType{SmallString(s)}() -SiteType(t::Integer) = SiteType{Tag(t)}() -SiteType(t::Tag) = SiteType{t}() +SiteType(t::Integer) = SiteType{SmallString(t)}() +SiteType(t::SmallString) = SiteType{t}() tag(::SiteType{T}) where {T} = T macro SiteType_str(s) - return SiteType{Tag(s)} + return SiteType{SmallString(s)} end # Keep TagType defined for backwards # compatibility; will be deprecated later const TagType = SiteType macro TagType_str(s) - return TagType{Tag(s)} + return TagType{SmallString(s)} end #--------------------------------------- @@ -145,7 +150,7 @@ OpName is a parameterized type which allows making strings into Julia types for the purpose of representing operator names. The main use of OpName is overloading the -`ITensors.op!` method which generates operators +`op!` method which generates operators for indices with certain tags such as "S=1/2". To make a OpName type, you can use the string @@ -156,7 +161,8 @@ the notation: `OpName("myop")` """ OpName(s::AbstractString) = OpName{Symbol(s)}() OpName(s::Symbol) = OpName{s}() -name(::OpName{N}) where {N} = N +# TODO: Avoid overloading `ITensors` version. +ITensors.name(::OpName{N}) where {N} = N macro OpName_str(s) return OpName{Symbol(s)} @@ -188,7 +194,7 @@ op(::SiteType, ::Index, ::AbstractString; kwargs...) = nothing function _sitetypes(ts::TagSet) Ntags = length(ts) - return SiteType[SiteType(data(ts)[n]) for n in 1:Ntags] + return SiteType[SiteType(TagSets.data(ts)[n]) for n in 1:Ntags] end _sitetypes(i::Index) = _sitetypes(tags(i)) @@ -223,7 +229,7 @@ Sz = op("Sz", s) ``` To see all of the operator names defined for the site types included with -ITensor, please view the [source code](https://github.com/ITensor/ITensors.jl/tree/main/src/physics/site_types) +ITensor, please view the [source code](https://github.com/ITensor/jl/tree/main/src/physics/site_types) for each site type. Note that some site types such as "S=1/2" and "Qubit" are aliases for each other and share operator definitions. """ @@ -307,7 +313,7 @@ function op(name::AbstractString, s::Index...; adjoint::Bool=false, kwargs...) op_mat = op(opn; kwargs...) if !isnothing(op_mat) rs = reverse(s) - res = itensor(op_mat, prime.(rs)..., ITensors.dag.(rs)...) + res = itensor(op_mat, prime.(rs)..., dag.(rs)...) adjoint && return swapprime(dag(res), 0 => 1) return res end @@ -320,8 +326,8 @@ function op(name::AbstractString, s::Index...; adjoint::Bool=false, kwargs...) op_mat = op(opn, st; kwargs...) if !isnothing(op_mat) rs = reverse(s) - #return itensor(op_mat, prime.(rs)..., ITensors.dag.(rs)...) - res = itensor(op_mat, prime.(rs)..., ITensors.dag.(rs)...) + #return itensor(op_mat, prime.(rs)..., dag.(rs)...) + res = itensor(op_mat, prime.(rs)..., dag.(rs)...) adjoint && return swapprime(dag(res), 0 => 1) return res end @@ -330,7 +336,7 @@ function op(name::AbstractString, s::Index...; adjoint::Bool=false, kwargs...) # otherwise try calling a function of the form: # op!(::ITensor, ::OpName, ::SiteType, ::Index...; kwargs...) # - Op = ITensor(prime.(s)..., ITensors.dag.(s)...) + Op = ITensor(prime.(s)..., dag.(s)...) for st in common_stypes op!(Op, opn, st, s...; kwargs...) if !isempty(Op) @@ -361,7 +367,7 @@ function op(name::AbstractString, s::Index...; adjoint::Bool=false, kwargs...) end end - Op = ITensor(prime.(s)..., ITensors.dag.(s)...) + Op = ITensor(prime.(s)..., dag.(s)...) for st in Iterators.product(stypes...) op!(Op, opn, st..., s...; kwargs...) if !isempty(Op) @@ -542,7 +548,8 @@ end StateName(s::AbstractString) = StateName{SmallString(s)}() StateName(s::SmallString) = StateName{s}() -name(::StateName{N}) where {N} = N +# TODO: Avoid overloading `ITensors` version. +ITensors.name(::StateName{N}) where {N} = N macro StateName_str(s) return StateName{SmallString(s)} @@ -641,8 +648,9 @@ state(sset::Vector{<:Index}, j::Integer, st; kwargs...) = state(sset[j], st; kwa end ValName(s::AbstractString) = ValName{SmallString(s)}() -ValName(s::SmallString) = ValName{s}() -name(::ValName{N}) where {N} = N +ValName(s::Symbol) = ValName{s}() +# TODO: Avoid overloading `ITensors` version. +ITensors.name(::ValName{N}) where {N} = N macro ValName_str(s) return ValName{SmallString(s)} diff --git a/src/physics/site_types/aliases.jl b/src/lib/SiteTypes/src/sitetypes/aliases.jl similarity index 100% rename from src/physics/site_types/aliases.jl rename to src/lib/SiteTypes/src/sitetypes/aliases.jl diff --git a/src/physics/site_types/boson.jl b/src/lib/SiteTypes/src/sitetypes/boson.jl similarity index 59% rename from src/physics/site_types/boson.jl rename to src/lib/SiteTypes/src/sitetypes/boson.jl index f7ae83f9cb..387edad277 100644 --- a/src/physics/site_types/boson.jl +++ b/src/lib/SiteTypes/src/sitetypes/boson.jl @@ -12,21 +12,19 @@ Create the Hilbert space for a site of type "Boson". Optionally specify the conserved symmetries and their quantum number labels. """ -ITensors.space(st::SiteType"Boson"; kwargs...) = space(alias(st); kwargs...) +space(st::SiteType"Boson"; kwargs...) = space(alias(st); kwargs...) -ITensors.val(vn::ValName, st::SiteType"Boson") = val(vn, alias(st)) +val(vn::ValName, st::SiteType"Boson") = val(vn, alias(st)) -function ITensors.state(sn::StateName, st::SiteType"Boson", s::Index; kwargs...) +function state(sn::StateName, st::SiteType"Boson", s::Index; kwargs...) return state(sn, alias(st), s; kwargs...) end -function ITensors.op(on::OpName, st::SiteType"Boson", ds::Int...; kwargs...) +function op(on::OpName, st::SiteType"Boson", ds::Int...; kwargs...) return op(on, alias(st), ds...; kwargs...) end -function ITensors.op( - on::OpName, st::SiteType"Boson", s1::Index, s_tail::Index...; kwargs... -) +function op(on::OpName, st::SiteType"Boson", s1::Index, s_tail::Index...; kwargs...) rs = reverse((s1, s_tail...)) ds = dim.(rs) opmat = op(on, st, ds...; kwargs...) diff --git a/src/physics/site_types/electron.jl b/src/lib/SiteTypes/src/sitetypes/electron.jl similarity index 51% rename from src/physics/site_types/electron.jl rename to src/lib/SiteTypes/src/sitetypes/electron.jl index 79fe51110a..d8fd90848e 100644 --- a/src/physics/site_types/electron.jl +++ b/src/lib/SiteTypes/src/sitetypes/electron.jl @@ -12,7 +12,7 @@ Create the Hilbert space for a site of type "Electron". Optionally specify the conserved symmetries and their quantum number labels. """ -function ITensors.space( +function space( ::SiteType"Electron"; conserve_qns=false, conserve_sz=conserve_qns, @@ -57,25 +57,25 @@ function ITensors.space( return 4 end -ITensors.val(::ValName"Emp", ::SiteType"Electron") = 1 -ITensors.val(::ValName"Up", ::SiteType"Electron") = 2 -ITensors.val(::ValName"Dn", ::SiteType"Electron") = 3 -ITensors.val(::ValName"UpDn", ::SiteType"Electron") = 4 -ITensors.val(::ValName"0", st::SiteType"Electron") = val(ValName("Emp"), st) -ITensors.val(::ValName"↑", st::SiteType"Electron") = val(ValName("Up"), st) -ITensors.val(::ValName"↓", st::SiteType"Electron") = val(ValName("Dn"), st) -ITensors.val(::ValName"↑↓", st::SiteType"Electron") = val(ValName("UpDn"), st) +val(::ValName"Emp", ::SiteType"Electron") = 1 +val(::ValName"Up", ::SiteType"Electron") = 2 +val(::ValName"Dn", ::SiteType"Electron") = 3 +val(::ValName"UpDn", ::SiteType"Electron") = 4 +val(::ValName"0", st::SiteType"Electron") = val(ValName("Emp"), st) +val(::ValName"↑", st::SiteType"Electron") = val(ValName("Up"), st) +val(::ValName"↓", st::SiteType"Electron") = val(ValName("Dn"), st) +val(::ValName"↑↓", st::SiteType"Electron") = val(ValName("UpDn"), st) -ITensors.state(::StateName"Emp", ::SiteType"Electron") = [1.0, 0, 0, 0] -ITensors.state(::StateName"Up", ::SiteType"Electron") = [0.0, 1, 0, 0] -ITensors.state(::StateName"Dn", ::SiteType"Electron") = [0.0, 0, 1, 0] -ITensors.state(::StateName"UpDn", ::SiteType"Electron") = [0.0, 0, 0, 1] -ITensors.state(::StateName"0", st::SiteType"Electron") = state(StateName("Emp"), st) -ITensors.state(::StateName"↑", st::SiteType"Electron") = state(StateName("Up"), st) -ITensors.state(::StateName"↓", st::SiteType"Electron") = state(StateName("Dn"), st) -ITensors.state(::StateName"↑↓", st::SiteType"Electron") = state(StateName("UpDn"), st) +state(::StateName"Emp", ::SiteType"Electron") = [1.0, 0, 0, 0] +state(::StateName"Up", ::SiteType"Electron") = [0.0, 1, 0, 0] +state(::StateName"Dn", ::SiteType"Electron") = [0.0, 0, 1, 0] +state(::StateName"UpDn", ::SiteType"Electron") = [0.0, 0, 0, 1] +state(::StateName"0", st::SiteType"Electron") = state(StateName("Emp"), st) +state(::StateName"↑", st::SiteType"Electron") = state(StateName("Up"), st) +state(::StateName"↓", st::SiteType"Electron") = state(StateName("Dn"), st) +state(::StateName"↑↓", st::SiteType"Electron") = state(StateName("UpDn"), st) -function ITensors.op(::OpName"Nup", ::SiteType"Electron") +function op(::OpName"Nup", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 @@ -83,11 +83,11 @@ function ITensors.op(::OpName"Nup", ::SiteType"Electron") 0.0 0.0 0.0 1.0 ] end -function ITensors.op(on::OpName"n↑", st::SiteType"Electron") +function op(on::OpName"n↑", st::SiteType"Electron") return op(alias(on), st) end -function ITensors.op(::OpName"Ndn", ::SiteType"Electron") +function op(::OpName"Ndn", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 @@ -95,11 +95,11 @@ function ITensors.op(::OpName"Ndn", ::SiteType"Electron") 0.0 0.0 0.0 1.0 ] end -function ITensors.op(on::OpName"n↓", st::SiteType"Electron") +function op(on::OpName"n↓", st::SiteType"Electron") return op(alias(on), st) end -function ITensors.op(::OpName"Nupdn", ::SiteType"Electron") +function op(::OpName"Nupdn", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 @@ -107,11 +107,11 @@ function ITensors.op(::OpName"Nupdn", ::SiteType"Electron") 0.0 0.0 0.0 1.0 ] end -function ITensors.op(on::OpName"n↑↓", st::SiteType"Electron") +function op(on::OpName"n↑↓", st::SiteType"Electron") return op(alias(on), st) end -function ITensors.op(::OpName"Ntot", ::SiteType"Electron") +function op(::OpName"Ntot", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 @@ -119,11 +119,11 @@ function ITensors.op(::OpName"Ntot", ::SiteType"Electron") 0.0 0.0 0.0 2.0 ] end -function ITensors.op(on::OpName"ntot", st::SiteType"Electron") +function op(on::OpName"ntot", st::SiteType"Electron") return op(alias(on), st) end -function ITensors.op(::OpName"Cup", ::SiteType"Electron") +function op(::OpName"Cup", ::SiteType"Electron") return [ 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 @@ -131,11 +131,11 @@ function ITensors.op(::OpName"Cup", ::SiteType"Electron") 0.0 0.0 0.0 0.0 ] end -function ITensors.op(on::OpName"c↑", st::SiteType"Electron") +function op(on::OpName"c↑", st::SiteType"Electron") return op(alias(on), st) end -function ITensors.op(::OpName"Cdagup", ::SiteType"Electron") +function op(::OpName"Cdagup", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 @@ -143,11 +143,11 @@ function ITensors.op(::OpName"Cdagup", ::SiteType"Electron") 0.0 0.0 1.0 0.0 ] end -function ITensors.op(on::OpName"c†↑", st::SiteType"Electron") +function op(on::OpName"c†↑", st::SiteType"Electron") return op(alias(on), st) end -function ITensors.op(::OpName"Cdn", ::SiteType"Electron") +function op(::OpName"Cdn", ::SiteType"Electron") return [ 0.0 0.0 1.0 0.0 0.0 0.0 0.0 -1.0 @@ -155,11 +155,11 @@ function ITensors.op(::OpName"Cdn", ::SiteType"Electron") 0.0 0.0 0.0 0.0 ] end -function ITensors.op(on::OpName"c↓", st::SiteType"Electron") +function op(on::OpName"c↓", st::SiteType"Electron") return op(alias(on), st) end -function ITensors.op(::OpName"Cdagdn", ::SiteType"Electron") +function op(::OpName"Cdagdn", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 @@ -167,11 +167,11 @@ function ITensors.op(::OpName"Cdagdn", ::SiteType"Electron") 0.0 -1.0 0.0 0.0 ] end -function ITensors.op(::OpName"c†↓", st::SiteType"Electron") +function op(::OpName"c†↓", st::SiteType"Electron") return op(OpName("Cdagdn"), st) end -function ITensors.op(::OpName"Aup", ::SiteType"Electron") +function op(::OpName"Aup", ::SiteType"Electron") return [ 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 @@ -179,11 +179,11 @@ function ITensors.op(::OpName"Aup", ::SiteType"Electron") 0.0 0.0 0.0 0.0 ] end -function ITensors.op(::OpName"a↑", st::SiteType"Electron") +function op(::OpName"a↑", st::SiteType"Electron") return op(OpName("Aup"), st) end -function ITensors.op(::OpName"Adagup", ::SiteType"Electron") +function op(::OpName"Adagup", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 @@ -191,11 +191,11 @@ function ITensors.op(::OpName"Adagup", ::SiteType"Electron") 0.0 0.0 1.0 0.0 ] end -function ITensors.op(::OpName"a†↑", st::SiteType"Electron") +function op(::OpName"a†↑", st::SiteType"Electron") return op(OpName("Adagup"), st) end -function ITensors.op(::OpName"Adn", ::SiteType"Electron") +function op(::OpName"Adn", ::SiteType"Electron") return [ 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 @@ -203,11 +203,11 @@ function ITensors.op(::OpName"Adn", ::SiteType"Electron") 0.0 0.0 0.0 0.0 ] end -function ITensors.op(::OpName"a↓", st::SiteType"Electron") +function op(::OpName"a↓", st::SiteType"Electron") return op(OpName("Adn"), st) end -function ITensors.op(::OpName"Adagdn", ::SiteType"Electron") +function op(::OpName"Adagdn", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 @@ -215,11 +215,11 @@ function ITensors.op(::OpName"Adagdn", ::SiteType"Electron") 0.0 1.0 0.0 0.0 ] end -function ITensors.op(::OpName"a†↓", st::SiteType"Electron") +function op(::OpName"a†↓", st::SiteType"Electron") return op(OpName("Adagdn"), st) end -function ITensors.op(::OpName"F", ::SiteType"Electron") +function op(::OpName"F", ::SiteType"Electron") return [ 1.0 0.0 0.0 0.0 0.0 -1.0 0.0 0.0 @@ -228,7 +228,7 @@ function ITensors.op(::OpName"F", ::SiteType"Electron") ] end -function ITensors.op(::OpName"Fup", ::SiteType"Electron") +function op(::OpName"Fup", ::SiteType"Electron") return [ 1.0 0.0 0.0 0.0 0.0 -1.0 0.0 0.0 @@ -236,11 +236,11 @@ function ITensors.op(::OpName"Fup", ::SiteType"Electron") 0.0 0.0 0.0 -1.0 ] end -function ITensors.op(::OpName"F↑", st::SiteType"Electron") +function op(::OpName"F↑", st::SiteType"Electron") return op(OpName("Fup"), st) end -function ITensors.op(::OpName"Fdn", ::SiteType"Electron") +function op(::OpName"Fdn", ::SiteType"Electron") return [ 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 @@ -248,11 +248,11 @@ function ITensors.op(::OpName"Fdn", ::SiteType"Electron") 0.0 0.0 0.0 -1.0 ] end -function ITensors.op(::OpName"F↓", st::SiteType"Electron") +function op(::OpName"F↓", st::SiteType"Electron") return op(OpName("Fdn"), st) end -function ITensors.op(::OpName"Sz", ::SiteType"Electron") +function op(::OpName"Sz", ::SiteType"Electron") #Op[s' => 2, s => 2] = +0.5 #return Op[s' => 3, s => 3] = -0.5 return [ @@ -263,11 +263,11 @@ function ITensors.op(::OpName"Sz", ::SiteType"Electron") ] end -function ITensors.op(::OpName"Sᶻ", st::SiteType"Electron") +function op(::OpName"Sᶻ", st::SiteType"Electron") return op(OpName("Sz"), st) end -function ITensors.op(::OpName"Sx", ::SiteType"Electron") +function op(::OpName"Sx", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 @@ -276,11 +276,11 @@ function ITensors.op(::OpName"Sx", ::SiteType"Electron") ] end -function ITensors.op(::OpName"Sˣ", st::SiteType"Electron") +function op(::OpName"Sˣ", st::SiteType"Electron") return op(OpName("Sx"), st) end -function ITensors.op(::OpName"S+", ::SiteType"Electron") +function op(::OpName"S+", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 @@ -289,17 +289,17 @@ function ITensors.op(::OpName"S+", ::SiteType"Electron") ] end -function ITensors.op(::OpName"S⁺", st::SiteType"Electron") +function op(::OpName"S⁺", st::SiteType"Electron") return op(OpName("S+"), st) end -function ITensors.op(::OpName"Sp", st::SiteType"Electron") +function op(::OpName"Sp", st::SiteType"Electron") return op(OpName("S+"), st) end -function ITensors.op(::OpName"Splus", st::SiteType"Electron") +function op(::OpName"Splus", st::SiteType"Electron") return op(OpName("S+"), st) end -function ITensors.op(::OpName"S-", ::SiteType"Electron") +function op(::OpName"S-", ::SiteType"Electron") return [ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 @@ -308,29 +308,29 @@ function ITensors.op(::OpName"S-", ::SiteType"Electron") ] end -function ITensors.op(::OpName"S⁻", st::SiteType"Electron") +function op(::OpName"S⁻", st::SiteType"Electron") return op(OpName("S-"), st) end -function ITensors.op(::OpName"Sm", st::SiteType"Electron") +function op(::OpName"Sm", st::SiteType"Electron") return op(OpName("S-"), st) end -function ITensors.op(::OpName"Sminus", st::SiteType"Electron") +function op(::OpName"Sminus", st::SiteType"Electron") return op(OpName("S-"), st) end -ITensors.has_fermion_string(::OpName"Cup", ::SiteType"Electron") = true -function ITensors.has_fermion_string(on::OpName"c↑", st::SiteType"Electron") +has_fermion_string(::OpName"Cup", ::SiteType"Electron") = true +function has_fermion_string(on::OpName"c↑", st::SiteType"Electron") return has_fermion_string(alias(on), st) end -ITensors.has_fermion_string(::OpName"Cdagup", ::SiteType"Electron") = true -function ITensors.has_fermion_string(on::OpName"c†↑", st::SiteType"Electron") +has_fermion_string(::OpName"Cdagup", ::SiteType"Electron") = true +function has_fermion_string(on::OpName"c†↑", st::SiteType"Electron") return has_fermion_string(alias(on), st) end -ITensors.has_fermion_string(::OpName"Cdn", ::SiteType"Electron") = true -function ITensors.has_fermion_string(on::OpName"c↓", st::SiteType"Electron") +has_fermion_string(::OpName"Cdn", ::SiteType"Electron") = true +function has_fermion_string(on::OpName"c↓", st::SiteType"Electron") return has_fermion_string(alias(on), st) end -ITensors.has_fermion_string(::OpName"Cdagdn", ::SiteType"Electron") = true -function ITensors.has_fermion_string(on::OpName"c†↓", st::SiteType"Electron") +has_fermion_string(::OpName"Cdagdn", ::SiteType"Electron") = true +function has_fermion_string(on::OpName"c†↓", st::SiteType"Electron") return has_fermion_string(alias(on), st) end diff --git a/src/physics/site_types/fermion.jl b/src/lib/SiteTypes/src/sitetypes/fermion.jl similarity index 58% rename from src/physics/site_types/fermion.jl rename to src/lib/SiteTypes/src/sitetypes/fermion.jl index 30913d26c9..008bdf0877 100644 --- a/src/physics/site_types/fermion.jl +++ b/src/lib/SiteTypes/src/sitetypes/fermion.jl @@ -13,7 +13,7 @@ Create the Hilbert space for a site of type "Fermion". Optionally specify the conserved symmetries and their quantum number labels. """ -function ITensors.space( +function space( ::SiteType"Fermion"; conserve_qns=false, conserve_nf=conserve_qns, @@ -59,53 +59,53 @@ function ITensors.space( return 2 end -ITensors.val(::ValName"Emp", ::SiteType"Fermion") = 1 -ITensors.val(::ValName"Occ", ::SiteType"Fermion") = 2 -ITensors.val(::ValName"0", st::SiteType"Fermion") = val(ValName("Emp"), st) -ITensors.val(::ValName"1", st::SiteType"Fermion") = val(ValName("Occ"), st) +val(::ValName"Emp", ::SiteType"Fermion") = 1 +val(::ValName"Occ", ::SiteType"Fermion") = 2 +val(::ValName"0", st::SiteType"Fermion") = val(ValName("Emp"), st) +val(::ValName"1", st::SiteType"Fermion") = val(ValName("Occ"), st) -ITensors.state(::StateName"Emp", ::SiteType"Fermion") = [1.0 0.0] -ITensors.state(::StateName"Occ", ::SiteType"Fermion") = [0.0 1.0] -ITensors.state(::StateName"0", st::SiteType"Fermion") = state(StateName("Emp"), st) -ITensors.state(::StateName"1", st::SiteType"Fermion") = state(StateName("Occ"), st) +state(::StateName"Emp", ::SiteType"Fermion") = [1.0 0.0] +state(::StateName"Occ", ::SiteType"Fermion") = [0.0 1.0] +state(::StateName"0", st::SiteType"Fermion") = state(StateName("Emp"), st) +state(::StateName"1", st::SiteType"Fermion") = state(StateName("Occ"), st) -function ITensors.op!(Op::ITensor, ::OpName"N", ::SiteType"Fermion", s::Index) +function op!(Op::ITensor, ::OpName"N", ::SiteType"Fermion", s::Index) return Op[s' => 2, s => 2] = 1.0 end -function ITensors.op!(Op::ITensor, on::OpName"n", st::SiteType"Fermion", s::Index) +function op!(Op::ITensor, on::OpName"n", st::SiteType"Fermion", s::Index) return op!(Op, alias(on), st, s) end -function ITensors.op!(Op::ITensor, ::OpName"C", ::SiteType"Fermion", s::Index) +function op!(Op::ITensor, ::OpName"C", ::SiteType"Fermion", s::Index) return Op[s' => 1, s => 2] = 1.0 end -function ITensors.op!(Op::ITensor, on::OpName"c", st::SiteType"Fermion", s::Index) +function op!(Op::ITensor, on::OpName"c", st::SiteType"Fermion", s::Index) return op!(Op, alias(on), st, s) end -function ITensors.op!(Op::ITensor, ::OpName"Cdag", ::SiteType"Fermion", s::Index) +function op!(Op::ITensor, ::OpName"Cdag", ::SiteType"Fermion", s::Index) return Op[s' => 2, s => 1] = 1.0 end -function ITensors.op!(Op::ITensor, on::OpName"c†", st::SiteType"Fermion", s::Index) +function op!(Op::ITensor, on::OpName"c†", st::SiteType"Fermion", s::Index) return op!(Op, alias(on), st, s) end -function ITensors.op!(Op::ITensor, on::OpName"cdag", st::SiteType"Fermion", s::Index) +function op!(Op::ITensor, on::OpName"cdag", st::SiteType"Fermion", s::Index) return op!(Op, alias(on), st, s) end -function ITensors.op!(Op::ITensor, ::OpName"F", ::SiteType"Fermion", s::Index) +function op!(Op::ITensor, ::OpName"F", ::SiteType"Fermion", s::Index) Op[s' => 1, s => 1] = +1.0 return Op[s' => 2, s => 2] = -1.0 end -ITensors.has_fermion_string(::OpName"C", ::SiteType"Fermion") = true -function ITensors.has_fermion_string(on::OpName"c", st::SiteType"Fermion") +has_fermion_string(::OpName"C", ::SiteType"Fermion") = true +function has_fermion_string(on::OpName"c", st::SiteType"Fermion") return has_fermion_string(alias(on), st) end -ITensors.has_fermion_string(::OpName"Cdag", ::SiteType"Fermion") = true -function ITensors.has_fermion_string(on::OpName"c†", st::SiteType"Fermion") +has_fermion_string(::OpName"Cdag", ::SiteType"Fermion") = true +function has_fermion_string(on::OpName"c†", st::SiteType"Fermion") return has_fermion_string(alias(on), st) end -function ITensors.has_fermion_string(on::OpName"cdag", st::SiteType"Fermion") +function has_fermion_string(on::OpName"cdag", st::SiteType"Fermion") return has_fermion_string(alias(on), st) end diff --git a/src/physics/site_types/generic_sites.jl b/src/lib/SiteTypes/src/sitetypes/generic_sites.jl similarity index 91% rename from src/physics/site_types/generic_sites.jl rename to src/lib/SiteTypes/src/sitetypes/generic_sites.jl index ea6b32840c..6c7ab60320 100644 --- a/src/physics/site_types/generic_sites.jl +++ b/src/lib/SiteTypes/src/sitetypes/generic_sites.jl @@ -1,3 +1,7 @@ +using LinearAlgebra: I +using NDTensors: NDTensors, dim, tensor +using ..ITensors: ITensor, itensor, settensor! + function op!( o::ITensor, ::OpName"Id", ::SiteType"Generic", s1::Index, sn::Index...; eltype=Float64 ) diff --git a/src/lib/SiteTypes/src/sitetypes/qubit.jl b/src/lib/SiteTypes/src/sitetypes/qubit.jl new file mode 100644 index 0000000000..149539355e --- /dev/null +++ b/src/lib/SiteTypes/src/sitetypes/qubit.jl @@ -0,0 +1,502 @@ +using ..ITensors: ITensors + +# +# Qubit site type +# + +# Define Qubit space in terms of +# Qubit/2 space, but use different +# defaults for QN names + +""" + space(::SiteType"Qubit"; + conserve_qns = false, + conserve_parity = conserve_qns, + conserve_number = false, + qnname_parity = "Parity", + qnname_number = "Number") + +Create the Hilbert space for a site of type "Qubit". + +Optionally specify the conserved symmetries and their quantum number labels. +""" +function space( + ::SiteType"Qubit"; + conserve_qns=false, + conserve_parity=conserve_qns, + conserve_number=false, + qnname_parity="Parity", + qnname_number="Number", +) + if conserve_number && conserve_parity + return [ + QN((qnname_number, 0), (qnname_parity, 0, 2)) => 1, + QN((qnname_number, 1), (qnname_parity, 1, 2)) => 1, + ] + elseif conserve_number + return [QN(qnname_number, 0) => 1, QN(qnname_number, 1) => 1] + elseif conserve_parity + return [QN(qnname_parity, 0, 2) => 1, QN(qnname_parity, 1, 2) => 1] + end + return 2 +end + +val(::ValName"0", ::SiteType"Qubit") = 1 +val(::ValName"1", ::SiteType"Qubit") = 2 +val(::ValName"Up", ::SiteType"Qubit") = 1 +val(::ValName"Dn", ::SiteType"Qubit") = 2 +val(::ValName"↑", ::SiteType"Qubit") = 1 +val(::ValName"↓", ::SiteType"Qubit") = 2 + +state(::StateName"0", ::SiteType"Qubit") = [1.0, 0.0] +state(::StateName"1", ::SiteType"Qubit") = [0.0, 1.0] +state(::StateName"+", ::SiteType"Qubit") = [1.0, 1.0] / √2 +state(::StateName"-", ::SiteType"Qubit") = [1.0, -1.0] / √2 +state(::StateName"i", ::SiteType"Qubit") = [1.0, im] / √2 +state(::StateName"-i", ::SiteType"Qubit") = [1.0, -im] / √2 +state(::StateName"Up", t::SiteType"Qubit") = state(StateName("0"), t) +state(::StateName"Dn", t::SiteType"Qubit") = state(StateName("1"), t) +state(::StateName"↑", t::SiteType"Qubit") = state(StateName("0"), t) +state(::StateName"↓", t::SiteType"Qubit") = state(StateName("1"), t) + +# Pauli eingenstates +state(::StateName"X+", t::SiteType"Qubit") = state(StateName("+"), t) +state(::StateName"Xp", t::SiteType"Qubit") = state(StateName("+"), t) +state(::StateName"X-", t::SiteType"Qubit") = state(StateName("-"), t) +state(::StateName"Xm", t::SiteType"Qubit") = state(StateName("-"), t) + +state(::StateName"Y+", t::SiteType"Qubit") = state(StateName("i"), t) +state(::StateName"Yp", t::SiteType"Qubit") = state(StateName("i"), t) +state(::StateName"Y-", t::SiteType"Qubit") = state(StateName("-i"), t) +state(::StateName"Ym", t::SiteType"Qubit") = state(StateName("-i"), t) + +state(::StateName"Z+", t::SiteType"Qubit") = state(StateName("0"), t) +state(::StateName"Zp", t::SiteType"Qubit") = state(StateName("0"), t) +state(::StateName"Z-", t::SiteType"Qubit") = state(StateName("1"), t) +state(::StateName"Zm", t::SiteType"Qubit") = state(StateName("1"), t) + +# SIC-POVMs +state(::StateName"Tetra1", t::SiteType"Qubit") = state(StateName("Z+"), t) +state(::StateName"Tetra2", t::SiteType"Qubit") = [ + 1 / √3 + √2 / √3 +] +state(::StateName"Tetra3", t::SiteType"Qubit") = [ + 1 / √3 + √2 / √3 * exp(im * 2π / 3) +] +state(::StateName"Tetra4", t::SiteType"Qubit") = [ + 1 / √3 + √2 / √3 * exp(im * 4π / 3) +] + +# +# 1-Qubit gates +# +op(::OpName"X", ::SiteType"Qubit") = [ + 0 1 + 1 0 +] + +op(::OpName"σx", t::SiteType"Qubit") = op("X", t) + +op(::OpName"σ1", t::SiteType"Qubit") = op("X", t) + +op(::OpName"Y", ::SiteType"Qubit") = [ + 0.0 -1.0im + 1.0im 0.0 +] + +op(::OpName"σy", t::SiteType"Qubit") = op("Y", t) + +op(::OpName"σ2", t::SiteType"Qubit") = op("Y", t) + +op(::OpName"iY", ::SiteType"Qubit") = [ + 0 1 + -1 0 +] +op(::OpName"iσy", t::SiteType"Qubit") = op("iY", t) + +op(::OpName"iσ2", t::SiteType"Qubit") = op("iY", t) + +op(::OpName"Z", ::SiteType"Qubit") = [ + 1 0 + 0 -1 +] + +op(::OpName"σz", t::SiteType"Qubit") = op("Z", t) + +op(::OpName"σ3", t::SiteType"Qubit") = op("Z", t) + +function op(::OpName"√NOT", ::SiteType"Qubit") + return [ + (1 + im)/2 (1 - im)/2 + (1 - im)/2 (1 + im)/2 + ] +end + +op(::OpName"√X", t::SiteType"Qubit") = op("√NOT", t) + +op(::OpName"H", ::SiteType"Qubit") = [ + 1/sqrt(2) 1/sqrt(2) + 1/sqrt(2) -1/sqrt(2) +] + +# Rϕ with ϕ = π/2 +op(::OpName"Phase", ::SiteType"Qubit"; ϕ::Number=π / 2) = [ + 1 0 + 0 exp(im * ϕ) +] + +op(::OpName"P", t::SiteType"Qubit"; kwargs...) = op("Phase", t; kwargs...) + +op(::OpName"S", t::SiteType"Qubit") = op("Phase", t; ϕ=π / 2) + +## Rϕ with ϕ = π/4 +op(::OpName"π/8", ::SiteType"Qubit") = [ + 1 0 + 0 1 / sqrt(2)+im / sqrt(2) +] + +op(::OpName"T", t::SiteType"Qubit") = op("π/8", t) + +# Rotation around X-axis +function op(::OpName"Rx", ::SiteType"Qubit"; θ::Number) + return [ + cos(θ / 2) -im*sin(θ / 2) + -im*sin(θ / 2) cos(θ / 2) + ] +end + +# Rotation around Y-axis +function op(::OpName"Ry", ::SiteType"Qubit"; θ::Number) + return [ + cos(θ / 2) -sin(θ / 2) + sin(θ / 2) cos(θ / 2) + ] +end + +# Rotation around Z-axis +function op(::OpName"Rz", ::SiteType"Qubit"; θ=nothing, ϕ=nothing) + isone(count(isnothing, (θ, ϕ))) || error( + "Must specify the keyword argument `θ` (or the deprecated `ϕ`) when creating an Rz gate, but not both.", + ) + isnothing(θ) && (θ = ϕ) + return [ + exp(-im * θ / 2) 0 + 0 exp(im * θ / 2) + ] +end + +# Rotation around generic axis n̂ +function op(::OpName"Rn", ::SiteType"Qubit"; θ::Real, ϕ::Real, λ::Real) + return [ + cos(θ / 2) -exp(im * λ)*sin(θ / 2) + exp(im * ϕ)*sin(θ / 2) exp(im * (ϕ + λ))*cos(θ / 2) + ] +end + +function op(::OpName"Rn̂", t::SiteType"Qubit"; kwargs...) + return op(OpName("Rn"), t; kwargs...) +end + +# +# 2-Qubit gates +# + +op(::OpName"CNOT", ::SiteType"Qubit") = [ + 1 0 0 0 + 0 1 0 0 + 0 0 0 1 + 0 0 1 0 +] + +op(::OpName"CX", t::SiteType"Qubit") = op("CNOT", t) + +op(::OpName"CY", ::SiteType"Qubit") = [ + 1 0 0 0 + 0 1 0 0 + 0 0 0 -im + 0 0 im 0 +] + +op(::OpName"CZ", ::SiteType"Qubit") = [ + 1 0 0 0 + 0 1 0 0 + 0 0 1 0 + 0 0 0 -1 +] + +function op(::OpName"CPHASE", ::SiteType"Qubit"; ϕ::Number) + return [ + 1 0 0 0 + 0 1 0 0 + 0 0 1 0 + 0 0 0 exp(im * ϕ) + ] +end +op(::OpName"Cphase", t::SiteType"Qubit"; kwargs...) = op("CPHASE", t; kwargs...) + +function op(::OpName"CRx", ::SiteType"Qubit"; θ::Number) + return [ + 1 0 0 0 + 0 1 0 0 + 0 0 cos(θ / 2) -im*sin(θ / 2) + 0 0 -im*sin(θ / 2) cos(θ / 2) + ] +end +op(::OpName"CRX", t::SiteType"Qubit"; kwargs...) = op("CRx", t; kwargs...) + +function op(::OpName"CRy", ::SiteType"Qubit"; θ::Number) + return [ + 1 0 0 0 + 0 1 0 0 + 0 0 cos(θ / 2) -sin(θ / 2) + 0 0 sin(θ / 2) cos(θ / 2) + ] +end +op(::OpName"CRY", t::SiteType"Qubit"; kwargs...) = op("CRy", t; kwargs...) + +function op(::OpName"CRz", ::SiteType"Qubit"; ϕ=nothing, θ=nothing) + isone(count(isnothing, (θ, ϕ))) || error( + "Must specify the keyword argument `θ` (or the deprecated `ϕ`) when creating a CRz gate, but not both.", + ) + isnothing(θ) && (θ = ϕ) + return [ + 1 0 0 0 + 0 1 0 0 + 0 0 exp(-im * θ / 2) 0 + 0 0 0 exp(im * θ / 2) + ] +end +op(::OpName"CRZ", t::SiteType"Qubit"; kwargs...) = op("CRz", t; kwargs...) + +function op(::OpName"CRn", ::SiteType"Qubit"; θ::Number, ϕ::Number, λ::Number) + return [ + 1 0 0 0 + 0 1 0 0 + 0 0 cos(θ / 2) -exp(im * λ)*sin(θ / 2) + 0 0 exp(im * ϕ)*sin(θ / 2) exp(im * (ϕ + λ))*cos(θ / 2) + ] +end +function op(::OpName"CRn̂", t::SiteType"Qubit"; kwargs...) + return op("CRn", t; kwargs...) +end + +op(::OpName"SWAP", ::SiteType"Qubit") = [ + 1 0 0 0 + 0 0 1 0 + 0 1 0 0 + 0 0 0 1 +] +op(::OpName"Swap", t::SiteType"Qubit") = op("SWAP", t) + +function op(::OpName"√SWAP", ::SiteType"Qubit") + return [ + 1 0 0 0 + 0 (1 + im)/2 (1 - im)/2 0 + 0 (1 - im)/2 (1 + im)/2 0 + 0 0 0 1 + ] +end +op(::OpName"√Swap", t::SiteType"Qubit") = op("√SWAP", t) + +op(::OpName"iSWAP", t::SiteType"Qubit") = [ + 1 0 0 0 + 0 0 im 0 + 0 im 0 0 + 0 0 0 1 +] +op(::OpName"iSwap", t::SiteType"Qubit") = op("iSWAP", t) + +function op(::OpName"√iSWAP", t::SiteType"Qubit") + return [ + 1 0 0 0 + 0 1/√2 im/√2 0 + 0 im/√2 1/√2 0 + 0 0 0 1 + ] +end +op(::OpName"√iSwap", t::SiteType"Qubit") = op("√iSWAP", t) + +# Ising (XX) coupling gate +function op(::OpName"Rxx", t::SiteType"Qubit"; ϕ::Number) + return [ + cos(ϕ) 0 0 -im*sin(ϕ) + 0 cos(ϕ) -im*sin(ϕ) 0 + 0 -im*sin(ϕ) cos(ϕ) 0 + -im*sin(ϕ) 0 0 cos(ϕ) + ] +end +op(::OpName"RXX", t::SiteType"Qubit"; kwargs...) = op("Rxx", t; kwargs...) + +# Ising (YY) coupling gate +function op(::OpName"Ryy", ::SiteType"Qubit"; ϕ::Number) + return [ + cos(ϕ) 0 0 im*sin(ϕ) + 0 cos(ϕ) -im*sin(ϕ) 0 + 0 -im*sin(ϕ) cos(ϕ) 0 + im*sin(ϕ) 0 0 cos(ϕ) + ] +end +op(::OpName"RYY", t::SiteType"Qubit"; kwargs...) = op("Ryy", t; kwargs...) + +# Ising (XY) coupling gate +function op(::OpName"Rxy", t::SiteType"Qubit"; ϕ::Number) + return [ + 1 0 0 0 + 0 cos(ϕ) -im*sin(ϕ) 0 + 0 -im*sin(ϕ) cos(ϕ) 0 + 0 0 0 1 + ] +end +op(::OpName"RXY", t::SiteType"Qubit"; kwargs...) = op("Rxy", t; kwargs...) + +# Ising (ZZ) coupling gate +function op(::OpName"Rzz", ::SiteType"Qubit"; ϕ::Number) + return [ + exp(-im * ϕ) 0 0 0 + 0 exp(im * ϕ) 0 0 + 0 0 exp(im * ϕ) 0 + 0 0 0 exp(-im * ϕ) + ] +end +op(::OpName"RZZ", t::SiteType"Qubit"; kwargs...) = op("Rzz", t; kwargs...) + +# +# 3-Qubit gates +# + +function op(::OpName"Toffoli", ::SiteType"Qubit") + return [ + 1 0 0 0 0 0 0 0 + 0 1 0 0 0 0 0 0 + 0 0 1 0 0 0 0 0 + 0 0 0 1 0 0 0 0 + 0 0 0 0 1 0 0 0 + 0 0 0 0 0 1 0 0 + 0 0 0 0 0 0 0 1 + 0 0 0 0 0 0 1 0 + ] +end + +op(::OpName"CCNOT", t::SiteType"Qubit") = op("Toffoli", t) + +op(::OpName"CCX", t::SiteType"Qubit") = op("Toffoli", t) + +op(::OpName"TOFF", t::SiteType"Qubit") = op("Toffoli", t) + +function op(::OpName"Fredkin", ::SiteType"Qubit") + return [ + 1 0 0 0 0 0 0 0 + 0 1 0 0 0 0 0 0 + 0 0 1 0 0 0 0 0 + 0 0 0 1 0 0 0 0 + 0 0 0 0 1 0 0 0 + 0 0 0 0 0 0 1 0 + 0 0 0 0 0 1 0 0 + 0 0 0 0 0 0 0 1 + ] +end + +op(::OpName"CSWAP", t::SiteType"Qubit") = op("Fredkin", t) +op(::OpName"CSwap", t::SiteType"Qubit") = op("Fredkin", t) + +op(::OpName"CS", t::SiteType"Qubit") = op("Fredkin", t) + +# +# 4-Qubit gates +# + +function op(::OpName"CCCNOT", ::SiteType"Qubit") + return [ + 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 + ] +end + +# spin-full operators +op(::OpName"Sz", ::SiteType"Qubit") = [ + 0.5 0.0 + 0.0 -0.5 +] + +op(::OpName"Sᶻ", t::SiteType"Qubit") = op(OpName("Sz"), t) + +op(::OpName"S+", ::SiteType"Qubit") = [ + 0 1 + 0 0 +] + +op(::OpName"S⁺", t::SiteType"Qubit") = op(OpName("S+"), t) + +op(::OpName"Splus", t::SiteType"Qubit") = op(OpName("S+"), t) + +op(::OpName"S-", ::SiteType"Qubit") = [ + 0 0 + 1 0 +] + +op(::OpName"S⁻", t::SiteType"Qubit") = op(OpName("S-"), t) + +op(::OpName"Sminus", t::SiteType"Qubit") = op(OpName("S-"), t) + +op(::OpName"Sx", ::SiteType"Qubit") = [ + 0.0 0.5 + 0.5 0.0 +] + +op(::OpName"Sˣ", t::SiteType"Qubit") = op(OpName("Sx"), t) + +op(::OpName"iSy", ::SiteType"Qubit") = [ + 0.0 0.5 + -0.5 0.0 +] + +op(::OpName"iSʸ", t::SiteType"Qubit") = op(OpName("iSy"), t) + +op(::OpName"Sy", ::SiteType"Qubit") = [ + 0.0 -0.5im + 0.5im 0.0 +] + +op(::OpName"Sʸ", t::SiteType"Qubit") = op(OpName("Sy"), t) + +op(::OpName"S2", ::SiteType"Qubit") = [ + 0.75 0.0 + 0.0 0.75 +] + +op(::OpName"S²", t::SiteType"Qubit") = op(OpName("S2"), t) + +op(::OpName"ProjUp", ::SiteType"Qubit") = [ + 1 0 + 0 0 +] + +op(::OpName"projUp", t::SiteType"Qubit") = op(OpName("ProjUp"), t) + +op(::OpName"Proj0", t::SiteType"Qubit") = op(OpName("ProjUp"), t) + +op(::OpName"ProjDn", ::SiteType"Qubit") = [ + 0 0 + 0 1 +] + +op(::OpName"projDn", t::SiteType"Qubit") = op(OpName("ProjDn"), t) + +op(::OpName"Proj1", t::SiteType"Qubit") = op(OpName("ProjDn"), t) diff --git a/src/physics/site_types/qudit.jl b/src/lib/SiteTypes/src/sitetypes/qudit.jl similarity index 94% rename from src/physics/site_types/qudit.jl rename to src/lib/SiteTypes/src/sitetypes/qudit.jl index 49cd7f193a..6644cf06e8 100644 --- a/src/physics/site_types/qudit.jl +++ b/src/lib/SiteTypes/src/sitetypes/qudit.jl @@ -1,3 +1,5 @@ +using ChainRulesCore: @non_differentiable + """ space(::SiteType"Qudit"; dim = 2, @@ -9,7 +11,7 @@ Create the Hilbert space for a site of type "Qudit". Optionally specify the conserved symmetries and their quantum number labels. """ -function ITensors.space( +function space( ::SiteType"Qudit"; dim=2, conserve_qns=false, @@ -22,11 +24,11 @@ function ITensors.space( return dim end -function ITensors.val(::ValName{N}, ::SiteType"Qudit") where {N} +function val(::ValName{N}, ::SiteType"Qudit") where {N} return parse(Int, String(N)) + 1 end -function ITensors.state(::StateName{N}, ::SiteType"Qudit", s::Index) where {N} +function state(::StateName{N}, ::SiteType"Qudit", s::Index) where {N} n = parse(Int, String(N)) st = zeros(dim(s)) st[n + 1] = 1.0 diff --git a/src/physics/site_types/spinhalf.jl b/src/lib/SiteTypes/src/sitetypes/spinhalf.jl similarity index 72% rename from src/physics/site_types/spinhalf.jl rename to src/lib/SiteTypes/src/sitetypes/spinhalf.jl index 5592ec01ab..c374d47c0d 100644 --- a/src/physics/site_types/spinhalf.jl +++ b/src/lib/SiteTypes/src/sitetypes/spinhalf.jl @@ -11,7 +11,7 @@ Create the Hilbert space for a site of type "S=1/2". Optionally specify the conserved symmetries and their quantum number labels. """ -function ITensors.space( +function space( ::SiteType"S=1/2"; conserve_qns=false, conserve_sz=conserve_qns, @@ -34,24 +34,24 @@ end # Use Qubit definition of any operator/state # called using S=1/2 SiteType -function ITensors.val(vn::ValName, ::SiteType"S=1/2"; kwargs...) +function val(vn::ValName, ::SiteType"S=1/2"; kwargs...) return val(vn, SiteType("Qubit"); kwargs...) end -function ITensors.state(sn::StateName, ::SiteType"S=1/2"; kwargs...) +function state(sn::StateName, ::SiteType"S=1/2"; kwargs...) return state(sn, SiteType("Qubit"); kwargs...) end -ITensors.op(o::OpName, ::SiteType"S=1/2"; kwargs...) = op(o, SiteType("Qubit"); kwargs...) +op(o::OpName, ::SiteType"S=1/2"; kwargs...) = op(o, SiteType("Qubit"); kwargs...) # Support the tag "SpinHalf" as equivalent to "S=1/2" space(::SiteType"SpinHalf"; kwargs...) = space(SiteType("S=1/2"); kwargs...) val(name::ValName, ::SiteType"SpinHalf") = val(name, SiteType("S=1/2")) -ITensors.state(name::StateName, ::SiteType"SpinHalf") = state(name, SiteType("S=1/2")) +state(name::StateName, ::SiteType"SpinHalf") = state(name, SiteType("S=1/2")) -function ITensors.op(o::OpName, ::SiteType"SpinHalf"; kwargs...) +function op(o::OpName, ::SiteType"SpinHalf"; kwargs...) return op(o, SiteType("S=1/2"); kwargs...) end @@ -61,6 +61,6 @@ space(::SiteType"S=½"; kwargs...) = space(SiteType("S=1/2"); kwargs...) val(name::ValName, ::SiteType"S=½") = val(name, SiteType("S=1/2")) -ITensors.state(name::StateName, ::SiteType"S=½") = state(name, SiteType("S=1/2")) +state(name::StateName, ::SiteType"S=½") = state(name, SiteType("S=1/2")) -ITensors.op(o::OpName, ::SiteType"S=½"; kwargs...) = op(o, SiteType("S=1/2"); kwargs...) +op(o::OpName, ::SiteType"S=½"; kwargs...) = op(o, SiteType("S=1/2"); kwargs...) diff --git a/src/lib/SiteTypes/src/sitetypes/spinone.jl b/src/lib/SiteTypes/src/sitetypes/spinone.jl new file mode 100644 index 0000000000..bae0b765df --- /dev/null +++ b/src/lib/SiteTypes/src/sitetypes/spinone.jl @@ -0,0 +1,159 @@ +using ..ITensors: complex!, QN + +""" + space(::SiteType"S=1"; + conserve_qns = false, + conserve_sz = conserve_qns, + qnname_sz = "Sz") + +Create the Hilbert space for a site of type "S=1". + +Optionally specify the conserved symmetries and their quantum number labels. +""" +function space( + ::SiteType"S=1"; conserve_qns=false, conserve_sz=conserve_qns, qnname_sz="Sz" +) + if conserve_sz + return [QN(qnname_sz, +2) => 1, QN(qnname_sz, 0) => 1, QN(qnname_sz, -2) => 1] + end + return 3 +end + +val(::ValName"Up", ::SiteType"S=1") = 1 +val(::ValName"Z0", ::SiteType"S=1") = 2 +val(::ValName"Dn", ::SiteType"S=1") = 3 + +val(::ValName"↑", st::SiteType"S=1") = 1 +val(::ValName"0", st::SiteType"S=1") = 2 +val(::ValName"↓", st::SiteType"S=1") = 3 + +val(::ValName"Z+", ::SiteType"S=1") = 1 +# -- Z0 is already defined above -- +val(::ValName"Z-", ::SiteType"S=1") = 3 + +state(::StateName"Up", ::SiteType"S=1") = [1.0, 0.0, 0.0] +state(::StateName"Z0", ::SiteType"S=1") = [0.0, 1.0, 0.0] +state(::StateName"Dn", ::SiteType"S=1") = [0.0, 0.0, 1.0] + +state(::StateName"↑", st::SiteType"S=1") = [1.0, 0.0, 0.0] +state(::StateName"0", st::SiteType"S=1") = [0.0, 1.0, 0.0] +state(::StateName"↓", st::SiteType"S=1") = [0.0, 0.0, 1.0] + +state(::StateName"Z+", st::SiteType"S=1") = [1.0, 0.0, 0.0] +# -- Z0 is already defined above -- +state(::StateName"Z-", st::SiteType"S=1") = [0.0, 0.0, 1.0] + +state(::StateName"X+", ::SiteType"S=1") = [1 / 2, 1 / sqrt(2), 1 / 2] +state(::StateName"X0", ::SiteType"S=1") = [-1 / sqrt(2), 0, 1 / sqrt(2)] +state(::StateName"X-", ::SiteType"S=1") = [1 / 2, -1 / sqrt(2), 1 / 2] + +state(::StateName"Y+", ::SiteType"S=1") = [-1 / 2, -im / sqrt(2), 1 / 2] +state(::StateName"Y0", ::SiteType"S=1") = [1 / sqrt(2), 0, 1 / sqrt(2)] +state(::StateName"Y-", ::SiteType"S=1") = [-1 / 2, im / sqrt(2), 1 / 2] + +function op!(Op::ITensor, ::OpName"Sz", ::SiteType"S=1", s::Index) + Op[s' => 1, s => 1] = +1.0 + return Op[s' => 3, s => 3] = -1.0 +end + +function op!(Op::ITensor, ::OpName"Sᶻ", t::SiteType"S=1", s::Index) + return op!(Op, OpName("Sz"), t, s) +end + +function op!(Op::ITensor, ::OpName"S+", ::SiteType"S=1", s::Index) + Op[s' => 2, s => 3] = sqrt(2) + return Op[s' => 1, s => 2] = sqrt(2) +end + +function op!(Op::ITensor, ::OpName"S⁺", t::SiteType"S=1", s::Index) + return op!(Op, OpName("S+"), t, s) +end + +function op!(Op::ITensor, ::OpName"Splus", t::SiteType"S=1", s::Index) + return op!(Op, OpName("S+"), t, s) +end + +function op!(Op::ITensor, ::OpName"Sp", t::SiteType"S=1", s::Index) + return op!(Op, OpName("S+"), t, s) +end + +function op!(Op::ITensor, ::OpName"S-", ::SiteType"S=1", s::Index) + Op[s' => 3, s => 2] = sqrt(2) + return Op[s' => 2, s => 1] = sqrt(2) +end + +function op!(Op::ITensor, ::OpName"S⁻", t::SiteType"S=1", s::Index) + return op!(Op, OpName("S-"), t, s) +end + +function op!(Op::ITensor, ::OpName"Sminus", t::SiteType"S=1", s::Index) + return op!(Op, OpName("S-"), t, s) +end + +function op!(Op::ITensor, ::OpName"Sm", t::SiteType"S=1", s::Index) + return op!(Op, OpName("S-"), t, s) +end + +function op!(Op::ITensor, ::OpName"Sx", ::SiteType"S=1", s::Index) + Op[s' => 2, s => 1] = 1 / sqrt(2) + Op[s' => 1, s => 2] = 1 / sqrt(2) + Op[s' => 3, s => 2] = 1 / sqrt(2) + return Op[s' => 2, s => 3] = 1 / sqrt(2) +end + +function op!(Op::ITensor, ::OpName"Sˣ", t::SiteType"S=1", s::Index) + return op!(Op, OpName("Sx"), t, s) +end + +function op!(Op::ITensor, ::OpName"iSy", ::SiteType"S=1", s::Index) + Op[s' => 2, s => 1] = -1 / sqrt(2) + Op[s' => 1, s => 2] = +1 / sqrt(2) + Op[s' => 3, s => 2] = -1 / sqrt(2) + return Op[s' => 2, s => 3] = +1 / sqrt(2) +end + +function op!(Op::ITensor, ::OpName"iSʸ", t::SiteType"S=1", s::Index) + return op!(Op, OpName("iSy"), t, s) +end + +function op!(Op::ITensor, ::OpName"Sy", ::SiteType"S=1", s::Index) + complex!(Op) + Op[s' => 2, s => 1] = +1im / sqrt(2) + Op[s' => 1, s => 2] = -1im / sqrt(2) + Op[s' => 3, s => 2] = +1im / sqrt(2) + return Op[s' => 2, s => 3] = -1im / sqrt(2) +end + +function op!(Op::ITensor, ::OpName"Sʸ", t::SiteType"S=1", s::Index) + return op!(Op, OpName("Sy"), t, s) +end + +function op!(Op::ITensor, ::OpName"Sz2", ::SiteType"S=1", s::Index) + Op[s' => 1, s => 1] = +1.0 + return Op[s' => 3, s => 3] = +1.0 +end + +function op!(Op::ITensor, ::OpName"Sx2", ::SiteType"S=1", s::Index) + Op[s' => 1, s => 1] = 0.5 + Op[s' => 3, s => 1] = 0.5 + Op[s' => 2, s => 2] = 1.0 + Op[s' => 1, s => 3] = 0.5 + return Op[s' => 3, s => 3] = 0.5 +end + +function op!(Op::ITensor, ::OpName"Sy2", ::SiteType"S=1", s::Index) + Op[s' => 1, s => 1] = +0.5 + Op[s' => 3, s => 1] = -0.5 + Op[s' => 2, s => 2] = +1.0 + Op[s' => 1, s => 3] = -0.5 + return Op[s' => 3, s => 3] = +0.5 +end + +space(::SiteType"SpinOne"; kwargs...) = space(SiteType("S=1"); kwargs...) + +state(name::StateName, ::SiteType"SpinOne") = state(name, SiteType("S=1")) +val(name::ValName, ::SiteType"SpinOne") = val(name, SiteType("S=1")) + +function op!(Op::ITensor, o::OpName, ::SiteType"SpinOne", s::Index) + return op!(Op, o, SiteType("S=1"), s) +end diff --git a/src/lib/SiteTypes/src/sitetypes/tj.jl b/src/lib/SiteTypes/src/sitetypes/tj.jl new file mode 100644 index 0000000000..063f46d721 --- /dev/null +++ b/src/lib/SiteTypes/src/sitetypes/tj.jl @@ -0,0 +1,234 @@ + +""" + space(::SiteType"tJ"; + conserve_qns = false, + conserve_sz = conserve_qns, + conserve_nf = conserve_qns, + conserve_nfparity = conserve_qns, + qnname_sz = "Sz", + qnname_nf = "Nf", + qnname_nfparity = "NfParity") + +Create the Hilbert space for a site of type "tJ". + +Optionally specify the conserved symmetries and their quantum number labels. +""" +function space( + ::SiteType"tJ"; + conserve_qns=false, + conserve_sz=conserve_qns, + conserve_nf=conserve_qns, + conserve_nfparity=conserve_qns, + qnname_sz="Sz", + qnname_nf="Nf", + qnname_nfparity="NfParity", + # Deprecated + conserve_parity=nothing, +) + if !isnothing(conserve_parity) + conserve_nfparity = conserve_parity + end + if conserve_sz && conserve_nf + return [ + QN((qnname_nf, 0, -1), (qnname_sz, 0)) => 1 + QN((qnname_nf, 1, -1), (qnname_sz, +1)) => 1 + QN((qnname_nf, 1, -1), (qnname_sz, -1)) => 1 + ] + elseif conserve_nf + return [ + QN(qnname_nf, 0, -1) => 1 + QN(qnname_nf, 1, -1) => 2 + ] + elseif conserve_sz + return [ + QN((qnname_sz, 0), (qnname_nfparity, 0, -2)) => 1 + QN((qnname_sz, +1), (qnname_nfparity, 1, -2)) => 1 + QN((qnname_sz, -1), (qnname_nfparity, 1, -2)) => 1 + ] + elseif conserve_nfparity + return [ + QN(qnname_nfparity, 0, -2) => 1 + QN(qnname_nfparity, 1, -2) => 2 + ] + end + return 3 +end + +val(::ValName"Emp", ::SiteType"tJ") = 1 +val(::ValName"Up", ::SiteType"tJ") = 2 +val(::ValName"Dn", ::SiteType"tJ") = 3 +val(::ValName"0", st::SiteType"tJ") = val(ValName("Emp"), st) +val(::ValName"↑", st::SiteType"tJ") = val(ValName("Up"), st) +val(::ValName"↓", st::SiteType"tJ") = val(ValName("Dn"), st) + +state(::StateName"Emp", ::SiteType"tJ") = [1.0, 0, 0] +state(::StateName"Up", ::SiteType"tJ") = [0.0, 1, 0] +state(::StateName"Dn", ::SiteType"tJ") = [0.0, 0, 1] +state(::StateName"0", st::SiteType"tJ") = state(StateName("Emp"), st) +state(::StateName"↑", st::SiteType"tJ") = state(StateName("Up"), st) +state(::StateName"↓", st::SiteType"tJ") = state(StateName("Dn"), st) + +function op!(Op::ITensor, ::OpName"Nup", ::SiteType"tJ", s::Index) + return Op[s' => 2, s => 2] = 1.0 +end +function op!(Op::ITensor, on::OpName"n↑", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Ndn", ::SiteType"tJ", s::Index) + return Op[s' => 3, s => 3] = 1.0 +end +function op!(Op::ITensor, on::OpName"n↓", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Ntot", ::SiteType"tJ", s::Index) + Op[s' => 2, s => 2] = 1.0 + return Op[s' => 3, s => 3] = 1.0 +end +function op!(Op::ITensor, on::OpName"ntot", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Cup", ::SiteType"tJ", s::Index) + return Op[s' => 1, s => 2] = 1.0 +end +function op!(Op::ITensor, on::OpName"c↑", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Cdagup", ::SiteType"tJ", s::Index) + return Op[s' => 2, s => 1] = 1.0 +end +function op!(Op::ITensor, on::OpName"c†↑", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Cdn", ::SiteType"tJ", s::Index) + return Op[s' => 1, s => 3] = 1.0 +end +function op!(Op::ITensor, on::OpName"c↓", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Cdagdn", ::SiteType"tJ", s::Index) + return Op[s' => 3, s => 1] = 1.0 +end +function op!(Op::ITensor, on::OpName"c†↓", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Aup", ::SiteType"tJ", s::Index) + return Op[s' => 1, s => 2] = 1.0 +end +function op!(Op::ITensor, on::OpName"a↑", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Adagup", ::SiteType"tJ", s::Index) + return Op[s' => 2, s => 1] = 1.0 +end +function op!(Op::ITensor, on::OpName"a†↑", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Adn", ::SiteType"tJ", s::Index) + return Op[s' => 1, s => 3] = 1.0 +end +function op!(Op::ITensor, on::OpName"a↓", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Adagdn", ::SiteType"tJ", s::Index) + return Op[s' => 3, s => 1] = 1.0 +end +function op!(Op::ITensor, on::OpName"a†↓", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"F", ::SiteType"tJ", s::Index) + Op[s' => 1, s => 1] = +1.0 + Op[s' => 2, s => 2] = -1.0 + return Op[s' => 3, s => 3] = -1.0 +end + +function op!(Op::ITensor, ::OpName"Fup", ::SiteType"tJ", s::Index) + Op[s' => 1, s => 1] = +1.0 + Op[s' => 2, s => 2] = -1.0 + return Op[s' => 3, s => 3] = +1.0 +end +function op!(Op::ITensor, on::OpName"F↑", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Fdn", ::SiteType"tJ", s::Index) + Op[s' => 1, s => 1] = +1.0 + Op[s' => 2, s => 2] = +1.0 + return Op[s' => 3, s => 3] = -1.0 +end +function op!(Op::ITensor, on::OpName"F↓", st::SiteType"tJ", s::Index) + return op!(Op, alias(on), st, s) +end + +function op!(Op::ITensor, ::OpName"Sz", ::SiteType"tJ", s::Index) + Op[s' => 2, s => 2] = +0.5 + return Op[s' => 3, s => 3] = -0.5 +end + +function op!(Op::ITensor, ::OpName"Sᶻ", st::SiteType"tJ", s::Index) + return op!(Op, OpName("Sz"), st, s) +end + +function op!(Op::ITensor, ::OpName"Sx", ::SiteType"tJ", s::Index) + Op[s' => 2, s => 3] = 0.5 + return Op[s' => 3, s => 2] = 0.5 +end + +function op!(Op::ITensor, ::OpName"Sˣ", st::SiteType"tJ", s::Index) + return op!(Op, OpName("Sx"), st, s) +end + +function op!(Op::ITensor, ::OpName"S+", ::SiteType"tJ", s::Index) + return Op[s' => 2, s => 3] = 1.0 +end + +function op!(Op::ITensor, ::OpName"S⁺", st::SiteType"tJ", s::Index) + return op!(Op, OpName("S+"), st, s) +end +function op!(Op::ITensor, ::OpName"Sp", st::SiteType"tJ", s::Index) + return op!(Op, OpName("S+"), st, s) +end +function op!(Op::ITensor, ::OpName"Splus", st::SiteType"tJ", s::Index) + return op!(Op, OpName("S+"), st, s) +end + +function op!(Op::ITensor, ::OpName"S-", ::SiteType"tJ", s::Index) + return Op[s' => 3, s => 2] = 1.0 +end + +function op!(Op::ITensor, ::OpName"S⁻", st::SiteType"tJ", s::Index) + return op!(Op, OpName("S-"), st, s) +end +function op!(Op::ITensor, ::OpName"Sm", st::SiteType"tJ", s::Index) + return op!(Op, OpName("S-"), st, s) +end +function op!(Op::ITensor, ::OpName"Sminus", st::SiteType"tJ", s::Index) + return op!(Op, OpName("S-"), st, s) +end + +has_fermion_string(::OpName"Cup", ::SiteType"tJ") = true +function has_fermion_string(on::OpName"c↑", st::SiteType"tJ") + return has_fermion_string(alias(on), st) +end +has_fermion_string(::OpName"Cdagup", ::SiteType"tJ") = true +function has_fermion_string(on::OpName"c†↑", st::SiteType"tJ") + return has_fermion_string(alias(on), st) +end +has_fermion_string(::OpName"Cdn", ::SiteType"tJ") = true +function has_fermion_string(on::OpName"c↓", st::SiteType"tJ") + return has_fermion_string(alias(on), st) +end +has_fermion_string(::OpName"Cdagdn", ::SiteType"tJ") = true +function has_fermion_string(on::OpName"c†↓", st::SiteType"tJ") + return has_fermion_string(alias(on), st) +end diff --git a/src/lib/SmallStrings/src/SmallStrings.jl b/src/lib/SmallStrings/src/SmallStrings.jl new file mode 100644 index 0000000000..02733b7834 --- /dev/null +++ b/src/lib/SmallStrings/src/SmallStrings.jl @@ -0,0 +1,4 @@ +module SmallStrings +include("smallstring.jl") +include("SmallStringsChainRulesCoreExt.jl") +end diff --git a/src/lib/SmallStrings/src/SmallStringsChainRulesCoreExt.jl b/src/lib/SmallStrings/src/SmallStringsChainRulesCoreExt.jl new file mode 100644 index 0000000000..c7a70784f7 --- /dev/null +++ b/src/lib/SmallStrings/src/SmallStringsChainRulesCoreExt.jl @@ -0,0 +1,5 @@ +module SmallStringsChainRulesCoreExt +using ChainRulesCore: @non_differentiable +using ..SmallStrings: SmallString +@non_differentiable SmallString(::Any) +end diff --git a/src/smallstring.jl b/src/lib/SmallStrings/src/smallstring.jl similarity index 97% rename from src/smallstring.jl rename to src/lib/SmallStrings/src/smallstring.jl index 696c31e3d6..ac660c5d72 100644 --- a/src/smallstring.jl +++ b/src/lib/SmallStrings/src/smallstring.jl @@ -1,3 +1,6 @@ +using BitIntegers: UInt256 +using StaticArrays: MVector, SVector + const IntChar = UInt16 const IntSmallString = UInt256 @@ -46,7 +49,7 @@ SmallString(s::SmallString) = SmallString(data(s)) Base.getindex(s::SmallString, n::Int) = getindex(s.data, n) function Base.setindex(s::SmallString, val, n::Int) - return SmallString(StaticArrays.setindex(s.data, val, n)) + return SmallString(Base.setindex(s.data, val, n)) end # TODO: rename to `isempty` diff --git a/src/tagset.jl b/src/lib/TagSets/src/TagSets.jl similarity index 85% rename from src/tagset.jl rename to src/lib/TagSets/src/TagSets.jl index b506f4dcfd..45d3152bf7 100644 --- a/src/tagset.jl +++ b/src/lib/TagSets/src/TagSets.jl @@ -1,10 +1,51 @@ -using BitIntegers +module TagSets +using BitIntegers: UInt256 +using DocStringExtensions: TYPEDSIGNATURES +# TODO: Move to `Nots` lib. +using ..ITensors: ITensors, Not, not +using ..SmallStrings: SmallString, cast_to_uint, isnull +using StaticArrays: MVector, SVector const IntTag = UInt256 # An integer that can be cast to a Tag const MTagStorage = MVector{16,IntTag} # A mutable tag storage, holding 16 characters const TagSetStorage{T,N} = SVector{N,T} const MTagSetStorage{T,N} = MVector{N,T} # A mutable tag storage +# +# Turn the strict tags checking on and off +# + +const _using_strict_tags = Ref(false) + +""" +$(TYPEDSIGNATURES) +See if checking for overflow of the number of tags of a TagSet +or the number of characters of a tag is enabled or disabled. + +See also [`ITensors.set_strict_tags!`](@ref). +""" +function using_strict_tags() + return _using_strict_tags[] +end + +""" +$(TYPEDSIGNATURES) +Enable or disable checking for overflow of the number of tags of a TagSet +or the number of characters of a tag. If enabled (set to `true`), an error +will be thrown if overflow occurs, otherwise the overflow will be ignored +and the extra tags or tag characters will be dropped. This could cause +unexpected bugs if tags are being used to distinguish Index objects that +have the same ids and prime levels, but that is generally discouraged and +should only be used if you know what you are doing. + +See also [`ITensors.using_strict_tags`](@ref). +""" +function set_strict_tags!(enable::Bool) + previous = using_strict_tags() + _using_strict_tags[] = enable + return previous +end + emptytag(::Type{IntTag}) = IntTag(0) function empty_storage(::Type{TagSetStorage{T,N}}) where {T,N} return TagSetStorage(ntuple(_ -> emptytag(T), Val(N))) @@ -55,7 +96,7 @@ function _addtag_ordered!(ts::MTagSetStorage, ntags::Int, tag::IntTag) end function _addtag!(ts::MTagSetStorage, ntags::Int, tag::IntTag) - t = Tag(tag) + t = SmallString(tag) # TODO: change to isempty, remove isnull if !isnull(t) ntags = _addtag_ordered!(ts, ntags, tag) @@ -154,13 +195,13 @@ Base.convert(::Type{TagSet}, str::String) = TagSet(str) Create a wrapper around a TagSet representing the set of indices that do not contain that TagSet. """ -not(ts::TagSet) = Not(ts) +ITensors.not(ts::TagSet) = Not(ts) Base.:!(ts::TagSet) = Not(ts) -not(ts::AbstractString) = Not(ts) +ITensors.not(ts::AbstractString) = Not(ts) """ -ITensors.data(T::TagSet) +data(T::TagSet) Get the raw storage of the TagSet. @@ -172,7 +213,7 @@ available. data(T::TagSet) = T.data Base.length(T::TagSet) = T.length -@propagate_inbounds getindex(T::TagSet, n::Integer) = Tag(data(T)[n]) +Base.@propagate_inbounds Base.getindex(T::TagSet, n::Integer) = SmallString(data(T)[n]) Base.copy(ts::TagSet) = TagSet(data(ts), length(ts)) function Base.:(==)(ts1::TagSet, ts2::TagSet) @@ -278,9 +319,9 @@ function tagstring(T::TagSet) N = length(T) N == 0 && return res for n in 1:(N - 1) - res *= "$(Tag(data(T)[n]))," + res *= "$(SmallString(data(T)[n]))," end - res *= "$(Tag(data(T)[N]))" + res *= "$(SmallString(data(T)[N]))" return res end @@ -338,7 +379,7 @@ function readcpp(io::IO, ::Type{TagSet}; format="v3") ntags = 0 for n in 1:4 t = readcpp(io, Tag; kwargs...) - if t != Tag() + if t != SmallString() ntags = _addtag_ordered!(mstore, ntags, IntTag(t)) end end @@ -348,3 +389,4 @@ function readcpp(io::IO, ::Type{TagSet}; format="v3") end return ts end +end diff --git a/src/name.jl b/src/name.jl new file mode 100644 index 0000000000..bacc04d3e7 --- /dev/null +++ b/src/name.jl @@ -0,0 +1 @@ +function name end diff --git a/src/nullspace.jl b/src/nullspace.jl index 96848eb06b..50d4206ad5 100644 --- a/src/nullspace.jl +++ b/src/nullspace.jl @@ -1,3 +1,5 @@ +using .QuantumNumbers: Arrow + # # NDTensors functionality # @@ -112,7 +114,7 @@ end # Make `Pair{QN,Int}` act like a regular `dim` NDTensors.dim(qnv::Pair{QN,Int}) = last(qnv) -Base.:*(qnv::Pair{QN,Int}, d::ITensors.Arrow) = qn(qnv) * d => dim(qnv) +Base.:*(qnv::Pair{QN,Int}, d::Arrow) = qn(qnv) * d => dim(qnv) # # ITensors functionality diff --git a/src/physics/site_types/qubit.jl b/src/physics/site_types/qubit.jl deleted file mode 100644 index 568649c521..0000000000 --- a/src/physics/site_types/qubit.jl +++ /dev/null @@ -1,500 +0,0 @@ -# -# Qubit site type -# - -# Define Qubit space in terms of -# Qubit/2 space, but use different -# defaults for QN names - -""" - space(::SiteType"Qubit"; - conserve_qns = false, - conserve_parity = conserve_qns, - conserve_number = false, - qnname_parity = "Parity", - qnname_number = "Number") - -Create the Hilbert space for a site of type "Qubit". - -Optionally specify the conserved symmetries and their quantum number labels. -""" -function ITensors.space( - ::SiteType"Qubit"; - conserve_qns=false, - conserve_parity=conserve_qns, - conserve_number=false, - qnname_parity="Parity", - qnname_number="Number", -) - if conserve_number && conserve_parity - return [ - QN((qnname_number, 0), (qnname_parity, 0, 2)) => 1, - QN((qnname_number, 1), (qnname_parity, 1, 2)) => 1, - ] - elseif conserve_number - return [QN(qnname_number, 0) => 1, QN(qnname_number, 1) => 1] - elseif conserve_parity - return [QN(qnname_parity, 0, 2) => 1, QN(qnname_parity, 1, 2) => 1] - end - return 2 -end - -ITensors.val(::ValName"0", ::SiteType"Qubit") = 1 -ITensors.val(::ValName"1", ::SiteType"Qubit") = 2 -ITensors.val(::ValName"Up", ::SiteType"Qubit") = 1 -ITensors.val(::ValName"Dn", ::SiteType"Qubit") = 2 -ITensors.val(::ValName"↑", ::SiteType"Qubit") = 1 -ITensors.val(::ValName"↓", ::SiteType"Qubit") = 2 - -ITensors.state(::StateName"0", ::SiteType"Qubit") = [1.0, 0.0] -ITensors.state(::StateName"1", ::SiteType"Qubit") = [0.0, 1.0] -ITensors.state(::StateName"+", ::SiteType"Qubit") = [1.0, 1.0] / √2 -ITensors.state(::StateName"-", ::SiteType"Qubit") = [1.0, -1.0] / √2 -ITensors.state(::StateName"i", ::SiteType"Qubit") = [1.0, im] / √2 -ITensors.state(::StateName"-i", ::SiteType"Qubit") = [1.0, -im] / √2 -ITensors.state(::StateName"Up", t::SiteType"Qubit") = state(StateName("0"), t) -ITensors.state(::StateName"Dn", t::SiteType"Qubit") = state(StateName("1"), t) -ITensors.state(::StateName"↑", t::SiteType"Qubit") = state(StateName("0"), t) -ITensors.state(::StateName"↓", t::SiteType"Qubit") = state(StateName("1"), t) - -# Pauli eingenstates -ITensors.state(::StateName"X+", t::SiteType"Qubit") = state(StateName("+"), t) -ITensors.state(::StateName"Xp", t::SiteType"Qubit") = state(StateName("+"), t) -ITensors.state(::StateName"X-", t::SiteType"Qubit") = state(StateName("-"), t) -ITensors.state(::StateName"Xm", t::SiteType"Qubit") = state(StateName("-"), t) - -ITensors.state(::StateName"Y+", t::SiteType"Qubit") = state(StateName("i"), t) -ITensors.state(::StateName"Yp", t::SiteType"Qubit") = state(StateName("i"), t) -ITensors.state(::StateName"Y-", t::SiteType"Qubit") = state(StateName("-i"), t) -ITensors.state(::StateName"Ym", t::SiteType"Qubit") = state(StateName("-i"), t) - -ITensors.state(::StateName"Z+", t::SiteType"Qubit") = state(StateName("0"), t) -ITensors.state(::StateName"Zp", t::SiteType"Qubit") = state(StateName("0"), t) -ITensors.state(::StateName"Z-", t::SiteType"Qubit") = state(StateName("1"), t) -ITensors.state(::StateName"Zm", t::SiteType"Qubit") = state(StateName("1"), t) - -# SIC-POVMs -state(::StateName"Tetra1", t::SiteType"Qubit") = state(StateName("Z+"), t) -state(::StateName"Tetra2", t::SiteType"Qubit") = [ - 1 / √3 - √2 / √3 -] -state(::StateName"Tetra3", t::SiteType"Qubit") = [ - 1 / √3 - √2 / √3 * exp(im * 2π / 3) -] -state(::StateName"Tetra4", t::SiteType"Qubit") = [ - 1 / √3 - √2 / √3 * exp(im * 4π / 3) -] - -# -# 1-Qubit gates -# -ITensors.op(::OpName"X", ::SiteType"Qubit") = [ - 0 1 - 1 0 -] - -ITensors.op(::OpName"σx", t::SiteType"Qubit") = op("X", t) - -ITensors.op(::OpName"σ1", t::SiteType"Qubit") = op("X", t) - -ITensors.op(::OpName"Y", ::SiteType"Qubit") = [ - 0.0 -1.0im - 1.0im 0.0 -] - -ITensors.op(::OpName"σy", t::SiteType"Qubit") = op("Y", t) - -ITensors.op(::OpName"σ2", t::SiteType"Qubit") = op("Y", t) - -ITensors.op(::OpName"iY", ::SiteType"Qubit") = [ - 0 1 - -1 0 -] -ITensors.op(::OpName"iσy", t::SiteType"Qubit") = op("iY", t) - -ITensors.op(::OpName"iσ2", t::SiteType"Qubit") = op("iY", t) - -ITensors.op(::OpName"Z", ::SiteType"Qubit") = [ - 1 0 - 0 -1 -] - -ITensors.op(::OpName"σz", t::SiteType"Qubit") = op("Z", t) - -ITensors.op(::OpName"σ3", t::SiteType"Qubit") = op("Z", t) - -function ITensors.op(::OpName"√NOT", ::SiteType"Qubit") - return [ - (1 + im)/2 (1 - im)/2 - (1 - im)/2 (1 + im)/2 - ] -end - -ITensors.op(::OpName"√X", t::SiteType"Qubit") = op("√NOT", t) - -ITensors.op(::OpName"H", ::SiteType"Qubit") = [ - 1/sqrt(2) 1/sqrt(2) - 1/sqrt(2) -1/sqrt(2) -] - -# Rϕ with ϕ = π/2 -ITensors.op(::OpName"Phase", ::SiteType"Qubit"; ϕ::Number=π / 2) = [ - 1 0 - 0 exp(im * ϕ) -] - -ITensors.op(::OpName"P", t::SiteType"Qubit"; kwargs...) = op("Phase", t; kwargs...) - -ITensors.op(::OpName"S", t::SiteType"Qubit") = op("Phase", t; ϕ=π / 2) - -## Rϕ with ϕ = π/4 -ITensors.op(::OpName"π/8", ::SiteType"Qubit") = [ - 1 0 - 0 1 / sqrt(2)+im / sqrt(2) -] - -ITensors.op(::OpName"T", t::SiteType"Qubit") = op("π/8", t) - -# Rotation around X-axis -function ITensors.op(::OpName"Rx", ::SiteType"Qubit"; θ::Number) - return [ - cos(θ / 2) -im*sin(θ / 2) - -im*sin(θ / 2) cos(θ / 2) - ] -end - -# Rotation around Y-axis -function ITensors.op(::OpName"Ry", ::SiteType"Qubit"; θ::Number) - return [ - cos(θ / 2) -sin(θ / 2) - sin(θ / 2) cos(θ / 2) - ] -end - -# Rotation around Z-axis -function ITensors.op(::OpName"Rz", ::SiteType"Qubit"; θ=nothing, ϕ=nothing) - isone(count(isnothing, (θ, ϕ))) || error( - "Must specify the keyword argument `θ` (or the deprecated `ϕ`) when creating an Rz gate, but not both.", - ) - isnothing(θ) && (θ = ϕ) - return [ - exp(-im * θ / 2) 0 - 0 exp(im * θ / 2) - ] -end - -# Rotation around generic axis n̂ -function ITensors.op(::OpName"Rn", ::SiteType"Qubit"; θ::Real, ϕ::Real, λ::Real) - return [ - cos(θ / 2) -exp(im * λ)*sin(θ / 2) - exp(im * ϕ)*sin(θ / 2) exp(im * (ϕ + λ))*cos(θ / 2) - ] -end - -function ITensors.op(::OpName"Rn̂", t::SiteType"Qubit"; kwargs...) - return ITensors.op(OpName("Rn"), t; kwargs...) -end - -# -# 2-Qubit gates -# - -ITensors.op(::OpName"CNOT", ::SiteType"Qubit") = [ - 1 0 0 0 - 0 1 0 0 - 0 0 0 1 - 0 0 1 0 -] - -ITensors.op(::OpName"CX", t::SiteType"Qubit") = op("CNOT", t) - -ITensors.op(::OpName"CY", ::SiteType"Qubit") = [ - 1 0 0 0 - 0 1 0 0 - 0 0 0 -im - 0 0 im 0 -] - -ITensors.op(::OpName"CZ", ::SiteType"Qubit") = [ - 1 0 0 0 - 0 1 0 0 - 0 0 1 0 - 0 0 0 -1 -] - -function ITensors.op(::OpName"CPHASE", ::SiteType"Qubit"; ϕ::Number) - return [ - 1 0 0 0 - 0 1 0 0 - 0 0 1 0 - 0 0 0 exp(im * ϕ) - ] -end -ITensors.op(::OpName"Cphase", t::SiteType"Qubit"; kwargs...) = op("CPHASE", t; kwargs...) - -function ITensors.op(::OpName"CRx", ::SiteType"Qubit"; θ::Number) - return [ - 1 0 0 0 - 0 1 0 0 - 0 0 cos(θ / 2) -im*sin(θ / 2) - 0 0 -im*sin(θ / 2) cos(θ / 2) - ] -end -ITensors.op(::OpName"CRX", t::SiteType"Qubit"; kwargs...) = ITensors.op("CRx", t; kwargs...) - -function ITensors.op(::OpName"CRy", ::SiteType"Qubit"; θ::Number) - return [ - 1 0 0 0 - 0 1 0 0 - 0 0 cos(θ / 2) -sin(θ / 2) - 0 0 sin(θ / 2) cos(θ / 2) - ] -end -ITensors.op(::OpName"CRY", t::SiteType"Qubit"; kwargs...) = ITensors.op("CRy", t; kwargs...) - -function ITensors.op(::OpName"CRz", ::SiteType"Qubit"; ϕ=nothing, θ=nothing) - isone(count(isnothing, (θ, ϕ))) || error( - "Must specify the keyword argument `θ` (or the deprecated `ϕ`) when creating a CRz gate, but not both.", - ) - isnothing(θ) && (θ = ϕ) - return [ - 1 0 0 0 - 0 1 0 0 - 0 0 exp(-im * θ / 2) 0 - 0 0 0 exp(im * θ / 2) - ] -end -ITensors.op(::OpName"CRZ", t::SiteType"Qubit"; kwargs...) = ITensors.op("CRz", t; kwargs...) - -function ITensors.op(::OpName"CRn", ::SiteType"Qubit"; θ::Number, ϕ::Number, λ::Number) - return [ - 1 0 0 0 - 0 1 0 0 - 0 0 cos(θ / 2) -exp(im * λ)*sin(θ / 2) - 0 0 exp(im * ϕ)*sin(θ / 2) exp(im * (ϕ + λ))*cos(θ / 2) - ] -end -function ITensors.op(::OpName"CRn̂", t::SiteType"Qubit"; kwargs...) - return ITensors.op("CRn", t; kwargs...) -end - -ITensors.op(::OpName"SWAP", ::SiteType"Qubit") = [ - 1 0 0 0 - 0 0 1 0 - 0 1 0 0 - 0 0 0 1 -] -ITensors.op(::OpName"Swap", t::SiteType"Qubit") = op("SWAP", t) - -function ITensors.op(::OpName"√SWAP", ::SiteType"Qubit") - return [ - 1 0 0 0 - 0 (1 + im)/2 (1 - im)/2 0 - 0 (1 - im)/2 (1 + im)/2 0 - 0 0 0 1 - ] -end -ITensors.op(::OpName"√Swap", t::SiteType"Qubit") = op("√SWAP", t) - -ITensors.op(::OpName"iSWAP", t::SiteType"Qubit") = [ - 1 0 0 0 - 0 0 im 0 - 0 im 0 0 - 0 0 0 1 -] -ITensors.op(::OpName"iSwap", t::SiteType"Qubit") = op("iSWAP", t) - -function ITensors.op(::OpName"√iSWAP", t::SiteType"Qubit") - return [ - 1 0 0 0 - 0 1/√2 im/√2 0 - 0 im/√2 1/√2 0 - 0 0 0 1 - ] -end -ITensors.op(::OpName"√iSwap", t::SiteType"Qubit") = op("√iSWAP", t) - -# Ising (XX) coupling gate -function ITensors.op(::OpName"Rxx", t::SiteType"Qubit"; ϕ::Number) - return [ - cos(ϕ) 0 0 -im*sin(ϕ) - 0 cos(ϕ) -im*sin(ϕ) 0 - 0 -im*sin(ϕ) cos(ϕ) 0 - -im*sin(ϕ) 0 0 cos(ϕ) - ] -end -ITensors.op(::OpName"RXX", t::SiteType"Qubit"; kwargs...) = op("Rxx", t; kwargs...) - -# Ising (YY) coupling gate -function ITensors.op(::OpName"Ryy", ::SiteType"Qubit"; ϕ::Number) - return [ - cos(ϕ) 0 0 im*sin(ϕ) - 0 cos(ϕ) -im*sin(ϕ) 0 - 0 -im*sin(ϕ) cos(ϕ) 0 - im*sin(ϕ) 0 0 cos(ϕ) - ] -end -ITensors.op(::OpName"RYY", t::SiteType"Qubit"; kwargs...) = op("Ryy", t; kwargs...) - -# Ising (XY) coupling gate -function ITensors.op(::OpName"Rxy", t::SiteType"Qubit"; ϕ::Number) - return [ - 1 0 0 0 - 0 cos(ϕ) -im*sin(ϕ) 0 - 0 -im*sin(ϕ) cos(ϕ) 0 - 0 0 0 1 - ] -end -ITensors.op(::OpName"RXY", t::SiteType"Qubit"; kwargs...) = op("Rxy", t; kwargs...) - -# Ising (ZZ) coupling gate -function ITensors.op(::OpName"Rzz", ::SiteType"Qubit"; ϕ::Number) - return [ - exp(-im * ϕ) 0 0 0 - 0 exp(im * ϕ) 0 0 - 0 0 exp(im * ϕ) 0 - 0 0 0 exp(-im * ϕ) - ] -end -ITensors.op(::OpName"RZZ", t::SiteType"Qubit"; kwargs...) = op("Rzz", t; kwargs...) - -# -# 3-Qubit gates -# - -function ITensors.op(::OpName"Toffoli", ::SiteType"Qubit") - return [ - 1 0 0 0 0 0 0 0 - 0 1 0 0 0 0 0 0 - 0 0 1 0 0 0 0 0 - 0 0 0 1 0 0 0 0 - 0 0 0 0 1 0 0 0 - 0 0 0 0 0 1 0 0 - 0 0 0 0 0 0 0 1 - 0 0 0 0 0 0 1 0 - ] -end - -ITensors.op(::OpName"CCNOT", t::SiteType"Qubit") = op("Toffoli", t) - -ITensors.op(::OpName"CCX", t::SiteType"Qubit") = op("Toffoli", t) - -ITensors.op(::OpName"TOFF", t::SiteType"Qubit") = op("Toffoli", t) - -function ITensors.op(::OpName"Fredkin", ::SiteType"Qubit") - return [ - 1 0 0 0 0 0 0 0 - 0 1 0 0 0 0 0 0 - 0 0 1 0 0 0 0 0 - 0 0 0 1 0 0 0 0 - 0 0 0 0 1 0 0 0 - 0 0 0 0 0 0 1 0 - 0 0 0 0 0 1 0 0 - 0 0 0 0 0 0 0 1 - ] -end - -ITensors.op(::OpName"CSWAP", t::SiteType"Qubit") = op("Fredkin", t) -ITensors.op(::OpName"CSwap", t::SiteType"Qubit") = op("Fredkin", t) - -ITensors.op(::OpName"CS", t::SiteType"Qubit") = op("Fredkin", t) - -# -# 4-Qubit gates -# - -function ITensors.op(::OpName"CCCNOT", ::SiteType"Qubit") - return [ - 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 - 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 - 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 - 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 - 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 - 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 - 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 - 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 - 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 - ] -end - -# spin-full operators -ITensors.op(::OpName"Sz", ::SiteType"Qubit") = [ - 0.5 0.0 - 0.0 -0.5 -] - -ITensors.op(::OpName"Sᶻ", t::SiteType"Qubit") = op(OpName("Sz"), t) - -ITensors.op(::OpName"S+", ::SiteType"Qubit") = [ - 0 1 - 0 0 -] - -ITensors.op(::OpName"S⁺", t::SiteType"Qubit") = op(OpName("S+"), t) - -ITensors.op(::OpName"Splus", t::SiteType"Qubit") = op(OpName("S+"), t) - -ITensors.op(::OpName"S-", ::SiteType"Qubit") = [ - 0 0 - 1 0 -] - -ITensors.op(::OpName"S⁻", t::SiteType"Qubit") = op(OpName("S-"), t) - -ITensors.op(::OpName"Sminus", t::SiteType"Qubit") = op(OpName("S-"), t) - -ITensors.op(::OpName"Sx", ::SiteType"Qubit") = [ - 0.0 0.5 - 0.5 0.0 -] - -ITensors.op(::OpName"Sˣ", t::SiteType"Qubit") = op(OpName("Sx"), t) - -ITensors.op(::OpName"iSy", ::SiteType"Qubit") = [ - 0.0 0.5 - -0.5 0.0 -] - -ITensors.op(::OpName"iSʸ", t::SiteType"Qubit") = op(OpName("iSy"), t) - -ITensors.op(::OpName"Sy", ::SiteType"Qubit") = [ - 0.0 -0.5im - 0.5im 0.0 -] - -ITensors.op(::OpName"Sʸ", t::SiteType"Qubit") = op(OpName("Sy"), t) - -ITensors.op(::OpName"S2", ::SiteType"Qubit") = [ - 0.75 0.0 - 0.0 0.75 -] - -ITensors.op(::OpName"S²", t::SiteType"Qubit") = op(OpName("S2"), t) - -ITensors.op(::OpName"ProjUp", ::SiteType"Qubit") = [ - 1 0 - 0 0 -] - -ITensors.op(::OpName"projUp", t::SiteType"Qubit") = op(OpName("ProjUp"), t) - -ITensors.op(::OpName"Proj0", t::SiteType"Qubit") = op(OpName("ProjUp"), t) - -ITensors.op(::OpName"ProjDn", ::SiteType"Qubit") = [ - 0 0 - 0 1 -] - -ITensors.op(::OpName"projDn", t::SiteType"Qubit") = op(OpName("ProjDn"), t) - -ITensors.op(::OpName"Proj1", t::SiteType"Qubit") = op(OpName("ProjDn"), t) diff --git a/src/physics/site_types/spinone.jl b/src/physics/site_types/spinone.jl deleted file mode 100644 index 959e9799fd..0000000000 --- a/src/physics/site_types/spinone.jl +++ /dev/null @@ -1,158 +0,0 @@ - -""" - space(::SiteType"S=1"; - conserve_qns = false, - conserve_sz = conserve_qns, - qnname_sz = "Sz") - -Create the Hilbert space for a site of type "S=1". - -Optionally specify the conserved symmetries and their quantum number labels. -""" -function ITensors.space( - ::SiteType"S=1"; conserve_qns=false, conserve_sz=conserve_qns, qnname_sz="Sz" -) - if conserve_sz - return [QN(qnname_sz, +2) => 1, QN(qnname_sz, 0) => 1, QN(qnname_sz, -2) => 1] - end - return 3 -end - -ITensors.val(::ValName"Up", ::SiteType"S=1") = 1 -ITensors.val(::ValName"Z0", ::SiteType"S=1") = 2 -ITensors.val(::ValName"Dn", ::SiteType"S=1") = 3 - -ITensors.val(::ValName"↑", st::SiteType"S=1") = 1 -ITensors.val(::ValName"0", st::SiteType"S=1") = 2 -ITensors.val(::ValName"↓", st::SiteType"S=1") = 3 - -ITensors.val(::ValName"Z+", ::SiteType"S=1") = 1 -# -- Z0 is already defined above -- -ITensors.val(::ValName"Z-", ::SiteType"S=1") = 3 - -ITensors.state(::StateName"Up", ::SiteType"S=1") = [1.0, 0.0, 0.0] -ITensors.state(::StateName"Z0", ::SiteType"S=1") = [0.0, 1.0, 0.0] -ITensors.state(::StateName"Dn", ::SiteType"S=1") = [0.0, 0.0, 1.0] - -ITensors.state(::StateName"↑", st::SiteType"S=1") = [1.0, 0.0, 0.0] -ITensors.state(::StateName"0", st::SiteType"S=1") = [0.0, 1.0, 0.0] -ITensors.state(::StateName"↓", st::SiteType"S=1") = [0.0, 0.0, 1.0] - -ITensors.state(::StateName"Z+", st::SiteType"S=1") = [1.0, 0.0, 0.0] -# -- Z0 is already defined above -- -ITensors.state(::StateName"Z-", st::SiteType"S=1") = [0.0, 0.0, 1.0] - -ITensors.state(::StateName"X+", ::SiteType"S=1") = [1 / 2, 1 / sqrt(2), 1 / 2] -ITensors.state(::StateName"X0", ::SiteType"S=1") = [-1 / sqrt(2), 0, 1 / sqrt(2)] -ITensors.state(::StateName"X-", ::SiteType"S=1") = [1 / 2, -1 / sqrt(2), 1 / 2] - -ITensors.state(::StateName"Y+", ::SiteType"S=1") = [-1 / 2, -im / sqrt(2), 1 / 2] -ITensors.state(::StateName"Y0", ::SiteType"S=1") = [1 / sqrt(2), 0, 1 / sqrt(2)] -ITensors.state(::StateName"Y-", ::SiteType"S=1") = [-1 / 2, im / sqrt(2), 1 / 2] - -function ITensors.op!(Op::ITensor, ::OpName"Sz", ::SiteType"S=1", s::Index) - Op[s' => 1, s => 1] = +1.0 - return Op[s' => 3, s => 3] = -1.0 -end - -function ITensors.op!(Op::ITensor, ::OpName"Sᶻ", t::SiteType"S=1", s::Index) - return op!(Op, OpName("Sz"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"S+", ::SiteType"S=1", s::Index) - Op[s' => 2, s => 3] = sqrt(2) - return Op[s' => 1, s => 2] = sqrt(2) -end - -function ITensors.op!(Op::ITensor, ::OpName"S⁺", t::SiteType"S=1", s::Index) - return op!(Op, OpName("S+"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Splus", t::SiteType"S=1", s::Index) - return op!(Op, OpName("S+"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sp", t::SiteType"S=1", s::Index) - return op!(Op, OpName("S+"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"S-", ::SiteType"S=1", s::Index) - Op[s' => 3, s => 2] = sqrt(2) - return Op[s' => 2, s => 1] = sqrt(2) -end - -function ITensors.op!(Op::ITensor, ::OpName"S⁻", t::SiteType"S=1", s::Index) - return op!(Op, OpName("S-"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sminus", t::SiteType"S=1", s::Index) - return op!(Op, OpName("S-"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sm", t::SiteType"S=1", s::Index) - return op!(Op, OpName("S-"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sx", ::SiteType"S=1", s::Index) - Op[s' => 2, s => 1] = 1 / sqrt(2) - Op[s' => 1, s => 2] = 1 / sqrt(2) - Op[s' => 3, s => 2] = 1 / sqrt(2) - return Op[s' => 2, s => 3] = 1 / sqrt(2) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sˣ", t::SiteType"S=1", s::Index) - return op!(Op, OpName("Sx"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"iSy", ::SiteType"S=1", s::Index) - Op[s' => 2, s => 1] = -1 / sqrt(2) - Op[s' => 1, s => 2] = +1 / sqrt(2) - Op[s' => 3, s => 2] = -1 / sqrt(2) - return Op[s' => 2, s => 3] = +1 / sqrt(2) -end - -function ITensors.op!(Op::ITensor, ::OpName"iSʸ", t::SiteType"S=1", s::Index) - return op!(Op, OpName("iSy"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sy", ::SiteType"S=1", s::Index) - complex!(Op) - Op[s' => 2, s => 1] = +1im / sqrt(2) - Op[s' => 1, s => 2] = -1im / sqrt(2) - Op[s' => 3, s => 2] = +1im / sqrt(2) - return Op[s' => 2, s => 3] = -1im / sqrt(2) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sʸ", t::SiteType"S=1", s::Index) - return op!(Op, OpName("Sy"), t, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sz2", ::SiteType"S=1", s::Index) - Op[s' => 1, s => 1] = +1.0 - return Op[s' => 3, s => 3] = +1.0 -end - -function ITensors.op!(Op::ITensor, ::OpName"Sx2", ::SiteType"S=1", s::Index) - Op[s' => 1, s => 1] = 0.5 - Op[s' => 3, s => 1] = 0.5 - Op[s' => 2, s => 2] = 1.0 - Op[s' => 1, s => 3] = 0.5 - return Op[s' => 3, s => 3] = 0.5 -end - -function ITensors.op!(Op::ITensor, ::OpName"Sy2", ::SiteType"S=1", s::Index) - Op[s' => 1, s => 1] = +0.5 - Op[s' => 3, s => 1] = -0.5 - Op[s' => 2, s => 2] = +1.0 - Op[s' => 1, s => 3] = -0.5 - return Op[s' => 3, s => 3] = +0.5 -end - -ITensors.space(::SiteType"SpinOne"; kwargs...) = space(SiteType("S=1"); kwargs...) - -ITensors.state(name::StateName, ::SiteType"SpinOne") = state(name, SiteType("S=1")) -ITensors.val(name::ValName, ::SiteType"SpinOne") = val(name, SiteType("S=1")) - -function ITensors.op!(Op::ITensor, o::OpName, ::SiteType"SpinOne", s::Index) - return op!(Op, o, SiteType("S=1"), s) -end diff --git a/src/physics/site_types/tj.jl b/src/physics/site_types/tj.jl deleted file mode 100644 index cbe8c164cd..0000000000 --- a/src/physics/site_types/tj.jl +++ /dev/null @@ -1,234 +0,0 @@ - -""" - space(::SiteType"tJ"; - conserve_qns = false, - conserve_sz = conserve_qns, - conserve_nf = conserve_qns, - conserve_nfparity = conserve_qns, - qnname_sz = "Sz", - qnname_nf = "Nf", - qnname_nfparity = "NfParity") - -Create the Hilbert space for a site of type "tJ". - -Optionally specify the conserved symmetries and their quantum number labels. -""" -function ITensors.space( - ::SiteType"tJ"; - conserve_qns=false, - conserve_sz=conserve_qns, - conserve_nf=conserve_qns, - conserve_nfparity=conserve_qns, - qnname_sz="Sz", - qnname_nf="Nf", - qnname_nfparity="NfParity", - # Deprecated - conserve_parity=nothing, -) - if !isnothing(conserve_parity) - conserve_nfparity = conserve_parity - end - if conserve_sz && conserve_nf - return [ - QN((qnname_nf, 0, -1), (qnname_sz, 0)) => 1 - QN((qnname_nf, 1, -1), (qnname_sz, +1)) => 1 - QN((qnname_nf, 1, -1), (qnname_sz, -1)) => 1 - ] - elseif conserve_nf - return [ - QN(qnname_nf, 0, -1) => 1 - QN(qnname_nf, 1, -1) => 2 - ] - elseif conserve_sz - return [ - QN((qnname_sz, 0), (qnname_nfparity, 0, -2)) => 1 - QN((qnname_sz, +1), (qnname_nfparity, 1, -2)) => 1 - QN((qnname_sz, -1), (qnname_nfparity, 1, -2)) => 1 - ] - elseif conserve_nfparity - return [ - QN(qnname_nfparity, 0, -2) => 1 - QN(qnname_nfparity, 1, -2) => 2 - ] - end - return 3 -end - -ITensors.val(::ValName"Emp", ::SiteType"tJ") = 1 -ITensors.val(::ValName"Up", ::SiteType"tJ") = 2 -ITensors.val(::ValName"Dn", ::SiteType"tJ") = 3 -ITensors.val(::ValName"0", st::SiteType"tJ") = val(ValName("Emp"), st) -ITensors.val(::ValName"↑", st::SiteType"tJ") = val(ValName("Up"), st) -ITensors.val(::ValName"↓", st::SiteType"tJ") = val(ValName("Dn"), st) - -ITensors.state(::StateName"Emp", ::SiteType"tJ") = [1.0, 0, 0] -ITensors.state(::StateName"Up", ::SiteType"tJ") = [0.0, 1, 0] -ITensors.state(::StateName"Dn", ::SiteType"tJ") = [0.0, 0, 1] -ITensors.state(::StateName"0", st::SiteType"tJ") = state(StateName("Emp"), st) -ITensors.state(::StateName"↑", st::SiteType"tJ") = state(StateName("Up"), st) -ITensors.state(::StateName"↓", st::SiteType"tJ") = state(StateName("Dn"), st) - -function ITensors.op!(Op::ITensor, ::OpName"Nup", ::SiteType"tJ", s::Index) - return Op[s' => 2, s => 2] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"n↑", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Ndn", ::SiteType"tJ", s::Index) - return Op[s' => 3, s => 3] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"n↓", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Ntot", ::SiteType"tJ", s::Index) - Op[s' => 2, s => 2] = 1.0 - return Op[s' => 3, s => 3] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"ntot", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Cup", ::SiteType"tJ", s::Index) - return Op[s' => 1, s => 2] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"c↑", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Cdagup", ::SiteType"tJ", s::Index) - return Op[s' => 2, s => 1] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"c†↑", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Cdn", ::SiteType"tJ", s::Index) - return Op[s' => 1, s => 3] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"c↓", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Cdagdn", ::SiteType"tJ", s::Index) - return Op[s' => 3, s => 1] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"c†↓", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Aup", ::SiteType"tJ", s::Index) - return Op[s' => 1, s => 2] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"a↑", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Adagup", ::SiteType"tJ", s::Index) - return Op[s' => 2, s => 1] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"a†↑", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Adn", ::SiteType"tJ", s::Index) - return Op[s' => 1, s => 3] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"a↓", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Adagdn", ::SiteType"tJ", s::Index) - return Op[s' => 3, s => 1] = 1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"a†↓", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"F", ::SiteType"tJ", s::Index) - Op[s' => 1, s => 1] = +1.0 - Op[s' => 2, s => 2] = -1.0 - return Op[s' => 3, s => 3] = -1.0 -end - -function ITensors.op!(Op::ITensor, ::OpName"Fup", ::SiteType"tJ", s::Index) - Op[s' => 1, s => 1] = +1.0 - Op[s' => 2, s => 2] = -1.0 - return Op[s' => 3, s => 3] = +1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"F↑", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Fdn", ::SiteType"tJ", s::Index) - Op[s' => 1, s => 1] = +1.0 - Op[s' => 2, s => 2] = +1.0 - return Op[s' => 3, s => 3] = -1.0 -end -function ITensors.op!(Op::ITensor, on::OpName"F↓", st::SiteType"tJ", s::Index) - return op!(Op, alias(on), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sz", ::SiteType"tJ", s::Index) - Op[s' => 2, s => 2] = +0.5 - return Op[s' => 3, s => 3] = -0.5 -end - -function ITensors.op!(Op::ITensor, ::OpName"Sᶻ", st::SiteType"tJ", s::Index) - return op!(Op, OpName("Sz"), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"Sx", ::SiteType"tJ", s::Index) - Op[s' => 2, s => 3] = 0.5 - return Op[s' => 3, s => 2] = 0.5 -end - -function ITensors.op!(Op::ITensor, ::OpName"Sˣ", st::SiteType"tJ", s::Index) - return op!(Op, OpName("Sx"), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"S+", ::SiteType"tJ", s::Index) - return Op[s' => 2, s => 3] = 1.0 -end - -function ITensors.op!(Op::ITensor, ::OpName"S⁺", st::SiteType"tJ", s::Index) - return op!(Op, OpName("S+"), st, s) -end -function ITensors.op!(Op::ITensor, ::OpName"Sp", st::SiteType"tJ", s::Index) - return op!(Op, OpName("S+"), st, s) -end -function ITensors.op!(Op::ITensor, ::OpName"Splus", st::SiteType"tJ", s::Index) - return op!(Op, OpName("S+"), st, s) -end - -function ITensors.op!(Op::ITensor, ::OpName"S-", ::SiteType"tJ", s::Index) - return Op[s' => 3, s => 2] = 1.0 -end - -function ITensors.op!(Op::ITensor, ::OpName"S⁻", st::SiteType"tJ", s::Index) - return op!(Op, OpName("S-"), st, s) -end -function ITensors.op!(Op::ITensor, ::OpName"Sm", st::SiteType"tJ", s::Index) - return op!(Op, OpName("S-"), st, s) -end -function ITensors.op!(Op::ITensor, ::OpName"Sminus", st::SiteType"tJ", s::Index) - return op!(Op, OpName("S-"), st, s) -end - -ITensors.has_fermion_string(::OpName"Cup", ::SiteType"tJ") = true -function ITensors.has_fermion_string(on::OpName"c↑", st::SiteType"tJ") - return has_fermion_string(alias(on), st) -end -ITensors.has_fermion_string(::OpName"Cdagup", ::SiteType"tJ") = true -function ITensors.has_fermion_string(on::OpName"c†↑", st::SiteType"tJ") - return has_fermion_string(alias(on), st) -end -ITensors.has_fermion_string(::OpName"Cdn", ::SiteType"tJ") = true -function ITensors.has_fermion_string(on::OpName"c↓", st::SiteType"tJ") - return has_fermion_string(alias(on), st) -end -ITensors.has_fermion_string(::OpName"Cdagdn", ::SiteType"tJ") = true -function ITensors.has_fermion_string(on::OpName"c†↓", st::SiteType"tJ") - return has_fermion_string(alias(on), st) -end diff --git a/src/qn/qnindex.jl b/src/qn/qnindex.jl index 83bbbe78c4..9bad042eb2 100644 --- a/src/qn/qnindex.jl +++ b/src/qn/qnindex.jl @@ -1,3 +1,7 @@ +using .QuantumNumbers: + QuantumNumbers, Arrow, Neither, Out, have_same_mods, have_same_qns, removeqn +using .SiteTypes: SiteTypes +using .TagSets: TagSets const QNBlock = Pair{QN,Int64} @@ -37,7 +41,7 @@ function (qn1::QNBlock + qn2::QNBlock) return QNBlock(qn(qn1), blockdim(qn1) + blockdim(qn2)) end -function removeqn(qn_block::QNBlock, qn_name::String) +function QuantumNumbers.removeqn(qn_block::QNBlock, qn_name::String) return removeqn(qn(qn_block), qn_name) => blockdim(qn_block) end @@ -65,7 +69,7 @@ function mergeblocks(qns::QNBlocks) return qnsC end -function removeqn(space::QNBlocks, qn_name::String; mergeblocks=true) +function QuantumNumbers.removeqn(space::QNBlocks, qn_name::String; mergeblocks=true) space = QNBlocks([removeqn(qn_block, qn_name) for qn_block in space]) if mergeblocks space = ITensors.mergeblocks(space) @@ -93,7 +97,7 @@ symmetrystyle(::NonQN, ::HasQNs) = HasQNs() hasqns(::QNBlocks) = true -function have_same_qns(qnblocks::QNBlocks) +function QuantumNumbers.have_same_qns(qnblocks::QNBlocks) qn1 = qn(qnblocks, 1) for n in 2:nblocks(qnblocks) !have_same_qns(qn1, qn(qnblocks, n)) && return false @@ -101,7 +105,7 @@ function have_same_qns(qnblocks::QNBlocks) return true end -function have_same_mods(qnblocks::QNBlocks) +function QuantumNumbers.have_same_mods(qnblocks::QNBlocks) qn1 = qn(qnblocks, 1) for n in 2:nblocks(qnblocks) !have_same_mods(qn1, qn(qnblocks, n)) && return false @@ -210,7 +214,7 @@ end function block(iv::Pair{<:Index}) i = ind(iv) - v = val(iv) + v = SiteTypes.val(iv) return block(space(i), v) end @@ -478,7 +482,7 @@ function combineblocks(i::QNIndex) end removeqns(i::QNIndex) = setdir(setspace(i, dim(i)), Neither) -function removeqn(i::QNIndex, qn_name::String; mergeblocks=true) +function QuantumNumbers.removeqn(i::QNIndex, qn_name::String; mergeblocks=true) return setspace(i, removeqn(space(i), qn_name; mergeblocks)) end mergeblocks(i::QNIndex) = setspace(i, mergeblocks(space(i))) @@ -534,7 +538,8 @@ function show(io::IO, i::QNIndex) idstr = "$(id(i) % 1000)" if length(tags(i)) > 0 print( - io, "(dim=$(dim(i))|id=$(idstr)|\"$(tagstring(tags(i)))\")$(primestring(plev(i)))" + io, + "(dim=$(dim(i))|id=$(idstr)|\"$(TagSets.tagstring(tags(i)))\")$(primestring(plev(i)))", ) else print(io, "(dim=$(dim(i))|id=$(idstr))$(primestring(plev(i)))") diff --git a/src/qn/qnitensor.jl b/src/qn/qnitensor.jl index db1c75305c..20adbfc71f 100644 --- a/src/qn/qnitensor.jl +++ b/src/qn/qnitensor.jl @@ -1,3 +1,6 @@ +using .QuantumNumbers: QuantumNumbers, removeqn +using NDTensors: sim + @propagate_inbounds @inline function _setindex!!( ::HasQNs, T::Tensor, x::Number, I::Integer... ) @@ -570,7 +573,7 @@ function splitblocks(A::ITensor, is=inds(A); tol=0) return A end -function removeqn(T::ITensor, qn_name::String; mergeblocks=true) +function QuantumNumbers.removeqn(T::ITensor, qn_name::String; mergeblocks=true) if !hasqns(T) return T end diff --git a/src/val.jl b/src/val.jl new file mode 100644 index 0000000000..b9da546551 --- /dev/null +++ b/src/val.jl @@ -0,0 +1 @@ +function val end diff --git a/test/base/test_exports.jl b/test/base/test_exports.jl index ccfdb65762..0072b0b9d2 100644 --- a/test/base/test_exports.jl +++ b/test/base/test_exports.jl @@ -3,7 +3,9 @@ using ITensors: ITensors using Test: @test, @testset include("utils/TestITensorsExportedNames/TestITensorsExportedNames.jl") using .TestITensorsExportedNames: ITENSORS_EXPORTED_NAMES -@testset "Test $name is exported" for name in ITENSORS_EXPORTED_NAMES - @test Base.isexported(ITensors, name) +@testset "Test exports of ITensors" begin + # @show setdiff(names(ITensors), ITENSORS_EXPORTED_NAMES) + # @show setdiff(ITENSORS_EXPORTED_NAMES, names(ITensors)) + @test issetequal(names(ITensors), ITENSORS_EXPORTED_NAMES) end end diff --git a/test/base/test_smallstring.jl b/test/base/test_smallstring.jl index 3c40987106..a56d332f97 100644 --- a/test/base/test_smallstring.jl +++ b/test/base/test_smallstring.jl @@ -1,6 +1,8 @@ using ITensors using Test +# TODO: Change to: +# using ITensors.SmallStrings: SmallString, Tag, isint, isnull, IntChar import ITensors: SmallString, Tag, isint, isnull, IntChar @testset "SmallString" begin diff --git a/test/lib/ITensorMPS/Project.toml b/test/lib/ITensorMPS/Project.toml new file mode 100644 index 0000000000..127ce9e065 --- /dev/null +++ b/test/lib/ITensorMPS/Project.toml @@ -0,0 +1,10 @@ +[deps] +ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" +Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" +HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" +ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" +NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" +OptimKit = "77e91f04-9b3b-57a6-a776-40b61faaebe0" +Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" +Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" diff --git a/test/lib/ITensorMPS/base/Project.toml b/test/lib/ITensorMPS/base/Project.toml index 1b64344521..4eceef30b9 100644 --- a/test/lib/ITensorMPS/base/Project.toml +++ b/test/lib/ITensorMPS/base/Project.toml @@ -1,5 +1,6 @@ [deps] Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" +HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5" JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" diff --git a/test/base/test_lattices.jl b/test/lib/ITensorMPS/base/test_lattices.jl similarity index 100% rename from test/base/test_lattices.jl rename to test/lib/ITensorMPS/base/test_lattices.jl