• 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

62.5
/src/channels/conversions.jl
1
################################################################################
2
# conversions functions
3
################################################################################
4

5
for qop in (:SuperOperator, :DynamicalMatrix, :Stinespring, :UnitaryChannel,
6
            :PostSelectionMeasurement)
7
    @eval convert(::Type{<:AbstractMatrix{<:Number}}, Φ::$qop) = Φ.matrix
×
8
end
9

10
"""
11
$(SIGNATURES)
12
- ?: type.
13
- `Φ`: list of Kraus operators.
14

15
Transforms list of Kraus operators into super-operator matrix.
16
"""
17
function Base.convert(::Type{SuperOperator{T1}}, Φ::KrausOperators{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
20✔
18
    m = sum(k⊗(conj.(k)) for k in Φ.matrices)
20✔
19
    SuperOperator{T1}(convert(T1,m), Φ.idim, Φ.odim)
20✔
20
end
21

22
"""
23
$(SIGNATURES)
24
- ?: type.
25
- `Φ`: list of Kraus operators.
26

27
Transforms list of Kraus operators into Stinespring representation of quantum channel.
28
"""
29
function Base.convert(::Type{Stinespring{T1}}, Φ::KrausOperators{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
10✔
30
    ko = orthogonalize(Φ)
10✔
31
    # TODO: improvement: transform to stacking
32
    m = sum(k ⊗ ket(i, ko.idim*ko.odim) for (i, k) in enumerate(ko.matrices))
10✔
33
    Stinespring{T1}(convert(T1,m), ko.idim, ko.odim)
10✔
34
end
35

36
"""
37
$(SIGNATURES)
38
- ?: type.
39
- `Φ`: list of Kraus operators.
40

41
Transforms list of Kraus operators into dynamical matrix.
42
"""
43
function Base.convert(::Type{DynamicalMatrix{T1}}, Φ::KrausOperators{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
27✔
44
    m = sum(res(k) * res(k)' for k in Φ.matrices)
27✔
45
    DynamicalMatrix{T1}(convert(T1,m), Φ.idim, Φ.odim)
27✔
46
end
47

48
"""
49
$(SIGNATURES)
50
- ?: type.
51
- `Φ`: super-operator matrix.
52

53
Transforms super-operator matrix into list of Kraus operators.
54
"""
55
function Base.convert(::Type{KrausOperators{T1}}, Φ::SuperOperator{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
6✔
56
    convert(KrausOperators{T1}, convert(DynamicalMatrix{T2}, Φ))
6✔
57
end
58

59
"""
60
$(SIGNATURES)
61
- ?: type.
62
- `Φ`: super-operator matrix.
63

64
Transforms super-operator matrix into dynamical matrix.
65
"""
66
function Base.convert(::Type{DynamicalMatrix{T1}}, Φ::SuperOperator{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
9✔
67
    m = reshuffle(Φ.matrix, [Φ.odim Φ.odim; Φ.idim Φ.idim])
9✔
68
    DynamicalMatrix{T1}(convert(T1,m), Φ.idim, Φ.odim)
9✔
69
end
70

71
"""
72
$(SIGNATURES)
73
- ?: type.
74
- `Φ`: super-operator matrix.
75

76
Transforms super-operator matrix into Stinespring representation of quantum channel.
77
"""
78
function Base.convert(::Type{Stinespring{T1}}, Φ::SuperOperator{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
3✔
79
    convert(Stinespring{T1}, convert(KrausOperators{T1}, Φ))
3✔
80
end
81

82
"""
83
$(SIGNATURES)
84
- ?: type.
85
- `Φ`: dynamical matrix.
86

87
Transforms dynamical matrix into list of Kraus operators.
88
"""
89
function Base.convert(::Type{KrausOperators{T1}}, Φ::DynamicalMatrix{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
22✔
90
    isnumbernotint(eltype(T1)) ? () : throw(ArgumentError("Kraus operators element type must be subtype of Real or Complex"))
22✔
91

92
    d = copy(Φ.matrix)
22✔
93
    for i=1:size(d, 1)
44✔
94
        d[i, i] = real(d[i, i])
104✔
95
    end
186✔
96
    F = eigen(Hermitian(d))
22✔
97

98
    v = T1[]
22✔
99
    for i in 1:length(F.values)
44✔
100
        if F.values[i] >= 0.0
104✔
101
            push!(v, sqrt(F.values[i]) * unres(F.vectors[:,i], Φ.idim))
616✔
102
        end
103
    end
186✔
104
    KrausOperators{T1}(v, Φ.idim, Φ.odim)
22✔
105
end
106

107
"""
108
$(SIGNATURES)
109
- ?: type.
110
- `Φ`: dynamical matrix.
111

112
Transforms dynamical matrix into Stinespring representation of quantum channel.
113
"""
114
function Base.convert(::Type{Stinespring{T1}}, Φ::DynamicalMatrix{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
3✔
115
    convert(Stinespring{T1}, convert(KrausOperators{T1}, Φ))
3✔
116
end
117

118
"""
119
$(SIGNATURES)
120
- ?: type.
121
- `Φ`: dynamical matrix.
122

123
Transforms dynamical matrix into super-operator matrix.
124
"""
125
function Base.convert(::Type{SuperOperator{T1}}, Φ::DynamicalMatrix{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
3✔
126
    m = reshuffle(Φ.matrix, [Φ.odim Φ.idim; Φ.odim Φ.idim])
3✔
127
    SuperOperator{T1}(convert(T1,m), Φ.idim, Φ.odim)
3✔
128
end
129

130
function Base.convert(::Type{KrausOperators{T1}}, Φ::UnitaryChannel{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
×
131
    KrausOperators{T1}(T1[convert(T1,Φ.matrix)], Φ.idim, Φ.odim)
×
132
end
133

134
function Base.convert(::Type{KrausOperators{T1}}, Φ::IdentityChannel{T2}) where {T1<:AbstractMatrix{N1}, T2<:AbstractMatrix{N2}} where {N1<:Number, N2<:Number}
×
135
    N = promote_type(N1, N2)
×
136
    # KrausOperators{T1}(T1[eye(N, Φ.idim)], Φ.idim, Φ.odim)
137
    KrausOperators{T1}(T1[Matrix{N}(I, Φ.idim, Φ.idim)], Φ.idim, Φ.odim)
×
138
end
139

140
function Base.convert(::Type{KrausOperators{T1}}, Φ::POVMMeasurement{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
×
141
    # TODO : Verify!
142
    # v = T1[ket(i-1, Φ.odim)*bra(j-1, Φ.idim)*sqrt(p) for (i, p) in enumerate(Φ.matrices) for j in 1:Φ.idim]
143
    isnumbernotint(eltype(T1)) ? () : throw(ArgumentError("Kraus operators element type must be subtype of Real or Complex"))
×
144
    v = T1[]
×
145
    for (i, p) in enumerate(Φ.matrices)
×
146
        sqrtp = sqrt(p)
×
147
        k = ket(i, Φ.odim)*sum(bra(j, Φ.idim)*sqrtp for j in 1:Φ.idim)
×
148
        push!(v, convert(T1, k))
×
149
    end
×
150
    KrausOperators{T1}(v, Φ.idim, Φ.odim)
×
151
end
152

153
function Base.convert(::Type{KrausOperators{T1}}, Φ::PostSelectionMeasurement{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
×
154
    m = Φ.matrix
×
155
    v = T1[convert(T1, m)]
×
156
    KrausOperators{T1}(v, Φ.idim, Φ.odim)
×
157
end
158

159
for chout in (:SuperOperator, :DynamicalMatrix, :Stinespring)
160
    for chin in (:UnitaryChannel, :IdentityChannel, :POVMMeasurement, :PostSelectionMeasurement)
161
        @eval begin
162
            function Base.convert(::Type{$chout{T1}}, Φ::$chin{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
×
163
                convert($chout{T1}, convert(KrausOperators{T1}, Φ))
×
164
            end
165
        end
166
    end
167
end
168

169

170
# function Base.convert(::Type{KrausOperators{T1}}, Φ::SuperOperator{T2}) where {T1<:AbstractMatrix{<:Number}, T2<:AbstractMatrix{<:Number}}
171
#     convert(KrausOperators{T1}, convert(DynamicalMatrix{T2}, Φ))
172
# 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