Qiskit / qiskit-metapackage

Qiskit is an open-source SDK for working with quantum computers at the level of circuits, algorithms, and application modules.
https://qiskit.org
Apache License 2.0
3.03k stars 749 forks source link

The expected value of the target circuit optimized by the minimize function does not converge #1321

Closed ntyz closed 3 years ago

ntyz commented 3 years ago

I want to get the optimal gate parameters that minimize the expectation of the target circuit,so I use the minimize function of scipy.optimize, but it seems like the value does not converge. So I want to know how to do it on right way. Here is my code:

`import numpy as np from scipy.optimize import minimize, rosen, rosen_der from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer from qiskit.circuit import QuantumCircuit, ParameterVector from qiskit.aqua import QuantumInstance from qiskit.aqua.operators import PauliExpectation, CircuitSampler, StateFn, CircuitOp, CircuitStateFn from qiskit.aqua.operators import X, Y, Z, I

hamilq = 0.5 X^Z + 0.7 Z^Y

def small_vqe(params): qreg = QuantumRegister(2) circ = QuantumCircuit(qreg) circ.h(0) circ.rz(params[0], qreg[0]) circ.rz(params[1], qreg[1]) op = hamilq

subc = CircuitStateFn(circ)
backend = Aer.get_backend('qasm_simulator')
q_instance = QuantumInstance(backend, shots=1024)
measurable_expression = StateFn(op, is_measurement=True).compose(subc)
expectation = PauliExpectation().convert(measurable_expression)
sampler = CircuitSampler(q_instance).convert(expectation)
exp_res = sampler.eval().real

print('Expectation Value = ', exp_res)
return exp_res

params = np.zeros(2) minimumq = minimize(small_vqe, params, method='COBYLA', tol=1e-30, options={'rhobeg': 1.0, 'maxiter': 10000, 'disp': False, 'catol': 0.0002})

print(minimumq)`

nonhermitian commented 3 years ago

Your tol is below machine precision. Try something like 1e-14 or larger.

ntyz commented 3 years ago

Your tol is below machine precision. Try something like 1e-14 or larger. yeah, I have tried with tol = 1e-10, but there is no obvious optimization trend

But I have a successful case of the same idea, but with different circuit like this:

`python import numpy as np from qiskit.aqua import QuantumInstance from qiskit.aqua.operators import StateFn, PauliExpectation, CircuitSampler, CircuitStateFn from scipy.optimize import minimize, rosen, rosen_der from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, Aer, execute from qiskit.aqua.operators import X, Y, Z, I

shots = 1024

w = [-1.0523732, 0.39793742, -0.3979374, -0.0112801, 0.18093119] op = 0 num = 5 for x in w: opx = I for i in range(num - 1): c = np.random.choice(range(2)) if c == 0: opx = opx ^ I else: opx = opx ^ Z op += x * opx

def objective_function(params): q = QuantumRegister(5, 'q') qc = QuantumCircuit(q) qc.u3(params[0], params[1], params[2], q[0]) qc.cu3(params[3], params[4], params[5], q[0], q[1]) qc.cu3(params[6], params[7], params[8], q[0], q[2]) qc.cu3(params[9], params[10], params[11], q[0], q[3]) qc.cu3(params[12], params[13], params[14], q[0], q[4]) qc.x(q[0]) qc.cu3(params[15], params[16], params[17], q[0], q[1]) qc.x(q[0]) qc.x(q[0]) qc.cu3(params[18], params[19], params[20], q[0], q[2]) qc.x(q[0]) qc.x(q[0]) qc.cu3(params[21], params[22], params[23], q[0], q[3]) qc.x(q[0]) qc.x(q[0]) qc.cu3(params[24], params[25], params[26], q[0], q[4]) qc.x(q[0])

qc = CircuitStateFn(qc)
backend = Aer.get_backend('qasm_simulator')
q_instance = QuantumInstance(backend, shots=1024)
measurable_expression = StateFn(op, is_measurement=True).compose(qc)
expectation = PauliExpectation().convert(measurable_expression)
sampler = CircuitSampler(q_instance).convert(expectation)
res = sampler.eval().real
print('Expectation Value = ', res)

return res

params = np.zeros(27)

minimum = minimize(objective_function, params, method='COBYLA', tol=1e-30, options={'rhobeg': 1.0, 'maxiter': 10000, 'disp': False, 'catol': 0.0002}) print(minimum) ` So I want to know the reasons for the first failure and the second success of the two optimizers