Skip to content

Commit 4e5b0fc

Browse files
committed
Implement basis checking api
1 parent 1629bf3 commit 4e5b0fc

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

src/QuantumInterface.jl

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,91 @@ function offset end
4444
# Standard methods
4545
##
4646

47+
"""
48+
multiplicable(a, b)
49+
50+
Check if any two subtypes of `StateVector`, `AbstractOperator`,
51+
or `AbstractSuperOperator` can be multiplied in the given order.
52+
53+
Spcefically this checks whether the right basis of a is equal
54+
to the left basis of b
55+
"""
56+
function multiplicable end
57+
58+
"""
59+
check_multiplicable(a, b)
60+
61+
Throw an [`IncompatibleBases`](@ref) error if the objects are
62+
not multiplicable as determined by `multiplicable(a, b)`.
63+
64+
If the macro `@compatiblebases` is used anywhere up the call stack,
65+
this check is disabled.
66+
"""
67+
function check_multiplicable end
68+
69+
"""
70+
addible(a, b)
71+
72+
Check if any two subtypes of `StateVector`, `AbstractOperator`,
73+
or `AbstractSuperOperator` can be added together.
74+
75+
Spcefically this checks whether the left basis of a is equal
76+
to the left basis of b and whether the right basis of a is equal
77+
to the right basis of b.
78+
"""
79+
function addible end
80+
81+
"""
82+
check_addible(a, b)
83+
84+
Throw an [`IncompatibleBases`](@ref) error if the objects are
85+
not addible as determined by `addible(a, b)`.
86+
87+
If the macro `@compatiblebases` is used anywhere up the call stack,
88+
this check is disabled.
89+
"""
90+
function check_addible end
91+
92+
"""
93+
issquare(a)
94+
95+
Check if any two subtypes of `StateVector`, `AbstractOperator`,
96+
or `AbstractSuperOperator` are square.
97+
98+
Spcefically this checks whether the left basis of a is equal
99+
to the right basis of a.
100+
For subtypes of `StateVector` this is always false.
101+
"""
102+
function addible end
103+
104+
"""
105+
check_issquare(a, b)
106+
107+
Throw an [`IncompatibleBases`](@ref) error if the objects are
108+
not addible as determined by `addible(a, b)`.
109+
110+
If the macro `@compatiblebases` is used anywhere up the call stack,
111+
this check is disabled.
112+
"""
113+
function check_addible end
114+
115+
const BASES_CHECK = Ref(true)
116+
117+
"""
118+
@compatiblebases
119+
120+
Macro to skip checks for compatible bases. Useful for `*`, `expect` and similar
121+
functions.
122+
"""
123+
macro compatiblebases(ex)
124+
return quote
125+
BASES_CHECK.x = false
126+
local val = $(esc(ex))
127+
BASES_CHECK.x = true
128+
val
129+
end
130+
end
131+
47132
function apply! end
48133

49134
function dagger end

src/bases.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ Exception that should be raised for an illegal algebraic operation.
118118
"""
119119
mutable struct IncompatibleBases <: Exception end
120120

121-
const BASES_CHECK = Ref(true)
122-
123121
"""
124122
@samebases
125123

0 commit comments

Comments
 (0)