Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistencies in projection tests for Stabilizer/MixedDestabilizer states using multi-qubit random_stabilizers and multi random_paulis #418

Closed
Fe-r-oz opened this issue Nov 3, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Fe-r-oz
Copy link
Contributor

Fe-r-oz commented Nov 3, 2024

Describe the bug 🐞

The projection tests for Stabilizer/MixedDestabilizer states are conducted when applying the projection operator. The projection operators qo_proj1 and qo_proj2 are defined as follows:

$$P_1 = \frac{1}{2}(\mathbb{I} - P) \quad \text{and} \quad P_2 = \frac{1}{2}(\mathbb{I} + P)$$

These operators are intended to project onto the eigenspaces corresponding to the eigenvalues of the Pauli operator p. The expected results from these projections can be computed using:

$$\text{result1} = \frac{1}{2} \left( P_1 \cdot \text{qo\_state} \cdot P_1^{\dagger} \right)$$

and

$$\text{result2} = \frac{1}{2} \left( P_2 \cdot \text{qo\_state} \cdot P_2^{\dagger} \right)$$

These calculations should reflect the post-projection state of the original Stabilizer/MixedDestabilizer state.

The tests currently pass only for specific, simple, single-qubit stages when working with the Stabilizer or MixedDestabilizer states, as shown below. However, inconsistencies arise when projecting states using the project! or projectrand! functions.

These discrepancies become evident when comparing the projected states with the theoretical results based on the MREs, especially in scenarios involving multi-qubit stabilizer states and multi-qubit Pauli operators. This issue is particularly noticeable when using random_stabilizer and random_pauli functions in these cases.

julia> using Test; using QuantumClifford; using QuantumOpticsBase;
julia> @testset "projection for Stabilizer/MixedStabilizer" begin
           simplestates = [(S"X",P"X"),(S"Y",P"Y"),(S"Z",P"Z"),(S"-X",P"-X"),(S"-Y",P"-Y"),(S"-Z",P"-Z")]
           for (s, p) in simplestates
               # one can use md = MixedDestabilizer(s)
               apply!(s, p) # or apply!(md, p) 
               qo_state = Operator(s) # or Operator(md) 
               projectrand!(s, p)[1] # or project(s, p)[1] or projectrand!(md, p)[1] # or project(md, p)[1] 
               qo_state_after_proj = Operator(s) # or Operator(md)
               qo_pauli = Operator(p)
               qo_proj1 = (identityoperator(qo_pauli) - qo_pauli)/2
               qo_proj2 = (identityoperator(qo_pauli) + qo_pauli)/2
               result1  = qo_proj1*qo_state*qo_proj1'
               result2  = qo_proj2*qo_state*qo_proj2'
               @test qo_state_after_proj  result2 || qo_state_after_proj  result1
           end
       end

Expected behavior

I think these tests should not fail.

Minimal Reproducible Example 👇

Testing with multi-qubit Stabilizer and multi-qubit Pauli Operators;

julia> using Test; using QuantumClifford; using QuantumOpticsBase;
julia> @testset "projection for Stabilizer/MixedStabilizer" begin
           for n in 1:4
               for repetition in 1:5
                   s = random_stabilizer(n) # or md = MixedDestabilizer(random_stabilizer(n))
                   p = random_pauli(n)
                   apply!(s, p)
                   qo_state = Operator(s)
                   projectrand!(s, p)[1] # or project(s, p)[1]
                   qo_state_after_proj = Operator(s)
                   qo_pauli = Operator(p)
                   qo_proj1 = (identityoperator(qo_pauli) - qo_pauli)/2
                   qo_proj2 = (identityoperator(qo_pauli) + qo_pauli)/2
                   result1  = qo_proj1*qo_state*qo_proj1'
                   result2  = qo_proj2*qo_state*qo_proj2'
                   @test qo_state_after_proj  result2 || qo_state_after_proj  result1
               end
           end
       end

Let's try the simple case: a single qubit but with repetition. It is observed that the same error occurs with single-qubit states when using repeated projections with random_stabilizer and random_pauli.

julia> using Test; using QuantumClifford; using QuantumOpticsBase;
julia> @testset "projection for Stabilizer/MixedStabilizer" begin
           for repetition in 1:5
               n = 1 
               s = random_stabilizer(n) # or md = MixedDestabilizer(random_stabilizer(n))
               p = random_pauli(n)
               apply!(s, p)
               qo_state = Operator(s)
               projectrand!(s, p)[1] # or project(s, p)[1]
               qo_state_after_proj = Operator(s)
               qo_pauli = Operator(p)
               qo_proj1 = (identityoperator(qo_pauli) - qo_pauli)/2
               qo_proj2 = (identityoperator(qo_pauli) + qo_pauli)/2
               result1  = qo_proj1*qo_state*qo_proj1'
               result2  = qo_proj2*qo_state*qo_proj2'
               @test qo_state_after_proj  result2 || qo_state_after_proj  result1
           end
       end
    end

Error & Stacktrace or other complete output produced by the MRE ⚠️

@test qo_state_after_proj  result2 || qo_state_after_proj  result1 #this is false and hence the errors

Stacktrace:
  [1] macro expansion
    @ ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/Test/src/Test.jl:679 [inlined]
  [2] macro expansion
    @ ./REPL[8]:15 [inlined]
  [3] macro expansion
    @ ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/Test/src/Test.jl:1700 [inlined]
  [4] top-level scope
    @ ./REPL[8]:2
  [5] eval
    @ ./boot.jl:430 [inlined]
  [6] eval_user_input(ast::Any, backend::REPL.REPLBackend, mod::Module)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:245
  [7] repl_backend_loop(backend::REPL.REPLBackend, get_module::Function)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:342
  [8] start_repl_backend(backend::REPL.REPLBackend, consumer::Any; get_module::Function)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:327
  [9] run_repl(repl::REPL.AbstractREPL, consumer::Any; backend_on_current_task::Bool, backend::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:483
 [10] run_repl(repl::REPL.AbstractREPL, consumer::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:469
 [11] (::Base.var"#1139#1141"{Bool, Symbol, Bool})(REPL::Module)
    @ Base ./client.jl:446
 [12] #invokelatest#2
    @ ./essentials.jl:1055 [inlined]
 [13] invokelatest
    @ ./essentials.jl:1052 [inlined]
 [14] run_main_repl(interactive::Bool, quiet::Bool, banner::Symbol, history_file::Bool, color_set::Bool)
    @ Base ./client.jl:430
 [15] repl_main
    @ ./client.jl:567 [inlined]
 [16] _start()
    @ Base ./client.jl:541
Test Summary:                           | Pass  Fail  Total  Time
project! for Stabilizer/MixedStabilizer |    4    16     20  1.9s
ERROR: Some tests did not pass: 4 passed, 16 failed, 0 errored, 0 broken.

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()

This MRE is reproduced with the nonclif branch.

Status `~/.julia/environments/v1.11/Project.toml`
⌅ [c3fe647b] AbstractAlgebra v0.40.9
  [4c88cf16] Aqua v0.8.9
⌅ [3e1990a7] Hecke v0.30.12
  [c3a54625] JET v0.9.12
⌅ [2edaba10] Nemo v0.43.3
⌃ [f1435218] Oscar v1.0.5
  [0525e862] QuantumClifford v0.10.0 
⌃ [4f57444f] QuantumOpticsBase v0.4.22
Info Packages marked with ⌃ and ⌅ have new versions available. Those with ⌃ may be upgradable, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use `status --outdated`
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Status `~/.julia/environments/v1.11/Manifest.toml`
⌅ [c3fe647b] AbstractAlgebra v0.40.9
  [621f4979] AbstractFFTs v1.5.0
⌃ [79e6a3ab] Adapt v4.1.0
⌅ [66b61cbe] AlgebraicSolving v0.4.14
  [66dad0bd] AliasTables v1.1.3
  [4c88cf16] Aqua v0.8.9
  [ec485272] ArnoldiMethod v0.4.0
  [4fba245c] ArrayInterface v7.16.0
  [f01c122e] BinaryWrappers v0.1.3
  [62783981] BitTwiddlingConvenienceFunctions v0.1.6
  [2a0fbf3d] CPUSummary v0.2.6
  [fb6a15b2] CloseOpenIntervals v0.1.13
  [da1fd8a2] CodeTracking v1.3.6
  [861a8166] Combinatorics v1.0.2
  [f70d9fcc] CommonWorldInvalidations v1.0.0
  [34da2185] Compat v4.16.0
  [adafc99b] CpuId v0.3.1
⌅ [1f15a43c] CxxWrap v0.14.2
  [9a962f9c] DataAPI v1.16.0
  [864edb3b] DataStructures v0.18.20
  [55939f99] DecFP v1.3.2
  [31c24e10] Distributions v0.25.112
  [ffbed154] DocStringExtensions v0.9.3
  [7a1cc6ca] FFTW v1.8.0
  [7868e603] FastExpm v1.1.0
  [442a2c76] FastGaussQuadrature v1.0.2
  [1a297f60] FillArrays v1.13.0
⌅ [c863536a] GAP v0.10.4
  [92c85e6c] GSL v1.0.1
  [86223c79] Graphs v1.12.0
⌅ [3e1990a7] Hecke v0.30.12
  [3e5b6fbb] HostCPUFeatures v0.1.17
  [34004b35] HypergeometricFunctions v0.3.24
⌃ [2cd5bd5f] ILog2 v0.2.4
  [615f187c] IfElse v0.1.1
  [d25df0c9] Inflate v0.1.5
  [92d709cd] IrrationalConstants v0.2.2
  [c3a54625] JET v0.9.12
  [692b3bcd] JLLWrappers v1.6.1
  [682c06a0] JSON v0.21.4
  [0f8b85d8] JSON3 v1.14.1
  [aa1ae85d] JuliaInterpreter v0.9.36
  [8ac3fa9e] LRUCache v1.6.1
  [10f19ff3] LayoutPointers v0.1.17
  [2ab3a3ac] LogExpFunctions v0.3.28
  [bdcacae8] LoopVectorization v0.12.171
  [6f1432cf] LoweredCodeUtils v3.0.5
  [1914dd2f] MacroTools v0.5.13
  [d125e4d3] ManualMemory v0.1.8
  [e1d29d7a] Missings v1.2.0
  [4fe8b98c] Mongoc v0.9.1
⌅ [2edaba10] Nemo v0.43.3
  [6fe1bfb0] OffsetArrays v1.14.1
  [bac558e1] OrderedCollections v1.6.3
⌃ [f1435218] Oscar v1.0.5
  [90014a1f] PDMats v0.11.31
  [65ce6f38] PackageExtensionCompat v1.0.2
  [69de0a69] Parsers v2.8.1
  [fa939f87] Pidfile v1.3.0
  [1d0040c9] PolyesterWeave v0.2.2
⌃ [d720cf60] Polymake v0.11.17
  [aea7be01] PrecompileTools v1.2.1
  [21216c6a] Preferences v1.4.3
  [43287f4e] PtrArrays v1.2.1
  [1fd47b50] QuadGK v2.11.1
  [0525e862] QuantumClifford v0.10.0
  [5717a53b] QuantumInterface v0.3.6
⌃ [4f57444f] QuantumOpticsBase v0.4.22
  [fb686558] RandomExtensions v0.4.4
  [2576dda1] RandomMatrices v0.5.5
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.0
  [79098fc4] Rmath v0.8.0
  [fdea26ae] SIMD v3.6.0
  [94e857df] SIMDTypes v0.1.0
  [476501e8] SLEEFPirates v0.6.43
  [6c6a2e73] Scratch v1.2.1
  [699a6c99] SimpleTraits v0.9.4
⌅ [bcd08a7b] Singular v0.22.6
  [a2af1166] SortingAlgorithms v1.2.1
  [276daf66] SpecialFunctions v2.4.0
  [aedffcd0] Static v1.1.1
  [0d7ed370] StaticArrayInterface v1.8.0
  [90137ffa] StaticArrays v1.9.8
  [1e83bf80] StaticArraysCore v1.4.3
  [10745b16] Statistics v1.11.1
  [82ae8749] StatsAPI v1.7.0
  [2913bbd2] StatsBase v0.34.3
  [4c63d2b9] StatsFuns v1.3.2
  [5e0ebb24] Strided v2.2.0
  [4db3bf67] StridedViews v0.3.2
  [856f2bd8] StructTypes v1.11.0
  [8e1ec7a9] SumTypes v0.5.8
  [8290d209] ThreadingUtilities v0.5.2
  [9d95972d] TupleTools v1.6.0
  [3a884ed6] UnPack v1.0.2
  [c4a57d5a] UnsafeArrays v1.0.6
⌃ [3d5dd08c] VectorizationBase v0.21.70
  [ae81ac8f] ASL_jll v0.1.3+0
  [e21ec000] Antic_jll v0.201.500+0
  [d9960996] Arb_jll v200.2300.0+0
  [6e34b625] Bzip2_jll v1.0.8+2
  [fcfa6d1b] Calcium_jll v0.401.100+0
  [47200ebd] DecFP_jll v2.0.3+1
  [f5851436] FFTW_jll v3.3.10+1
⌅ [e134572f] FLINT_jll v200.900.9+0
⌅ [5cd7a574] GAP_jll v400.1200.200+9
⌅ [de1ad85e] GAP_lib_jll v400.1201.200+0
⌅ [ba154793] GAP_pkg_juliainterface_jll v0.800.300+3
  [e8aa6df9] GLPK_jll v5.0.1+0
  [1b77fbbe] GSL_jll v2.7.2+0
  [e33a78d0] Hwloc_jll v2.11.2+0
  [1d5cc7b8] IntelOpenMP_jll v2024.2.1+0
  [9cc047cb] Ipopt_jll v300.1400.1600+0
  [1d63c593] LLVMOpenMP_jll v18.1.7+0
  [dd4b983a] LZO_jll v2.10.2+1
  [5ced341a] Lz4_jll v1.10.0+1
  [d00139f3] METIS_jll v5.1.2+0
  [856f044c] MKL_jll v2024.2.0+0
⌅ [d7ed1dd3] MUMPS_seq_jll v500.700.300+0
⌅ [90100e71] MongoC_jll v1.19.1+0
  [68e3532b] Ncurses_jll v6.5.0+1
  [76642167] Ninja_jll v1.12.1+0
  [656ef2d0] OpenBLAS32_jll v0.3.28+3
⌅ [458c3c95] OpenSSL_jll v1.1.23+1
  [efe28fd5] OpenSpecFun_jll v0.5.5+0
  [80dd9cbb] PPL_jll v1.2.1+0
  [83958c19] Perl_jll v5.34.1+0
  [05236dd9] Readline_jll v8.2.1+1
  [f50d1b31] Rmath_jll v0.5.1+0
⌅ [e5ac4fe4] SCIP_jll v800.0.301+0
  [319450e9] SPRAL_jll v2024.5.8+0
⌅ [43d676ae] Singular_jll v403.216.1602+0
  [36f60fef] TOPCOM_jll v0.17.8+0
  [3161d3a3] Zstd_jll v1.5.6+1
  [508c9074] bliss_jll v0.77.0+1
⌅ [28df3c45] boost_jll v1.76.0+1
  [f07e07eb] cddlib_jll v0.94.14+0
  [5558cf25] cohomCalg_jll v0.32.0+0
  [1493ae25] lib4ti2_jll v1.6.10+0
⌅ [3eaa8342] libcxxwrap_julia_jll v0.11.2+1
⌅ [4d8266f6] libpolymake_julia_jll v0.11.4+0
⌅ [ae4fbd8f] libsingular_julia_jll v0.44.2+0
  [3873f7d0] lrslib_jll v0.3.3+0
⌅ [6d01cc9a] msolve_jll v0.6.5+0
⌅ [55c6dc9b] nauty_jll v2.6.13+1
⌅ [6690c6e9] normaliz_jll v300.900.301+0
  [1317d2d5] oneTBB_jll v2021.12.0+0
⌅ [7c209550] polymake_jll v400.1100.1+0
⌅ [10f31823] polymake_oscarnumber_jll v0.2.11+0
  [fe1e1685] snappy_jll v1.2.1+0
  [0dad84c5] ArgTools v1.1.2
  [56f22d72] Artifacts v1.11.0
  [2a0f44e3] Base64 v1.11.0
  [ade2ca70] Dates v1.11.0
  [8ba89e20] Distributed v1.11.0
  [f43a241f] Downloads v1.6.0
  [7b1f6079] FileWatching v1.11.0
  [b77e0a4c] InteractiveUtils v1.11.0
  [4af54fe1] LazyArtifacts v1.11.0
  [b27032c2] LibCURL v0.6.4
  [76f85450] LibGit2 v1.11.0
  [8f399da3] Libdl v1.11.0
  [37e2e46d] LinearAlgebra v1.11.0
  [56ddb016] Logging v1.11.0
  [d6f4376e] Markdown v1.11.0
  [a63ad114] Mmap v1.11.0
  [ca575930] NetworkOptions v1.2.0
  [44cfe95a] Pkg v1.11.0
  [de0858da] Printf v1.11.0
  [3fa0cd96] REPL v1.11.0
  [9a3f8284] Random v1.11.0
  [ea8e919c] SHA v0.7.0
  [9e88b42a] Serialization v1.11.0
  [1a1011a3] SharedArrays v1.11.0
  [6462fe0b] Sockets v1.11.0
  [2f01184e] SparseArrays v1.11.0
  [f489334b] StyledStrings v1.11.0
  [4607b0f0] SuiteSparse
  [fa267f1f] TOML v1.0.3
  [a4e569a6] Tar v1.10.0
  [8dfed614] Test v1.11.0
  [cf7118a7] UUIDs v1.11.0
  [4ec0a83e] Unicode v1.11.0
  [e66e0078] CompilerSupportLibraries_jll v1.1.1+0
  [781609d7] GMP_jll v6.3.0+0
  [deac9b47] LibCURL_jll v8.6.0+0
  [e37daf67] LibGit2_jll v1.7.2+0
  [29816b5a] LibSSH2_jll v1.11.0+1
  [3a97d323] MPFR_jll v4.2.1+0
  [c8ffd9c3] MbedTLS_jll v2.28.6+0
  [14a3606d] MozillaCACerts_jll v2023.12.12
  [4536629a] OpenBLAS_jll v0.3.27+1
  [05823500] OpenLibm_jll v0.8.1+2
  [bea87d4a] SuiteSparse_jll v7.7.0+0
  [83775a58] Zlib_jll v1.2.13+1
  [8e850b90] libblastrampoline_jll v5.11.0+0
  [8e850ede] nghttp2_jll v1.59.0+0
  [3f19e933] p7zip_jll v17.4.0+2
  • Output of versioninfo()
julia> versioninfo()
Julia Version 1.11.1
Commit 8f5b7ca12ad (2024-10-16 10:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, skylake)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)
@Fe-r-oz Fe-r-oz added the bug Something isn't working label Nov 3, 2024
@Fe-r-oz Fe-r-oz changed the title Inconsistencies in Projection Tests for Stabilizer/MixedDestabilizer States Using Multi-Qubit Random Stabilizers and Pauli Operators Inconsistencies in projection tests for Stabilizer/MixedDestabilizer states using multi-qubit random_stabilizers and multi random_paulis Nov 3, 2024
Fe-r-oz added a commit to Fe-r-oz/QuantumClifford.jl that referenced this issue Nov 8, 2024
… multi-qubit random_stabilizer and random_pauli for MixedDestabilizer/Stabilizer states
@Fe-r-oz
Copy link
Contributor Author

Fe-r-oz commented Nov 8, 2024

The primary issue in the current testing lies in a scaling discrepancy within one branch, as demonstrated in the tests below:

julia> @testset "projection for Stabilizer/MixedStabilizer" begin
           for repetition in 1:5
               n = 1 
               s = random_stabilizer(n) # or md = MixedDestabilizer(random_stabilizer(n))
               p = random_pauli(n)
               println(s)
               println(p)
               apply!(s, p)
               qo_state = Operator(s)
               projectrand!(s, p)[1] # or project(s, p)[1]
               qo_state_after_proj = Operator(s)
               println(qo_state_after_proj)
               qo_pauli = Operator(p)
               qo_proj1 = (identityoperator(qo_pauli) - qo_pauli)/2
               qo_proj2 = (identityoperator(qo_pauli) + qo_pauli)/2
               result1  = qo_proj1*qo_state*qo_proj1'
               result2  = qo_proj2*qo_state*qo_proj2'
               println(result1)
               println(result2)
               @test qo_state_after_proj  result2 || qo_state_after_proj  result1
           end
       end

- Y
+ Z
Operator(dim=2x2)
  basis: Spin(1/2)
 0.0+0.0im  0.0+0.0im
 0.0+0.0im  1.0+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
 0.0+0.0im  0.0+0.0im
 0.0+0.0im  0.5+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
 0.5+0.0im  0.0+0.0im
 0.0+0.0im  0.0+0.0im
projection for Stabilizer/MixedStabilizer: Test Failed at REPL[12]:20
  Expression: qo_state_after_proj  result2 || qo_state_after_proj  result1

Stacktrace:
  [1] macro expansion
    @ ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/Test/src/Test.jl:679 [inlined]
  [2] macro expansion
    @ ./REPL[12]:20 [inlined]
  [3] macro expansion
    @ ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/Test/src/Test.jl:1700 [inlined]
  [4] top-level scope
    @ ./REPL[12]:2
  [5] eval
    @ ./boot.jl:430 [inlined]
  [6] eval_user_input(ast::Any, backend::REPL.REPLBackend, mod::Module)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:245
  [7] repl_backend_loop(backend::REPL.REPLBackend, get_module::Function)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:342
  [8] start_repl_backend(backend::REPL.REPLBackend, consumer::Any; get_module::Function)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:327
  [9] run_repl(repl::REPL.AbstractREPL, consumer::Any; backend_on_current_task::Bool, backend::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:483
 [10] run_repl(repl::REPL.AbstractREPL, consumer::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:469
 [11] (::Base.var"#1139#1141"{Bool, Symbol, Bool})(REPL::Module)
    @ Base ./client.jl:446
 [12] #invokelatest#2
    @ ./essentials.jl:1055 [inlined]
 [13] invokelatest
    @ ./essentials.jl:1052 [inlined]
 [14] run_main_repl(interactive::Bool, quiet::Bool, banner::Symbol, history_file::Bool, color_set::Bool)
    @ Base ./client.jl:430
 [15] repl_main
    @ ./client.jl:567 [inlined]
 [16] _start()
    @ Base ./client.jl:541
+ X
+ _
Operator(dim=2x2)
  basis: Spin(1/2)
 0.5+0.0im  0.5+0.0im
 0.5+0.0im  0.5+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
 0.0+0.0im  0.0+0.0im
 0.0+0.0im  0.0+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
 0.5+0.0im  0.5+0.0im
 0.5+0.0im  0.5+0.0im
- X
+ _
Operator(dim=2x2)
  basis: Spin(1/2)
  0.5+0.0im  -0.5-0.0im
 -0.5+0.0im   0.5+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
 0.0+0.0im  0.0+0.0im
 0.0+0.0im  0.0+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
  0.5+0.0im  -0.5+0.0im
 -0.5+0.0im   0.5+0.0im
- Z
+ X
Operator(dim=2x2)
  basis: Spin(1/2)
  0.5+0.0im  -0.5-0.0im
 -0.5+0.0im   0.5+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
  0.25+0.0im  -0.25+0.0im
 -0.25+0.0im   0.25+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
 0.25+0.0im  0.25+0.0im
 0.25+0.0im  0.25+0.0im
projection for Stabilizer/MixedStabilizer: Test Failed at REPL[12]:20
  Expression: qo_state_after_proj  result2 || qo_state_after_proj  result1

Stacktrace:
  [1] macro expansion
    @ ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/Test/src/Test.jl:679 [inlined]
  [2] macro expansion
    @ ./REPL[12]:20 [inlined]
  [3] macro expansion
    @ ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/Test/src/Test.jl:1700 [inlined]
  [4] top-level scope
    @ ./REPL[12]:2
  [5] eval
    @ ./boot.jl:430 [inlined]
  [6] eval_user_input(ast::Any, backend::REPL.REPLBackend, mod::Module)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:245
  [7] repl_backend_loop(backend::REPL.REPLBackend, get_module::Function)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:342
  [8] start_repl_backend(backend::REPL.REPLBackend, consumer::Any; get_module::Function)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:327
  [9] run_repl(repl::REPL.AbstractREPL, consumer::Any; backend_on_current_task::Bool, backend::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:483
 [10] run_repl(repl::REPL.AbstractREPL, consumer::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.1+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:469
 [11] (::Base.var"#1139#1141"{Bool, Symbol, Bool})(REPL::Module)
    @ Base ./client.jl:446
 [12] #invokelatest#2
    @ ./essentials.jl:1055 [inlined]
 [13] invokelatest
    @ ./essentials.jl:1052 [inlined]
 [14] run_main_repl(interactive::Bool, quiet::Bool, banner::Symbol, history_file::Bool, color_set::Bool)
    @ Base ./client.jl:430
 [15] repl_main
    @ ./client.jl:567 [inlined]
 [16] _start()
    @ Base ./client.jl:541
+ Y
+ Y
Operator(dim=2x2)
  basis: Spin(1/2)
 0.5+0.0im  0.0-0.5im
 0.0+0.5im  0.5+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
 0.0+0.0im  0.0+0.0im
 0.0+0.0im  0.0+0.0im
Operator(dim=2x2)
  basis: Spin(1/2)
 0.5+0.0im  0.0-0.5im
 0.0+0.5im  0.5+0.0im
Test Summary:                             | Pass  Fail  Total  Time
projection for Stabilizer/MixedStabilizer |    3     2      5  3.8s
ERROR: Some tests did not pass: 3 passed, 2 failed, 0 errored, 0 broken.

The fix:

julia> using Test; using QuantumClifford; using QuantumOpticsBase;
julia> @testset "projection for Stabilizer/MixedStabilizer" begin
           for n in 1:10
               for repetition in 1:5
                   s = random_stabilizer(n) # or md = MixedDestabilizer(random_stabilizer(n))
                   p = random_pauli(n)
                   apply!(s, p)
                   qo_state = Operator(s)
                   projectrand!(s, p)[1] # or project(s, p)[1]
                   qo_state_after_proj = Operator(s)
                   qo_pauli = Operator(p)
                   qo_proj1 = (identityoperator(qo_pauli) - qo_pauli)/2
                   qo_proj2 = (identityoperator(qo_pauli) + qo_pauli)/2
                   result1  = qo_proj1*qo_state*qo_proj1'
                   result2  = qo_proj2*qo_state*qo_proj2'
                   @test qo_state_after_proj  result2 || qo_state_after_proj  result1 || qo_state_after_proj  2*result2 || qo_state_after_proj  2*result1
               end
           end
       end

@Fe-r-oz
Copy link
Contributor Author

Fe-r-oz commented Nov 8, 2024

I think that the proper fix is to normalize the projected states:

norm_qo_state_after_proj = qo_state_after_proj / tr(qo_state_after_proj)
norm_result1 = result1 / tr(result1)
norm_result2 = result2 / tr(result2)
@test norm_qo_state_after_proj  norm_result2 || norm_qo_state_after_proj  norm_result1

This test was originally suggested by Stefan in this comment. However, the projected states in the original suggestion were not normalized, which led to errors for Stabilizer and MixedDestabilizer states when performing repeated multi-qubit projections.

@Krastanov
Copy link
Member

Yup, it seems some normalizations were missing from my example. Thanks for clarifying it.

The issue tracker is not the best place to discuss issues about unmerged (and even not yet written) PRs. Let's keep such discussions within the appropriate PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants