• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iitis / QuantumInformation.jl / 4861454641

pending completion
4861454641

Pull #103

github

GitHub
Merge 817c8b246 into 7644f36f9
Pull Request #103: Bloch vector

6 of 6 new or added lines in 1 file covered. (100.0%)

546 of 723 relevant lines covered (75.52%)

64.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.54
/src/channels/constructors.jl
1
export AbstractQuantumOperation, KrausOperators, SuperOperator, DynamicalMatrix,
2
    Stinespring, UnitaryChannel, IdentityChannel, POVMMeasurement,
3
    PostSelectionMeasurement
4
    
5
################################################################################
6
# Channels definitions and constructors
7
################################################################################
8

9
abstract type AbstractQuantumOperation{T<:AbstractMatrix{<:Number}} end
10

11
"""
12
$(SIGNATURES)
13
- `T`: quantum channel map.
14

15
Representation of quantum channel by Kraus operators.
16
"""
17
struct KrausOperators{T<:AbstractMatrix{<:Number}} <: AbstractQuantumOperation{T}
18
    matrices::Vector{T}
19
    idim::Int
20
    odim::Int
21
    function KrausOperators{T1}(v::Vector{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
66✔
22
        sizes = [size(k) for k in v]
66✔
23
        for s in sizes[2:end]
67✔
24
            if s!=sizes[1]
297✔
25
                throw(ArgumentError("Kraus operators list contains matrices of different dimension"))
1✔
26
            end
27
        end
212✔
28
        odim, idim = sizes[1]
65✔
29
        new{T1}(map(T1, v), idim, odim)
65✔
30
    end
31
end
32

33
function KrausOperators{T1}(v::Vector{T2}, idim::Int, odim::Int) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
22✔
34
    all((odim, idim) == size(k) for k in v) ? () : throw(ArgumentError("Matrix size and operator dimensions mismatch"))
22✔
35
    KrausOperators{T1}(v)
22✔
36
end
37

38
length(Φ::KrausOperators) = length(Φ.matrices)
×
39

40
function orthogonalize(Φ::KrausOperators{T}) where {T<:AbstractMatrix{<:Number}}
10✔
41
    convert(KrausOperators{T}, convert(DynamicalMatrix{T}, Φ))
10✔
42
end
43

44
"""
45
$(SIGNATURES)
46
- `T`: quantum channel map.
47

48
Representation of quantum channel by super-operator.
49
"""
50
struct SuperOperator{T<:AbstractMatrix{<:Number}} <: AbstractQuantumOperation{T}
51
    matrix::T
52
    idim::Int
53
    odim::Int
54
    function SuperOperator{T1}(m::T2) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
24✔
55
        r, c = size(m)
24✔
56
        sr = isqrt(r)
24✔
57
        sc = isqrt(c)
24✔
58
        if r!=sr^2 || c!=sc^2
48✔
59
            throw(ArgumentError("Superoperator matrix has invalid dimensions"))
×
60
        end
61
        odim, idim = sr, sc
24✔
62
        new{T1}(convert(T1, m), idim, odim)
24✔
63
    end
64
end
65

66
function SuperOperator{T1}(m::T2, idim::Int, odim::Int) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
24✔
67
    (odim^2, idim^2) == size(m) ? () : throw(ArgumentError("Matrix size and operator dimensions mismatch"))
24✔
68
    SuperOperator{T1}(m)
24✔
69
end
70

71
"""
72
$(SIGNATURES)
73
- `channel`: quantum channel map.
74
- `idim`: square root of the [super-operator](https://en.wikipedia.org/wiki/Superoperator) matrix input dimension.
75
- `odim`: square root of the [super-operator](https://en.wikipedia.org/wiki/Superoperator) matrix output dimension.
76

77
Transforms quntum channel into super-operator matrix.
78
"""
79
function SuperOperator{T}(channel::Function, idim::Int, odim::Int) where T<:AbstractMatrix{<:Number}
1✔
80
    odim > 0 && idim > 0 ? () : throw(ArgumentError("Channel dimensions have to be nonnegative"))
1✔
81

82
    m = zeros(eltype(T), idim^2, odim^2)
16✔
83
    for (i, e) in enumerate(ElementaryBasisIterator{Matrix{Int}}(idim, odim))
2✔
84
        m[:, i] = res(channel(e))
4✔
85
    end
7✔
86
    SuperOperator(m, idim, odim)
1✔
87
end
88

89
"""
90
$(SIGNATURES)
91
- `T`: quantum channel map.
92

93
Representation of quantum channel by Dynamical matrix operators.
94
"""
95
struct DynamicalMatrix{T<:AbstractMatrix{<:Number}} <: AbstractQuantumOperation{T}
96
    matrix::T
97
    idim::Int
98
    odim::Int
99
    function DynamicalMatrix{T1}(m, idim, odim) where {T1<:AbstractMatrix{<:Number}}
49✔
100
        r, c = size(m)
49✔
101
        if r!=c || r!=idim*odim
97✔
102
            throw(ArgumentError("DynamicalMatrix matrix has invalid dimensions"))
1✔
103
        end
104
        new(convert(T1, m), idim, odim)
48✔
105
    end
106
end
107

108
"""
109
$(SIGNATURES)
110
- `T`: quantum channel map.
111

112
Stinespring representation of quantum channel.
113
"""
114
struct Stinespring{T<:AbstractMatrix{<:Number}} <: AbstractQuantumOperation{T}
115
    matrix::T
116
    idim::Int
117
    odim::Int
118
    function Stinespring{T1}(m, idim, odim) where {T1<:AbstractMatrix{<:Number}}
10✔
119
        r, c = size(m)
10✔
120
        if r!=idim * (odim^2) || c!=idim
20✔
121
            throw(ArgumentError("Stinespring matrix has invalid dimensions"))
×
122
        end
123
        new(T1(m), idim, odim)
10✔
124
    end
125
end
126

127
"""
128
$(SIGNATURES)
129
- `T`: quantum channel map.
130

131
Representation of unitary channel.
132
"""
133
struct UnitaryChannel{T<:AbstractMatrix{<:Number}} <: AbstractQuantumOperation{T}
134
    matrix::T
135
    idim::Int
136
    odim::Int
137
    function UnitaryChannel{T1}(m::T2) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
7✔
138
        odim, idim = size(m)
7✔
139
        idim == odim ? () : throw(ArgumentError("UnitaryChannel matrix has to be square"))
8✔
140
        new{T1}(convert(T1,m), idim, odim)
6✔
141
    end
142
end
143

144
function UnitaryChannel{T1}(m::T2, idim::Int, odim::Int) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
4✔
145
    (odim, idim) == size(m) ? () : throw(ArgumentError("Matrix size and operator dimensions mismatch"))
5✔
146
    UnitaryChannel{T1}(convert(T1,m))
3✔
147
end
148

149
"""
150
$(SIGNATURES)
151
- `T`: quantum channel map.
152

153
Representation of identity channel.
154
"""
155
struct IdentityChannel{T<:AbstractMatrix{<:Number}} <: AbstractQuantumOperation{T}
156
    idim::Int
157
    odim::Int
158
    function IdentityChannel{T}(dim::Int) where T<:AbstractMatrix{<:Number}
×
159
         new{T}(dim, dim)
×
160
    end
161
end
162

163
IdentityChannel(dim::Int) = IdentityChannel{Matrix{ComplexF64}}(dim)
×
164

165
################################################################################
166
# measurements
167
################################################################################
168
struct POVMMeasurement{T<:AbstractMatrix{<:Number}} <: AbstractQuantumOperation{T}
169
    matrices::Vector{T}
170
    idim::Int
171
    odim::Int
172
    function POVMMeasurement{T1}(v::Vector{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
6✔
173
        sizes = [size(p) for p in v]
6✔
174
        for s in sizes[2:end]
6✔
175
            if s!=sizes[1]
22✔
176
                throw(ArgumentError("POVM operators list contains matrices of different dimension"))
×
177
            end
178
        end
17✔
179
        idim = size(v[1], 1) 
6✔
180
        odim = length(v)
6✔
181

182
        new{T1}(map(T1, v), idim, odim)
6✔
183
    end
184
end
185

186
function POVMMeasurement{T1}(v::Vector{T2}, idim::Int, odim::Int) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
×
187
    all((idim, idim) == size(p) for p in v) ? () : throw(ArgumentError("POVMs must be square matrices of size equal to operator inupt dimension"))
×
188
    odim == length(v) ? () : throw(ArgumentError("Operator output dimension must match number of POVM operators"))
×
189
    POVMMeasurement{T1}(v)
×
190
end
191

192
struct PostSelectionMeasurement{T<:AbstractMatrix{<:Number}} <: AbstractQuantumOperation{T}
193
    matrix::T
194
    idim::Int
195
    odim::Int
196
    function PostSelectionMeasurement{T1}(m::T2) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
×
197
        odim, idim = size(m)
×
198
        new{T1}(convert(T1,m), idim, odim)
×
199
    end
200
end
201

202
function PostSelectionMeasurement{T1}(m::T2, idim::Int, odim::Int) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
×
203
    odim, idim == size(m) ? () : throw(ArgumentError("Matrix size and operator dimensions mismatch"))
×
204
    PostSelectionMeasurement{T1}(m)
×
205
end
206

207
################################################################################
208
# typeless constructors
209
################################################################################
210
for qop in (:SuperOperator, :UnitaryChannel, :PostSelectionMeasurement)
211
    @eval begin
212
        function $qop(m::T) where T<:AbstractMatrix{<:Number}
4✔
213
            $qop{T}(m)
4✔
214
        end
215

216
        function $qop(m::T, idim::Int, odim::Int) where T<:AbstractMatrix{<:Number}
3✔
217
            $qop{T}(m, idim, odim)
3✔
218
        end
219
    end
220
end
221

222
for qop in (:DynamicalMatrix, :Stinespring)
223
    @eval begin
224
        function $qop(m::T, idim::Int, odim::Int) where T<:AbstractMatrix{<:Number}
8✔
225
            $qop{T}(m, idim, odim)
8✔
226
        end
227
    end
228
end
229

230
for qop in (:KrausOperators, :POVMMeasurement)
231
    @eval begin
232
        function $qop(v::T) where T<:Vector{M} where M<:AbstractMatrix{<:Number}
47✔
233
            $qop{M}(v)
47✔
234
        end
235

236
        function $qop(v::T, idim::Int, odim::Int) where  T<:Vector{M} where M<:AbstractMatrix{<:Number}
×
237
            $qop{M}(v)
×
238
        end
239
    end
240
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc