Source code for qlauncher.hampy.utils

 1"""
 2Utility functions for Hampy objects.
 3"""
 4
 5from qiskit.quantum_info import Pauli, SparsePauliOp
 6
 7from qlauncher.hampy.object import Equation
 8
 9
[docs] 10def shift_affected_qubits(equation: Equation, shift: int) -> Equation: 11 """ 12 Shifts the qubits affected by the equation by the given amount, wrapping around if the index goes out of bounds. 13 14 For each Pauli in the equation hamiltonian, shifts the Pauli string by the given amount. 15 i.e (shift = 1) IZIZ -> ZIZI, etc. !Might be unwanted! ZIII -> IIIZ 16 Keeps the coefficients the same. 17 18 Args: 19 equation: Equation to shift 20 shift: Amount to shift by 21 22 Returns: 23 Equation: New equation affecting the shifted qubits 24 """ 25 op = equation.hamiltonian 26 27 if shift == 0: 28 return equation 29 30 n_paulis = [] 31 n_coeffs = [] 32 33 for p_string, coeff in op.label_iter(): 34 p_string = p_string[shift:] + p_string[:shift] 35 n_paulis.append(Pauli(data=p_string)) 36 n_coeffs.append(coeff) 37 38 new_op = SparsePauliOp(n_paulis, coeffs=n_coeffs) 39 40 return Equation(new_op)