Skip to content

Commit 1672e2f

Browse files
committed
Move current basis checking to new file deprecated.jl
This is in anticipation of new basis checking interface and eventual future deprecation of current basis checking
1 parent 1b31070 commit 1672e2f

File tree

3 files changed

+58
-92
lines changed

3 files changed

+58
-92
lines changed

src/bases.jl

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,6 @@ CompositeBasis(bases::Vector) = CompositeBasis((bases...,))
4545

4646
Base.:(==)(b1::T, b2::T) where T<:CompositeBasis = equal_shape(b1.shape, b2.shape)
4747

48-
"""
49-
equal_shape(a, b)
50-
51-
Check if two shape vectors are the same.
52-
"""
53-
function equal_shape(a, b)
54-
if a === b
55-
return true
56-
end
57-
if length(a) != length(b)
58-
return false
59-
end
60-
for i=1:length(a)
61-
if a[i]!=b[i]
62-
return false
63-
end
64-
end
65-
return true
66-
end
67-
6848
##
6949
# Common bases
7050
##

src/deprecated.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,61 @@ function equal_bases(a, b)
1212
end
1313

1414
Base.@deprecate PauliBasis(num_qubits) NQubitBasis(num_qubits) false
15+
16+
function equal_shape(a, b)
17+
if a === b
18+
return true
19+
end
20+
if length(a) != length(b)
21+
return false
22+
end
23+
for i=1:length(a)
24+
if a[i]!=b[i]
25+
return false
26+
end
27+
end
28+
return true
29+
end
30+
31+
macro samebases(ex)
32+
return quote
33+
BASES_CHECK.x = false
34+
local val = $(esc(ex))
35+
BASES_CHECK.x = true
36+
val
37+
end
38+
end
39+
40+
function check_samebases(b1, b2)
41+
if BASES_CHECK[] && !samebases(b1, b2)
42+
throw(IncompatibleBases())
43+
end
44+
end
45+
46+
function check_multiplicable(b1, b2)
47+
if BASES_CHECK[] && !multiplicable(b1, b2)
48+
throw(IncompatibleBases())
49+
end
50+
end
51+
52+
samebases(b1::Basis, b2::Basis) = b1==b2
53+
samebases(b1::Tuple{Basis, Basis}, b2::Tuple{Basis, Basis}) = b1==b2 # for checking superoperators
54+
samebases(a::AbstractOperator) = samebases(a.basis_l, a.basis_r)::Bool # FIXME issue #12
55+
samebases(a::AbstractOperator, b::AbstractOperator) = samebases(a.basis_l, b.basis_l)::Bool && samebases(a.basis_r, b.basis_r)::Bool # FIXME issue #12
56+
check_samebases(a::Union{AbstractOperator, AbstractSuperOperator}) = check_samebases(a.basis_l, a.basis_r) # FIXME issue #12
57+
58+
multiplicable(b1::Basis, b2::Basis) = b1==b2
59+
60+
function multiplicable(b1::CompositeBasis, b2::CompositeBasis)
61+
if !equal_shape(b1.shape,b2.shape)
62+
return false
63+
end
64+
for i=1:length(b1.shape)
65+
if !multiplicable(b1.bases[i], b2.bases[i])
66+
return false
67+
end
68+
end
69+
return true
70+
end
71+
72+
multiplicable(a::AbstractOperator, b::AbstractOperator) = multiplicable(a.basis_r, b.basis_l) # FIXME issue #12

src/linalg.jl

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,6 @@
44

55
const BASES_CHECK = Ref(true)
66

7-
"""
8-
@samebases
9-
10-
Macro to skip checks for same bases. Useful for `*`, `expect` and similar
11-
functions.
12-
"""
13-
macro samebases(ex)
14-
return quote
15-
BASES_CHECK.x = false
16-
local val = $(esc(ex))
17-
BASES_CHECK.x = true
18-
val
19-
end
20-
end
21-
22-
"""
23-
samebases(a, b)
24-
25-
Test if two objects have the same bases.
26-
"""
27-
samebases(b1::Basis, b2::Basis) = b1==b2
28-
samebases(b1::Tuple{Basis, Basis}, b2::Tuple{Basis, Basis}) = b1==b2 # for checking superoperators
29-
30-
"""
31-
check_samebases(a, b)
32-
33-
Throw an [`IncompatibleBases`](@ref) error if the objects don't have
34-
the same bases.
35-
"""
36-
function check_samebases(b1, b2)
37-
if BASES_CHECK[] && !samebases(b1, b2)
38-
throw(IncompatibleBases())
39-
end
40-
end
41-
42-
43-
"""
44-
multiplicable(a, b)
45-
46-
Check if two objects are multiplicable.
47-
"""
48-
multiplicable(b1::Basis, b2::Basis) = b1==b2
49-
50-
function multiplicable(b1::CompositeBasis, b2::CompositeBasis)
51-
if !equal_shape(b1.shape,b2.shape)
52-
return false
53-
end
54-
for i=1:length(b1.shape)
55-
if !multiplicable(b1.bases[i], b2.bases[i])
56-
return false
57-
end
58-
end
59-
return true
60-
end
61-
62-
"""
63-
check_multiplicable(a, b)
64-
65-
Throw an [`IncompatibleBases`](@ref) error if the objects are
66-
not multiplicable.
67-
"""
68-
function check_multiplicable(b1, b2)
69-
if BASES_CHECK[] && !multiplicable(b1, b2)
70-
throw(IncompatibleBases())
71-
end
72-
end
73-
74-
samebases(a::AbstractOperator) = samebases(a.basis_l, a.basis_r)::Bool # FIXME issue #12
75-
samebases(a::AbstractOperator, b::AbstractOperator) = samebases(a.basis_l, b.basis_l)::Bool && samebases(a.basis_r, b.basis_r)::Bool # FIXME issue #12
76-
check_samebases(a::Union{AbstractOperator, AbstractSuperOperator}) = check_samebases(a.basis_l, a.basis_r) # FIXME issue #12
77-
multiplicable(a::AbstractOperator, b::AbstractOperator) = multiplicable(a.basis_r, b.basis_l) # FIXME issue #12
78-
797
##
808
# tensor, reduce, ptrace
819
##

0 commit comments

Comments
 (0)