From 900035922734bb6115cfa276b7a10096214f31f9 Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 29 Jul 2019 13:20:45 +0800 Subject: [PATCH] fix #59 (#60) * fix #59 --- src/abstract_block.jl | 4 ++-- src/primitive/general_matrix_gate.jl | 18 ++++++++++++++++++ test/primitive/general_matrix_gate.jl | 4 ++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/abstract_block.jl b/src/abstract_block.jl index 6e4640f4b..95323e188 100644 --- a/src/abstract_block.jl +++ b/src/abstract_block.jl @@ -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 diff --git a/src/primitive/general_matrix_gate.jl b/src/primitive/general_matrix_gate.jl index 1488c9ddf..2ac0fd268 100644 --- a/src/primitive/general_matrix_gate.jl +++ b/src/primitive/general_matrix_gate.jl @@ -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) @@ -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 diff --git a/test/primitive/general_matrix_gate.jl b/test/primitive/general_matrix_gate.jl index 03ef299a0..75bed26de 100644 --- a/test/primitive/general_matrix_gate.jl +++ b/test/primitive/general_matrix_gate.jl @@ -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