5353# one-based indexing, as opposed to `state("0") == [1, 0]`.
5454stateexpr (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+ )
7076const 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
7888function 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... )
8296end
0 commit comments