Skip to content

Commit fac7984

Browse files
committed
Fix some corner cases in state parsing
1 parent fac2df9 commit fac7984

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

src/sitetypes/qubit.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ alias(::SiteType"SpinHalf=1/2") = SiteType"Qubit"()
66

77
Base.length(::SiteType"Qubit") = 2
88

9-
# `eigvecs(Z)`
10-
# (::StateName"0", ::Tuple{SiteType"Qubit"}) = [1, 0]
11-
129
@state_alias "Up" "0"
1310
@state_alias "" "0"
1411
@state_alias "Z+" "0"
1512
@state_alias "Zp" "0"
1613

17-
# (::StateName"1", ::Tuple{SiteType"Qubit"}) = [0, 1]
18-
1914
@state_alias "" "1"
2015
@state_alias "Dn" "1"
2116
@state_alias "Z-" "1"

src/state.jl

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ end
5353
# one-based indexing, as opposed to `state("0") == [1, 0]`.
5454
stateexpr(n::Integer; kwargs...) = StateName(n; kwargs...)
5555

56-
const DAGGER_STRING = randstring()
57-
const UPARROW_STRING = randstring()
58-
const DOWNARROW_STRING = randstring()
59-
const PLUS_STRING = randstring()
60-
const MINUS_STRING = randstring()
61-
const EXPR_REPLACEMENTS = (
56+
randcharstring() = randstring(['A':'Z'; 'a':'z'], 12)
57+
const DAGGER_STRING = randcharstring()
58+
const UPARROW_STRING = randcharstring()
59+
const DOWNARROW_STRING = randcharstring()
60+
const PLUS_STRING = randcharstring()
61+
const MINUS_STRING = randcharstring()
62+
const VERTICALBAR_STRING = randcharstring()
63+
const RANGLE_STRING = randcharstring()
64+
const EXPR_REPLACEMENTS_1 = (
6265
"" => DAGGER_STRING,
6366
"" => UPARROW_STRING,
6467
"" => DOWNARROW_STRING,
@@ -67,16 +70,27 @@ const EXPR_REPLACEMENTS = (
6770
r"(\S)\+" => SubstitutionString("\\1$(PLUS_STRING)"),
6871
r"(\S)\-" => SubstitutionString("\\1$(MINUS_STRING)"),
6972
)
73+
const EXPR_REPLACEMENTS_2 = (
74+
r"\|(\S*)⟩" => SubstitutionString("$(VERTICALBAR_STRING)\\1$(RANGLE_STRING)"),
75+
)
7076
const INVERSE_EXPR_REPLACEMENTS = (
7177
DAGGER_STRING => "",
7278
UPARROW_STRING => "",
7379
DOWNARROW_STRING => "",
7480
PLUS_STRING => "+",
7581
MINUS_STRING => "-",
82+
# We remove the bra-ket notation and search for states
83+
# with names stored inside.
84+
VERTICALBAR_STRING => "",
85+
RANGLE_STRING => "",
7686
)
7787

7888
function state_or_op_expr(ntype::Type, n::String; kwargs...)
79-
n = replace(n, EXPR_REPLACEMENTS...)
89+
# Do this in two rounds since for some reason
90+
# one round doesn't work for expressions
91+
# like `"|+⟩"`.
92+
n = replace(n, EXPR_REPLACEMENTS_1...)
93+
n = replace(n, EXPR_REPLACEMENTS_2...)
8094
depth = 1
8195
return state_or_op_expr(ntype, Meta.parse(n), depth; kwargs...)
8296
end

test/test_basics.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,6 @@ const elts = (real_elts..., complex_elts...)
142142
end
143143
end
144144
end
145-
@testset "state" begin
146-
@test state(1) == [1, 0]
147-
@test state(2) == [0, 1]
148-
@test state(1, 3) == [1, 0, 0]
149-
@test state(2, 3) == [0, 1, 0]
150-
end
151145
@testset "op parsing" begin
152146
@test Matrix(opexpr("X * Y")) == op("X") * op("Y")
153147
@test op("X * Y") == op("X") * op("Y")
@@ -167,4 +161,19 @@ const elts = (real_elts..., complex_elts...)
167161
@test op(name, SiteType("Electron")) == Matrix(OpName(name), SiteType("Electron"))
168162
end
169163
end
164+
@testset "state" begin
165+
@test state(1) == [1, 0]
166+
@test state("0") == [1, 0]
167+
@test state(2) == [0, 1]
168+
@test state("1") == [0, 1]
169+
@test state(1, 3) == [1, 0, 0]
170+
@test state("0", 3) == [1, 0, 0]
171+
@test state(2, 3) == [0, 1, 0]
172+
@test state("1", 3) == [0, 1, 0]
173+
@test state(3, 3) == [0, 0, 1]
174+
@test state("2", 3) == [0, 0, 1]
175+
176+
@test state("|0⟩ + 2|+⟩") == state("0") + 2 * state("+")
177+
@test state("|0⟩ ⊗ |+⟩") == kron(state("0"), state("+"))
178+
end
170179
end

0 commit comments

Comments
 (0)