Skip to content

Commit

Permalink
Merge pull request #175 from UCL-CCS/to-openfermion
Browse files Browse the repository at this point in the history
Faster `PauliwordOp.to_openfermion`
  • Loading branch information
AlexisRalli authored Dec 14, 2023
2 parents a9c3c72 + 8fa551d commit 46a62f3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion symmer/operators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ def to_openfermion(self) -> QubitOperator:
"""
open_f = QubitOperator()
for P_sym, coeff in zip(self.symp_matrix, self.coeff_vec):
open_f+=QubitOperator(' '.join([Pi+str(i) for i,Pi in enumerate(symplectic_to_string(P_sym)) if Pi!='I']), coeff)
open_f+=symplectic_to_openfermion(P_sym, coeff)
return open_f

@cached_property
Expand Down
31 changes: 31 additions & 0 deletions symmer/operators/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ def symplectic_to_string(symp_vec) -> str:

return Pword_string

def symplectic_to_openfermion(symp_vec, coeff) -> str:
"""Returns string form of symplectic vector defined as (X | Z).
Args:
symp_vec (array): symplectic Pauliword array
Returns:
Pword_string (str): String version of symplectic array
"""
n_qubits = len(symp_vec) // 2

X_block = symp_vec[:n_qubits]
Z_block = symp_vec[n_qubits:]

Y_loc = np.logical_and(X_block, Z_block)
X_loc = np.logical_xor(Y_loc, X_block)
Z_loc = np.logical_xor(Y_loc, Z_block)

char_aray = np.array(list("I" * n_qubits), dtype=str)

char_aray[Y_loc] = "Y"
char_aray[X_loc] = "X"
char_aray[Z_loc] = "Z"

indices = np.array(range(n_qubits), dtype=str)
char_aray = np.char.add(char_aray, indices)[np.where(char_aray != "I")[0]]

Pword_string = " ".join(char_aray)

return QubitOperator(Pword_string, coeff)

def string_to_symplectic(pauli_str, n_qubits):
"""
Args:
Expand Down

0 comments on commit 46a62f3

Please sign in to comment.