|
317 | 317 |
|
318 | 318 | @testset "projection" begin |
319 | 319 | N = 10 |
320 | | - a = fock(N, 3) |
321 | | - @test proj(a) ≈ proj(a') ≈ sparse(ket2dm(a)) ≈ projection(N, 3, 3) |
322 | | - @test isket(a') == false |
323 | | - @test isbra(a') == true |
324 | | - @test shape(a) == (N,) |
325 | | - @test shape(a') == (1, N) |
326 | | - @test norm(a) ≈ 1 |
327 | | - @test norm(a') ≈ 1 |
| 320 | + ψ = fock(N, 3) |
| 321 | + @test proj(ψ) ≈ proj(ψ') ≈ sparse(ket2dm(ψ)) ≈ projection(N, 3, 3) |
| 322 | + @test isket(ψ') == false |
| 323 | + @test isbra(ψ') == true |
| 324 | + @test shape(ψ) == (N,) |
| 325 | + @test shape(ψ') == (1, N) |
| 326 | + @test norm(ψ) ≈ 1 |
| 327 | + @test norm(ψ') ≈ 1 |
| 328 | + |
| 329 | + @testset "Type Inference (proj)" begin |
| 330 | + @inferred proj(ψ) |
| 331 | + @inferred proj(ψ') |
| 332 | + end |
328 | 333 | end |
329 | 334 |
|
330 | 335 | @testset "dot product" begin |
331 | 336 | ψ = rand_ket(10) |
332 | 337 | @test dot(ψ, ψ) ≈ ψ' * ψ ≈ norm(ψ) ≈ 1.0 |
| 338 | + |
| 339 | + @testset "Type Inference (dot)" begin |
| 340 | + @inferred dot(ψ, ψ) |
| 341 | + @inferred ψ' * ψ |
| 342 | + @inferred norm(ψ) |
| 343 | + end |
333 | 344 | end |
334 | 345 |
|
335 | 346 | @testset "normalization" begin |
336 | 347 | # normalize, normalize!, unit |
337 | 348 | N = 10 |
338 | | - a = Qobj(rand(ComplexF64, N)) |
339 | | - M = a * a' |
340 | | - @test (norm(a) ≈ 1) == false |
| 349 | + ψ = Qobj(rand(ComplexF64, N)) |
| 350 | + M = ψ * ψ' |
| 351 | + @test (norm(ψ) ≈ 1) == false |
341 | 352 | @test (norm(M) ≈ 1) == false |
342 | | - @test (norm(unit(a)) ≈ 1) == true |
| 353 | + @test (norm(unit(ψ)) ≈ 1) == true |
343 | 354 | @test (norm(unit(M)) ≈ 1) == true |
344 | | - @test (norm(a) ≈ 1) == false # Again, to be sure that it is still non-normalized |
| 355 | + @test (norm(ψ) ≈ 1) == false # Again, to be sure that it is still non-normalized |
345 | 356 | @test (norm(M) ≈ 1) == false # Again, to be sure that it is still non-normalized |
346 | | - normalize!(a) |
| 357 | + normalize!(ψ) |
347 | 358 | normalize!(M) |
348 | | - @test (norm(a) ≈ 1) == true |
| 359 | + @test (norm(ψ) ≈ 1) == true |
349 | 360 | @test (norm(M) ≈ 1) == true |
350 | | - @test M ≈ a * a' |
| 361 | + @test M ≈ ψ * ψ' |
351 | 362 | @test (unit(qeye(N)) ≈ (qeye(N) / N)) == true |
| 363 | + |
| 364 | + @testset "Type Inference (normalize)" begin |
| 365 | + ψ = Qobj(rand(ComplexF64, N)) |
| 366 | + M = ket2dm(ψ) |
| 367 | + @inferred normalize(ψ) |
| 368 | + @inferred normalize(M) |
| 369 | + end |
352 | 370 | end |
353 | 371 |
|
354 | 372 | @testset "expectation value" begin |
|
362 | 380 | ψ = fock(N, 3) |
363 | 381 | @test norm(ψ' * a) ≈ 2 |
364 | 382 | @test expect(a' * a, ψ' * a) ≈ 16 |
| 383 | + |
| 384 | + ρ = rand_dm(N) |
| 385 | + @test expect(a, ρ) ≈ tr(a * ρ) |
| 386 | + @test variance(a, ρ) ≈ tr(a^2 * ρ) - tr(a * ρ)^2 |
| 387 | + |
| 388 | + @testset "Type Inference (expect)" begin |
| 389 | + @inferred expect(a, ψ) |
| 390 | + @inferred expect(a, ψ') |
| 391 | + @inferred variance(a, ψ) |
| 392 | + @inferred variance(a, ψ') |
| 393 | + @inferred expect(a, ρ) |
| 394 | + @inferred variance(a, ρ) |
| 395 | + end |
365 | 396 | end |
366 | 397 |
|
367 | 398 | @testset "get coherence" begin |
|
371 | 402 | ρ = ket2dm(ψ) |
372 | 403 | α, δρ = get_coherence(ρ) |
373 | 404 | @test isapprox(abs(α), 3, atol = 1e-5) && abs2(δρ[1, 1]) > 0.999 |
| 405 | + |
| 406 | + @testset "Type Inference (get_coherence)" begin |
| 407 | + @inferred get_coherence(ψ) |
| 408 | + @inferred get_coherence(ρ) |
| 409 | + end |
374 | 410 | end |
375 | 411 |
|
376 | 412 | @testset "SVD and Schatten p-norm" begin |
|
382 | 418 | @test svdvals(vs)[1] ≈ √(vs' * vs) |
383 | 419 | @test norm(Md, 1) ≈ sum(sqrt, abs.(eigenenergies(Md' * Md))) atol = 1e-6 |
384 | 420 | @test norm(Ms, 1) ≈ sum(sqrt, abs.(eigenenergies(Ms' * Ms))) atol = 1e-6 |
| 421 | + |
| 422 | + @testset "Type Inference (SVD and Schatten p-norm)" begin |
| 423 | + @inferred svdvals(vd) |
| 424 | + @inferred svdvals(vs) |
| 425 | + @inferred norm(Md, 1) |
| 426 | + @inferred norm(Ms, 1) |
| 427 | + end |
385 | 428 | end |
386 | 429 |
|
387 | 430 | @testset "purity" begin |
|
395 | 438 | @test purity(ψd) ≈ norm(ψd)^2 ≈ 1.0 |
396 | 439 | @test purity(ρ1) ≈ 1.0 |
397 | 440 | @test (1.0 / N) <= purity(ρ2) <= 1.0 |
| 441 | + |
| 442 | + @testset "Type Inference (purity)" begin |
| 443 | + @inferred purity(ψ) |
| 444 | + @inferred purity(ψd) |
| 445 | + @inferred purity(ρ1) |
| 446 | + @inferred purity(ρ2) |
| 447 | + end |
398 | 448 | end |
399 | 449 |
|
400 | 450 | @testset "trace distance" begin |
|
407 | 457 | @test tracedist(ρz0, ψz1) ≈ 1.0 |
408 | 458 | @test tracedist(ψz1, ρz0) ≈ 1.0 |
409 | 459 | @test tracedist(ρz0, ρz1) ≈ 1.0 |
| 460 | + |
| 461 | + @testset "Type Inference (trace distance)" begin |
| 462 | + @inferred tracedist(ψz0, ψx0) |
| 463 | + @inferred tracedist(ρz0, ψz1) |
| 464 | + @inferred tracedist(ψz1, ρz0) |
| 465 | + @inferred tracedist(ρz0, ρz1) |
| 466 | + end |
410 | 467 | end |
411 | 468 |
|
412 | 469 | @testset "sqrt and fidelity" begin |
|
418 | 475 | @test sqrtm(M0) ≈ sqrtm(sparse_to_dense(M0)) |
419 | 476 | @test isapprox(fidelity(M0, M1), fidelity(ψ1, M0); atol = 1e-6) |
420 | 477 | @test isapprox(fidelity(ψ1, ψ2), fidelity(ket2dm(ψ1), ket2dm(ψ2)); atol = 1e-6) |
| 478 | + |
| 479 | + @testset "Type Inference (sqrt and fidelity)" begin |
| 480 | + @inferred sqrtm(M0) |
| 481 | + @inferred fidelity(M0, M1) |
| 482 | + @inferred fidelity(ψ1, M0) |
| 483 | + @inferred fidelity(ψ1, ψ2) |
| 484 | + end |
421 | 485 | end |
422 | 486 |
|
423 | 487 | @testset "log, exp, sinm, cosm" begin |
|
469 | 533 | ρ2 = dense_to_sparse(ρ1) |
470 | 534 | @test tidyup(ρ2, tol) != ρ2 |
471 | 535 | @test dense_to_sparse(tidyup(ρ1, tol)) == tidyup(ρ2, tol) |
| 536 | + |
| 537 | + @testset "Type Inference (tidyup)" begin |
| 538 | + @inferred tidyup(ψ1, tol) |
| 539 | + @inferred tidyup(ρ1, tol) |
| 540 | + @inferred tidyup(ψ2, tol) |
| 541 | + @inferred tidyup(ρ2, tol) |
| 542 | + end |
472 | 543 | end |
473 | 544 |
|
474 | 545 | @testset "ptrace" begin |
|
550 | 621 | @test_throws ArgumentError permute(bra_bdca, wrong_order2) |
551 | 622 | @test_throws ArgumentError permute(op_bdca, wrong_order1) |
552 | 623 | @test_throws ArgumentError permute(op_bdca, wrong_order2) |
| 624 | + |
| 625 | + @testset "Type Inference (permute)" begin |
| 626 | + @inferred permute(ket_bdca, (2, 4, 3, 1)) |
| 627 | + @inferred permute(bra_bdca, (2, 4, 3, 1)) |
| 628 | + @inferred permute(op_bdca, (2, 4, 3, 1)) |
| 629 | + end |
553 | 630 | end |
554 | 631 | end |
0 commit comments