Skip to content

Commit 17916cf

Browse files
committed
Add convert requirement in check_state
This adds a requirement in `check_state` that `convert(typeof(state), mutable_state)` for `mutable_state = similar(state)` must be defined.
1 parent d6dfe6f commit 17916cf

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/interfaces/state.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ QuantumPropagators.Interfaces.supports_inplace) is `true` (read-write vector):
7777
7878
Note: When `supports_inplace(state)` is `true`, `similar(state)` is already
7979
required to return the *same type* as `state` (see above). The vector interface
80-
checks are weaker and complementary.
80+
checks are weaker and complementary. However, in all cases:
81+
82+
* `convert(typeof(state), mutable_state)` for `mutable_state = similar(state)`
83+
must be defined.
8184
8285
It is strongly recommended to always support immutable operations (also for
8386
mutable states)
@@ -558,6 +561,22 @@ function check_state(
558561
success = false
559562
end
560563

564+
try
565+
mutable_state = similar(state)
566+
converted = convert(typeof(state), mutable_state)
567+
if !(converted isa typeof(state))
568+
quiet ||
569+
@error "$(px)`convert($(typeof(state)), similar(state))` must return a `$(typeof(state))`, not $(typeof(converted))"
570+
success = false
571+
end
572+
catch exc
573+
quiet || @error(
574+
"$(px)`convert(typeof(state), mutable_state)` for `mutable_state = similar(state)` must be defined.",
575+
exception = (exc, catch_abbreviated_backtrace())
576+
)
577+
success = false
578+
end
579+
561580
if supports_inplace(state)
562581

563582
try

0 commit comments

Comments
 (0)