# Error when computing the self-designed gradient in qiskit

I had question with the way to define a new kind of gradient to improve the performance of VQE. The gradient formula I used consists of multiply energy (measurement result of the Hamiltonian) with the gradient of the energy. I follow the instruction of the qiskit gradient framework page, and take the following code as an example

#General imports
import numpy as np

#Operator Imports
from qiskit.opflow import Z, X, I, Y, StateFn, CircuitStateFn, SummedOp

#Circuit imports
from qiskit.circuit import QuantumCircuit, QuantumRegister, Parameter, ParameterVector, ParameterExpression
from qiskit.circuit.library import EfficientSU2

a = Parameter('a')
b = Parameter('b')
q = QuantumRegister(1)
qc = QuantumCircuit(q)
qc.h(q)
qc.rz(a, q[0])
qc.rx(b, q[0])

# Instantiate the Hamiltonian observable
H1 = (2 * X) + Z
H2 = X+Y+Z

# Combine the Hamiltonian observable and the state
op1 = ~StateFn(H1) @ CircuitStateFn(primitive=qc, coeff=1.)
op2 = ~StateFn(H2) @ CircuitStateFn(primitive=qc, coeff=1.)
op = op1 @ op2
print('op is', op)
params = [a, b]

# Define the values to be assigned to the parameters
value_dict = {a: np.pi / 4, b: np.pi}

# Convert the operator and the gradient target params into the respective operator
# Print the operator corresponding to the Gradient


The last line of the code pops an exception: Exception has occurred: ValueError Composition with a Statefunctions in the first operand is not defined. How to get rid of the exception in this case? Also, I need to get the result for the multiplication of two measured operator in the process (op1 and op2), does the code above op=op1@op2 can achieve this?

• I don't quite understand what you want to do, maybe you could explain the evaluations you want to compute? The @ operator does not multiply, it is a shortcut for the compose method, so op=op1@op2 is not a multiplication. – Cryoris Jul 7 at 14:55