rigetti / grove

Quantum algorithms built using pyQuil.
http://rigetti.com/forest
Apache License 2.0
375 stars 123 forks source link

VQE on QVM with noise model not compatible #222

Open pacobontenbal opened 3 years ago

pacobontenbal commented 3 years ago

I am using Grove's VQE to run on a QVM with a noise model but I get a TypeError when I run it. Below is an example of how one would use the VQE on a QVM with a noise model:

from pyquil import get_qc
from pyquil.quil import Program
from pyquil.gates import *
from pyquil.paulis import PauliTerm, PauliSum
from grove.pyvqe.vqe import VQE
from scipy.optimize import minimize
import numpy as np
from functools import partial 

noisy_qvm = get_qc("Aspen-8", as_qvm=True, noisy=True)

def ansatz(thetas):
    bell_singlet = Program()
    bell_singlet += Program(X(0), XY(thetas[0], 0, 1), RZ(thetas[1], 0))

    return bell_singlet

def hamiltonian():
    pauliTerms = []
    pXX = PauliTerm('X', 0) * PauliTerm ('X', 1)
    pYY = PauliTerm('Y', 0) * PauliTerm ('Y', 1)

    pauliTerms.append(pXX)
    pauliTerms.append(pYY)

    return PauliSum(pauliTerms)

def vqe_run(thetas):
        dispList = []

        vqe_inst = VQE(minimizer=minimize, minimizer_kwargs={'method': "Powell"})
        vqe_run = vqe_inst.vqe_run(partial(ansatz), hamiltonian(), thetas, disp=dispList.append, samples=10000, qvm=noisy_qvm.qam)

        return vqe_run

vqe_run([np.pi/2, -np.pi/2])

Running this gives the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-a5e26cfc1196> in <module>
----> 1 vqe_run([np.pi/2, -np.pi/2])

<ipython-input-18-8e6da3871d03> in vqe_run(thetas)
      3 
      4         vqe_inst = VQE(minimizer=minimize, minimizer_kwargs={'method': "Powell"})
----> 5         vqe_run = vqe_inst.vqe_run(partial(ansatz), hamiltonian(), thetas, disp=dispList.append, samples=10000, qvm=noisy_qvm.qam)
      6 
      7         return vqe_run

/opt/conda/lib/python3.8/site-packages/grove/pyvqe/vqe.py in vqe_run(self, variational_state_evolve, hamiltonian, initial_params, gate_noise, measurement_noise, jacobian, qvm, disp, samples, return_all)
    170             self.minimizer_kwargs['jac'] = jacobian
    171 
--> 172         result = self.minimizer(*args, **self.minimizer_kwargs)
    173 
    174         if hasattr(result, 'status'):

/opt/conda/lib/python3.8/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    606         return _minimize_neldermead(fun, x0, args, callback, **options)
    607     elif meth == 'powell':
--> 608         return _minimize_powell(fun, x0, args, callback, bounds, **options)
    609     elif meth == 'cg':
    610         return _minimize_cg(fun, x0, args, jac, callback, **options)

/opt/conda/lib/python3.8/site-packages/scipy/optimize/optimize.py in _minimize_powell(func, x0, args, callback, bounds, xtol, ftol, maxiter, maxfev, disp, direc, return_all, **unknown_options)
   2919                           OptimizeWarning, 3)
   2920 
-> 2921     fval = squeeze(func(x))
   2922     x1 = x.copy()
   2923     iter = 0

/opt/conda/lib/python3.8/site-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
    425     def function_wrapper(*wrapper_args):
    426         ncalls[0] += 1
--> 427         return function(*(wrapper_args + args))
    428 
    429     return ncalls, function_wrapper

/opt/conda/lib/python3.8/site-packages/grove/pyvqe/vqe.py in objective_function(params)
    143             """
    144             pyquil_prog = variational_state_evolve(params)
--> 145             mean_value = self.expectation(pyquil_prog, hamiltonian, samples, qvm)
    146             self._current_expectation = mean_value  # store for printing
    147             return mean_value

/opt/conda/lib/python3.8/site-packages/grove/pyvqe/vqe.py in expectation(pyquil_prog, pauli_sum, samples, qvm)
    265 
    266                             meas_outcome = \
--> 267                                 expectation_from_sampling(pyquil_prog + meas_basis_change,
    268                                                           qubits_to_measure,
    269                                                           qvm,

/opt/conda/lib/python3.8/site-packages/grove/pyvqe/vqe.py in expectation_from_sampling(pyquil_program, marked_qubits, qvm, samples)
    313         pyquil_program.measure(qindex, qindex)
    314 
--> 315     bitstring_samples = qvm.run(pyquil_program, range(max(marked_qubits) + 1), trials=samples)
    316     bitstring_tuples = list(map(tuple, bitstring_samples))
    317 

/opt/conda/lib/python3.8/site-packages/pyquil/api/_error_reporting.py in wrapper(*args, **kwargs)
    249             global_error_context.log[key] = pre_entry
    250 
--> 251         val = func(*args, **kwargs)
    252 
    253         # poke the return value of that call in

TypeError: run() got an unexpected keyword argument 'trials'

Is there a way to run a VQE with a noise model on a QVM?