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