diff --git a/Project.toml b/Project.toml index cdff9028e..3159c0b3a 100644 --- a/Project.toml +++ b/Project.toml @@ -64,11 +64,4 @@ SparseArrays = "1" SpecialFunctions = "2" StaticArraysCore = "1" StochasticDiffEq = "6" -Test = "1" julia = "1.10" - -[extras] -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[targets] -test = ["Test"] diff --git a/docs/src/resources/contributing.md b/docs/src/resources/contributing.md index be2d20016..5518c6998 100644 --- a/docs/src/resources/contributing.md +++ b/docs/src/resources/contributing.md @@ -38,6 +38,12 @@ The tests are divided into several test groups, where the group names are define make GROUP=Core test ``` +### [Test Item Framework for Core tests](@id doc-Contribute:Test-Item-Framework-for-Core-tests) + +The tests in `GROUP=Core` are provided using the [Test Item Framework](https://www.julia-vscode.org/docs/stable/userguide/testitems/), which structures the test codes into `@testitems` and makes it easier to run individually. + +The [VS Code](https://code.visualstudio.com/) and its [Julia extension](https://www.julia-vscode.org/) provides us with options to run individual `@testitems`. It is much easier to find the specific core test that failed since the [Julia extension](https://www.julia-vscode.org/) in [VS Code](https://code.visualstudio.com/) will collect all core test failures and then display them in a structured way, directly at the place in the code where a specific core test failed. See [here](https://www.julia-vscode.org/docs/stable/userguide/testitems/) for more details. + ## [Julia Code Format](@id doc-Contribute:Julia-Code-Format) We use [`JuliaFormatter.jl`](https://github.com/domluna/JuliaFormatter.jl) to format all the source codes. The code style and extra formatting options is defined in the file `.JuliaFormatter.toml` in the repository. diff --git a/src/qobj/quantum_object_base.jl b/src/qobj/quantum_object_base.jl index 08c3de46b..9fcbb0502 100644 --- a/src/qobj/quantum_object_base.jl +++ b/src/qobj/quantum_object_base.jl @@ -30,6 +30,8 @@ Constructor representing a bra state ``\langle\psi|``. """ struct Bra <: QuantumObjectType end +Base.show(io::IO, ::Bra) = print(io, "Bra()") + @doc raw""" Ket <: QuantumObjectType @@ -37,6 +39,8 @@ Constructor representing a ket state ``|\psi\rangle``. """ struct Ket <: QuantumObjectType end +Base.show(io::IO, ::Ket) = print(io, "Ket()") + @doc raw""" Operator <: QuantumObjectType @@ -44,6 +48,8 @@ Constructor representing an operator ``\hat{O}``. """ struct Operator <: QuantumObjectType end +Base.show(io::IO, ::Operator) = print(io, "Operator()") + @doc raw""" SuperOperator <: SuperOperatorType @@ -51,6 +57,8 @@ Constructor representing a super-operator ``\hat{\mathcal{O}}`` acting on vector """ struct SuperOperator <: SuperOperatorType end +Base.show(io::IO, ::SuperOperator) = print(io, "SuperOperator()") + @doc raw""" OperatorBra <: QuantumObjectType @@ -58,6 +66,8 @@ Constructor representing a bra state in the [`SuperOperator`](@ref) formalism `` """ struct OperatorBra <: QuantumObjectType end +Base.show(io::IO, ::OperatorBra) = print(io, "OperatorBra()") + @doc raw""" OperatorKet <: QuantumObjectType @@ -65,6 +75,8 @@ Constructor representing a ket state in the [`SuperOperator`](@ref) formalism `` """ struct OperatorKet <: QuantumObjectType end +Base.show(io::IO, ::OperatorKet) = print(io, "OperatorKet()") + @doc raw""" size(A::AbstractQuantumObject) size(A::AbstractQuantumObject, idx::Int) diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 000000000..6c91482ab --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,14 @@ +[deps] +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +SciMLOperators = "c0aeaf25-5076-4817-a8d5-81caf7dfa961" +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" + +[compat] +Pkg = "1" +Test = "1" +TestItemRunner = "1" diff --git a/test/core-test/block_diagonal_form.jl b/test/core-test/block_diagonal_form.jl index cc96a48d9..201b63ff2 100644 --- a/test/core-test/block_diagonal_form.jl +++ b/test/core-test/block_diagonal_form.jl @@ -1,4 +1,4 @@ -@testset "Block Diagonal Form" begin +@testitem "Block Diagonal Form" begin # Block Diagonal Form N = 20 Δ = 0 diff --git a/test/core-test/code-quality/Project.toml b/test/core-test/code-quality/Project.toml index b1ac4d754..5cadfefb9 100644 --- a/test/core-test/code-quality/Project.toml +++ b/test/core-test/code-quality/Project.toml @@ -2,6 +2,7 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" QuantumToolbox = "6c2fb7c5-b903-41d2-bc5e-5a7c320b9fab" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] Aqua = "0.8" diff --git a/test/core-test/correlations_and_spectrum.jl b/test/core-test/correlations_and_spectrum.jl index 381cd9e08..d889e6d52 100644 --- a/test/core-test/correlations_and_spectrum.jl +++ b/test/core-test/correlations_and_spectrum.jl @@ -1,4 +1,4 @@ -@testset "Correlations and Spectrum" begin +@testitem "Correlations and Spectrum" begin N = 10 Id = qeye(N) a = destroy(N) diff --git a/test/core-test/dynamical-shifted-fock.jl b/test/core-test/dynamical-shifted-fock.jl index 663ac6494..ff9fd3ffc 100644 --- a/test/core-test/dynamical-shifted-fock.jl +++ b/test/core-test/dynamical-shifted-fock.jl @@ -1,4 +1,4 @@ -@testset "Dynamical Shifted Fock" begin +@testitem "Dynamical Shifted Fock" begin F = 3 Δ = 0.25 κ = 1 diff --git a/test/core-test/dynamical_fock_dimension_mesolve.jl b/test/core-test/dynamical_fock_dimension_mesolve.jl index 4c75eef0b..cd213ec00 100644 --- a/test/core-test/dynamical_fock_dimension_mesolve.jl +++ b/test/core-test/dynamical_fock_dimension_mesolve.jl @@ -1,5 +1,5 @@ ### DYNAMICAL FOCK DIMENSION ### -@testset "Dynamical Fock Dimension" begin +@testitem "Dynamical Fock Dimension" begin F, Δ, κ = 5, 0.25, 1 t_l = range(0, 15, length = 100) diff --git a/test/core-test/eigenvalues_and_operators.jl b/test/core-test/eigenvalues_and_operators.jl index a2fe6bcc5..c87702ae8 100644 --- a/test/core-test/eigenvalues_and_operators.jl +++ b/test/core-test/eigenvalues_and_operators.jl @@ -1,4 +1,4 @@ -@testset "Eigenvalues and Operators" begin +@testitem "Eigenvalues and Operators" begin σx = sigmax() result = eigenstates(σx, sparse = false) λd, ψd, Td = result diff --git a/test/core-test/entropy_and_metric.jl b/test/core-test/entropy_and_metric.jl index b90a059f7..79208ca7c 100644 --- a/test/core-test/entropy_and_metric.jl +++ b/test/core-test/entropy_and_metric.jl @@ -1,4 +1,4 @@ -@testset "entropy" begin +@testitem "entropy" begin base = 2 λ = rand() ψ = rand_ket(10) @@ -51,7 +51,7 @@ end end -@testset "entanglement and concurrence" begin +@testitem "entanglement and concurrence" begin # bell state ψb = bell_state(Val(1), Val(0)) ρb = ket2dm(ψb) @@ -87,7 +87,7 @@ end end end -@testset "trace and Hilbert-Schmidt distance" begin +@testitem "trace and Hilbert-Schmidt distance" begin ψz0 = basis(2, 0) ψz1 = basis(2, 1) ρz0 = to_sparse(ket2dm(ψz0)) @@ -117,7 +117,7 @@ end end end -@testset "fidelity, Bures metric, and Hellinger distance" begin +@testitem "fidelity, Bures metric, and Hellinger distance" begin M0 = rand_dm(5) ψ1 = rand_ket(5) ψ2 = rand_ket(5) diff --git a/test/core-test/generalized_master_equation.jl b/test/core-test/generalized_master_equation.jl index 03d2989e0..d3686a92a 100644 --- a/test/core-test/generalized_master_equation.jl +++ b/test/core-test/generalized_master_equation.jl @@ -1,4 +1,6 @@ -@testset "Generalized Master Equation" begin +@testitem "Generalized Master Equation" begin + using LinearAlgebra + N_c = 30 N_trunc = 10 tol = 1e-14 diff --git a/test/core-test/low_rank_dynamics.jl b/test/core-test/low_rank_dynamics.jl index 07faf788f..a939a8d27 100644 --- a/test/core-test/low_rank_dynamics.jl +++ b/test/core-test/low_rank_dynamics.jl @@ -1,4 +1,6 @@ -@testset "Low Rank Dynamics" begin +@testitem "Low Rank Dynamics" begin + using LinearAlgebra + # Define lattice Nx, Ny = 2, 3 latt = Lattice(Nx = Nx, Ny = Ny) @@ -14,12 +16,12 @@ i = 1 for j in 1:N_modes - i += 1 + global i += 1 i <= M && (ϕ[i] = multisite_operator(latt, j => sigmap()) * ϕ[1]) end for k in 1:(N_modes-1) for l in (k+1):N_modes - i += 1 + global i += 1 i <= M && (ϕ[i] = multisite_operator(latt, k => sigmap(), l => sigmap()) * ϕ[1]) end end diff --git a/test/core-test/negativity_and_partial_transpose.jl b/test/core-test/negativity_and_partial_transpose.jl index 475cd343e..44ad9deb6 100644 --- a/test/core-test/negativity_and_partial_transpose.jl +++ b/test/core-test/negativity_and_partial_transpose.jl @@ -1,4 +1,4 @@ -@testset "Negativity and Partial Transpose" verbose = true begin +@testitem "Negativity and Partial Transpose" begin @testset "negativity" begin rho = (1 / 40) * Qobj( [ diff --git a/test/core-test/progress_bar.jl b/test/core-test/progress_bar.jl index 274f5d57d..2792a6e0a 100644 --- a/test/core-test/progress_bar.jl +++ b/test/core-test/progress_bar.jl @@ -1,4 +1,4 @@ -@testset "Progress Bar" begin +@testitem "Progress Bar" begin bar_width = 30 strLength = 67 + bar_width # including "\r" in the beginning of the string prog = ProgressBar(bar_width, enable = true, bar_width = bar_width, interval = 0.2) diff --git a/test/core-test/quantum_objects.jl b/test/core-test/quantum_objects.jl index c01eeca33..81afc501b 100644 --- a/test/core-test/quantum_objects.jl +++ b/test/core-test/quantum_objects.jl @@ -1,4 +1,8 @@ -@testset "Quantum Objects" verbose = true begin +@testitem "Quantum Objects" begin + using LinearAlgebra + using SparseArrays + using StaticArraysCore + # ArgumentError: type is incompatible with vector or matrix @testset "ArgumentError" begin a = rand(ComplexF64, 2) diff --git a/test/core-test/quantum_objects_evo.jl b/test/core-test/quantum_objects_evo.jl index 14e5a3270..a417f52ac 100644 --- a/test/core-test/quantum_objects_evo.jl +++ b/test/core-test/quantum_objects_evo.jl @@ -1,4 +1,9 @@ -@testset "Quantum Objects Evolution" verbose = true begin +@testitem "Quantum Objects Evolution" begin + using LinearAlgebra + using SparseArrays + using StaticArraysCore + using SciMLOperators + # DomainError: incompatible between size of array and type @testset "Thrown Errors" begin a = MatrixOperator(rand(ComplexF64, 3, 2)) diff --git a/test/core-test/states_and_operators.jl b/test/core-test/states_and_operators.jl index 1fd77976a..6d0aef108 100644 --- a/test/core-test/states_and_operators.jl +++ b/test/core-test/states_and_operators.jl @@ -1,4 +1,8 @@ -@testset "States and Operators" verbose = true begin +@testitem "States and Operators" begin + import QuantumToolbox: position, momentum + using LinearAlgebra + using SparseArrays + @testset "zero state" begin v1 = zero_ket(4) v2 = zero_ket((2, 2)) diff --git a/test/core-test/steady_state.jl b/test/core-test/steady_state.jl index 9981d5abb..d78c93c33 100644 --- a/test/core-test/steady_state.jl +++ b/test/core-test/steady_state.jl @@ -1,4 +1,4 @@ -@testset "Steady State" begin +@testitem "Steady State" begin N = 10 a = destroy(N) a_d = a' diff --git a/test/core-test/time_evolution.jl b/test/core-test/time_evolution.jl index 5abb7d18d..a67c97706 100644 --- a/test/core-test/time_evolution.jl +++ b/test/core-test/time_evolution.jl @@ -1,4 +1,7 @@ -@testset "Time Evolution and Partial Trace" verbose = true begin +@testitem "Time Evolution and Partial Trace" begin + using Random + using SciMLOperators + # Global definition of the system N = 10 a = kron(destroy(N), qeye(2)) diff --git a/test/core-test/utilities.jl b/test/core-test/utilities.jl index 3b5f6173c..c14a023f4 100644 --- a/test/core-test/utilities.jl +++ b/test/core-test/utilities.jl @@ -1,4 +1,4 @@ -@testset "Utilities" verbose = true begin +@testitem "Utilities" begin @testset "n_thermal" begin ω1 = rand(Float64) ω2 = rand(Float64) diff --git a/test/core-test/wigner.jl b/test/core-test/wigner.jl index e48b53497..58e292781 100644 --- a/test/core-test/wigner.jl +++ b/test/core-test/wigner.jl @@ -1,4 +1,4 @@ -@testset "Wigner" begin +@testitem "Wigner" begin α = 0.5 + 0.8im ψ = coherent(30, α) ρ = to_sparse(ket2dm(ψ), 1e-6) diff --git a/test/ext-test/cpu/autodiff/Project.toml b/test/ext-test/cpu/autodiff/Project.toml index 314afe38b..e07db5ee7 100644 --- a/test/ext-test/cpu/autodiff/Project.toml +++ b/test/ext-test/cpu/autodiff/Project.toml @@ -2,4 +2,4 @@ Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" QuantumToolbox = "6c2fb7c5-b903-41d2-bc5e-5a7c320b9fab" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" -Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" +Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" \ No newline at end of file diff --git a/test/ext-test/cpu/autodiff/zygote.jl b/test/ext-test/cpu/autodiff/zygote.jl index 824da7a12..bf2775a05 100644 --- a/test/ext-test/cpu/autodiff/zygote.jl +++ b/test/ext-test/cpu/autodiff/zygote.jl @@ -1,4 +1,4 @@ -@testset "Zygote.jl Autodiff" verbose=true begin +@testset "Zygote Extension" verbose=true begin @testset "sesolve" begin coef_Ω(p, t) = p[1] diff --git a/test/runtests.jl b/test/runtests.jl index 7407d82fb..b650417cf 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,49 +1,28 @@ using Test +using TestItemRunner using Pkg -# Importing only the necessary functions to keep track the re-export of the functions -import LinearAlgebra: Diagonal, I, mul!, triu, tril, triu!, tril! -import SparseArrays: sparse, sprand, spzeros, spdiagm, nnz, SparseVector, SparseMatrixCSC, AbstractSparseMatrix -import StaticArraysCore: SVector +const GROUP_LIST = String["All", "Core", "Code-Quality", "AutoDiff_Ext", "Makie_Ext", "CUDA_Ext"] const GROUP = get(ENV, "GROUP", "All") +(GROUP in GROUP_LIST) || throw(ArgumentError("Unknown GROUP = $GROUP")) -const testdir = dirname(@__FILE__) - -# Put core tests in alphabetical order -core_tests = [ - "block_diagonal_form.jl", - "correlations_and_spectrum.jl", - "dynamical_fock_dimension_mesolve.jl", - "dynamical-shifted-fock.jl", - "eigenvalues_and_operators.jl", - "entropy_and_metric.jl", - "generalized_master_equation.jl", - "low_rank_dynamics.jl", - "negativity_and_partial_transpose.jl", - "progress_bar.jl", - "quantum_objects.jl", - "quantum_objects_evo.jl", - "states_and_operators.jl", - "steady_state.jl", - "time_evolution.jl", - "utilities.jl", - "wigner.jl", -] - +# Core tests if (GROUP == "All") || (GROUP == "Core") - using QuantumToolbox - import QuantumToolbox: position, momentum - import Random: MersenneTwister - import SciMLOperators: MatrixOperator, NullOperator, IdentityOperator + import QuantumToolbox QuantumToolbox.about() - for test in core_tests - include(joinpath(testdir, "core-test", test)) - end + println("\nStart running Core tests...\n") + @run_package_tests verbose=true end +######################################################################## +# Use traditional Test.jl instead of TestItemRunner.jl for other tests # +######################################################################## + +const testdir = dirname(@__FILE__) + if (GROUP == "All") || (GROUP == "Code-Quality") Pkg.activate("core-test/code-quality") Pkg.develop(PackageSpec(path = dirname(@__DIR__))) @@ -52,6 +31,8 @@ if (GROUP == "All") || (GROUP == "Code-Quality") using QuantumToolbox using Aqua, JET + (GROUP == "Code-Quality") && QuantumToolbox.about() # print version info. for code quality CI in GitHub + include(joinpath(testdir, "core-test", "code-quality", "code_quality.jl")) end @@ -65,6 +46,8 @@ if (GROUP == "AutoDiff_Ext") using Enzyme using SciMLSensitivity + QuantumToolbox.about() + include(joinpath(testdir, "ext-test", "cpu", "autodiff", "zygote.jl")) end @@ -86,6 +69,8 @@ if (GROUP == "CUDA_Ext") Pkg.instantiate() using QuantumToolbox + import LinearAlgebra: Diagonal + import StaticArraysCore: SVector using CUDA using CUDA.CUSPARSE # CUDA.allowscalar(false) # This is already set in the extension script