@@ -67,18 +67,56 @@ function getDefaultMenuSelection(
6767 return selection ;
6868}
6969
70- const reKetResult = / ^ \[ (?: ( Z e r o | O n e | L o s s ) , * ) * ( Z e r o | O n e | L o s s ) \] $ / ;
70+ // Matches a simple, single-level, bracketed CSV list with no empty elements, e.g.:
71+ // [Zero, One, Loss]
72+ // [0, 1, .]
73+ // [missing qubit, one, zero, result unknown]
74+ //
75+ // Tokens may be arbitrary strings so long as they don't include ',', '[' or ']'.
76+ // Empty values such as "[,,,]" or "[Zero,,One]" are rejected.
77+ //
78+
79+ // A word is any non-whitespace, non-comma, non-bracket sequence
80+ const ketCsvWord = "[^\\s,\\[\\]]+" ;
81+
82+ // A value is one or more words separated by whitespace
83+ const ketCsvValue = `\\s*(?:${ ketCsvWord } \\s*)+` ;
84+
85+ // The body is one or more values separated by commas and surrounded by brackets
86+ const ketCsvBody = `\\[(?:${ ketCsvValue } ,)*${ ketCsvValue } \\]` ;
87+
88+ const reKetResult = new RegExp ( `^\\s*${ ketCsvBody } \\s*$` ) ;
7189function resultToKet ( result : string ) : string {
7290 if ( typeof result !== "string" ) return "ERROR" ;
7391
7492 if ( reKetResult . test ( result ) ) {
75- // The result is a simple array of Zero and One
76- // The below will return an array of "Zero" or "One" in the order found
77- const matches = result . match ( / ( O n e | Z e r o | L o s s ) / g) ;
93+ // The result is a simple, bracketed CSV list. Convert each token to a ket digit:
94+ // Zero/0 -> 0
95+ // One/1 -> 1
96+ // else -> -
97+ const inner = result . trim ( ) . slice ( 1 , - 1 ) . trim ( ) ;
98+ const tokens = inner . length ? inner . split ( "," ) : [ ] ;
99+
78100 let ket = "|" ;
79- matches ?. forEach (
80- ( digit ) => ( ket += digit == "One" ? "1" : digit == "Zero" ? "0" : "-" ) ,
81- ) ;
101+ for ( const token of tokens ) {
102+ const trimmed = token . trim ( ) ;
103+ const unquoted = trimmed
104+ . replace ( / ^ " ( .* ) " $ / , "$1" )
105+ . replace ( / ^ ' ( .* ) ' $ / , "$1" )
106+ . trim ( ) ;
107+
108+ // If any element is empty after trimming/unquoting, don't treat it as a ket label.
109+ if ( unquoted . length === 0 ) return result ;
110+
111+ const normalized = unquoted . toLowerCase ( ) ;
112+
113+ ket +=
114+ normalized === "zero" || normalized === "0"
115+ ? "0"
116+ : normalized === "one" || normalized === "1"
117+ ? "1"
118+ : "-" ;
119+ }
82120 ket += "⟩" ;
83121 return ket ;
84122 } else {
0 commit comments