@@ -147,7 +147,8 @@ class initialization (``Pauli('-iXYZ')``). A ``Pauli`` object can be
147
147
# Set the max Pauli string size before truncation
148
148
__truncate__ = 50
149
149
150
- _VALID_LABEL_PATTERN = re .compile (r"^[+-]?1?[ij]?[IXYZ]+$" )
150
+ _VALID_LABEL_PATTERN = re .compile (r"(?P<coeff>[+-]?1?[ij]?)(?P<pauli>[IXYZ]*)" )
151
+ _CANONICAL_PHASE_LABEL = {"" : 0 , "-i" : 1 , "-" : 2 , "i" : 3 }
151
152
152
153
def __init__ (self , data = None , x = None , * , z = None , label = None ):
153
154
"""Initialize the Pauli.
@@ -613,17 +614,15 @@ def _from_label(label):
613
614
Raises:
614
615
QiskitError: if Pauli string is not valid.
615
616
"""
616
- if Pauli ._VALID_LABEL_PATTERN .match (label ) is None :
617
+ match_ = Pauli ._VALID_LABEL_PATTERN .fullmatch (label )
618
+ if match_ is None :
617
619
raise QiskitError (f'Pauli string label "{ label } " is not valid.' )
618
-
619
- # Split string into coefficient and Pauli
620
- pauli , coeff = _split_pauli_label (label )
621
-
622
- # Convert coefficient to phase
623
- phase = 0 if not coeff else _phase_from_label (coeff )
620
+ phase = Pauli ._CANONICAL_PHASE_LABEL [
621
+ (match_ ["coeff" ] or "" ).replace ("1" , "" ).replace ("+" , "" ).replace ("j" , "i" )
622
+ ]
624
623
625
624
# Convert to Symplectic representation
626
- pauli_bytes = np .frombuffer (pauli .encode ("ascii" ), dtype = np .uint8 )[::- 1 ]
625
+ pauli_bytes = np .frombuffer (match_ [ " pauli" ] .encode ("ascii" ), dtype = np .uint8 )[::- 1 ]
627
626
ys = pauli_bytes == ord ("Y" )
628
627
base_x = np .logical_or (pauli_bytes == ord ("X" ), ys ).reshape (1 , - 1 )
629
628
base_z = np .logical_or (pauli_bytes == ord ("Z" ), ys ).reshape (1 , - 1 )
@@ -698,33 +697,5 @@ def _from_circuit(cls, instr):
698
697
return ret ._z , ret ._x , ret ._phase
699
698
700
699
701
- # ---------------------------------------------------------------------
702
- # Label parsing helper functions
703
- # ---------------------------------------------------------------------
704
-
705
-
706
- def _split_pauli_label (label ):
707
- """Split Pauli label into unsigned group label and coefficient label"""
708
- span = re .search (r"[IXYZ]+" , label ).span ()
709
- pauli = label [span [0 ] :]
710
- coeff = label [: span [0 ]]
711
- if span [1 ] != len (label ):
712
- invalid = set (re .sub (r"[IXYZ]+" , "" , label [span [0 ] :]))
713
- raise QiskitError (
714
- f"Pauli string contains invalid characters { invalid } ∉ ['I', 'X', 'Y', 'Z']"
715
- )
716
- return pauli , coeff
717
-
718
-
719
- def _phase_from_label (label ):
720
- """Return the phase from a label"""
721
- # Returns None if label is invalid
722
- label = label .replace ("+" , "" , 1 ).replace ("1" , "" , 1 ).replace ("j" , "i" , 1 )
723
- phases = {"" : 0 , "-i" : 1 , "-" : 2 , "i" : 3 }
724
- if label not in phases :
725
- raise QiskitError (f"Invalid Pauli phase label '{ label } '" )
726
- return phases [label ]
727
-
728
-
729
700
# Update docstrings for API docs
730
701
generate_apidocs (Pauli )
0 commit comments