diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index e32def8..fe0b2c1 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -19,13 +19,16 @@ jobs: matrix: version: - '1.7' - - 'nightly' + - '1.8' os: - ubuntu-latest - macOS-latest arch: - x64 #- x86 + allow_failures: + - julia_version: 'nightly' + steps: - uses: actions/checkout@v2 - uses: julia-actions/setup-julia@v1 @@ -39,24 +42,24 @@ jobs: - uses: codecov/codecov-action@v2 with: files: lcov.info - docs: - name: Documentation - runs-on: ubuntu-latest - permissions: - contents: write - statuses: write - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 - with: - version: '1' - - uses: julia-actions/julia-buildpkg@v1 - - uses: julia-actions/julia-docdeploy@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - run: | - julia --project=docs -e ' - using Documenter: DocMeta, doctest - using PauliOperators - DocMeta.setdocmeta!(PauliOperators, :DocTestSetup, :(using PauliOperators); recursive=true) - doctest(PauliOperators)' +# docs: +# name: Documentation +# runs-on: ubuntu-latest +# permissions: +# contents: write +# statuses: write +# steps: +# - uses: actions/checkout@v2 +# - uses: julia-actions/setup-julia@v1 +# with: +# version: '1' +# - uses: julia-actions/julia-buildpkg@v1 +# - uses: julia-actions/julia-docdeploy@v1 +# env: +# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} +# - run: | +# julia --project=docs -e ' +# using Documenter: DocMeta, doctest +# using PauliOperators +# DocMeta.setdocmeta!(PauliOperators, :DocTestSetup, :(using PauliOperators); recursive=true) +# doctest(PauliOperators)' diff --git a/Manifest.toml b/Manifest.toml index 3b11b20..ee24653 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -44,7 +44,7 @@ version = "1.3.2" deps = ["Arpack", "InteractiveUtils", "LinearAlgebra", "LinearMaps", "Printf", "Random"] git-tree-sha1 = "181fe1ffc1f9adb45e558be29f9a76d1a4b17757" repo-rev = "main" -repo-url = "https://github.com/nmayhall-vt/BlockDavidson.jl" +repo-url = "git@github.com:nmayhall-vt/BlockDavidson.jl.git" uuid = "b4c031f8-e7c9-4879-896c-063a10ff8103" version = "0.1.0" @@ -93,10 +93,10 @@ uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" version = "0.9.3" [[deps.Documenter]] -deps = ["ANSIColoredPrinters", "Base64", "Dates", "DocStringExtensions", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"] -git-tree-sha1 = "39fd748a73dce4c05a9655475e437170d8fb1b67" +deps = ["ANSIColoredPrinters", "AbstractTrees", "Base64", "Dates", "DocStringExtensions", "Downloads", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "MarkdownAST", "Pkg", "PrecompileTools", "REPL", "RegistryInstances", "SHA", "Test", "Unicode"] +git-tree-sha1 = "f1a7eaf2b5ac7a4feb27973a09d25daf80dc56db" uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4" -version = "0.27.25" +version = "1.0.1" [[deps.Downloads]] deps = ["ArgTools", "LibCURL", "NetworkOptions"] @@ -153,6 +153,11 @@ git-tree-sha1 = "31e996f0a15c7b280ba9f76636b3ff9e2ae58c9a" uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" version = "0.21.4" +[[deps.LazilyInitializedFields]] +git-tree-sha1 = "410fe4739a4b092f2ffe36fcb0dcc3ab12648ce1" +uuid = "0e77f7df-68c5-4e49-93ce-4cd80f5598bf" +version = "1.2.1" + [[deps.LeftChildRightSiblingTrees]] deps = ["AbstractTrees"] git-tree-sha1 = "fb6803dafae4a5d62ea5cab204b1e657d9737e7f" @@ -195,6 +200,12 @@ uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" +[[deps.MarkdownAST]] +deps = ["AbstractTrees", "Markdown"] +git-tree-sha1 = "e8513266815200c0c8f522d6d44ffb5e9b366ae4" +uuid = "d0879d2d-cac2-40c8-9cee-1863dc0c7391" +version = "0.1.1" + [[deps.MbedTLS_jll]] deps = ["Artifacts", "Libdl"] uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" @@ -260,6 +271,12 @@ git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b" uuid = "189a3867-3050-52da-a836-e630ba90ab69" version = "1.2.2" +[[deps.RegistryInstances]] +deps = ["LazilyInitializedFields", "Pkg", "TOML", "Tar"] +git-tree-sha1 = "ffd19052caf598b8653b99404058fce14828be51" +uuid = "2792f1a3-b283-48e8-9a74-f99dce5104f3" +version = "0.1.0" + [[deps.Requires]] deps = ["UUIDs"] git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" diff --git a/src/PauliOperators.jl b/src/PauliOperators.jl index 0b9d472..6f8f073 100644 --- a/src/PauliOperators.jl +++ b/src/PauliOperators.jl @@ -43,6 +43,7 @@ export otimes export clip! export ⊗ export ⊕ +export expectation_value end diff --git a/src/function_add.jl b/src/function_add.jl index 4ee52de..8e3c9d4 100644 --- a/src/function_add.jl +++ b/src/function_add.jl @@ -12,7 +12,7 @@ function Base.:+(p1::ScaledPauli{N}, p2::ScaledPauli{N}) where {T,N} end end function Base.:+(p::ScaledPauli{N}, a::Number) where {T,N} - return p + ScaledPauli{N}(a, Pauli{N}(0,0,0)) + return p + ScaledPauli{N}(a, Pauli{N}(0,FixedPhasePauli{N}(0,0))) end Base.:+(p::ScaledPauli{N}, a::Pauli{N}) where {T,N} = p + ScaledPauli{N}(1, a) Base.:+(a::Pauli{N}, p::ScaledPauli{N}) where {T,N} = p + a diff --git a/src/operations.jl b/src/operations.jl index a892f67..9c1a7f0 100644 --- a/src/operations.jl +++ b/src/operations.jl @@ -133,13 +133,15 @@ function negate(p::Pauli{N}) where N end """ - is_diagonal(p::Pauli) + is_diagonal(p::FixedPhasePauli) Check if operator is diagonal in the computational (z) basis. E.g., does this operator consist of only I and/or Z? """ -function is_diagonal(p::Pauli) - return count_ones(p.pauli.x) == 0 +function is_diagonal(p::FixedPhasePauli) + return count_ones(p.x) == 0 end +is_diagonal(p::Pauli) = is_diagonal(p.pauli) +is_diagonal(p::ScaledPauli) = is_diagonal(p.pauli) @@ -153,11 +155,20 @@ end compute expectation value of Pauli `o` for a product state `ket` """ -function expectation_value_sign(p::Pauli{N}, ket::KetBitString{N}) where N + +function expectation_value(p::FixedPhasePauli{N}, ket::KetBitString{N}) where N is_diagonal(p) || return 0.0 - println(p) + count_ones(p.z & ket.v) % 2 == 0 || return -1 + return 1 +end +function expectation_value(p::Pauli{N}, ket::KetBitString{N}) where N + is_diagonal(p) || return 0.0 + count_ones(p.pauli.z & ket.v) % 2 == 0 || return -(1im)^p.θ return (1im)^p.θ end + +expectation_value_sign(p::Pauli{N}, ket::KetBitString{N}) where N = expectation_value(p::Pauli{N}, ket::KetBitString{N}) where N +expectation_value_sign(p::FixedPhasePauli{N}, ket::KetBitString{N}) where N = expectation_value(p::FixedPhasePauli{N}, ket::KetBitString{N}) where N \ No newline at end of file diff --git a/src/type_KetBitString.jl b/src/type_KetBitString.jl index 2a16c63..d1ea259 100644 --- a/src/type_KetBitString.jl +++ b/src/type_KetBitString.jl @@ -55,4 +55,16 @@ function Base.string(p::KetBitString{N}) where N out[i] = 1 end return join(out[1:N]) +end + + +""" + Base.Vector(k::KetBitString{N}) where N + +TBW +""" +function Base.Vector(k::KetBitString{N}) where N + vec = zeros(Int8,2^N) + vec[k.v+1] = 1 + return vec end \ No newline at end of file diff --git a/src/type_PauliSum.jl b/src/type_PauliSum.jl index 0cc0c80..d3f09e6 100644 --- a/src/type_PauliSum.jl +++ b/src/type_PauliSum.jl @@ -16,9 +16,12 @@ end TBW """ -function PauliSum(N) +function PauliSum(N::Integer) return PauliSum{N}(Dict{FixedPhasePauli{N},ComplexF64}()) end +function PauliSum(o::Pauli{N}) where N + return PauliSum{N}(Dict(o.pauli => get_phase(o))) +end """ @@ -36,9 +39,9 @@ Base.get(ps::PauliSum{N}, p::FixedPhasePauli{N}) where N = get(ps.ops, p, zero(C Base.get(ps::PauliSum{N}, p::Pauli{N}) where N = get(ps.ops, p.pauli, zero(ComplexF64)) Base.get(ps::PauliSum{N}, p::ScaledPauli{N}) where N = get(ps.ops, p.pauli, zero(ComplexF64)) Base.keys(ps::PauliSum) = keys(ps.ops) -Base.getindex(ps::PauliSum{N}, p::Pauli{N}) where N = ps.ops[p] +Base.getindex(ps::PauliSum{N}, p::Pauli{N}) where N = ps.ops[p.pauli] Base.getindex(ps::PauliSum{N}, p::FixedPhasePauli{N}) where N = ps.ops[p] -Base.setindex!(ps::PauliSum{N}, v, p::Pauli{N}) where N = ps.ops[p] = v*get_phase(p) +Base.setindex!(ps::PauliSum{N}, v, p::Pauli{N}) where N = ps.ops[p.pauli] = v*get_phase(p) Base.setindex!(ps::PauliSum{N}, v, p::FixedPhasePauli{N}) where N = ps.ops[p] = v Base.haskey(ps::PauliSum, v) = haskey(ps.ops, v) @@ -164,15 +167,7 @@ end Delete Pauli's with coeffs smaller than thresh """ function clip!(ps::PauliSum; thresh=1e-16) - to_delete = [] - for (op,coeff) in ps.ops - if abs(coeff) < thresh - push!(to_delete, op) - end - end - for k in to_delete - delete!(ps.ops, k) - end + filter!(p->abs(p.second) > thresh, ps.ops) end """ diff --git a/test/tests.jl b/test/tests.jl index 3c8a2db..a719398 100644 --- a/test/tests.jl +++ b/test/tests.jl @@ -70,6 +70,12 @@ using Random display(sum1) println() + display(a) + display(sum1[a]) + display(sum1[a.pauli]) + sum1[a] = 3 + display(a) + sum2 = a + d display(sum2) @@ -176,6 +182,19 @@ using Random @test Pauli("YZXZ") * KetBitString([1,1,1,0]) == (1im, KetBitString([0,1,0,0])) @test Pauli("XZXZ") * KetBitString([1,1,1,0]) == (-1, KetBitString([0,1,0,0])) + for i in 1:50 + N = 8 + # o = Pauli("XYZI") + # v = KetBitString([1,0,0,0]) + o = random_Pauli(N) + v = KetBitString{N}(rand(1:2^N-1)) + @test expectation_value(o, v) == Vector(v)'*Matrix(o)*Vector(v) + + o = random_FixedPhasePauli(N) + v = KetBitString{N}(rand(1:2^N-1)) + @test expectation_value(o, v) == Vector(v)'*Matrix(o)*Vector(v) + end + # ScaledPauli N=8 for i in 1:10