Skip to content

Commit

Permalink
add executed_ansatz as class variable for QAOA and VQE algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
armand-gautier authored and g-poveda committed Sep 12, 2024
1 parent 4c01e91 commit ee88e0c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions discrete_optimization/generic_tools/qiskit_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ def cost_func(params, ansatz, hamiltonian, estimator, callback_dict):


def execute_ansatz_with_Hamiltonian(
backend, ansatz, hamiltonian, use_session: Optional[bool] = False, **kwargs
solver, backend, ansatz, hamiltonian, use_session: Optional[bool] = False, **kwargs
) -> np.ndarray:
"""
@param solver: the solver who solve the problem, must be a QiskitSolver
@param backend: the backend use to run the circuit (simulator or real device)
@param ansatz: the quantum circuit
@param hamiltonian: the hamiltonian corresponding to the problem
Expand All @@ -151,6 +152,7 @@ def execute_ansatz_with_Hamiltonian(
target=target, optimization_level=optimization_level
)
new_ansatz = pm.run(ansatz)
solver.set_executed_ansatz(new_ansatz)
hamiltonian = hamiltonian.apply_layout(new_ansatz.layout)

# open a session if desired
Expand Down Expand Up @@ -242,6 +244,10 @@ def __init__(
**kwargs,
):
super().__init__(problem, params_objective_function)
self.executed_ansatz = None

def set_executed_ansatz(self, ansatz):
self.executed_ansatz = ansatz


class QiskitQAOASolver(QiskitSolver, Hyperparametrizable):
Expand Down Expand Up @@ -311,7 +317,7 @@ def solve(
self.ansatz.parameter_bounds = bounds

result = execute_ansatz_with_Hamiltonian(
self.backend, self.ansatz, hamiltonian, use_session, **kwargs
self, self.backend, self.ansatz, hamiltonian, use_session, **kwargs
)
result = conv.interpret(result)

Expand Down Expand Up @@ -391,7 +397,7 @@ def solve(
self.ansatz = EfficientSU2(hamiltonian.num_qubits)

result = execute_ansatz_with_Hamiltonian(
self.backend, self.ansatz, hamiltonian, use_session, **kwargs
self, self.backend, self.ansatz, hamiltonian, use_session, **kwargs
)
result = conv.interpret(result)

Expand Down

0 comments on commit ee88e0c

Please sign in to comment.