Skip to content

Commit

Permalink
fix #59 (#60)
Browse files Browse the repository at this point in the history
* fix #59
  • Loading branch information
GiggleLiu authored Jul 29, 2019
1 parent 8d0ae48 commit 9000359
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/abstract_block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ abstract type AbstractBlock{N} end
Apply a block (of quantum circuit) to a quantum register.
"""
@interface function apply!(r::AbstractRegister, b::AbstractBlock)
@interface function apply!(r::AbstractRegister{B, T}, b::AbstractBlock) where {B, T}
_check_size(r, b)
r.state .= mat(b) * r.state
r.state .= mat(T, b) * r.state
return r
end

Expand Down
18 changes: 18 additions & 0 deletions src/primitive/general_matrix_gate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Create a [`GeneralMatrixBlock`](@ref) with a matrix `m`.
julia> matblock(ComplexF64[0 1;1 0])
matblock(...)
```
!!!warn
Instead of converting it to the default data type `ComplexF64`,
this will return its contained matrix when calling `mat`.
"""
matblock(m::AbstractMatrix) = GeneralMatrixBlock(m)

Expand All @@ -43,6 +48,19 @@ Create a [`GeneralMatrixBlock`](@ref) with a matrix `m`.
"""
matblock(m::AbstractBlock) = GeneralMatrixBlock(mat(m))

"""
mat(A::GeneralMatrixBlock)
Return the matrix of general matrix block.
!!!warn
Instead of converting it to the default data type `ComplexF64`,
this will return its contained matrix.
"""
mat(A::GeneralMatrixBlock) = A.mat

function mat(::Type{T}, A::GeneralMatrixBlock) where T
if eltype(A.mat) == T
return A.mat
Expand Down
4 changes: 4 additions & 0 deletions test/primitive/general_matrix_gate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ reg = rand_state(2)

a = rand_unitary(2)
@test mat(matblock(a))' == mat(matblock(a)')

# fallback test #59, return a correct matrix type for matblock
a = rand_unitary(2) .|> ComplexF32
@test eltype(mat(matblock(a))) == ComplexF32

0 comments on commit 9000359

Please sign in to comment.