entropicalabs / openqaoa

Multi-backend SDK for quantum optimisation
MIT License
116 stars 58 forks source link

QAOA evaluate circuit #192

Closed raulconchello closed 1 year ago

raulconchello commented 1 year ago

Implementation of qaoa.evaluate_circuit method

Example

We get some optimized parameters, using -in this case- analytical simulator

from openqaoa.problems import QUBO
from openqaoa import QAOA
from openqaoa.backends import create_device
#create random problem
problem = QUBO.random_instance(6)

#create qaoa object, with analytical simulator
q_analytic = QAOA()
device = create_device(location='local', name='analytical_simulator')
q_analytic.set_device(device)
q_analytic.set_circuit_properties(p=1, param_type='standard', init_type='rand', mixer_hamiltonian='x')
q_analytic.set_classical_optimizer(maxiter=100, method='vgd', jac="finite_difference")

#compile and optimize
q_analytic.compile(problem)
q_analytic.optimize()
opt_params = q_analytic.result.optimized['angles']
print(opt_params)

[Out]: [0.587019236113, 249.611957397334]

Now we evaluate a qaoa circuit with a QPU or simulator using the optimized parameters found above.

#create qaoa object, with shot simulator
q_shots = QAOA()
device = create_device(location='local', name='qiskit.shot_simulator')
q_shots.set_device(device)
q_shots.set_circuit_properties(p=1, param_type='standard', init_type='rand', mixer_hamiltonian='x')
q_shots.set_classical_optimizer(maxiter=100, method='vgd', jac="finite_difference")

#compile (we don't optimize)
q_shots.compile(problem)

#evaluate the qaoa circuit with the optimized parameters
results_eval_circuit = q_shots.evaluate_circuit(opt_params)
print(results_eval_circuit)

_[Out]: { 'cost': -44.783801067124, 'uncertainty': 103.578710845481, 'counts': {...} }

codecov[bot] commented 1 year ago

Codecov Report

Merging #192 (932d1cd) into dev (54d0c70) will increase coverage by 0.73%. The diff coverage is 99.53%.

@@            Coverage Diff             @@
##              dev     #192      +/-   ##
==========================================
+ Coverage   88.81%   89.54%   +0.73%     
==========================================
  Files          32       33       +1     
  Lines        5812     6237     +425     
==========================================
+ Hits         5162     5585     +423     
- Misses        650      652       +2     
Impacted Files Coverage Δ
tests/test_notebooks.py 94.00% <66.66%> (-1.75%) :arrow_down:
tests/test_benchmark.py 99.59% <99.59%> (ø)
tests/test_analytical_simulator.py 100.00% <100.00%> (ø)
tests/test_parameters.py 99.70% <100.00%> (+0.06%) :arrow_up:
tests/test_utilities.py 99.42% <100.00%> (ø)
tests/test_workflows.py 99.56% <100.00%> (+0.06%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

Q-lds commented 1 year ago

Let's remember to close Issue https://github.com/entropicalabs/openqaoa/issues/186 once this PR is merged!

vishal-ph commented 1 year ago

NOTE: run black formatter before merging