Skip to content
This repository was archived by the owner on Dec 18, 2021. It is now read-only.

Commit f3849ef

Browse files
Roger-luoGiggleLiu
authored andcommitted
Add more test (#28)
* add doc test * add test for pauli_string; add doc test to ci * update ignore * rm build cache * add more test * update travis * update travis * add more doctests * fix dispatch for phase gate, add test * update travis * Update .travis.yml Co-Authored-By: Roger-luo <[email protected]> * add some docs * fix qcbm dispatch * fix counting * rm print * add tests for qcbm * update docs Project.toml * rm name * add a comment
1 parent 2368555 commit f3849ef

16 files changed

+304
-21
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
*.jl.cov
44
*.jl.*.cov
55
*.jl.mem
6+
7+
docs/build/
8+
docs/site/

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ matrix:
1313
fast_finish: true
1414
notifications:
1515
email: false
16+
jobs:
17+
include:
18+
- stage: "DocTest"
19+
julia: 1.1 # we only do doc test on latest stable release
20+
os: linux
21+
script:
22+
- julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
23+
- julia --project=docs/ --code-coverage=user docs/make.jl
1624
after_success:
1725
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
1826
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder())'

docs/Project.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[deps]
2+
BitBasis = "50ba71b6-fa0f-514d-ae9a-0916efc90dcf"
3+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
4+
YaoArrayRegister = "e600142f-9330-5003-8abb-0ebd767abc51"
5+
YaoBase = "a8f54c17-34bc-5a9d-b050-f522fe3f755f"
6+
YaoBlocks = "418bc28f-b43b-5e0b-a6e7-61bbc1a2c1df"
7+
Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"

docs/make.jl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Documenter
2+
using YaoArrayRegister, YaoBase, YaoBlocks
3+
4+
makedocs(
5+
modules = [YaoBlocks],
6+
doctest = true,
7+
clean = false,
8+
sitename = "YaoBlocks.jl",
9+
pages = ["index.md"]
10+
)

docs/src/index.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```@meta
2+
DocTestSetup = quote
3+
using Yao, YaoBase, YaoBlocks, YaoArrayRegister
4+
end
5+
```
6+
7+
```@autodocs
8+
Modules = [YaoBlocks]
9+
```

src/blocktools.jl

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ function parse_block(n::Int, x::AbstractBlock{N}) where N
77
return x
88
end
99

10+
parse_block(n::Int, x::Pair{Int, <:AbstractBlock{N}}) where N = x
11+
1012
"""
1113
prewalk(f, src::AbstractBlock)
1214

src/composite/concentrator.jl

+40
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,46 @@ end
2525
Create a [`Concentrator`](@ref) block with total number of current active qubits `n`,
2626
which concentrates given wire location together to `length(locs)` active qubits,
2727
and relax the concentration afterwards.
28+
29+
# Example
30+
31+
Concentrator is equivalent to [`put`](@ref) a block on given position mathematically, but more efficient
32+
and convenient for large blocks.
33+
34+
```jldoctest
35+
julia> r = rand_state(3)
36+
ArrayReg{1, Complex{Float64}, Array...}
37+
active qubits: 3/3
38+
39+
julia> apply!(copy(r), concentrate(X, 1)) ≈ apply!(copy(r), put(1=>X))
40+
true
41+
```
42+
43+
It works for in-contigious locations as well
44+
45+
```jldoctest
46+
julia> r = rand_state(4)
47+
ArrayReg{1, Complex{Float64}, Array...}
48+
active qubits: 4/4
49+
50+
julia> cc = concentrate(4, kron(X, Y), (1, 3))
51+
nqubits: 4, datatype: Complex{Float64}
52+
Concentrator: (1, 3)
53+
└─ kron
54+
├─ 1=>X gate
55+
└─ 2=>Y gate
56+
57+
julia> pp = chain(4, put(1=>X), put(3=>Y))
58+
nqubits: 4, datatype: Complex{Float64}
59+
chain
60+
├─ put on (1)
61+
│ └─ X gate
62+
└─ put on (3)
63+
└─ Y gate
64+
65+
julia> apply!(copy(r), cc) ≈ apply!(copy(r), pp)
66+
true
67+
```
2868
"""
2969
function concentrate(n::Int, block::AbstractBlock, locs)
3070
return Concentrator{n}(block, Tuple(locs))

src/composite/pauli_strings.jl

+34-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,41 @@ end
1010

1111
# NOTE: PauliString has a fixed size `N`, thus by default, it should use
1212
# SizedVector, or this block could be actually not correct.
13+
14+
"""
15+
PauliString(xs::PauliGate...)
16+
17+
Create a `PauliString` from some Pauli gates.
18+
19+
# Example
20+
21+
```jldoctest
22+
julia> PauliString(X, Y, Z)
23+
nqubits: 3, datatype: Complex{Float64}
24+
PauliString
25+
├─ X gate
26+
├─ Y gate
27+
└─ Z gate
28+
```
29+
"""
1330
PauliString(xs::PauliGate{T}...) where T = PauliString(SizedVector{length(xs), PauliGate{T}}(xs))
1431

32+
"""
33+
PauliString(list::Vector)
34+
35+
Create a `PauliString` from a list of Pauli gates.
36+
37+
# Example
38+
39+
```jldoctest
40+
julia> PauliString([X, Y, Z])
41+
nqubits: 3, datatype: Complex{Float64}
42+
PauliString
43+
├─ X gate
44+
├─ Y gate
45+
└─ Z gate
46+
```
47+
"""
1548
function PauliString(xs::Vector)
1649
T = datatype(first(xs))
1750
for each in xs
@@ -38,7 +71,7 @@ YaoBase.isunitary(::PauliString) = true
3871

3972
Base.copy(ps::PauliString) = PauliString(copy(ps.blocks))
4073
Base.getindex(ps::PauliString, x) = getindex(ps.blocks, x)
41-
Base.lastindex(ps::PauliString, x) = lastindex(ps.blocks, x)
74+
Base.lastindex(ps::PauliString) = lastindex(ps.blocks)
4275
Base.iterate(ps::PauliString) = iterate(ps.blocks)
4376
Base.iterate(ps::PauliString, st) = iterate(ps.blocks, st)
4477
Base.length(ps::PauliString) = length(ps.blocks)

src/composite/put_block.jl

+33
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ end
2121
2222
Create a [`PutBlock`](@ref) with total number of active qubits, and a pair of
2323
location and block to put on.
24+
25+
# Example
26+
27+
```jldoctest
28+
julia> put(4, 1=>X)
29+
nqubits: 4, datatype: Complex{Float64}
30+
put on (1)
31+
└─ X gate
32+
```
33+
34+
If you want to put a multi-qubit gate on specific locations, you need to write down all possible locations.
35+
36+
```jldoctest
37+
julia> put(4, (1, 3)=>kron(X, Y))
38+
nqubits: 4, datatype: Complex{Float64}
39+
put on (1, 3)
40+
└─ kron
41+
├─ 1=>X gate
42+
└─ 2=>Y gate
43+
```
44+
45+
The outter locations creates a scope which make it seems to be a contiguous two qubits for the block inside `PutBlock`.
46+
47+
!!! tips
48+
It is better to use [`concentrate`](@ref) instead of `put` for large blocks, since put will use the matrix of its contents
49+
directly instead of making use of what's in it. `put` is more efficient for small blocks.
2450
"""
2551
put(total::Int, pa::Pair{NTuple{M, Int}, <:AbstractBlock}) where M =
2652
PutBlock{total}(pa.second, pa.first)
@@ -31,6 +57,13 @@ put(total::Int, pa::Pair{<:Any, <:AbstractBlock}) = PutBlock{total}(pa.second, T
3157
put(pair) -> f(n)
3258
3359
Lazy curried version of [`put`](@ref).
60+
61+
# Example
62+
63+
```jldoctest
64+
julia> put(1=>X)
65+
(n -> put(n, 1 => X gate))
66+
```
3467
"""
3568
put(pa::Pair) = (n -> put(n, pa))
3669

src/composite/repeated.jl

+41
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,47 @@ end
2525
2626
Create a [`RepeatedBlock`](@ref) with total number of qubits `n` and the block
2727
to repeat on given location or on all the locations.
28+
29+
# Example
30+
31+
This will create a repeat block which puts 4 X gates on each location.
32+
33+
```jldoctest
34+
julia> repeat(4, X)
35+
nqubits: 4, datatype: Complex{Float64}
36+
repeat on (1, 2, 3, 4)
37+
└─ X gate
38+
```
39+
40+
You can also specify the location
41+
42+
```jldoctest
43+
julia> repeat(4, X, (1, 2))
44+
nqubits: 4, datatype: Complex{Float64}
45+
repeat on (1, 2)
46+
└─ X gate
47+
```
48+
49+
But repeat won't copy the gate, thus, if it is a gate with parameter, e.g a `phase(0.1)`, the parameter
50+
will change simultaneously.
51+
52+
```jldoctest
53+
julia> g = repeat(4, phase(0.1))
54+
nqubits: 4, datatype: Complex{Float64}
55+
repeat on (1, 2, 3, 4)
56+
└─ phase(0.1)
57+
58+
julia> g.content
59+
phase(0.1)
60+
61+
julia> g.content.theta = 0.2
62+
0.2
63+
64+
julia> g
65+
nqubits: 4, datatype: Complex{Float64}
66+
repeat on (1, 2, 3, 4)
67+
└─ phase(0.2)
68+
```
2869
"""
2970
Base.repeat(n::Int, x::AbstractBlock, locs::Int...) =
3071
repeat(n, x, locs)

src/primitive/const_gate_gen.jl

+25
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,28 @@ Base.adjoint(::SGate) = Sdag
4242
Base.adjoint(::SdagGate) = S
4343
Base.adjoint(::TGate) = Tdag
4444
Base.adjoint(::TdagGate) = T
45+
46+
# Docs
47+
"""
48+
X
49+
XGate{T} <: ConstantGate{1, T}
50+
51+
Pauli X gate. `X` is the instance of `XGate`.
52+
"""
53+
X, XGate
54+
55+
"""
56+
Y
57+
YGate{T} <: ConstantGate{1, T}
58+
59+
Pauli Y gate. `Y` is the instance of `YGate`.
60+
"""
61+
Y, YGate
62+
63+
"""
64+
Z
65+
ZGate{T} <: ConstantGate{1, T}
66+
67+
Pauli Z gate. `Z` is the instance of `YGate`.
68+
"""
69+
Z, ZGate

src/primitive/general_matrix_gate.jl

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ GeneralMatrixBlock(m::AbstractMatrix) = GeneralMatrixBlock{log2i.(size(m))...}(m
2626
matblock(m::AbstractMatrix)
2727
2828
Create a [`GeneralMatrixBlock`](@ref) with a matrix `m`.
29+
30+
# Example
31+
32+
```jldoctest
33+
34+
```
2935
"""
3036
matblock(m::AbstractMatrix) = GeneralMatrixBlock(m)
3137

test/composite/chain.jl

+51-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using Test, YaoBase, YaoBlocks, YaoArrayRegister
99
@test g.blocks == [kron(2, X, Y), kron(2, 1=>phase(0.1))]
1010
blks = [X, Y, Rx(0.3)]
1111
@test chsubblocks(g, blks) |> subblocks |> collect == blks
12+
@test chsubblocks(chain(X, Y, Z), X for _ in 1:3) |> subblocks |> collect == [X, X, X]
1213

1314
c1 = ChainBlock(put(5, 1=>X), put(5, 3=>Y))
1415
c2 = ChainBlock(put(5, 4=>X), put(5, 5=>Y))
@@ -22,18 +23,22 @@ using Test, YaoBase, YaoBlocks, YaoArrayRegister
2223
c = ChainBlock([X, Y])
2324
c[1] = put(1, 1=>X)
2425
@test c[1] == put(1, 1=>X)
25-
26-
list = []
27-
push!(list, X)
28-
@test chain(list) == chain(X)
29-
30-
@test chain(4, []) == chain(4)
3126
end
3227

3328
@testset "test chain" begin
3429
@test chain(kron(1=>X), control(2, 1=>X))(4) |> nqubits == 4
3530
@test chain(control(4, 2, 1=>X), kron(1=>X)) |> nqubits == 4
3631
@test chain(control(4, 2, 1=>X), kron(4, 1=>X)) |> nqubits == 4
32+
33+
list = []
34+
push!(list, X)
35+
@test chain(list) == chain(X)
36+
37+
@test chain(4, []) == chain(4)
38+
@test chain(X for _ in 1:3) == chain(X, X, X)
39+
@test chain(put(1=>X))(4) == chain(put(4, 1=>X))
40+
@test chain(put(1=>X), put(2=>X))(4) == chain(put(4, 1=>X), put(4, 2=>X))
41+
@test chain()(4) == chain(4)
3742
end
3843

3944
@testset "#15" begin
@@ -61,6 +66,46 @@ end
6166
r = rand_state(2)
6267
@test statevec(apply!(copy(r), g)) mat(g) * r.state
6368
apply!(copy(r), g)
69+
70+
@test chain(X, Y, Z, H)[2:4] == chain(Y, Z, H)
71+
72+
@testset "copy" begin
73+
g = chain(phase(0.1))
74+
g1 = copy(g)
75+
g[1] = X
76+
@test g1[1] == phase(0.1)
77+
end
78+
79+
@testset "similar" begin
80+
g = chain(X, Y, Z)
81+
@test similar(g) == chain(1)
82+
end
83+
84+
@testset "push!" begin
85+
g = chain(2, put(1=>X))
86+
push!(g, put(2=>X))
87+
@test g == chain(2, put(1=>X), put(2=>X))
88+
end
89+
90+
@testset "append!" begin
91+
g1 = chain(X, Y, Z)
92+
g2 = chain(Z, Z, Z)
93+
@test append!(g1, g2) == chain(X, Y, Z, Z, Z, Z) == g1
94+
end
95+
96+
@testset "prepend!" begin
97+
g1 = chain(X, Y, Z)
98+
g2 = chain(Z, Z, Z)
99+
@test prepend!(g1, g2) == chain(Z, Z, Z, X, Y, Z) == g1
100+
end
101+
102+
@testset "adjoint" begin
103+
@test adjoint(chain(X, Y, Z, H)) == chain(H, Z, Y, X)
104+
end
105+
106+
@testset "insert!" begin
107+
@test insert!(chain(X, Y, Z), 2, H) == chain(X, H, Y, Z)
108+
end
64109
end
65110

66111
@testset "test iteration" begin

test/composite/composite.jl

+8
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ end
3535
@testset "test pauli string" begin
3636
include("pauli_string.jl")
3737
end
38+
39+
@testset "test single block chsubblocks" begin
40+
@test chsubblocks(chain(X), Y) == chain(Y)
41+
@test chsubblocks(kron(X), Y) == kron(Y)
42+
@test chsubblocks(roll(X), Y) == roll(Y)
43+
@test chsubblocks(prod(X), Y) == prod(Y)
44+
@test chsubblocks(sum(X), Y) == sum(Y)
45+
end

0 commit comments

Comments
 (0)