# How to find the expectation value of several circuits using Qiskit aqua operator logic?

I am using the method from this SE answer to calculate the expectation value using qiskit aqua operator logic which works well for a single circuit. Here is a simplified code snippet to demonstrate how it works.

import qiskit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer
import numpy as np
from qiskit.aqua import QuantumInstance
from qiskit.aqua.operators import PauliExpectation, CircuitSampler, StateFn, CircuitOp, CircuitStateFn

qctl = QuantumRegister(1)
psi  = QuantumCircuit(qctl)
psi  = CircuitStateFn(psi)

qctl = QuantumRegister(2)
op   = QuantumCircuit(qctl)
op.z(0)
op.ry(np.pi/4,0)
op   = CircuitOp(op)

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


However, I am applying it to a VQE application and for each iteration I have $x>1$ circuits. There is no issue when I run on my local machine because there is no queue time, but on the IBMQ machines I would like to submit a batch of the aqua expectation value circuits for each iteration to reduce queue times. For a list of circuits I can submit a batch using the IBMQJobManager() function, however, the method I am using does not store circuits as a list. My question is, can I use the jobmanager with the aqua expectation value to submit a batch of jobs to IBMQ? If not, is there a different way to submit a batch of the aqua expectation value circuits?

• This isn’t completely related, but Qiskit Aqua is being deprecated, so I recommend you start using the new packaging. Check out the migration guide. – epelaaez Jun 15 at 21:50
• Good to know, I appreciate the info! – thespaceman Jun 15 at 22:00
• Can you share the code you are currently using with VQE and IBMQJobManager()? – epelaaez Jun 16 at 4:29
• I don't yet have a code with VQE and IBMQJobManager, perhaps I wasn't clear enough. To clarify, I would like to use the IBMQJobManager but to do so I would need a list of circuits as input for the jobmanger. The issue I am facing is that the built in aqua expectation value method that I am using does not create a circuit which can be put into a list. At least I don't think that it does... – thespaceman Jun 16 at 6:20

There is no need to use IBMQJobManager as QuantumInstance will do the necessary work for you. Just add your CircuitOps to a ListOp then pass it to StateFn constructor:

ops = []

# ... Construct your first circuit ...
# Now, add it to the list:
ops.append(op1)

# ... Construct your second circuit ...
# Now, add it to the list:
ops.append(op2)

measurable_expression = StateFn(ListOp(ops), is_measurement=True).compose(psi)


This way, QuantumInstance will assemble these circuits into a single Qobj and pass it to the backend.

Note that if the number of circuits is larger than backend.configuration().max_experiments, it will be splitted into multiple payloads.

• Excellent! It is so simple, but as a new qiskit user it is not obvious to me how I would figure that out on my own. Do you have any suggestions for learning the intricacies of qiskit? – thespaceman Jun 18 at 16:29
• Many features of Qiskit don't have good documentation. And since it evolves rapidly, its documentation becomes obsolete as fast. Sometimes my best option is to go through the code to find the answers I'm searching for. – Egretta.Thula Jun 18 at 17:08