|
| 1 | +module QuantumToolboxMetalExt |
| 2 | + |
| 3 | +using QuantumToolbox |
| 4 | +import Metal: mtl, MtlArray |
| 5 | + |
| 6 | +@doc raw""" |
| 7 | + MtlArray(A::QuantumObject) |
| 8 | +If `A.data` is an arbitrary array, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `Metal.MtlArray` for gpu calculations. |
| 9 | +Note that this function will always change element type into `32`-bit (`Int32`, `Float32`, and `ComplexF32`). |
| 10 | +""" |
| 11 | +MtlArray(A::QuantumObject{<:AbstractArray{T}}) where {T<:Number} = QuantumObject(MtlArray(A.data), A.type, A.dims) |
| 12 | +MtlArray(A::QuantumObject{<:AbstractArray{T}}) where {T<:Int64} = QuantumObject(MtlArray{Int32}(A.data), A.type, A.dims) |
| 13 | +MtlArray(A::QuantumObject{<:AbstractArray{T}}) where {T<:Float64} = |
| 14 | + QuantumObject(MtlArray{Float32}(A.data), A.type, A.dims) |
| 15 | +MtlArray(A::QuantumObject{<:AbstractArray{T}}) where {T<:ComplexF64} = |
| 16 | + QuantumObject(MtlArray{ComplexF32}(A.data), A.type, A.dims) |
| 17 | + |
| 18 | +@doc raw""" |
| 19 | + MtlArray{T}(A::QuantumObject) |
| 20 | +If `A.data` is an arbitrary array, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `Metal.MtlArray` with element type `T` for gpu calculations. |
| 21 | +""" |
| 22 | +MtlArray{T}(A::QuantumObject{<:AbstractArray{Tq}}) where {T,Tq<:Number} = |
| 23 | + QuantumObject(MtlArray{T}(A.data), A.type, A.dims) |
| 24 | + |
| 25 | +@doc raw""" |
| 26 | + mtl(A::QuantumObject) |
| 27 | +Return a new [`QuantumObject`](@ref) where `A.data` is in the type of `Metal` arrays for gpu calculations. |
| 28 | +Note that this function will always change element type into `32`-bit (`Int32`, `Float32`, and `ComplexF32`). |
| 29 | +""" |
| 30 | +mtl(A::QuantumObject{<:AbstractArray{T}}) where {T<:Int64} = QuantumObject(MtlArray{Int32}(A.data), A.type, A.dims) |
| 31 | +mtl(A::QuantumObject{<:AbstractArray{T}}) where {T<:Float64} = QuantumObject(MtlArray{Float32}(A.data), A.type, A.dims) |
| 32 | +mtl(A::QuantumObject{<:AbstractArray{T}}) where {T<:ComplexF64} = |
| 33 | + QuantumObject(MtlArray{ComplexF32}(A.data), A.type, A.dims) |
| 34 | + |
| 35 | +## TODO: Remove the following part if Metal.jl support `sparse` |
| 36 | +import LinearAlgebra: Transpose, Adjoint |
| 37 | +import QuantumToolbox: _spre, _spost, _sprepost |
| 38 | +_spre(A::MtlArray, Id::AbstractMatrix) = kron(Id, A) |
| 39 | +_spre(A::Tranpose{T,<:MtlArray}, Id::AbstractMatrix) where {T<:Number} = kron(Id, A) |
| 40 | +_spre(A::Adjoint{T,<:MtlArray}, Id::AbstractMatrix) where {T<:Number} = kron(Id, A) |
| 41 | +_spost(B::MtlArray, Id::AbstractMatrix) = kron(transpose(B), Id) |
| 42 | +_spost(B::Tranpose{T,<:MtlArray}, Id::AbstractMatrix) where {T<:Number} = kron(transpose(B), Id) |
| 43 | +_spost(B::Adjoint{T,<:MtlArray}, Id::AbstractMatrix) where {T<:Number} = kron(transpose(B), Id) |
| 44 | +_sprepost(A::MtlArray, B::MtlArray) = kron(transpose(B), A) |
| 45 | +_sprepost(A::MtlArray, B::Tranpose{T,<:MtlArray}) where {T<:Number} = kron(transpose(B), A) |
| 46 | +_sprepost(A::MtlArray, B::Adjoint{T,<:MtlArray}) where {T<:Number} = kron(transpose(B), A) |
| 47 | +_sprepost(A::Tranpose{T,<:MtlArray}, B::MtlArray) where {T<:Number} = kron(transpose(B), A) |
| 48 | +_sprepost(A::Tranpose{T1,<:MtlArray}, B::Tranpose{T2,<:MtlArray}) where {T1<:Number,T2<:Number} = kron(transpose(B), A) |
| 49 | +_sprepost(A::Tranpose{T1,<:MtlArray}, B::Adjoint{T2,<:MtlArray}) where {T1<:Number,T2<:Number} = kron(transpose(B), A) |
| 50 | +_sprepost(A::Adjoint{T,<:MtlArray}, B::MtlArray) where {T<:Number} = kron(transpose(B), A) |
| 51 | +_sprepost(A::Adjoint{T1,<:MtlArray}, B::Tranpose{T2,<:MtlArray}) where {T1<:Number,T2<:Number} = kron(transpose(B), A) |
| 52 | +_sprepost(A::Adjoint{T1,<:MtlArray}, B::Adjoint{T2,<:MtlArray}) where {T1<:Number,T2<:Number} = kron(transpose(B), A) |
| 53 | + |
| 54 | +end |
0 commit comments