Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DilhanM committed Jun 1, 2024
1 parent 42f2aa0 commit 1c8311e
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion Production/supporting_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,37 @@ def get_XY_mixer(num_qubits, num_rot):
else:
raise ValueError("Number of rotomers not supported.")

def generate_pauli_zij(n, i, j):
if i<0 or i >= n or j<0 or j>=n:
raise ValueError(f"Indices out of bounds for n={n} qubits. ")

pauli_str = ['I']*n

if i == j:
pauli_str[i] = 'Z'
else:
pauli_str[i] = 'Z'
pauli_str[j] = 'Z'

return Pauli(''.join(pauli_str))

def get_q_hamiltonian(num_qubits, H):
q_hamiltonian = SparsePauliOp(Pauli('I'*num_qubits), coeffs=[0])

for i in range(num_qubits):
for j in range(i+1, num_qubits):
if H[i][j] != 0:
pauli = generate_pauli_zij(num_qubits, i, j)
op = SparsePauliOp(pauli, coeffs=[H[i][j]])
q_hamiltonian += op

for i in range(num_qubits):
pauli = generate_pauli_zij(num_qubits, i, i)
Z_i = SparsePauliOp(pauli, coeffs=[H[i][i]])
q_hamiltonian += Z_i

return q_hamiltonian

def generate_initial_bitstring(num_qubits, num_rot):
if num_rot == 2:
bitstring = [(i%2) for i in range(num_qubits)]
Expand Down Expand Up @@ -206,4 +237,14 @@ def safe_literal_eval(value):
return ast.literal_eval(value)
except (ValueError, SyntaxError) as e:
print(f"Error evaluating string: {value}, {e}")
return None
return None

class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return super(NumpyEncoder, self).default(obj)

0 comments on commit 1c8311e

Please sign in to comment.